Skip to content

Commit

Permalink
[#678][#575] Use mixinStandardHelpOptions in AutoComplete$App (add …
Browse files Browse the repository at this point in the history
…the `--version` option); add Exit Status section in usage help message.

Closes #678
Closes #575
  • Loading branch information
remkop committed May 4, 2019
1 parent 82a4b79 commit 418670e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ 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://github.com/nicolasmingo) and [jrevault](https://github.com/jrevault) for raising this and subsequent discussion.
- [#672] Need way to send errors back from subcommand. Thanks to [Garret Wilson](https://github.com/garretwilson) for raising this.
- [#678] Exit Status section in usage help message.
- [#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://github.com/ifedorenko) for raising this.
- [#679] Documentation: Update examples for new execute API. Add examples for exit code control and custom exception handlers.

Expand Down
23 changes: 17 additions & 6 deletions src/main/java/picocli/AutoComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.util.ArrayList;
Expand All @@ -35,6 +36,7 @@
import picocli.CommandLine.Model.OptionSpec;

import static java.lang.String.*;
import static picocli.CommandLine.Model.UsageMessageSpec.keyValuesMap;

/**
* Stand-alone tool that generates bash auto-complete scripts for picocli-based command line applications.
Expand Down Expand Up @@ -69,6 +71,14 @@ 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 All @@ -91,9 +101,10 @@ private static boolean syspropDefinedAndNotFalse(String key) {
/**
* CLI command class for generating completion script.
*/
@Command(name = "picocli.AutoComplete", sortOptions = false,
@Command(name = "picocli.AutoComplete", mixinStandardHelpOptions = true,
version = "picocli.AutoComplete v4.0.0-alpha-3-SNAPSHOT", sortOptions = false,
description = "Generates a bash completion script for the specified command class.",
footerHeading = "%n@|bold Exit Code|@%n",
footerHeading = "%n@|bold System Properties:|@%n",
footer = {"Set the following system properties to control the exit code of this program:",
" \"@|yellow picocli.autocomplete.systemExitOnSuccess|@\" - call `System.exit(0)` when",
" execution completes normally",
Expand Down Expand Up @@ -132,8 +143,7 @@ private static class App implements Callable<Integer> {
@Option(names = {"-f", "--force"}, description = "Overwrite existing script files.")
boolean overwriteIfExists;

@Option(names = { "-h", "--help"}, usageHelp = true, description = "Display this help message and quit.")
boolean usageHelpRequested;
@Spec CommandSpec spec;

public Integer call() throws Exception {
IFactory factory = CommandLine.defaultFactory();
Expand Down Expand Up @@ -170,8 +180,9 @@ public Integer call() throws Exception {

private boolean checkExists(final File file) {
if (file.exists()) {
System.err.printf("ERROR: picocli.AutoComplete: %s exists. Specify --force to overwrite.%n", file.getAbsolutePath());
CommandLine.usage(this, System.err);
PrintWriter err = spec.commandLine().getErr();
err.printf("ERROR: picocli.AutoComplete: %s exists. Specify --force to overwrite.%n", file.getAbsolutePath());
spec.commandLine().usage(err);
return true;
}
return false;
Expand Down
17 changes: 13 additions & 4 deletions src/test/java/picocli/AutoCompleteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private static String toString(Object obj) {
}

private static final String AUTO_COMPLETE_APP_USAGE = String.format("" +
"Usage: picocli.AutoComplete [-fhw] [-c=<factoryClass>] [-n=<commandName>]%n" +
"Usage: picocli.AutoComplete [-fhVw] [-c=<factoryClass>] [-n=<commandName>]%n" +
" [-o=<autoCompleteScript>] <commandLineFQCN>%n" +
"Generates a bash completion script for the specified command class.%n" +
" <commandLineFQCN> Fully qualified class name of the annotated @Command%n" +
Expand All @@ -210,9 +210,18 @@ private static String toString(Object obj) {
" -w, --writeCommandScript Write a '<commandName>' sample command script to the%n" +
" same directory as the completion script.%n" +
" -f, --force Overwrite existing script files.%n" +
" -h, --help Display this help message and quit.%n" +
" -h, --help Show this help message and exit.%n" +
" -V, --version Print version information and exit.%n" +
"%n" +
"Exit Code%n" +
"Exit Codes:%n" +
" 0 Successful program execution%n" +
" 1 Usage error: user input for the command was incorrect, e.g., the wrong%n" +
" number of arguments, a bad flag, a bad syntax in a parameter, etc.%n" +
" 2 The specified command script exists (Specify --force to overwrite).%n" +
" 3 The specified completion script exists (Specify --force to overwrite).%n" +
" 4 An exception occurred while generating the completion script.%n" +
"%n" +
"System Properties:%n" +
"Set the following system properties to control the exit code of this program:%n" +
" \"picocli.autocomplete.systemExitOnSuccess\" - call `System.exit(0)` when%n" +
" execution completes normally%n" +
Expand Down Expand Up @@ -639,7 +648,7 @@ private String expectedCompletionScriptForAutoCompleteApp() {
" PREV_WORD=${COMP_WORDS[COMP_CWORD-1]}\n" +
"\n" +
" COMMANDS=\"\"\n" +
" FLAG_OPTS=\"-w --writeCommandScript -f --force -h --help\"\n" +
" FLAG_OPTS=\"-w --writeCommandScript -f --force -h --help -V --version\"\n" +
" ARG_OPTS=\"-c --factory -n --name -o --completionScript\"\n" +
"\n" +
" compopt +o default\n" +
Expand Down

0 comments on commit 418670e

Please sign in to comment.