Skip to content

Commit 6a8e909

Browse files
committed
enhance command line with --version parameter (SAP#339)
1 parent 948e46e commit 6a8e909

File tree

5 files changed

+109
-30
lines changed

5 files changed

+109
-30
lines changed

com.sap.adt.abapcleaner.gui/src/com/sap/adt/abapcleaner/gui/FrmMain.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,20 @@ public static void main(String[] args) {
145145
cleanInteractively(null, ABAP.NEWEST_RELEASE, null, persistency.getStartupPath(), false, null, null, null, false);
146146

147147
} else {
148-
if (commandLineArgs.hasErrors()) {
148+
if (commandLineArgs.action == CommandLineAction.SHOW_HELP) {
149+
System.out.print(CommandLineArgs.getHelp(persistency));
150+
151+
} else if (commandLineArgs.action == CommandLineAction.SHOW_VERSION) {
152+
System.out.print(Program.PRODUCT_NAME + " " + Program.getVersion());
153+
154+
} else if (commandLineArgs.hasErrors()) {
149155
if (commandLineArgs.hasErrors()) {
150156
System.err.println(commandLineArgs.errors);
151157
System.err.println("");
152158
System.err.flush();
153159
}
154160
System.out.print(CommandLineArgs.getHelp(persistency));
155161

156-
} else if (commandLineArgs.showHelp) {
157-
System.out.print(CommandLineArgs.getHelp(persistency));
158-
159162
} else {
160163
// use application args for automatic cleanup
161164
cleanAutomatically(commandLineArgs, System.out);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.sap.adt.abapcleaner.programbase;
2+
3+
public enum CommandLineAction {
4+
SHOW_HELP,
5+
SHOW_VERSION,
6+
CLEANUP;
7+
8+
public static final int SIZE = java.lang.Integer.SIZE;
9+
10+
public int getValue() {
11+
return this.ordinal();
12+
}
13+
14+
public static CommandLineAction forValue(int value) {
15+
return values()[value];
16+
}
17+
}

com.sap.adt.abapcleaner/src/com/sap/adt/abapcleaner/programbase/CommandLineArgs.java

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
public class CommandLineArgs {
88
private static final String LINE_SEP = System.lineSeparator();
99

10+
// options for help or version info
11+
private static final String OPT_HELP_WINDOWS = "/?";
12+
private static final String OPT_HELP_LINUX = "/man";
13+
private static final String OPT_VERSION = "--version";
14+
15+
// options for cleanup
1016
private static final String OPT_SOURCE_FILE = "--sourcefile";
1117
private static final String OPT_SOURCE_CODE = "--source";
1218
private static final String OPT_LINE_RANGE = "--linerange";
@@ -30,8 +36,6 @@ public class CommandLineArgs {
3036
private static final String[] allOptions = new String[] { OPT_SOURCE_FILE, OPT_SOURCE_CODE, OPT_LINE_RANGE, OPT_TARGET_FILE, OPT_SOURCE_DIR, OPT_RECURSIVE, OPT_TARGET_DIR, OPT_FILE_FILTER, OPT_PROFILE, OPT_PROFILE_DATA, OPT_RELEASE, OPT_CRLF, OPT_OVERWRITE, OPT_PARTIAL_RESULT, OPT_STATS, OPT_USED_RULES };
3137

3238
private static final String EXECUTABLE_NAME = ".\\abap-cleanerc.exe";
33-
private static final String OPT_HELP_WINDOWS = "/?";
34-
private static final String OPT_HELP_LINUX = "/man";
3539
private static final char LINE_RANGE_SEP = '-';
3640
private static final String LINE_RANGE_EXAMPLE = "\"20-35\"";
3741

@@ -47,11 +51,20 @@ public class CommandLineArgs {
4751

4852
@SuppressWarnings("unused")
4953
public static CommandLineArgs create(Persistency persistency, String[] args) {
54+
final String LINE_SEP = System.lineSeparator();
55+
5056
if (args == null || args.length == 0)
5157
return null;
5258

53-
final String LINE_SEP = System.lineSeparator();
54-
59+
// check whether help or version info is requested
60+
if (args.length == 1 && (args[0].equals(OPT_HELP_WINDOWS) || args[0].equals(OPT_HELP_LINUX))) {
61+
return new CommandLineArgs(CommandLineAction.SHOW_HELP);
62+
63+
} else if (args.length == 1 && args[0].equals(OPT_VERSION)) {
64+
return new CommandLineArgs(CommandLineAction.SHOW_VERSION);
65+
}
66+
67+
// in all other cases, cleanup is requested:
5568
String sourceCode = null;
5669
String targetPath = null;
5770
CleanupRange cleanupRange = null;
@@ -153,9 +166,6 @@ public static CommandLineArgs create(Persistency persistency, String[] args) {
153166
} else if (arg.equals(OPT_USED_RULES)) {
154167
showUsedRules = true;
155168

156-
} else if (arg.equals(OPT_HELP_WINDOWS) || arg.equals(OPT_HELP_LINUX)) {
157-
showHelp = true;
158-
159169
} else if (arg.equals(OPT_FILE_FILTER)) {
160170
fileFilter = nextArg;
161171

@@ -216,10 +226,22 @@ public static String getHelp(Persistency persistency) {
216226

217227
StringBuilder sb = new StringBuilder();
218228

219-
String usagePrefix = " " + EXECUTABLE_NAME;
229+
String usagePrefix = " " + EXECUTABLE_NAME;
220230
String spacePrefix = StringUtil.repeatChar(' ', usagePrefix.length());
221231

222-
sb.append("Usage for single file:");
232+
sb.append("Getting help and version information:");
233+
sb.append(LINE_SEP);
234+
sb.append(usagePrefix);
235+
sb.append(" " + OPT_HELP_WINDOWS);
236+
sb.append(LINE_SEP);
237+
sb.append(usagePrefix);
238+
sb.append(" " + OPT_HELP_LINUX);
239+
sb.append(LINE_SEP);
240+
sb.append(usagePrefix);
241+
sb.append(" " + OPT_VERSION);
242+
sb.append(LINE_SEP + LINE_SEP);
243+
244+
sb.append("Cleanup of single file:");
223245
sb.append(LINE_SEP);
224246
sb.append(usagePrefix);
225247
sb.append(" {" + OPT_SOURCE_FILE + " sourcefile");
@@ -242,7 +264,7 @@ public static String getHelp(Persistency persistency) {
242264
sb.append(" [" + OPT_USED_RULES + "]");
243265
sb.append(LINE_SEP + LINE_SEP);
244266

245-
sb.append("Example for single file:");
267+
sb.append("Example for cleanup of single file:");
246268
sb.append(LINE_SEP);
247269
sb.append(usagePrefix);
248270
sb.append(" " + OPT_SOURCE_FILE + " \"CL_ANY_CLASS.txt\"");
@@ -255,7 +277,7 @@ public static String getHelp(Persistency persistency) {
255277
sb.append(" " + OPT_USED_RULES);
256278
sb.append(LINE_SEP + LINE_SEP);
257279

258-
sb.append("Usage for multiple files:");
280+
sb.append("Cleanup of multiple files:");
259281
sb.append(LINE_SEP);
260282
sb.append(usagePrefix);
261283
sb.append(" " + OPT_SOURCE_DIR + " sourcedir");
@@ -276,7 +298,7 @@ public static String getHelp(Persistency persistency) {
276298
sb.append(" [" + OPT_USED_RULES + "]");
277299
sb.append(LINE_SEP + LINE_SEP);
278300

279-
sb.append("Example for multiple files:");
301+
sb.append("Example for cleanup of multiple files:");
280302
sb.append(LINE_SEP);
281303
sb.append(usagePrefix);
282304
sb.append(" " + OPT_SOURCE_DIR + " \"C:\\temp\\source\"");
@@ -288,7 +310,7 @@ public static String getHelp(Persistency persistency) {
288310
sb.append(" " + OPT_OVERWRITE);
289311
sb.append(LINE_SEP + LINE_SEP);
290312

291-
sb.append("Options: ");
313+
sb.append("Options for cleanup: ");
292314
sb.append(LINE_SEP);
293315
sb.append(getOptionHelp(OPT_SOURCE_FILE, "File name of an ABAP source file which is input to the cleanup."));
294316
sb.append(getOptionHelp(OPT_SOURCE_CODE, "ABAP source code which is input to the cleanup."));
@@ -337,6 +359,7 @@ private static String getOptionsLinePrefix(String option) {
337359

338360
// -------------------------------------------------------------------------
339361

362+
public final CommandLineAction action;
340363
public final String sourceCode;
341364
public final CleanupRange cleanupRange;
342365
public final String targetPath;
@@ -354,15 +377,38 @@ private static String getOptionsLinePrefix(String option) {
354377
public final boolean showStats;
355378
public final boolean showUsedRules;
356379
public final String errors;
357-
public final boolean showHelp;
358380

359381
public boolean hasErrors() { return !StringUtil.isNullOrEmpty(errors); }
360382

361383
public boolean isInSingleSourceMode() { return sourceDir == null; }
362384

363385
public boolean writesResultCodeToOutput() { return isInSingleSourceMode() && StringUtil.isNullOrEmpty(targetPath); }
364386

387+
private CommandLineArgs(CommandLineAction action) {
388+
this.action = action;
389+
390+
this.sourceCode = null;
391+
this.targetPath = null;
392+
this.cleanupRange = null;
393+
394+
this.sourcePaths = null;
395+
this.sourceDir = null;
396+
this.targetDir = null;
397+
398+
this.profileData = null;
399+
this.abapRelease = null;
400+
401+
this.lineSeparator = null;
402+
this.overwrite = false;
403+
this.partialResult = false;
404+
this.showStats = false;
405+
this.showUsedRules = false;
406+
this.errors = null;
407+
}
408+
365409
private CommandLineArgs(String sourceCode, String targetPath, CleanupRange cleanupRange, String profileData, String abapRelease, String lineSeparator, boolean overwrite, boolean partialResult, boolean showStats, boolean showUsedRules, String errors, boolean showHelp) {
410+
this.action = CommandLineAction.CLEANUP;
411+
366412
this.sourceCode = sourceCode;
367413
this.targetPath = targetPath;
368414
this.cleanupRange = cleanupRange;
@@ -380,11 +426,11 @@ private CommandLineArgs(String sourceCode, String targetPath, CleanupRange clean
380426
this.showStats = showStats;
381427
this.showUsedRules = showUsedRules;
382428
this.errors = errors;
383-
this.showHelp = showHelp;
384-
385429
}
386430

387431
private CommandLineArgs(String sourceDir, String[] sourcePaths, String targetDir, String profileData, String abapRelease, String lineSeparator, boolean overwrite, boolean showStats, boolean showUsedRules, String errors, boolean showHelp) {
432+
this.action = CommandLineAction.CLEANUP;
433+
388434
this.sourceCode = null;
389435
this.cleanupRange = null;
390436
this.targetPath = null;
@@ -402,6 +448,5 @@ private CommandLineArgs(String sourceDir, String[] sourcePaths, String targetDir
402448
this.showStats = showStats;
403449
this.showUsedRules = showUsedRules;
404450
this.errors = errors;
405-
this.showHelp = showHelp;
406451
}
407452
}

docs/usage.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,30 @@ To integrate ABAP cleaner in other processes, you may call the "abap-cleaner**c*
101101
from the command line with the following arguments for cleaning either a single file or multiple files in a directory:
102102

103103
```
104-
Usage for single file:
104+
Getting help and version information:
105+
.\abap-cleanerc.exe /?
106+
.\abap-cleanerc.exe /man
107+
.\abap-cleanerc.exe --version
108+
109+
Cleanup of single file:
105110
.\abap-cleanerc.exe {--sourcefile sourcefile / --source sourcecode } [--linerange linerange]
106111
[{ --profile profile / --profiledata profiledata }] [--release release]
107112
[--crlf] [--targetfile targetfile [--overwrite]] [--partialresult]
108113
[--stats] [--usedrules]
109114
110-
Example for single file:
115+
Example for cleanup of single file:
111116
.\abap-cleanerc.exe --sourcefile "CL_ANY_CLASS.txt" --linerange "20-35" --profile "team profile.cfj" --release "757" --targetfile "result\CL_ANY_CLASS.txt" --overwrite --stats --usedrules
112117
113-
Usage for multiple files:
118+
Cleanup of multiple files:
114119
.\abap-cleanerc.exe --sourcedir sourcedir [--filepattern filepattern] [--recursive]
115120
[{ --profile profile / --profiledata profiledata }] [--release release]
116121
[--targetdir targetdir [--overwrite]]
117122
[--stats] [--usedrules]
118123
119-
Example for multiple files:
124+
Example for cleanup of multiple files:
120125
.\abap-cleanerc.exe --sourcedir "C:\temp\source" --filepattern "*.txt" --recursive --profile "team profile.cfj" --release "757" --targetdir "C:\temp\target" --overwrite
121126
122-
Options:
127+
Options for cleanup:
123128
--sourcefile File name of an ABAP source file which is input to the cleanup.
124129
--source ABAP source code which is input to the cleanup.
125130
Please use either --sourcefile or --source or --sourcedir.

test/com.sap.adt.abapcleaner.test/src/com/sap/adt/abapcleaner/programbase/CommandLineArgsTest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,24 @@ void testCreateEmpty() {
4949
@Test
5050
void testCreateForHelp() {
5151
CommandLineArgs args = CommandLineArgs.create(persistency, new String[] { "/?" } );
52-
assertTrue(args.showHelp);
52+
assertEquals(CommandLineAction.SHOW_HELP, args.action);
5353

5454
args = CommandLineArgs.create(persistency, new String[] { "/man" } );
55-
assertTrue(args.showHelp);
55+
assertEquals(CommandLineAction.SHOW_HELP, args.action);
56+
}
57+
58+
@Test
59+
void testCreateForVersion() {
60+
CommandLineArgs args = CommandLineArgs.create(persistency, new String[] { "--version" } );
61+
assertEquals(CommandLineAction.SHOW_VERSION, args.action);
5662
}
5763

5864
@Test
5965
void testCreateFromSourceCodeWithNoOptions() {
6066
CommandLineArgs args = CommandLineArgs.create(persistency, new String[] {
6167
"--source", anySourceCode } );
6268

69+
assertEquals(CommandLineAction.CLEANUP, args.action);
6370
assertEquals(anySourceCode, args.sourceCode);
6471
assertNull(args.cleanupRange);
6572
assertNull(args.profileData);
@@ -72,7 +79,6 @@ void testCreateFromSourceCodeWithNoOptions() {
7279
assertFalse(args.showStats);
7380
assertFalse(args.showUsedRules);
7481
assertEquals("", args.errors);
75-
assertFalse(args.showHelp);
7682

7783
assertFalse(args.hasErrors());
7884
assertTrue(args.isInSingleSourceMode());
@@ -97,6 +103,7 @@ void testCreateFromSourceFileWithAllOptions() {
97103
"--targetfile", targetPath,
98104
"--overwrite", "--partialresult", "--stats", "--usedrules"} );
99105

106+
assertEquals(CommandLineAction.CLEANUP, args.action);
100107
assertEquals(anySourceCode, args.sourceCode);
101108
assertEquals(20, args.cleanupRange.startLine);
102109
assertEquals(35, args.cleanupRange.lastLine);
@@ -111,7 +118,6 @@ void testCreateFromSourceFileWithAllOptions() {
111118
assertTrue(args.showStats);
112119
assertTrue(args.showUsedRules);
113120
assertEquals("", args.errors);
114-
assertFalse(args.showHelp);
115121

116122
assertFalse(args.hasErrors());
117123
assertTrue(args.isInSingleSourceMode());
@@ -127,6 +133,7 @@ void testCreateFromSourceDir() {
127133
"--recursive",
128134
"--overwrite"} );
129135

136+
assertEquals(CommandLineAction.CLEANUP, args.action);
130137
assertEquals(1, args.sourcePaths.length);
131138
assertTrue(args.overwrite);
132139
}
@@ -143,6 +150,7 @@ void testCreateFromSourceDirWithTargetdir() {
143150
"--recursive",
144151
"--overwrite" } );
145152

153+
assertEquals(CommandLineAction.CLEANUP, args.action);
146154
assertEquals(1, args.sourcePaths.length);
147155
assertTrue(args.overwrite);
148156

@@ -157,6 +165,7 @@ void testCreateWithProfileDataAndLineRangeStartOnly() {
157165
"--source", anySourceCode,
158166
"--profiledata", anyProfileData } );
159167

168+
assertEquals(CommandLineAction.CLEANUP, args.action);
160169
assertEquals(anySourceCode, args.sourceCode);
161170
assertEquals(anyProfileData, args.profileData);
162171
assertFalse(args.hasErrors());

0 commit comments

Comments
 (0)