-
Notifications
You must be signed in to change notification settings - Fork 422
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
Improve (shorter) error message on invalid input #480
Labels
Milestone
Comments
This was referenced Oct 23, 2018
Update: from 4.0, the code that handles this lives in the default Current implementation: public int handleParseException(ParameterException ex, String[] args) {
CommandLine cmd = ex.getCommandLine();
DefaultExceptionHandler.internalHandleParseException(ex, cmd.getErr(), cmd.getColorScheme());
return mappedExitCode(ex, cmd.getExitCodeExceptionMapper(), cmd.getCommandSpec().exitCodeOnInvalidInput());
}
...
class DefaultExceptionHandler {
static void internalHandleParseException(ParameterException ex, PrintWriter writer, Help.ColorScheme colorScheme) {
writer.println(ex.getMessage());
if (!UnmatchedArgumentException.printSuggestions(ex, writer)) {
ex.getCommandLine().usage(writer, colorScheme);
}
}
}
...
private static int mappedExitCode(Throwable t, IExitCodeExceptionMapper mapper, int defaultExitCode) {
try {
return (mapper != null) ? mapper.getExitCode(t) : defaultExitCode;
} catch (Exception ex) {
ex.printStackTrace();
return defaultExitCode;
}
} |
From 4.0, custom code to accomplish this would look something like this: static class ShortErrorMessageHandler implements IParameterExceptionHandler {
public int handleParseException(ParameterException ex, String[] args) {
CommandLine cmd = ex.getCommandLine();
PrintWriter writer = cmd.getErr();
writer.println(ex.getMessage());
UnmatchedArgumentException.printSuggestions(ex, writer);
writer.print(cmd.getHelp().fullSynopsis()); // since 4.1
CommandSpec spec = cmd.getCommandSpec();
writer.printf("Try '%s --help' for more information.%n", spec.qualifiedName());
return cmd.getExitCodeExceptionMapper() != null
? cmd.getExitCodeExceptionMapper().getExitCode(ex)
: spec.exitCodeOnInvalidInput();
}
} Install the custom handler like this: new CommandLine(new MyApp())
.setParameterExceptionHandler(new ShortErrorMessageHandler())
.execute(args); |
remkop
added a commit
that referenced
this issue
Oct 11, 2019
remkop
added a commit
that referenced
this issue
Oct 12, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Improve the error message shown on invalid input:
Example:
The help hint should be configurable with something like
@Command(invalidInputMessage = { “Try 'grep --help' for more information.” })
.Then, if
invalidInputMessage
is set, the default Exception handler would show only the synopsis followed by this message. If not set, the full usage help message is shown.TBD: how to make this available to applications that do their own error handling? For example:
The text was updated successfully, but these errors were encountered: