@Retention(value=RUNTIME) @Target(value={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 and 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.
|
public abstract 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
public abstract 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)
public abstract String separator
"="
. Spaces are also accepted.CommandLine.Help.separator
,
CommandLine.setSeparator(String)
public abstract String[] version
CommandLine.printVersionHelp(PrintStream)
public abstract String headerHeading
CommandLine.Help.headerHeading(Object...)
public abstract String[] header
CommandLine.Help.header
,
CommandLine.Help.header(Object...)
public abstract String synopsisHeading
"Usage: "
(without a line
break between the heading and the synopsis text).CommandLine.Help.synopsisHeading(Object...)
public abstract 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)
public abstract String[] customSynopsis
CommandLine.Help.customSynopsis
,
CommandLine.Help.customSynopsis(Object...)
public abstract String descriptionHeading
CommandLine.Help.descriptionHeading(Object...)
public abstract String[] description
CommandLine.Help.description
,
CommandLine.Help.description(Object...)
public abstract String parameterListHeading
CommandLine.Help.parameterListHeading(Object...)
public abstract String optionListHeading
CommandLine.Help.optionListHeading(Object...)
public abstract boolean sortOptions
false
to show Options in declaration order. The default is to sort alphabetically.CommandLine.Help.sortOptions
public abstract char requiredOptionMarker
CommandLine.Help.requiredOptionMarker
public abstract 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
public abstract String commandListHeading
"Commands:%n"
(with a line break at the end).CommandLine.Help.commandListHeading(Object...)
public abstract String footerHeading
CommandLine.Help.footerHeading(Object...)
public abstract String[] footer
CommandLine.Help.footer
,
CommandLine.Help.footer(Object...)
Copyright © 1999-2023 The Apache Software Foundation. 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.