public static class CommandLine.PropertiesDefaultProvider extends Object implements CommandLine.IDefaultValueProvider
IDefaultValueProvider
implementation that loads default values for command line
options and positional parameters from a properties file or Properties
object.
".<YOURCOMMAND>.properties"
in the user home directory, where "<YOURCOMMAND>"
is the name of the command.
If a command has aliases in addition to its name,
these aliases are also used to try to find the properties file. For example:
@Command(name = "git", defaultValueProvider = PropertiesDefaultProvider.class) class Git { }
The above will try to load default values from new File(System.getProperty("user.home"), ".git.properties")
.
The location of the properties file can also be controlled with system property "picocli.defaults.<YOURCOMMAND>.path"
,
in which case the value of the property must be the path to the file containing the default values.
The location of the properties file may also be specified programmatically. For example:
CommandLine cmd = new CommandLine(new MyCommand()); File defaultsFile = new File("path/to/config/mycommand.properties"); cmd.setDefaultValueProvider(new PropertiesDefaultProvider(defaultsFile)); cmd.execute(args);
For options, the key is either the descriptionKey, or the option's longest name.
For positional parameters, the key is either the descriptionKey, or the positional parameter's param label.
End users may not know what the descriptionKey
of your options and positional parameters are, so be sure
to document that with your application.
The default values for options and positional parameters of subcommands can be included in the
properties file for the top-level command, so that end users need to maintain only a single file.
This can be achieved by prefixing the key with the command's qualified name.
For example, to give the `git commit`
command's --cleanup
option a
default value of strip
, define a key of git.commit.cleanup
and assign
it a default value.
# /home/remko/.git.properties git.commit.cleanup = strip
Constructor and Description |
---|
PropertiesDefaultProvider()
Default constructor, used when this default value provider is specified in
the annotations:
|
PropertiesDefaultProvider(File file)
This constructor loads default values from the specified properties file.
|
PropertiesDefaultProvider(Properties properties)
This constructor loads default values from the specified properties object.
|
Modifier and Type | Method and Description |
---|---|
String |
defaultValue(CommandLine.Model.ArgSpec argSpec)
Returns the default value for an option or positional parameter or
null . |
String |
toString() |
public PropertiesDefaultProvider()
@Command(name = "mycmd", defaultValueProvider = PropertiesDefaultProvider.class) class MyCommand // ...
This loads default values from a properties file named
".mycmd.properties"
in the user home directory.
The location of the properties file can also be controlled with system property "picocli.defaults.<YOURCOMMAND>.path"
,
in which case the value of the property must be the path to the file containing the default values.
public PropertiesDefaultProvider(Properties properties)
CommandLine cmd = new CommandLine(new MyCommand()); Properties defaults = getProperties(); cmd.setDefaultValueProvider(new PropertiesDefaultProvider(defaults)); cmd.execute(args);
properties
- the properties containing the default valuesthe PropertiesDefaultProvider class description
public PropertiesDefaultProvider(File file)
CommandLine cmd = new CommandLine(new MyCommand()); File defaultsFile = new File("path/to/config/file.properties"); cmd.setDefaultValueProvider(new PropertiesDefaultProvider(defaultsFile)); cmd.execute(args);
file
- the file to load default values from. Must be non-null
and
must contain default values in the standard java Properties
format.the PropertiesDefaultProvider class description
public String defaultValue(CommandLine.Model.ArgSpec argSpec) throws Exception
CommandLine.IDefaultValueProvider
null
.
The returned value is converted to the type of the option/positional parameter
via the same type converter used when populating this option/positional
parameter from a command line argument.defaultValue
in interface CommandLine.IDefaultValueProvider
argSpec
- the option or positional parameter, never null
null
if
this provider has no default value for the specified option or positional parameterException
- when there was a problem obtaining the default value