@Retention(RUNTIME) @Target({TYPE,LOCAL_VARIABLE,PACKAGE}) public static @interface CommandLine.Command
Annotate your class with @Command
when you want more control over the format of the generated help
message.
@Command(name = "Encrypt", description = "Encrypt FILE(s), or standard input, to standard output or to the output file.", footer = "Copyright (c) 2017") public class Encrypt { @Parameters(paramLabel = "FILE", type = File.class, description = "Any number of input files") private List<File> files = new ArrayList<File>(); @Option(names = { "-o", "--out" }, description = "Output file (default: print to console)") private File outputFile; }
The structure of a help message looks like this:
Usage: <commandName> [OPTIONS] [FILE...]
[FILE...] Any number of input files
-h, --help prints this help message and exits
Modifier and Type | Optional Element | Description |
---|---|---|
boolean |
abbreviateSynopsis |
Specify
true to generate an abbreviated synopsis like "<main> [OPTIONS] [PARAMETERS...]" . |
String |
commandListHeading |
Set the heading preceding the subcommands list.
|
String[] |
customSynopsis |
Specify one or more custom synopsis lines to display instead of an auto-generated synopsis.
|
String[] |
description |
Optional text to display between the synopsis line(s) and the list of options.
|
String |
descriptionHeading |
Set the heading preceding the description section.
|
String[] |
footer |
Optional text to display after the list of options.
|
String |
footerHeading |
Set the heading preceding the footer section.
|
String[] |
header |
Optional summary description of the command, shown before the synopsis.
|
String |
headerHeading |
Set the heading preceding the header section.
|
String |
name |
Program name to show in the synopsis.
|
String |
optionListHeading |
Set the heading preceding the options list.
|
String |
parameterListHeading |
Set the heading preceding the parameters list.
|
char |
requiredOptionMarker |
Prefix required options with this character in the options list.
|
String |
separator |
String that separates options from option parameters.
|
boolean |
showDefaultValues |
Specify
true to show default values in the description column of the options list (except for
boolean options). |
boolean |
sortOptions |
Specify
false to show Options in declaration order. |
Class<?>[] |
subcommands |
A list of classes to instantiate and register as subcommands.
|
String |
synopsisHeading |
Set the heading preceding the synopsis text.
|
String[] |
version |
Version information for this command, to print to the console when the user specifies an
option to request version help.
|
String name
"<main class>"
is used.
For declaratively added subcommands, this attribute is also used
by the parser to recognize subcommands in the command line arguments.CommandLine.Help.commandName
Class<?>[] subcommands
CommandLine.addSubcommand(String, Object)
method. For example, this:
@Command(subcommands = { GitStatus.class, GitCommit.class, GitBranch.class }) public class Git { ... } CommandLine commandLine = new CommandLine(new Git());is equivalent to this:
// alternative: programmatically add subcommands. // NOTE: in this case there should be no `subcommands` attribute on the @Command annotation. @Command public class Git { ... } CommandLine commandLine = new CommandLine(new Git()) .addSubcommand("status", new GitStatus()) .addSubcommand("commit", new GitCommit()) .addSubcommand("branch", new GitBranch());
CommandLine.addSubcommand(String, Object)
String separator
"="
. Spaces are also accepted.CommandLine.Help.separator
,
CommandLine.setSeparator(String)
String[] version
CommandLine.printVersionHelp(PrintStream)
String headerHeading
CommandLine.Help.headerHeading(Object...)
String[] header
CommandLine.Help.header
,
CommandLine.Help.header(Object...)
String synopsisHeading
"Usage: "
(without a line
break between the heading and the synopsis text).CommandLine.Help.synopsisHeading(Object...)
boolean abbreviateSynopsis
true
to generate an abbreviated synopsis like "<main> [OPTIONS] [PARAMETERS...]"
.
By default, a detailed synopsis with individual option names and parameters is generated.CommandLine.Help.abbreviateSynopsis
,
CommandLine.Help.abbreviatedSynopsis()
,
CommandLine.Help.detailedSynopsis(Comparator, boolean)
String[] customSynopsis
CommandLine.Help.customSynopsis
,
CommandLine.Help.customSynopsis(Object...)
String descriptionHeading
CommandLine.Help.descriptionHeading(Object...)
String[] description
CommandLine.Help.description
,
CommandLine.Help.description(Object...)
String parameterListHeading
CommandLine.Help.parameterListHeading(Object...)
String optionListHeading
CommandLine.Help.optionListHeading(Object...)
boolean sortOptions
false
to show Options in declaration order. The default is to sort alphabetically.CommandLine.Help.sortOptions
char requiredOptionMarker
CommandLine.Help.requiredOptionMarker
boolean showDefaultValues
true
to show default values in the description column of the options list (except for
boolean options). False by default.CommandLine.Help.showDefaultValues
String commandListHeading
"Commands:%n"
(with a line break at the end).CommandLine.Help.commandListHeading(Object...)
String footerHeading
CommandLine.Help.footerHeading(Object...)
String[] footer
CommandLine.Help.footer
,
CommandLine.Help.footer(Object...)
Copyright © 1999-2018. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.