Skip to content

Commit

Permalink
[#682][#680] annotation API for exitCodeList and exitCodeListHeading;…
Browse files Browse the repository at this point in the history
… bugfix in interpolator
  • Loading branch information
remkop committed May 6, 2019
1 parent a9bc991 commit 57f76cc
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 134 deletions.
2 changes: 2 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ With the new execute API the ColorScheme class will start to play a more central
- [#663] How to remove stacktraces on error. Thanks to [Nicolas Mingo](https:/nicolasmingo) and [jrevault](https:/jrevault) for raising this and subsequent discussion.
- [#672] Need way to send errors back from subcommand. Thanks to [Garret Wilson](https:/garretwilson) for raising this.
- [#678] Exit Status section in usage help message.
- [#680] Add annotation API for exitCodeList and exitCodeListHeading.
- [#575] Use mixinStandardHelpOptions in `AutoComplete$App` (add the `--version` option)
- [#676] Bugfix: non-defined variables in `defaultValue` now correctly resolve to `null`, and options and positional parameters are now correctly considered `required` only if their default value is `null` after variable interpolation. Thanks to [ifedorenko](https:/ifedorenko) for raising this.
- [#682] Bug: incorrect evaluation for multiple occurrences of a variable.
- [#679] Documentation: Update examples for new execute API. Add examples for exit code control and custom exception handlers.
- [#681] Documentation: Add exit code section to Internationalization example in user manual.

Expand Down
65 changes: 37 additions & 28 deletions docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1330,35 +1330,12 @@ When the end user specified invalid input, the `execute` method prints an error
If the business logic of the command throws an exception, the `execute` method prints the stack trace of the exception and returns an exit code. This can be customized by configuring a `IExecutionExceptionHandler`.

=== Usage Help Exit Code Section
By default, the usage help message does not display the exit code section.
Applications that call `System.exit` need to configure this manually by calling `CommandLine.setExitCodeHelpSection(String, Map)`
or by calling `UsageMessageSpec.exitCodeListHeading` and `UsageMessageSpec.exitCodeList`. For example:
By default, the usage help message does not include exit code information.
Applications that call `System.exit` need to configure the usage help message to show exit code details,
either with the `exitCodeListHeading` and `exitCodeList` annotation attributes,
or programmatically by calling `UsageMessageSpec.exitCodeListHeading` and `UsageMessageSpec.exitCodeList`.

```java
// import static picocli.CommandLine.Model.UsageMessageSpec.keyValuesMap;
@Command class App {}
CommandLine cmd = new CommandLine(new App());
cmd.setExitCodeHelpSection("Exit Codes:%n",
keyValuesMap(" 0:Successful program execution",
"64:Usage error: user input for the command was incorrect, " +
"e.g., the wrong number of arguments, a bad flag, " +
"a bad syntax in a parameter, etc.",
"70:Internal software error: an exception occurred when invoking " +
"the business logic of this command."));
cmd.usage(System.out);
```

This will print the following message to the console:

```
Usage: <main class>
Exit Codes:
0 Successful program execution
64 Usage error: user input for the command was incorrect, e.g., the wrong
number of arguments, a bad flag, a bad syntax in a parameter, etc.
70 Internal software error: an exception occurred when invoking the
business logic of this command.
```
See <<Exit Code List>> for details.

=== Execution Configuration

Expand Down Expand Up @@ -2103,6 +2080,38 @@ Use the `footer` attribute to specify one or more lines to show below the genera

Each element of the attribute String array is displayed on a separate line.


=== Exit Code List
By default, the usage help message does not display <<Generating an Exit Code,exit code>> information.
Applications that call `System.exit` need to configure the `exitCodeListHeading` and `exitCodeList` annotation attributes.
For example:

```java
@Command(mixinStandardHelpOptions = true,
exitCodeListHeading = "Exit Codes:%n",
exitCodeList = {
" 0:Successful program execution",
"64:Usage error: user input for the command was incorrect, " +
"e.g., the wrong number of arguments, a bad flag, " +
"a bad syntax in a parameter, etc.",
"70:Internal software error: an exception occurred when invoking " +
"the business logic of this command."})
class App {}
new CommandLine(new App()).usage(System.out);
```

This will print the following usage help message to the console:

```
Usage: <main class>
Exit Codes:
0 Successful program execution
64 Usage error: user input for the command was incorrect, e.g., the wrong
number of arguments, a bad flag, a bad syntax in a parameter, etc.
70 Internal software error: an exception occurred when invoking the
business logic of this command.
```

=== Format Specifiers
All usage help message elements can have embedded line separator (`%n`) format specifiers.
These are converted to the platform-specific line separator when the usage help message is printed.
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/picocli/AutoComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,6 @@ public int handleExecutionException(Exception ex, CommandLine commandLine, Parse
};
int exitCode = new CommandLine(new App())
.setExecutionExceptionHandler(errorHandler)
.setExitCodeHelpSection("%nExit Codes:%n",
keyValuesMap("0:Successful program execution",
"1:Usage error: user input for the command was incorrect, " +
"e.g., the wrong number of arguments, a bad flag, " +
"a bad syntax in a parameter, etc.",
"2:The specified command script exists (Specify --force to overwrite).",
"3:The specified completion script exists (Specify --force to overwrite).",
"4:An exception occurred while generating the completion script."))
.execute(args);
if ((exitCode == EXIT_CODE_SUCCESS && exitOnSuccess()) || (exitCode != EXIT_CODE_SUCCESS && exitOnError())) {
System.exit(exitCode);
Expand Down Expand Up @@ -112,6 +104,16 @@ private static boolean syspropDefinedAndNotFalse(String key) {
" when an error occurs",
"If these system properties are not defined or have value \"false\", this program completes without terminating the JVM."
},
exitCodeListHeading = "%nExit Codes:%n",
exitCodeList = {
"0:Successful program execution",
"1:Usage error: user input for the command was incorrect, " +
"e.g., the wrong number of arguments, a bad flag, " +
"a bad syntax in a parameter, etc.",
"2:The specified command script exists (Specify --force to overwrite).",
"3:The specified completion script exists (Specify --force to overwrite).",
"4:An exception occurred while generating the completion script."
},
exitCodeOnInvalidInput = EXIT_CODE_INVALID_INPUT,
exitCodeOnExecutionException = EXIT_CODE_EXECUTION_ERROR)
private static class App implements Callable<Integer> {
Expand Down
Loading

0 comments on commit 57f76cc

Please sign in to comment.