Skip to content

Commit 9791bd9

Browse files
committed
[#2228] DOC: Clarify that ParseResult passed to IExecutionExceptionHandler is the top-level parse result
Closes #2228
1 parent d2359e0 commit 9791bd9

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Artifacts in this release are signed by Remko Popma (6601 E5C0 8DCC BB96).
3939
* [#2053] [#2175] CLEAN: Remove unused extra format arguments. Thanks to [Michael Vorburger](https://github.com/vorburger) for the pull request.
4040
* [#2171] DOC: Fix a few typos in CommandLine's JavaDoc. Thanks to [Michael Vorburger](https://github.com/vorburger) for the pull request.
4141
* [#2217] DOC: Clarify documentation for negatable options. Thanks to [dbear496](https://github.com/dbear496) for raising this.
42+
* [#2228] DOC: Clarify that `ParseResult` passed to `IExecutionExceptionHandler` is the top-level parse result, not the parse result of the subcommand that failed. Thanks to [Abel Salgado Romero](https://github.com/abelsromero) for raising this.
4243
* [#2047] DEP: Bump andymckay/append-gist-action from 1fbfbbce708a39bd45846f0955ed5521f2099c6d to 6e8d64427fe47cbacf4ab6b890411f1d67c07f3e
4344
* [#2091] DEP: Bump actions/checkout from 3.5.2 to 3.6.0
4445
* [#2108] DEP: Bump actions/checkout from 3.6.0 to 4.0.0

src/main/java/picocli/CommandLine.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,10 +1775,14 @@ public interface IExecutionExceptionHandler {
17751775
* {@code Callable} command and returns an exit code suitable for returning from {@link #execute(String...)}.
17761776
* @param ex the Exception thrown by the {@code Runnable}, {@code Callable} or {@code Method} user object of the command
17771777
* @param commandLine the CommandLine representing the command or subcommand where the exception occurred
1778-
* @param parseResult the result of parsing the command line arguments
1778+
* @param fullParseResult the result of parsing the command line arguments.
1779+
* This is the ParseResult of the <b>top-level command</b>.
1780+
* Note that if the exception occurred in a subcommand, you may want to inspect the ParseResult of
1781+
* the subcommand that threw the exception, which can be obtained by calling {@code commandLine.getParseResult()}
1782+
* on the CommandLine object passed to this method.
17791783
* @return an exit code
17801784
*/
1781-
int handleExecutionException(Exception ex, CommandLine commandLine, ParseResult parseResult) throws Exception;
1785+
int handleExecutionException(Exception ex, CommandLine commandLine, ParseResult fullParseResult) throws Exception;
17821786
}
17831787

17841788
/** Abstract superclass for {@link IParseResultHandler2} and {@link IExceptionHandler2} implementations.

src/test/java/picocli/Issue2228.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99

1010
public class Issue2228 {
1111

12-
@Command
13-
static class TestCommand implements Runnable {
12+
@Command(subcommands = Issue2228.SubCommand.class)
13+
static class TestCommand {
14+
}
15+
16+
@Command(name = "subsub")
17+
static class SubCommand implements Runnable {
1418

15-
@Option(names = "-x")
16-
public boolean x;
19+
@Option(names = "-y")
20+
public boolean y;
1721

1822
public void run() {
19-
throw new IllegalStateException("failing, just for fun");
23+
throw new IllegalStateException("Y failing, just for fun");
2024
}
2125
}
2226

@@ -25,23 +29,25 @@ public void testParseResult() {
2529
final CommandLine commandLine = new CommandLine(new Issue2228.TestCommand());
2630
final boolean[] handled = new boolean[] {false};
2731
commandLine.setExecutionExceptionHandler(new CommandLine.IExecutionExceptionHandler() {
28-
public int handleExecutionException(Exception ex, CommandLine exCmdLine, ParseResult parseResult) throws Exception {
32+
public int handleExecutionException(Exception ex, CommandLine exCmdLine, ParseResult fullParseResult) throws Exception {
2933
handled[0] = true;
30-
assertSame(commandLine, exCmdLine);
3134

32-
ParseResult after = commandLine.getParseResult();
33-
printParseResult(after, "commandLine.getParseResult()");
34-
assertFalse(after.matchedArgs().isEmpty());
35-
assertFalse(after.matchedOptions().isEmpty());
35+
ParseResult subResult = commandLine.getSubcommands().get("subsub").getParseResult();
36+
//printParseResult(subResult, "subsub subcommand");
37+
assertFalse(subResult.matchedArgs().isEmpty());
38+
assertFalse(subResult.matchedOptions().isEmpty());
39+
40+
//printParseResult(fullParseResult, "ExecutionExceptionHandler method arg");
41+
assertNotNull(fullParseResult);
42+
assertFalse(fullParseResult.subcommand().matchedArgs().isEmpty());
43+
assertFalse(fullParseResult.subcommand().matchedOptions().isEmpty());
3644

37-
printParseResult(parseResult, "ExecutionExceptionHandler method arg");
38-
assertNotNull(parseResult);
39-
assertFalse(parseResult.matchedArgs().isEmpty());
40-
assertFalse(parseResult.matchedOptions().isEmpty());
45+
assertTrue(fullParseResult.matchedArgs().isEmpty());
46+
assertTrue(fullParseResult.matchedOptions().isEmpty());
4147
return 0;
4248
}
4349
});
44-
commandLine.execute("-x");
50+
commandLine.execute("subsub", "-y");
4551
assertTrue("ExecutionExceptionHandler tests were executed", handled[0]);
4652
}
4753

0 commit comments

Comments
 (0)