Class CommandLine

java.lang.Object
org.apache.commons.cli.CommandLine
All Implemented Interfaces:
Serializable

public class CommandLine extends Object implements Serializable
Represents list of arguments parsed against a Options descriptor.

It allows querying of a boolean hasOption(String opt), in addition to retrieving the getOptionValue(String opt) for options requiring arguments.

Additionally, any left-over or unrecognized arguments, are available for further processing.

See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      The serial version UID.
      See Also:
    • args

      private final List<String> args
      The unrecognized options/arguments
    • options

      private final List<Option> options
      The processed options
    • deprecatedHandler

      private final transient Consumer<Option> deprecatedHandler
      The deprecated option handler.

      If you want to serialize this field, use a serialization proxy.

  • Constructor Details

    • CommandLine

      protected CommandLine()
      Creates a command line.
    • CommandLine

      private CommandLine(List<String> args, List<Option> options, Consumer<Option> deprecatedHandler)
      Creates a command line.
  • Method Details

    • builder

      public static CommandLine.Builder builder()
      Creates a new builder.
      Returns:
      a new builder.
      Since:
      1.7.0
    • addArg

      protected void addArg(String arg)
      Adds left-over unrecognized option/argument.
      Parameters:
      arg - the unrecognized option/argument.
    • addOption

      protected void addOption(Option opt)
      Adds an option to the command line. The values of the option are stored.
      Parameters:
      opt - the processed option.
    • get

      private <T> T get(Supplier<T> supplier)
    • getArgList

      public List<String> getArgList()
      Gets any left-over non-recognized options and arguments
      Returns:
      remaining items passed in but not parsed as a List.
    • getArgs

      public String[] getArgs()
      Gets any left-over non-recognized options and arguments
      Returns:
      remaining items passed in but not parsed as an array.
    • getOptionObject

      @Deprecated public Object getOptionObject(char opt)
      Deprecated.
      due to System.err message. Instead use getParsedOptionValue(char)
      Gets the Object type of this Option.
      Parameters:
      opt - the name of the option.
      Returns:
      the type of opt.
    • getOptionObject

      @Deprecated public Object getOptionObject(String opt)
      Deprecated.
      due to System.err message. Instead use getParsedOptionValue(String)
      Gets the Object type of this Option.
      Parameters:
      opt - the name of the option.
      Returns:
      the type of this Option.
    • getOptionProperties

      public Properties getOptionProperties(Option option)
      Gets the map of values associated to the option. This is convenient for options specifying Java properties like -Dparam1=value1 -Dparam2=value2. All odd numbered values are property keys and even numbered values are property values. If there are an odd number of values the last value is assumed to be a boolean flag and the value is "true".
      Parameters:
      option - name of the option.
      Returns:
      The Properties mapped by the option, never null even if the option doesn't exists.
      Since:
      1.5.0
    • getOptionProperties

      public Properties getOptionProperties(String opt)
      Gets the map of values associated to the option. This is convenient for options specifying Java properties like -Dparam1=value1 -Dparam2=value2. The first argument of the option is the key, and the 2nd argument is the value. If the option has only one argument (-Dfoo) it is considered as a boolean flag and the value is "true".
      Parameters:
      opt - name of the option.
      Returns:
      The Properties mapped by the option, never null even if the option doesn't exists.
      Since:
      1.2
    • getOptions

      public Option[] getOptions()
      Gets an array of the processed Options.
      Returns:
      an array of the processed Options.
    • getOptionValue

      public String getOptionValue(char opt)
      Gets the first argument, if any, of this option.
      Parameters:
      opt - the character name of the option.
      Returns:
      Value of the argument if option is set, and has an argument, otherwise null.
    • getOptionValue

      public String getOptionValue(char opt, String defaultValue)
      Gets the argument, if any, of an option.
      Parameters:
      opt - character name of the option
      defaultValue - is the default value to be returned if the option is not specified.
      Returns:
      Value of the argument if option is set, and has an argument, otherwise defaultValue.
    • getOptionValue

      public String getOptionValue(char opt, Supplier<String> defaultValue)
      Gets the argument, if any, of an option.
      Parameters:
      opt - character name of the option
      defaultValue - is a supplier for the default value to be returned if the option is not specified.
      Returns:
      Value of the argument if option is set, and has an argument, otherwise defaultValue.
      Since:
      1.7.0
    • getOptionValue

      public String getOptionValue(Option option)
      Gets the first argument, if any, of this option.
      Parameters:
      option - the name of the option.
      Returns:
      Value of the argument if option is set, and has an argument, otherwise null.
      Since:
      1.5.0
    • getOptionValue

      public String getOptionValue(Option option, String defaultValue)
      Gets the first argument, if any, of an option.
      Parameters:
      option - name of the option.
      defaultValue - is the default value to be returned if the option is not specified.
      Returns:
      Value of the argument if option is set, and has an argument, otherwise defaultValue.
      Since:
      1.5.0
    • getOptionValue

      public String getOptionValue(Option option, Supplier<String> defaultValue)
      Gets the first argument, if any, of an option.
      Parameters:
      option - name of the option.
      defaultValue - is a supplier for the default value to be returned if the option is not specified.
      Returns:
      Value of the argument if option is set, and has an argument, otherwise defaultValue.
      Since:
      1.7.0
    • getOptionValue

      public String getOptionValue(String opt)
      Gets the first argument, if any, of this option.
      Parameters:
      opt - the name of the option.
      Returns:
      Value of the argument if option is set, and has an argument, otherwise null.
    • getOptionValue

      public String getOptionValue(String opt, String defaultValue)
      Gets the first argument, if any, of an option.
      Parameters:
      opt - name of the option.
      defaultValue - is the default value to be returned if the option is not specified.
      Returns:
      Value of the argument if option is set, and has an argument, otherwise defaultValue.
    • getOptionValue

      public String getOptionValue(String opt, Supplier<String> defaultValue)
      Gets the first argument, if any, of an option.
      Parameters:
      opt - name of the option.
      defaultValue - is a supplier for the default value to be returned if the option is not specified.
      Returns:
      Value of the argument if option is set, and has an argument, otherwise defaultValue.
      Since:
      1.7.0
    • getOptionValues

      public String[] getOptionValues(char opt)
      Gets the array of values, if any, of an option.
      Parameters:
      opt - character name of the option.
      Returns:
      Values of the argument if option is set, and has an argument, otherwise null.
    • getOptionValues

      public String[] getOptionValues(Option option)
      Gets the array of values, if any, of an option.
      Parameters:
      option - string name of the option.
      Returns:
      Values of the argument if option is set, and has an argument, otherwise null.
      Since:
      1.5.0
    • getOptionValues

      public String[] getOptionValues(String opt)
      Gets the array of values, if any, of an option.
      Parameters:
      opt - string name of the option.
      Returns:
      Values of the argument if option is set, and has an argument, otherwise null.
    • getParsedOptionValue

      public <T> T getParsedOptionValue(char opt) throws ParseException
      Gets a version of this Option converted to a particular type.
      Type Parameters:
      T - The return type for the method.
      Parameters:
      opt - the name of the option.
      Returns:
      the value parsed into a particular object.
      Throws:
      ParseException - if there are problems turning the option value into the desired type
      Since:
      1.5.0
      See Also:
    • getParsedOptionValue

      public <T> T getParsedOptionValue(char opt, Supplier<T> defaultValue) throws ParseException
      Gets a version of this Option converted to a particular type.
      Type Parameters:
      T - The return type for the method.
      Parameters:
      opt - the name of the option.
      defaultValue - the default value to return if opt is not set.
      Returns:
      the value parsed into a particular object.
      Throws:
      ParseException - if there are problems turning the option value into the desired type
      Since:
      1.7.0
      See Also:
    • getParsedOptionValue

      public <T> T getParsedOptionValue(char opt, T defaultValue) throws ParseException
      Gets a version of this Option converted to a particular type.
      Type Parameters:
      T - The return type for the method.
      Parameters:
      opt - the name of the option.
      defaultValue - the default value to return if opt is not set.
      Returns:
      the value parsed into a particular object.
      Throws:
      ParseException - if there are problems turning the option value into the desired type
      Since:
      1.7.0
      See Also:
    • getParsedOptionValue

      public <T> T getParsedOptionValue(Option option) throws ParseException
      Gets a version of this Option converted to a particular type.
      Type Parameters:
      T - The return type for the method.
      Parameters:
      option - the name of the option.
      Returns:
      the value parsed into a particular object.
      Throws:
      ParseException - if there are problems turning the option value into the desired type
      Since:
      1.5.0
      See Also:
    • getParsedOptionValue

      public <T> T getParsedOptionValue(Option option, Supplier<T> defaultValue) throws ParseException
      Gets a version of this Option converted to a particular type.
      Type Parameters:
      T - The return type for the method.
      Parameters:
      option - the name of the option.
      defaultValue - the default value to return if opt is not set.
      Returns:
      the value parsed into a particular object.
      Throws:
      ParseException - if there are problems turning the option value into the desired type
      Since:
      1.7.0
      See Also:
    • getParsedOptionValue

      public <T> T getParsedOptionValue(Option option, T defaultValue) throws ParseException
      Gets a version of this Option converted to a particular type.
      Type Parameters:
      T - The return type for the method.
      Parameters:
      option - the name of the option.
      defaultValue - the default value to return if opt is not set.
      Returns:
      the value parsed into a particular object.
      Throws:
      ParseException - if there are problems turning the option value into the desired type
      Since:
      1.7.0
      See Also:
    • getParsedOptionValue

      public <T> T getParsedOptionValue(String opt) throws ParseException
      Gets a version of this Option converted to a particular type.
      Type Parameters:
      T - The return type for the method.
      Parameters:
      opt - the name of the option.
      Returns:
      the value parsed into a particular object.
      Throws:
      ParseException - if there are problems turning the option value into the desired type
      Since:
      1.2
      See Also:
    • getParsedOptionValue

      public <T> T getParsedOptionValue(String opt, Supplier<T> defaultValue) throws ParseException
      Gets a version of this Option converted to a particular type.
      Type Parameters:
      T - The return type for the method.
      Parameters:
      opt - the name of the option.
      defaultValue - the default value to return if opt is not set.
      Returns:
      the value parsed into a particular object.
      Throws:
      ParseException - if there are problems turning the option value into the desired type
      Since:
      1.7.0
      See Also:
    • getParsedOptionValue

      public <T> T getParsedOptionValue(String opt, T defaultValue) throws ParseException
      Gets a version of this Option converted to a particular type.
      Type Parameters:
      T - The return type for the method.
      Parameters:
      opt - the name of the option.
      defaultValue - the default value to return if opt is not set.
      Returns:
      the value parsed into a particular object.
      Throws:
      ParseException - if there are problems turning the option value into the desired type
      Since:
      1.7.0
      See Also:
    • handleDeprecated

      private void handleDeprecated(Option option)
      Handles deprecated options.
      Parameters:
      option - a deprecated option.
    • hasOption

      public boolean hasOption(char opt)
      Tests to see if an option has been set.
      Parameters:
      opt - character name of the option.
      Returns:
      true if set, false if not.
    • hasOption

      public boolean hasOption(Option opt)
      Tests to see if an option has been set.
      Parameters:
      opt - the option to check.
      Returns:
      true if set, false if not.
      Since:
      1.5.0
    • hasOption

      public boolean hasOption(String opt)
      Tests to see if an option has been set.
      Parameters:
      opt - Short name of the option.
      Returns:
      true if set, false if not.
    • iterator

      public Iterator<Option> iterator()
      Returns an iterator over the Option members of CommandLine.
      Returns:
      an Iterator over the processed Option members of this CommandLine.
    • processPropertiesFromValues

      private void processPropertiesFromValues(Properties props, List<String> values)
      Parses a list of values as properties. All odd numbered values are property keys and even numbered values are property values. If there are an odd number of values the last value is assumed to be a boolean with a value of "true".
      Parameters:
      props - the properties to update.
      values - the list of values to parse.
    • resolveOption

      private Option resolveOption(String opt)
      Retrieves the option object given the long or short option as a String
      Parameters:
      opt - short or long name of the option, may be null.
      Returns:
      Canonicalized option.