Skip to content

Commit c9a5c83

Browse files
Fixed issue causing CLI errors when using --help or --version, while not providing a required command parameter. Resolves #293.
1 parent 6c65ce7 commit c9a5c83

File tree

2 files changed

+42
-26
lines changed
  • cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor
  • metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli

2 files changed

+42
-26
lines changed

cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -344,36 +344,52 @@ protected Options toOptions() {
344344
public ExitStatus processCommand() {
345345
CommandLineParser parser = new DefaultParser();
346346
CommandLine cmdLine;
347-
try {
348-
cmdLine = parser.parse(toOptions(), getExtraArgs().toArray(new String[0]));
349-
} catch (ParseException ex) {
350-
String msg = ex.getMessage();
351-
assert msg != null;
352-
return handleInvalidCommand(msg);
353-
}
354347

355-
if (cmdLine.hasOption(NO_COLOR_OPTION)) {
356-
handleNoColor();
357-
}
358-
359-
if (cmdLine.hasOption(QUIET_OPTION)) {
360-
handleQuiet();
361-
}
348+
// this uses a two phase approach where:
349+
// phase 1: checks if help or version are used
350+
// phase 2: executes the command
362351

352+
// phase 1
363353
ExitStatus retval = null;
364-
if (cmdLine.hasOption(VERSION_OPTION)) {
365-
showVersion();
366-
retval = ExitCode.OK.exit();
367-
} else if (cmdLine.hasOption(HELP_OPTION)) {
368-
showHelp();
369-
retval = ExitCode.OK.exit();
370-
// } else {
371-
// retval = handleInvalidCommand(commandResult, options,
372-
// "Invalid command arguments: " +
373-
// cmdLine.getArgList().stream().collect(Collectors.joining(" ")));
354+
{
355+
try {
356+
Options phase1Options = new Options();
357+
phase1Options.addOption(HELP_OPTION);
358+
phase1Options.addOption(VERSION_OPTION);
359+
360+
cmdLine = parser.parse(phase1Options, getExtraArgs().toArray(new String[0]), true);
361+
} catch (ParseException ex) {
362+
String msg = ex.getMessage();
363+
assert msg != null;
364+
return handleInvalidCommand(msg);
365+
}
366+
367+
if (cmdLine.hasOption(VERSION_OPTION)) {
368+
showVersion();
369+
retval = ExitCode.OK.exit();
370+
} else if (cmdLine.hasOption(HELP_OPTION)) {
371+
showHelp();
372+
retval = ExitCode.OK.exit();
373+
}
374374
}
375375

376376
if (retval == null) {
377+
// phase 2
378+
try {
379+
cmdLine = parser.parse(toOptions(), getExtraArgs().toArray(new String[0]));
380+
} catch (ParseException ex) {
381+
String msg = ex.getMessage();
382+
assert msg != null;
383+
return handleInvalidCommand(msg);
384+
}
385+
386+
if (cmdLine.hasOption(NO_COLOR_OPTION)) {
387+
handleNoColor();
388+
}
389+
390+
if (cmdLine.hasOption(QUIET_OPTION)) {
391+
handleQuiet();
392+
}
377393
retval = invokeCommand(cmdLine);
378394
}
379395

metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ private static Stream<Arguments> providesValues() {
7070
{
7171
add(Arguments.of(new String[] {}, ExitCode.INVALID_COMMAND, NO_EXCEPTION_CLASS));
7272
add(Arguments.of(new String[] { "-h" }, ExitCode.OK, NO_EXCEPTION_CLASS));
73-
add(Arguments.of(new String[] { "generate-schema", "--help" }, ExitCode.INVALID_COMMAND,
73+
add(Arguments.of(new String[] { "generate-schema", "--help" }, ExitCode.OK,
7474
NO_EXCEPTION_CLASS));
7575
add(Arguments.of(new String[] { "validate", "--help" }, ExitCode.OK, NO_EXCEPTION_CLASS));
76-
add(Arguments.of(new String[] { "validate-content", "--help" }, ExitCode.INVALID_COMMAND,
76+
add(Arguments.of(new String[] { "validate-content", "--help" }, ExitCode.OK,
7777
NO_EXCEPTION_CLASS));
7878
add(Arguments.of(
7979
new String[] { "validate",

0 commit comments

Comments
 (0)