Class CommandLine.PropertiesDefaultProvider
- java.lang.Object
-
- picocli.CommandLine.PropertiesDefaultProvider
-
- All Implemented Interfaces:
CommandLine.IDefaultValueProvider
- Enclosing class:
- CommandLine
public static class CommandLine.PropertiesDefaultProvider extends java.lang.Object implements CommandLine.IDefaultValueProvider
IDefaultValueProvider
implementation that loads default values for command line options and positional parameters from a properties file orProperties
object.Location
By default, this implementation tries to find a properties file named".<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);
Format
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.Subcommands
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 ofstrip
, define a key ofgit.commit.cleanup
and assign it a default value.# /home/remko/.git.properties git.commit.cleanup = strip
- Since:
- 4.1
-
-
Field Summary
Fields Modifier and Type Field Description private java.io.File
location
private java.util.Properties
properties
-
Constructor Summary
Constructors Constructor Description PropertiesDefaultProvider()
Default constructor, used when this default value provider is specified in the annotations:PropertiesDefaultProvider(java.io.File file)
This constructor loads default values from the specified properties file.PropertiesDefaultProvider(java.util.Properties properties)
This constructor loads default values from the specified properties object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.util.Properties
createProperties(java.io.File file, CommandLine.Model.CommandSpec commandSpec)
java.lang.String
defaultValue(CommandLine.Model.ArgSpec argSpec)
Returns the default value for an option or positional parameter ornull
.private java.lang.String
getValue(java.lang.String key, CommandLine.Model.CommandSpec spec)
private static java.util.Properties
loadProperties(CommandLine.Model.CommandSpec commandSpec)
private java.lang.String
optionDefaultValue(CommandLine.Model.OptionSpec option)
private java.lang.String
positionalDefaultValue(CommandLine.Model.PositionalParamSpec positional)
private static java.lang.String
stripPrefix(java.lang.String prefixed)
java.lang.String
toString()
-
-
-
Constructor Detail
-
PropertiesDefaultProvider
public PropertiesDefaultProvider()
Default constructor, used when this default value provider is specified in the annotations:@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.
-
PropertiesDefaultProvider
public PropertiesDefaultProvider(java.util.Properties properties)
This constructor loads default values from the specified properties object. This may be used programmatically. For example:CommandLine cmd = new CommandLine(new MyCommand()); Properties defaults = getProperties(); cmd.setDefaultValueProvider(new PropertiesDefaultProvider(defaults)); cmd.execute(args);
- Parameters:
properties
- the properties containing the default values- See Also:
the PropertiesDefaultProvider class description
-
PropertiesDefaultProvider
public PropertiesDefaultProvider(java.io.File file)
This constructor loads default values from the specified properties file. This may be used programmatically. For example:CommandLine cmd = new CommandLine(new MyCommand()); File defaultsFile = new File("path/to/config/file.properties"); cmd.setDefaultValueProvider(new PropertiesDefaultProvider(defaultsFile)); cmd.execute(args);
- Parameters:
file
- the file to load default values from. Must be non-null
and must contain default values in the standard javaProperties
format.- See Also:
the PropertiesDefaultProvider class description
-
-
Method Detail
-
createProperties
private static java.util.Properties createProperties(java.io.File file, CommandLine.Model.CommandSpec commandSpec)
-
loadProperties
private static java.util.Properties loadProperties(CommandLine.Model.CommandSpec commandSpec)
-
defaultValue
public java.lang.String defaultValue(CommandLine.Model.ArgSpec argSpec) throws java.lang.Exception
Description copied from interface:CommandLine.IDefaultValueProvider
Returns the default value for an option or positional parameter ornull
. 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.- Specified by:
defaultValue
in interfaceCommandLine.IDefaultValueProvider
- Parameters:
argSpec
- the option or positional parameter, nevernull
- Returns:
- the default value for the option or positional parameter, or
null
if this provider has no default value for the specified option or positional parameter - Throws:
java.lang.Exception
- when there was a problem obtaining the default value
-
optionDefaultValue
private java.lang.String optionDefaultValue(CommandLine.Model.OptionSpec option)
-
stripPrefix
private static java.lang.String stripPrefix(java.lang.String prefixed)
-
positionalDefaultValue
private java.lang.String positionalDefaultValue(CommandLine.Model.PositionalParamSpec positional)
-
getValue
private java.lang.String getValue(java.lang.String key, CommandLine.Model.CommandSpec spec)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-