Package org.apache.commons.cli
Class Option.Builder
java.lang.Object
org.apache.commons.cli.Option.Builder
- Enclosing class:
Option
Builds
Option
instances using descriptive methods.
Example usage:
Option option = Option.builder("a").required(true).longOpt("arg-name").build();
- Since:
- 1.3
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
The number of argument values this option can have.private String
The name of the argument for this option.private Converter
<?, ?> The converter to convert to type.The default type.private DeprecatedAttributes
Specifies whether this option is deprecated.private String
Description of the option.private String
The long representation of the option.private String
The name of the option.private boolean
Specifies whether the argument value of this Option is optional.private boolean
Specifies whether this option is required to be present.private Class
<?> The type of this Option.private char
The character that is the value separator. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionSets the display name for the argument value.build()
Constructs an Option with the values declared by thisOption.Builder
.Sets the converter for the option.Marks this Option as deprecated.deprecated
(DeprecatedAttributes deprecated) Sets whether the Option is deprecated.Sets the description for this option.hasArg()
Tests whether the Option will require an argument.hasArg
(boolean hasArg) Tests whether the Option has an argument or not.hasArgs()
Tests whether the Option can have unlimited argument values.Sets the long name of the Option.numberOfArgs
(int argCount) Sets the number of argument values the Option can take.Sets the name of the Option.optionalArg
(boolean optionalArg) Sets whether the Option can have an optional argument.required()
Marks this Option as required.required
(boolean required) Sets whether the Option is required.private static Class
<?> Returns the input Class or the default type (String) if null.Sets the type of the Option.The Option will use '=' as a means to separate argument value.valueSeparator
(char valueSeparator) The Option will usesep
as a means to separate argument values.
-
Field Details
-
DEFAULT_TYPE
The default type. -
argCount
private int argCountThe number of argument values this option can have. -
argName
The name of the argument for this option. -
converter
The converter to convert to type. -
deprecated
Specifies whether this option is deprecated. -
description
Description of the option. -
longOption
The long representation of the option. -
option
The name of the option. -
optionalArg
private boolean optionalArgSpecifies whether the argument value of this Option is optional. -
required
private boolean requiredSpecifies whether this option is required to be present. -
type
The type of this Option. -
valueSeparator
private char valueSeparatorThe character that is the value separator.
-
-
Constructor Details
-
Builder
Constructs a newBuilder
with the minimum required parameters for anOption
instance.- Parameters:
option
- short representation of the option.- Throws:
IllegalArgumentException
- if there are any non valid Option characters inopt
.
-
-
Method Details
-
toType
Returns the input Class or the default type (String) if null.- Parameters:
type
- the candidate Class.- Returns:
- the input Class or the default type (String) if null.
-
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
Constructs an Option with the values declared by thisOption.Builder
.- Returns:
- the new
Option
. - Throws:
IllegalArgumentException
- if neitheropt
orlongOpt
has been set.
-
converter
Sets the converter for the option.Note: see
TypeHandler
for serialization discussion.- Parameters:
converter
- the Converter to use.- Returns:
- this builder, to allow method chaining.
- Since:
- 1.7.0
-
deprecated
Marks this Option as deprecated.- Returns:
- this builder.
- Since:
- 1.7.0
-
deprecated
Sets whether the Option is deprecated.- Parameters:
deprecated
- specifies whether the Option is deprecated.- Returns:
- this builder.
- Since:
- 1.7.0
-
desc
Sets the description for this option.- Parameters:
description
- the description of the option.- Returns:
- this builder, to allow method chaining.
-
hasArg
Tests whether the Option will require an argument.- Returns:
- this builder, to allow method chaining.
-
hasArg
Tests whether 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
Tests whether the Option can have unlimited argument values.- Returns:
- this builder.
-
longOpt
Sets the long name of the Option.- Parameters:
longOpt
- the long name of the Option- Returns:
- this builder.
-
numberOfArgs
Sets the number of argument values the Option can take.- Parameters:
argCount
- the number of argument values- Returns:
- this builder.
-
option
Sets the name of the Option.- Parameters:
option
- the name of the Option.- Returns:
- this builder.
- Throws:
IllegalArgumentException
- if there are any non valid Option characters inopt
.- Since:
- 1.5.0
-
optionalArg
Sets whether the Option can have an optional argument.- Parameters:
optionalArg
- specifies whether the Option can have an optional argument.- Returns:
- this builder.
-
required
Marks this Option as required.- Returns:
- this builder.
-
required
Sets whether the Option is required.- Parameters:
required
- specifies whether the Option is required.- Returns:
- this builder.
-
type
Sets the type of the Option.- Parameters:
type
- the type of the Option.- Returns:
- this builder.
-
valueSeparator
The Option will use '=' as a means to separate argument value.- Returns:
- this builder.
-
valueSeparator
The Option will usesep
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:
valueSeparator
- The value separator.- Returns:
- this builder.
-