@Retention(value=RUNTIME) @Target(value={FIELD,METHOD,PARAMETER}) public static @interface CommandLine.Parameters
Fields annotated with @Parameters
will be initialized with positional parameters. By specifying the
index()
attribute you can pick the exact position or a range of positional parameters to apply. If no
index is specified, the field will get all positional parameters (and so it should be an array or a collection).
In the case of command methods (annotated with @Command
), method parameters may be annotated with @Parameters
,
but are are considered positional parameters by default, unless they are annotated with @Option
.
Command class example:
import static picocli.CommandLine.*; public class MyCalcParameters { @Parameters(description = "Any number of input numbers") private List<BigDecimal> files = new ArrayList<BigDecimal>(); @Option(names = { "-h", "--help" }, usageHelp = true, description = "Display this help and exit") private boolean help; }
A field cannot be annotated with both @Parameters
and @Option
or a ParameterException
is thrown.
Modifier and Type | Fields and Description |
---|---|
static String |
NULL_VALUE
Special value that can be used in some annotation attributes to designate
null . |
Modifier and Type | Optional Element and Description |
---|---|
String |
arity
Specifies the minimum number of required parameters and the maximum number of accepted parameters.
|
Class<? extends Iterable<String>> |
completionCandidates
Use this attribute to specify an
Iterable<String> class that generates completion candidates for
this positional parameter. |
Class<? extends CommandLine.ITypeConverter<?>>[] |
converter
Optionally specify one or more
CommandLine.ITypeConverter classes to use to convert the command line argument into
a strongly typed value (or key-value pair for map fields). |
String |
defaultValue
Returns the default value of this positional parameter, before splitting and type conversion.
|
String[] |
description
Description of the parameter(s), used when generating the usage documentation.
|
String |
descriptionKey
ResourceBundle key for this option.
|
boolean |
echo
Use this attribute to control whether user input for an interactive positional parameter is echoed to the console or not.
|
boolean |
hidden
Set
hidden=true if this parameter should not be included in the usage message. |
boolean |
hideParamSyntax
Returns whether usage syntax decorations around the paramLabel should be suppressed.
|
String |
index
Specify an index ("0", or "1", etc.) to pick which of the command line arguments should be assigned to this
field.
|
boolean |
interactive
Set
interactive=true if this positional parameter will prompt the end user for a value (like a password). |
String |
mapFallbackValue
For positional parameters of type Map, setting the
mapFallbackValue to any value allows end user
to specify key-only parameters for this parameter. |
Class<? extends CommandLine.IParameterConsumer> |
parameterConsumer
Optionally specify a custom
IParameterConsumer to temporarily suspend picocli's parsing logic
and process one or more command line arguments in a custom manner. |
String |
paramLabel
Specify a
paramLabel for the parameter to be used in the usage help message. |
Class<? extends CommandLine.IParameterPreprocessor> |
preprocessor
Returns the preprocessor for this positional parameter.
|
String |
prompt
Use this attribute to customize the text displayed to the end user for an interactive positional parameter when asking for user input.
|
CommandLine.ScopeType |
scope
Determines on which command(s) this positional parameter exists: on this command only (the default), or
whether this is a "global" parameter that is applied to this command and all subcommands, sub-subcommands, etc.
|
CommandLine.Help.Visibility |
showDefaultValue
Use this attribute to control for a specific positional parameter whether its default value should be shown in the usage
help message.
|
String |
split
Specify a regular expression to use to split positional parameter values before applying them to the field.
|
String |
splitSynopsisLabel
Specify a string to show the split option parameter values in usage
|
Class<?>[] |
type
Optionally specify a
type to control exactly what Class the positional parameter should be converted
to. |
public static final String NULL_VALUE
null
.defaultValue()
,
mapFallbackValue()
public abstract String index
public abstract String[] description
May contain embedded format specifiers like %n
line separators. Literal percent '%'
characters must be escaped with another %
.
The description may contain variables that are rendered when help is requested.
The string ${DEFAULT-VALUE}
is replaced with the default value of the positional parameter. This is regardless of
the command's showDefaultValues
setting or the positional parameter's showDefaultValue
setting.
The string ${COMPLETION-CANDIDATES}
is replaced with the completion candidates generated by
completionCandidates()
in the description for this positional parameter.
Also, embedded %n
newline markers are converted to actual newlines.
public abstract String arity
CommandLine.MissingParameterException
is thrown by the CommandLine.parse(String...)
method.
The default depends on the type of the parameter: booleans require no parameters, arrays and Collections accept zero to any number of parameters, and any other type accepts one parameter.
For single-value parameters, setting arity = "0..1"
makes a positional parameter optional, while setting arity = "1"
makes it required.
Required parameters that are part of a group are required within the group, not required within the command: the group's multiplicity determines whether the group itself is required or optional.
public abstract String paramLabel
paramLabel
for the parameter to be used in the usage help message. If omitted,
picocli uses the field name in fish brackets ('<'
and '>'
) by default. Example:
class Example { @Parameters(paramLabel="FILE", description="path of the input FILE(s)") private File[] inputFiles; }
By default, the above gives a usage help message like the following:
Usage: <main class> [FILE...] [FILE...] path of the input FILE(s)
public abstract boolean hideParamSyntax
false
: by default, the paramLabel is surrounded with '['
and ']'
characters
if the value is optional and followed by ellipses ("...") when multiple values can be specified.public abstract Class<?>[] type
Optionally specify a type
to control exactly what Class the positional parameter should be converted
to. This may be useful when the field type is an interface or an abstract class. For example, a field can
be declared to have type java.lang.Number
, and annotating @Parameters(type=Short.class)
ensures that the positional parameter value is converted to a Short
before setting the field value.
For array fields whose component type is an interface or abstract class, specify the concrete component type.
For example, a field with type Number[]
may be annotated with @Parameters(type=Short.class)
to ensure that positional parameter values are converted to Short
before adding an element to the array.
Picocli will use the CommandLine.ITypeConverter
that is
registered for the specified type to convert
the raw String values before modifying the field value.
Prior to 2.0, the type
attribute was necessary for Collection
and Map
fields,
but starting from 2.0 picocli will infer the component type from the generic type's type arguments.
For example, for a field of type Map<TimeUnit, Long>
picocli will know the positional parameter
should be split up in key=value pairs, where the key should be converted to a java.util.concurrent.TimeUnit
enum value, and the value should be converted to a Long
. No @Parameters(type=...)
type attribute
is required for this. For generic types with wildcards, picocli will take the specified upper or lower bound
as the Class to convert to, unless the @Parameters
annotation specifies an explicit type
attribute.
If the field type is a raw collection or a raw map, and you want it to contain other values than Strings,
or if the generic type's type arguments are interfaces or abstract classes, you may
specify a type
attribute to control the Class that the positional parameter should be converted to.
public abstract Class<? extends CommandLine.ITypeConverter<?>>[] converter
CommandLine.ITypeConverter
classes to use to convert the command line argument into
a strongly typed value (or key-value pair for map fields). This is useful when a particular field should
use a custom conversion that is different from the normal conversion for the field's type.
For example, for a specific field you may want to use a converter that maps the constant names defined
in java.sql.Types
to the int
value of these constants, but any other int
fields should
not be affected by this and should continue to use the standard int converter that parses numeric values.
CommandLine.registerConverter(Class, ITypeConverter)
public abstract String split
split
regex can only be specified on multi-value options and positional parameters.""
if the value should not be splitString.split(String)
public abstract String splitSynopsisLabel
public abstract boolean hidden
hidden=true
if this parameter should not be included in the usage message.public abstract String defaultValue
To get a null
default value, omit specifying a default value or use the special value NULL_VALUE
-
for positional parameters of type Optional<T>
that will result in the Optional.empty()
value being assigned when the positional parameters is not specified on the command line.
public abstract CommandLine.Help.Visibility showDefaultValue
CommandLine.Command.showDefaultValues()
is set true
on the command. Use this attribute to specify whether the default value
for this specific positional parameter should always be shown or never be shown, regardless of the command setting.
Note that picocli 3.2 allows embedding default values by specifying the variable
${DEFAULT-VALUE}
anywhere in the description that ignores this setting.
public abstract Class<? extends Iterable<String>> completionCandidates
Iterable<String>
class that generates completion candidates for
this positional parameter. For map fields, completion candidates should be in key=value
form.
Completion candidates are used in bash completion scripts generated by the picocli.AutoComplete
class.
Unfortunately, picocli.AutoComplete
is not very good yet at generating completions for positional parameters.
CommandLine.IFactory
public abstract boolean interactive
interactive=true
if this positional parameter will prompt the end user for a value (like a password).
Only supported for single-value positional parameters (not arrays, collections or maps).
When running on Java 6 or greater and echo = false
(the default),
this will use the Console.readPassword()
API to get a value without echoing input to the console,
otherwise it will simply read a value from System.in
.public abstract boolean echo
echo = true
, the user input is echoed to the console.
This attribute is ignored when interactive = false
(the default).CommandLine.Model.ArgSpec.echo()
public abstract String prompt
interactive = false
(the default).CommandLine.Model.ArgSpec.prompt()
public abstract String descriptionKey
paramLabel() + "[" + index() + "]"
as key.CommandLine.Model.ArgSpec.description()
public abstract CommandLine.ScopeType scope
public abstract Class<? extends CommandLine.IParameterConsumer> parameterConsumer
IParameterConsumer
to temporarily suspend picocli's parsing logic
and process one or more command line arguments in a custom manner.public abstract String mapFallbackValue
mapFallbackValue
to any value allows end user
to specify key-only parameters for this parameter. For example, key
instead of key=value
.
The value specified in this annotation is the value that is put into the Map for the user-specified key.
Use the special value NULL_VALUE
to specify null
-
for maps of type Map<K, Optional<V>>
that will result in Optional.empty()
values in the map when only the key is specified.
If no mapFallbackValue
is set, key-only Map parameters like -Dkey
are considered invalid user input and cause a CommandLine.ParameterException
to be thrown.
CommandLine.Model.ArgSpec.mapFallbackValue()
public abstract Class<? extends CommandLine.IParameterPreprocessor> preprocessor
CommandLine.IParameterPreprocessor