Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix use of --help with missing required command arguments #327

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading