Skip to content

Commit

Permalink
Fixed issue causing CLI errors when using --help or --version, while …
Browse files Browse the repository at this point in the history
…not providing a required command parameter. Resolves #293. (#327)
  • Loading branch information
david-waltermire authored Mar 14, 2024
1 parent 0b573a0 commit 4ddd632
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,36 +344,52 @@ protected Options toOptions() {
public ExitStatus processCommand() {
CommandLineParser parser = new DefaultParser();
CommandLine cmdLine;
try {
cmdLine = parser.parse(toOptions(), getExtraArgs().toArray(new String[0]));
} catch (ParseException ex) {
String msg = ex.getMessage();
assert msg != null;
return handleInvalidCommand(msg);
}

if (cmdLine.hasOption(NO_COLOR_OPTION)) {
handleNoColor();
}

if (cmdLine.hasOption(QUIET_OPTION)) {
handleQuiet();
}
// this uses a two phase approach where:
// phase 1: checks if help or version are used
// phase 2: executes the command

// phase 1
ExitStatus retval = null;
if (cmdLine.hasOption(VERSION_OPTION)) {
showVersion();
retval = ExitCode.OK.exit();
} else if (cmdLine.hasOption(HELP_OPTION)) {
showHelp();
retval = ExitCode.OK.exit();
// } else {
// retval = handleInvalidCommand(commandResult, options,
// "Invalid command arguments: " +
// cmdLine.getArgList().stream().collect(Collectors.joining(" ")));
{
try {
Options phase1Options = new Options();
phase1Options.addOption(HELP_OPTION);
phase1Options.addOption(VERSION_OPTION);

cmdLine = parser.parse(phase1Options, getExtraArgs().toArray(new String[0]), true);
} catch (ParseException ex) {
String msg = ex.getMessage();
assert msg != null;
return handleInvalidCommand(msg);
}

if (cmdLine.hasOption(VERSION_OPTION)) {
showVersion();
retval = ExitCode.OK.exit();
} else if (cmdLine.hasOption(HELP_OPTION)) {
showHelp();
retval = ExitCode.OK.exit();
}
}

if (retval == null) {
// phase 2
try {
cmdLine = parser.parse(toOptions(), getExtraArgs().toArray(new String[0]));
} catch (ParseException ex) {
String msg = ex.getMessage();
assert msg != null;
return handleInvalidCommand(msg);
}

if (cmdLine.hasOption(NO_COLOR_OPTION)) {
handleNoColor();
}

if (cmdLine.hasOption(QUIET_OPTION)) {
handleQuiet();
}
retval = invokeCommand(cmdLine);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ private static Stream<Arguments> providesValues() {
{
add(Arguments.of(new String[] {}, ExitCode.INVALID_COMMAND, NO_EXCEPTION_CLASS));
add(Arguments.of(new String[] { "-h" }, ExitCode.OK, NO_EXCEPTION_CLASS));
add(Arguments.of(new String[] { "generate-schema", "--help" }, ExitCode.INVALID_COMMAND,
add(Arguments.of(new String[] { "generate-schema", "--help" }, ExitCode.OK,
NO_EXCEPTION_CLASS));
add(Arguments.of(new String[] { "validate", "--help" }, ExitCode.OK, NO_EXCEPTION_CLASS));
add(Arguments.of(new String[] { "validate-content", "--help" }, ExitCode.INVALID_COMMAND,
add(Arguments.of(new String[] { "validate-content", "--help" }, ExitCode.OK,
NO_EXCEPTION_CLASS));
add(Arguments.of(
new String[] { "validate",
Expand Down

0 comments on commit 4ddd632

Please sign in to comment.