Package picocli

Class CommandLine.PropertiesDefaultProvider

  • All Implemented Interfaces:
    CommandLine.IDefaultValueProvider
    Enclosing class:
    CommandLine

    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.

    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 of strip, define a key of git.commit.cleanup and assign it a default value.

     # /home/remko/.git.properties
     git.commit.cleanup = strip
     
    Since:
    4.1
    • 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.

        See Also:
        the PropertiesDefaultProvider class description
      • PropertiesDefaultProvider

        public PropertiesDefaultProvider​(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​(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 java Properties format.
        See Also:
        the PropertiesDefaultProvider class description
    • Method Detail

      • defaultValue

        public String defaultValue​(CommandLine.Model.ArgSpec argSpec)
                            throws Exception
        Description copied from interface: CommandLine.IDefaultValueProvider
        Returns the default value for an option or positional parameter or 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.
        Specified by:
        defaultValue in interface CommandLine.IDefaultValueProvider
        Parameters:
        argSpec - the option or positional parameter, never null
        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:
        Exception - when there was a problem obtaining the default value