-
Notifications
You must be signed in to change notification settings - Fork 426
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
subcommand method ParameterException is hidden by InvocationTargetException #554
Comments
Can you please provide a way to reproduce the problem? Ideally some source code for the command and user input that triggers the problem. |
I was able to reproduce the issue with the following test: class Test538 {
@Rule
public final SystemErrRule systemErrRule = new SystemErrRule().enableLog().muteForSuccessfulTests();
@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog().muteForSuccessfulTests();
/** Test for https://github.com/remkop/picocli/issues/554 */
@Command(name = "maincommand")
class MainCommand implements Runnable {
@Spec CommandSpec spec;
public void run() { throw new UnsupportedOperationException("must specify a subcommand"); }
@Command
public void subcommand(@Option(names = "-x") String x) {
System.out.println("x=" + x);
}
@Command
public void explicit(@Option(names = "-v") boolean v) {
CommandLine commandLine = spec.subcommands().get("explicit");
throw new CommandLine.ParameterException(commandLine, "Validation failed");
}
}
@Test
public void testSubcommandMethodExceptionHandling() {
String expected = String.format("" +
"Unknown option: -y%n" +
"Usage: maincommand subcommand [-x=<arg0>]%n" +
" -x=<arg0>%n");
CommandLine.run(new MainCommand(), "subcommand", "-y");
assertEquals(expected, this.systemErrRule.getLog());
assertEquals("", this.systemOutRule.getLog());
}
@Test
public void testSubcommandMethodThrowingParameterException() {
String expected = String.format("" +
"Validation failed%n" +
"Usage: maincommand explicit [-v]%n" +
" -v%n");
CommandLine.run(new MainCommand(), "explicit", "-v");
assertEquals(expected, this.systemErrRule.getLog());
assertEquals("", this.systemOutRule.getLog());
}
} Note to self: the patch below fixes the problem.
I will apply this fix as soon as I can. |
Exactly. Thank you for looking into this. I just went looking into providing the required information, but you were faster. |
Fixed in master. |
picocli/src/main/java/picocli/CommandLine.java
Line 1072 in 9d086d9
Actual behaviour: Throwing a ParameterException from a command method is wrapped in InvocationTargetException and is not catched and handled as ParameterException. No help output.
Expected behaviour: ParameterExceptions get handled like in Callable or Runnable methods and print help.
The text was updated successfully, but these errors were encountered: