public static class CommandLine.Help extends Object
The CommandLine.Command
annotation provides the easiest way to customize usage help messages. See
the Manual for details.
This Help class provides high-level functions to create sections of the usage help message and headings
for these sections. Instead of calling the CommandLine.usage(PrintStream, CommandLine.Help.ColorScheme)
method, application authors may want to create a custom usage help message by reorganizing sections in a
different order and/or adding custom sections.
Finally, the Help class contains inner classes and interfaces that can be used to create custom help messages.
Renders a field annotated with CommandLine.Option
or CommandLine.Parameters
to an array of CommandLine.Help.Ansi.Text
values.
By default, these values are
required
)label
for parameters)Other components rely on this ordering.
Delegates to the renderers to create CommandLine.Help.Ansi.Text
values for the annotated fields, and uses a
CommandLine.Help.TextTable
to display these values in tabular format. Layout is responsible for deciding which values
to display where in the table. By default, Layout shows one option or parameter per table row.
Responsible for spacing out CommandLine.Help.Ansi.Text
values according to the CommandLine.Help.Column
definitions the table was
created with. Columns have a width, indentation, and an overflow policy that decides what to do if a value is
longer than the column's width.
Encapsulates rich text with styles and colors in a way that other components like CommandLine.Help.TextTable
are
unaware of the embedded ANSI escape codes.
Modifier and Type | Class and Description |
---|---|
static class |
CommandLine.Help.Ansi
Provides methods and inner classes to support using ANSI escape codes in usage help messages.
|
static class |
CommandLine.Help.ColorScheme
All usage help message are generated with a color scheme that assigns certain styles and colors to common
parts of a usage message: the command name, options, positional parameters and option parameters.
|
static class |
CommandLine.Help.Column
|
static interface |
CommandLine.Help.IOptionRenderer
When customizing online help for
Option details, a custom IOptionRenderer can be
used to create textual representation of an Option in a tabular format: one or more rows, each containing
one or more columns. |
static interface |
CommandLine.Help.IParameterRenderer
When customizing online help for
Parameters details, a custom IParameterRenderer
can be used to create textual representation of a Parameters field in a tabular format: one or more rows,
each containing one or more columns. |
static interface |
CommandLine.Help.IParamLabelRenderer
When customizing online usage help for an option parameter or a positional parameter, a custom
IParamLabelRenderer can be used to render the parameter name or label to a String. |
static class |
CommandLine.Help.Layout
Use a Layout to format usage help text for options and parameters in tabular format.
|
static class |
CommandLine.Help.TextTable
Responsible for spacing out
CommandLine.Help.Ansi.Text values according to the CommandLine.Help.Column definitions the table was
created with. |
Modifier and Type | Field and Description |
---|---|
Boolean |
abbreviateSynopsis
If
true , the synopsis line(s) will show an abbreviated synopsis without detailed option names. |
String |
commandListHeading
Optional heading preceding the subcommand list.
|
String |
commandName
The String to use as the program name in the synopsis line of the help message.
|
String[] |
customSynopsis
Optional custom synopsis lines to use instead of the auto-generated synopsis.
|
protected static String |
DEFAULT_COMMAND_NAME
Constant String holding the default program name: "
|
protected static String |
DEFAULT_SEPARATOR
Constant String holding the default string that separates options from option parameters: "="
|
String[] |
description
Optional text lines to use as the description of the help message, displayed between the synopsis and the
options list.
|
String |
descriptionHeading
Optional heading preceding the description section.
|
String[] |
footer
Optional footer text lines displayed at the bottom of the help message.
|
String |
footerHeading
Optional heading preceding the footer section.
|
String[] |
header
Optional header lines displayed at the top of the help message.
|
String |
headerHeading
Optional heading preceding the header section.
|
List<Field> |
optionFields
Immutable list of fields annotated with
CommandLine.Option , in declaration order. |
String |
optionListHeading
Optional heading preceding the options list.
|
CommandLine.Help.IParamLabelRenderer |
parameterLabelRenderer
Option and positional parameter value label renderer used for the synopsis line(s) and the option list.
|
String |
parameterListHeading
Optional heading preceding the parameter list.
|
List<Field> |
positionalParametersFields
Immutable list of fields annotated with
CommandLine.Parameters , or an empty list if no such field exists. |
Character |
requiredOptionMarker
Character used to prefix required options in the options list.
|
String |
separator
The String to use as the separator between options and option parameters.
|
Boolean |
showDefaultValues
If
true , the options list will show default values for all options except booleans. |
Boolean |
sortOptions
If
true , the options list is sorted alphabetically. |
String |
synopsisHeading
Optional heading preceding the synopsis.
|
Constructor and Description |
---|
Help(Object command)
Constructs a new
Help instance with a default color scheme, initialized from annotatations
on the specified class and superclasses. |
Help(Object command,
CommandLine.Help.Ansi ansi)
Constructs a new
Help instance with a default color scheme, initialized from annotatations
on the specified class and superclasses. |
Help(Object command,
CommandLine.Help.ColorScheme colorScheme)
Constructs a new
Help instance with the specified color scheme, initialized from annotatations
on the specified class and superclasses. |
Modifier and Type | Method and Description |
---|---|
String |
abbreviatedSynopsis()
Generates a generic synopsis like
<command name> [OPTIONS] [PARAM1 [PARAM2]...] , omitting parts
that don't apply to the command (e.g., does not show [OPTIONS] if the command has no options). |
CommandLine.Help |
addAllSubcommands(Map<String,CommandLine> commands)
Registers all specified subcommands with this Help.
|
CommandLine.Help |
addSubcommand(String commandName,
Object command)
Registers the specified subcommand with this Help.
|
CommandLine.Help.Ansi |
ansi()
Returns whether ANSI escape codes are enabled or not.
|
String |
commandList()
Returns a 2-column list with command names and the first line of their header or (if absent) description.
|
String |
commandListHeading(Object... params)
Returns the text displayed before the command list; an empty string if there are no commands,
otherwise the result of
String.format(commandListHeading, params) . |
CommandLine.Help.Layout |
createDefaultLayout()
Returns a
Layout instance configured with the user preferences captured in this Help instance. |
CommandLine.Help.IOptionRenderer |
createDefaultOptionRenderer()
|
CommandLine.Help.IParameterRenderer |
createDefaultParameterRenderer()
Returns a new default ParameterRenderer which converts
Parameters to four columns of
text to match the default TextTable column layout. |
CommandLine.Help.IParamLabelRenderer |
createDefaultParamLabelRenderer()
Returns a new default value renderer that separates option parameters from their options with the specified separator string, surrounds optional parameters with
'[' and ']'
characters and uses ellipses ("...") to indicate that any number of a parameter are allowed. |
static CommandLine.Help.IOptionRenderer |
createMinimalOptionRenderer()
Returns a new minimal OptionRenderer which converts
Options to a single row with two columns
of text: an option name and a description. |
static CommandLine.Help.IParameterRenderer |
createMinimalParameterRenderer()
Returns a new minimal ParameterRenderer which converts
Parameters to a single row with
two columns of text: an option name and a description. |
static CommandLine.Help.IParamLabelRenderer |
createMinimalParamLabelRenderer()
Returns a value renderer that returns the
paramLabel if defined or the field name otherwise. |
static Comparator<Field> |
createShortOptionArityAndNameComparator()
Sorts Fields annotated with
Option by their option max arity first, by
min arity next, and by option name last. |
static Comparator<Field> |
createShortOptionNameComparator()
Sorts Fields annotated with
Option by their option name in case-insensitive alphabetic order. |
String |
customSynopsis(Object... params)
Returns command custom synopsis as a string.
|
static CommandLine.Help.ColorScheme |
defaultColorScheme(CommandLine.Help.Ansi ansi)
Creates and returns a new
CommandLine.Help.ColorScheme initialized with picocli default values: commands are bold,
options and parameters use a yellow foreground, and option parameters use italic. |
String |
description(Object... params)
Returns command description text as a string.
|
String |
descriptionHeading(Object... params)
Returns the text displayed before the description text; an empty string if there is no description,
otherwise the result of
String.format(descriptionHeading, params) . |
String |
detailedSynopsis(Comparator<Field> optionSort,
boolean clusterBooleanOptions)
Deprecated.
use
detailedSynopsis(int, Comparator, boolean) instead. |
String |
detailedSynopsis(int synopsisHeadingLength,
Comparator<Field> optionSort,
boolean clusterBooleanOptions)
Generates a detailed synopsis message showing all options and parameters.
|
String |
footer(Object... params)
Returns command footer text as a string.
|
String |
footerHeading(Object... params)
Returns the text displayed before the footer text; the result of
String.format(footerHeading, params) . |
String |
header(Object... params)
Returns the command header text as a string.
|
String |
headerHeading(Object... params)
Returns the text displayed before the header text; the result of
String.format(headerHeading, params) . |
static StringBuilder |
join(CommandLine.Help.Ansi ansi,
String[] values,
StringBuilder sb,
Object... params)
Formats each of the specified values and appends it to the specified StringBuilder.
|
String |
optionList()
Returns a description of the options supported by the application.
|
String |
optionList(CommandLine.Help.Layout layout,
Comparator<Field> optionSort,
CommandLine.Help.IParamLabelRenderer valueLabelRenderer)
Sorts all
Options with the specified comparator (if the comparator is non-null ),
then adds all non-hidden options to the
specified TextTable and returns the result of TextTable.toString(). |
String |
optionListHeading(Object... params)
Returns the text displayed before the option list; an empty string if there are no options,
otherwise the result of
String.format(optionListHeading, params) . |
String |
parameterList()
Returns the section of the usage help message that lists the parameters with their descriptions.
|
String |
parameterList(CommandLine.Help.Layout layout,
CommandLine.Help.IParamLabelRenderer paramLabelRenderer)
Returns the section of the usage help message that lists the parameters with their descriptions.
|
String |
parameterListHeading(Object... params)
Returns the text displayed before the positional parameter list; an empty string if there are no positional
parameters, otherwise the result of
String.format(parameterListHeading, params) . |
static Comparator<String> |
shortestFirst()
Sorts short strings before longer strings.
|
String |
synopsis()
Deprecated.
use
synopsis(int) instead |
String |
synopsis(int synopsisHeadingLength)
Returns a synopsis for the command, reserving the specified space for the synopsis heading.
|
String |
synopsisHeading(Object... params)
Returns the text displayed before the synopsis text; the result of
String.format(synopsisHeading, params) . |
int |
synopsisHeadingLength()
Returns the number of characters the synopsis heading will take on the same line as the synopsis.
|
protected static final String DEFAULT_COMMAND_NAME
protected static final String DEFAULT_SEPARATOR
public final List<Field> optionFields
CommandLine.Option
, in declaration order.public final List<Field> positionalParametersFields
CommandLine.Parameters
, or an empty list if no such field exists.public String separator
"="
by default,
initialized from CommandLine.Command.separator()
if defined.parameterLabelRenderer
public String commandName
DEFAULT_COMMAND_NAME
by default, initialized from CommandLine.Command.name()
if defined.public String[] description
CommandLine.Command.description()
if the Command
annotation is present,
otherwise this is an empty array and the help message has no description.
Applications may programmatically set this field to create a custom help message.public String[] customSynopsis
CommandLine.Command.customSynopsis()
if the Command
annotation is present,
otherwise this is an empty array and the synopsis is generated.
Applications may programmatically set this field to create a custom help message.public String[] header
CommandLine.Command.header()
if the Command
annotation is present, otherwise this is an empty array and the help message has no
header. Applications may programmatically set this field to create a custom help message.public String[] footer
CommandLine.Command.footer()
if the Command
annotation is present, otherwise this is an empty array and
the help message has no footer.
Applications may programmatically set this field to create a custom help message.public CommandLine.Help.IParamLabelRenderer parameterLabelRenderer
createDefaultParamLabelRenderer()
, which takes a snapshot
of the separator
at construction time. If the separator is modified after Help construction, you
may need to re-initialize this field by calling createDefaultParamLabelRenderer()
again.public Boolean abbreviateSynopsis
true
, the synopsis line(s) will show an abbreviated synopsis without detailed option names.public Boolean sortOptions
true
, the options list is sorted alphabetically.public Boolean showDefaultValues
true
, the options list will show default values for all options except booleans.public Character requiredOptionMarker
public String headerHeading
CommandLine.Command.headerHeading()
, or null.public String synopsisHeading
CommandLine.Command.synopsisHeading()
, "Usage: "
by default.public String descriptionHeading
CommandLine.Command.descriptionHeading()
, or null.public String parameterListHeading
CommandLine.Command.parameterListHeading()
, or null.public String optionListHeading
CommandLine.Command.optionListHeading()
, or null.public String commandListHeading
CommandLine.Command.commandListHeading()
. "Commands:%n"
by default.public String footerHeading
CommandLine.Command.footerHeading()
, or null.public Help(Object command)
Help
instance with a default color scheme, initialized from annotatations
on the specified class and superclasses.command
- the annotated object to create usage help forpublic Help(Object command, CommandLine.Help.Ansi ansi)
Help
instance with a default color scheme, initialized from annotatations
on the specified class and superclasses.command
- the annotated object to create usage help foransi
- whether to emit ANSI escape codes or notpublic Help(Object command, CommandLine.Help.ColorScheme colorScheme)
Help
instance with the specified color scheme, initialized from annotatations
on the specified class and superclasses.command
- the annotated object to create usage help forcolorScheme
- the color scheme to usepublic CommandLine.Help addAllSubcommands(Map<String,CommandLine> commands)
commands
- maps the command names to the associated CommandLine objectCommandLine.getSubcommands()
public CommandLine.Help addSubcommand(String commandName, Object command)
commandName
- the name of the subcommand to display in the usage messagecommand
- the annotated object to get more information from@Deprecated public String synopsis()
synopsis(int)
insteadabbreviatedSynopsis()
,
detailedSynopsis(Comparator, boolean)
public String synopsis(int synopsisHeadingLength)
synopsisHeadingLength
- the length of the synopsis heading that will be displayed on the same lineabbreviatedSynopsis()
,
detailedSynopsis(Comparator, boolean)
,
synopsisHeading
public String abbreviatedSynopsis()
<command name> [OPTIONS] [PARAM1 [PARAM2]...]
, omitting parts
that don't apply to the command (e.g., does not show [OPTIONS] if the command has no options).@Deprecated public String detailedSynopsis(Comparator<Field> optionSort, boolean clusterBooleanOptions)
detailedSynopsis(int, Comparator, boolean)
instead.[ ]
).optionSort
- comparator to sort options or null
if options should not be sortedclusterBooleanOptions
- true
if boolean short options should be clustered into a single stringpublic String detailedSynopsis(int synopsisHeadingLength, Comparator<Field> optionSort, boolean clusterBooleanOptions)
[ ]
).synopsisHeadingLength
- the length of the synopsis heading that will be displayed on the same lineoptionSort
- comparator to sort options or null
if options should not be sortedclusterBooleanOptions
- true
if boolean short options should be clustered into a single stringpublic int synopsisHeadingLength()
detailedSynopsis(int, Comparator, boolean)
public String optionList()
Returns a description of the options supported by the application. This implementation sorts options alphabetically, and shows only the non-hidden options in a tabular format using the default renderer and default layout.
optionList(Layout, Comparator, IParamLabelRenderer)
public String optionList(CommandLine.Help.Layout layout, Comparator<Field> optionSort, CommandLine.Help.IParamLabelRenderer valueLabelRenderer)
Options
with the specified comparator
(if the comparator is non-null
),
then adds all non-hidden options to the
specified TextTable and returns the result of TextTable.toString().layout
- responsible for rendering the option listoptionSort
- determines in what order Options
should be listed. Declared order if null
valueLabelRenderer
- used for options with a parameterpublic String parameterList()
public String parameterList(CommandLine.Help.Layout layout, CommandLine.Help.IParamLabelRenderer paramLabelRenderer)
layout
- the layout to useparamLabelRenderer
- for rendering parameter namespublic static StringBuilder join(CommandLine.Help.Ansi ansi, String[] values, StringBuilder sb, Object... params)
ansi
- whether the result should contain ANSI escape codes or notvalues
- the values to format and append to the StringBuildersb
- the StringBuilder to collect the formatted stringsparams
- the parameters to pass to the format method when formatting each valuepublic String customSynopsis(Object... params)
CommandLine.Command.customSynopsis()
annotation attribute or programmatically
by setting the Help instance's customSynopsis
field.params
- Arguments referenced by the format specifiers in the synopsis stringspublic String description(Object... params)
CommandLine.Command.description()
annotation attribute or programmatically by
setting the Help instance's description
field.params
- Arguments referenced by the format specifiers in the description stringspublic String header(Object... params)
CommandLine.Command.header()
annotation attribute or programmatically by
setting the Help instance's header
field.params
- Arguments referenced by the format specifiers in the header stringspublic String footer(Object... params)
CommandLine.Command.footer()
annotation attribute or programmatically by
setting the Help instance's footer
field.params
- Arguments referenced by the format specifiers in the footer stringspublic String headerHeading(Object... params)
String.format(headerHeading, params)
.params
- the parameters to use to format the header headingpublic String synopsisHeading(Object... params)
String.format(synopsisHeading, params)
.params
- the parameters to use to format the synopsis headingpublic String descriptionHeading(Object... params)
String.format(descriptionHeading, params)
.params
- the parameters to use to format the description headingpublic String parameterListHeading(Object... params)
String.format(parameterListHeading, params)
.params
- the parameters to use to format the parameter list headingpublic String optionListHeading(Object... params)
String.format(optionListHeading, params)
.params
- the parameters to use to format the option list headingpublic String commandListHeading(Object... params)
String.format(commandListHeading, params)
.params
- the parameters to use to format the command list headingpublic String footerHeading(Object... params)
String.format(footerHeading, params)
.params
- the parameters to use to format the footer headingpublic String commandList()
public CommandLine.Help.Layout createDefaultLayout()
Layout
instance configured with the user preferences captured in this Help instance.public CommandLine.Help.IOptionRenderer createDefaultOptionRenderer()
Options
to five columns of text to match
the default TextTable column layout. The first row of values looks like this:
CommandLine.Option.description()
arrayFollowing this, there will be one row for each of the remaining elements of the CommandLine.Option.description()
array, and these rows look like {"", "", "", "", option.description()[i]}
.
If configured, this option renderer adds an additional row to display the default field value.
public static CommandLine.Help.IOptionRenderer createMinimalOptionRenderer()
Options
to a single row with two columns
of text: an option name and a description. If multiple names or descriptions exist, the first value is used.public CommandLine.Help.IParameterRenderer createDefaultParameterRenderer()
Parameters
to four columns of
text to match the default TextTable column layout. The first row of values looks like this:
CommandLine.Help.IParamLabelRenderer
CommandLine.Parameters.description()
arrayFollowing this, there will be one row for each of the remaining elements of the CommandLine.Parameters.description()
array, and these rows look like {"", "", "", param.description()[i]}
.
If configured, this parameter renderer adds an additional row to display the default field value.
public static CommandLine.Help.IParameterRenderer createMinimalParameterRenderer()
Parameters
to a single row with
two columns of text: an option name and a description. If multiple descriptions exist, the first value is used.public static CommandLine.Help.IParamLabelRenderer createMinimalParamLabelRenderer()
paramLabel
if defined or the field name otherwise.public CommandLine.Help.IParamLabelRenderer createDefaultParamLabelRenderer()
'['
and ']'
characters and uses ellipses ("...") to indicate that any number of a parameter are allowed.public static Comparator<Field> createShortOptionNameComparator()
Option
by their option name in case-insensitive alphabetic order. If an
Option has multiple names, the shortest name is used for the sorting. Help options follow non-help options.public static Comparator<Field> createShortOptionArityAndNameComparator()
Option
by their option max arity first, by
min arity next, and by option name last.public static Comparator<String> shortestFirst()
public CommandLine.Help.Ansi ansi()
public static CommandLine.Help.ColorScheme defaultColorScheme(CommandLine.Help.Ansi ansi)
CommandLine.Help.ColorScheme
initialized with picocli default values: commands are bold,
options and parameters use a yellow foreground, and option parameters use italic.ansi
- whether the usage help message should contain ANSI escape codes or notCopyright © 1999-2020 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.