Class Option.Builder

java.lang.Object
org.apache.commons.cli.Option.Builder
Enclosing class:
Option

public static final class Option.Builder extends Object
A nested builder class to create Option instances using descriptive methods.

Example usage:

 Option option = Option.builder("a").required(true).longOpt("arg-name").build();
 
Since:
1.3
  • Field Details

    • option

      private String option
      The name of the option
    • description

      private String description
      description of the option
    • longOption

      private String longOption
      The long representation of the option
    • argName

      private String argName
      The name of the argument for this option
    • required

      private boolean required
      specifies whether this option is required to be present
    • optionalArg

      private boolean optionalArg
      specifies whether the argument value of this Option is optional
    • argCount

      private int argCount
      The number of argument values this option can have
    • type

      private Class<?> type
      The type of this Option
    • valueSeparator

      private char valueSeparator
      The character that is the value separator
  • Constructor Details

    • Builder

      private Builder(String option) throws IllegalArgumentException
      Constructs a new Builder with the minimum required parameters for an Option instance.
      Parameters:
      option - short representation of the option
      Throws:
      IllegalArgumentException - if there are any non valid Option characters in opt
  • Method Details

    • argName

      public Option.Builder argName(String argName)
      Sets the display name for the argument value.
      Parameters:
      argName - the display name for the argument value.
      Returns:
      this builder, to allow method chaining
    • build

      public Option build()
      Constructs an Option with the values declared by this Option.Builder.
      Returns:
      the new Option
      Throws:
      IllegalArgumentException - if neither opt or longOpt has been set
    • desc

      public Option.Builder desc(String description)
      Sets the description for this option.
      Parameters:
      description - the description of the option.
      Returns:
      this builder, to allow method chaining
    • hasArg

      public Option.Builder hasArg()
      Indicates that the Option will require an argument.
      Returns:
      this builder, to allow method chaining
    • hasArg

      public Option.Builder hasArg(boolean hasArg)
      Indicates if the Option has an argument or not.
      Parameters:
      hasArg - specifies whether the Option takes an argument or not
      Returns:
      this builder, to allow method chaining
    • hasArgs

      public Option.Builder hasArgs()
      Indicates that the Option can have unlimited argument values.
      Returns:
      this builder, to allow method chaining
    • longOpt

      public Option.Builder longOpt(String longOpt)
      Sets the long name of the Option.
      Parameters:
      longOpt - the long name of the Option
      Returns:
      this builder, to allow method chaining
    • numberOfArgs

      public Option.Builder numberOfArgs(int numberOfArgs)
      Sets the number of argument values the Option can take.
      Parameters:
      numberOfArgs - the number of argument values
      Returns:
      this builder, to allow method chaining
    • option

      public Option.Builder option(String option) throws IllegalArgumentException
      Sets the name of the Option.
      Parameters:
      option - the name of the Option
      Returns:
      this builder, to allow method chaining
      Throws:
      IllegalArgumentException - if there are any non valid Option characters in opt
      Since:
      1.5.0
    • optionalArg

      public Option.Builder optionalArg(boolean isOptional)
      Sets whether the Option can have an optional argument.
      Parameters:
      isOptional - specifies whether the Option can have an optional argument.
      Returns:
      this builder, to allow method chaining
    • required

      public Option.Builder required()
      Marks this Option as required.
      Returns:
      this builder, to allow method chaining
    • required

      public Option.Builder required(boolean required)
      Sets whether the Option is mandatory.
      Parameters:
      required - specifies whether the Option is mandatory
      Returns:
      this builder, to allow method chaining
    • type

      public Option.Builder type(Class<?> type)
      Sets the type of the Option.
      Parameters:
      type - the type of the Option
      Returns:
      this builder, to allow method chaining
    • valueSeparator

      public Option.Builder valueSeparator()
      The Option will use '=' as a means to separate argument value.
      Returns:
      this builder, to allow method chaining
    • valueSeparator

      public Option.Builder valueSeparator(char sep)
      The Option will use sep as a means to separate argument values.

      Example:

       Option opt = Option.builder("D").hasArgs().valueSeparator('=').build();
       Options options = new Options();
       options.addOption(opt);
       String[] args = {"-Dkey=value"};
       CommandLineParser parser = new DefaultParser();
       CommandLine line = parser.parse(options, args);
       String propertyName = line.getOptionValues("D")[0]; // will be "key"
       String propertyValue = line.getOptionValues("D")[1]; // will be "value"
       
      Parameters:
      sep - The value separator.
      Returns:
      this builder, to allow method chaining