Changelog History
Page 33
-
v3.0.0-mixinStandardHelpOptions Changes
π This release introduces the
mixinStandardHelpOptions
command attribute. When this attribute is set totrue
, picocli adds a mixin to the command that addsusageHelp
andversionHelp
options to the command. For example:@Command(mixinStandardHelpOptions = true, version = "auto help demo - picocli 3.0") class AutoHelpDemo implements Runnable { @Option(names = "--option", description = "Some option.") String option; @Override public void run() { } }
Commands with
mixinStandardHelpOptions
do not need to explicitly declare fields annotated with@Option(usageHelp = true)
and@Option(versionHelp = true)
any more. The usage help message for the above example looks like this:Usage: <main class> [-hV] [--option=<option>] --option=<option> Some option. -h, --help Show this help message and exit. -V, --version Print version information and exit.
-
v3.0.0-HelpCommand Changes
π From this release, picocli provides a
help
subcommand (picocli.CommandLine.HelpCommand
) that can be installed as a subcommand on any application command to provide usage help for the parent command or sibling subcommands. For example:@Command(subcommands = HelpCommand.class) class AutoHelpDemo implements Runnable { @Option(names = "--option", description = "Some option.") String option; @Override public void run() { } }
-
v3.0.0-fixes Changes
- π [#371] Fixed bug where autocompletion did not work correctly for subcommands with embedded hyphens. Thanks to Paulius Fadelis for the bug report.
- [#372] Simplify Kotlin example in user manual. Thanks to Dustin Spicuzza.
-
v3.0.0-exit Changes
0οΈβ£ From picocli v3.0, the built-in parse result handlers (
RunFirst
,RunLast
andRunAll
) and exception handler (DefaultExceptionHandler
) can specify an exit code. If an exit code was specified, the handler terminates the JVM with the specified status code when finished.@Command class ExitCodeDemo implements Runnable { public void run() { throw new ParameterException(new CommandLine(this), "exit code demo"); } public static void main(String... args) { CommandLine cmd = new CommandLine(new ExitCodeDemo()); cmd.parseWithHandlers( new RunLast().andExit(123), new DefaultExceptionHandler().andExit(456), args); } }
π¨ Running this command prints the following to stderr and exits the JVM with status code
456
.exit code demo Usage: <main class>
Custom handlers can extend
AbstractHandler
to inherit this behaviour. -
v3.0.0-deprecated
-
v3.0.0-breaking
-
v3.0.0-beta Changes
π Previously, the usage message layout had a fixed width long option name column: this column is always 24 characters, even if none of the options have a long option name.
This gives weird-looking usage help messages in some cases. For example:
@Command(name="ls") class App { @Option(names = "-a", description = "display all files") boolean a; @Option(names = "-l", description = "use a long listing format") boolean l; @Option(names = "-t", description = "sort by modification time") boolean t; }
The usage message for this example was:
Usage: ls [-alt] -a display all files -l use a long listing format -t sort by modification time
π From this release, picocli adjusts the width of the long option name column to the longest name (max 24).
The new usage message for this example looks like this:
Usage: ls [-alt] -a display all files -l use a long listing format -t sort by modification time
-
v3.0.0-alpha Changes
π This release offers a programmatic API for creating command line applications, in addition to annotations. The programmatic API allows applications to dynamically create command line options on the fly, and also makes it possible to create idiomatic domain-specific languages for processing command line arguments, using picocli, in other JVM languages.
Note that the programmatic API is incubating and the API may change in subsequent releases. If you have suggestions for improving the programmatic API, please raise a ticket on GitHub!
Example
CommandSpec spec = CommandSpec.create(); spec.mixinStandardHelpOptions(true); // usageHelp and versionHelp options spec.addOption(OptionSpec.builder("-c", "--count") .paramLabel("COUNT") .type(int.class) .description("number of times to execute").build()); spec.addPositional(PositionalParamSpec.builder() .paramLabel("FILES") .type(List.class) .auxiliaryTypes(File.class) // List<File> .description("The files to process").build()); CommandLine commandLine = new CommandLine(spec); commandLine.parseWithSimpleHandlers(new AbstractSimpleParseResultHandler() { public void handle(ParseResult pr) { int count = pr.optionValue('c', 1); List<File> files = pr.positionalValue(0, Collections.<File>emptyList()); for (int i = 0; i < count; i++) { for (File f : files) { System.out.printf("%d: %s%n", i, f); } } } }, args);
CommandSpec (INCUBATING)
π§
CommandSpec
models a command. It is the programmatic variant of the@Command
annotation. It has a name and a version, both of which may be empty. It also has aUsageMessageSpec
to configure aspects of the usage help message and aParserSpec
that can be used to control the behaviour of the parser.OptionSpec and PositionalParamSpec (INCUBATING)
OptionSpec
models a named option, andPositionalParamSpec
models one or more positional parameters. They are the programmatic variant of the@Option
and@Parameters
annotations, respectively.π An
OptionSpec
must have at least one name, which is used during parsing to match command line arguments. Other attributes can be left empty and picocli will give them a reasonable default value. This defaulting is whyOptionSpec
objects are created with a builder: this allows you to specify only some attributes and let picocli initialise the other attributes. For example, if only the optionβs name is specified, picocli assumes the option takes no parameters (arity = 0), and is of typeboolean
. Another example, if arity is larger than1
, picocli sets the type toList
and theauxiliary type
toString
.0οΈβ£
PositionalParamSpec
objects donβt have names, but have an index range instead. A singlePositionalParamSpec
object can capture multiple positional parameters. The default index range is set to0..*
(all indices). A command may have multiplePositionalParamSpec
objects to capture positional parameters at different index ranges. This can be useful if positional parameters at different index ranges have different data types.π§ Similar to
OptionSpec
objects, Once aPositionalParamSpec
is constructed, its configuration becomes immutable, but itsvalue
can still be modified. Usually the value is set during command line parsing when a non-option command line argument is encountered at a position in its index range. -
v2.3.0-promoted Changes
π Promoted features are features that were incubating in previous versions of picocli but are now supported and subject to backwards compatibility.
π No features have been promoted in this picocli release.