public static final class Option.Builder
extends java.lang.Object
Option
instances
using descriptive methods.
Example usage:
Option option = Option.builder("a") .required(true) .longOpt("arg-name") .build();
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
argName
the name of the argument for this option
|
private java.lang.String |
description
description of the option
|
private java.lang.String |
longOpt
the long representation of the option
|
private int |
numberOfArgs
the number of argument values this option can have
|
private java.lang.String |
opt
the name of the option
|
private boolean |
optionalArg
specifies whether the argument value of this Option is optional
|
private boolean |
required
specifies whether this option is required to be present
|
private java.lang.Class<?> |
type
the type of this Option
|
private char |
valuesep
the character that is the value separator
|
Modifier | Constructor and Description |
---|---|
private |
Builder(java.lang.String opt)
Constructs a new
Builder with the minimum
required parameters for an Option instance. |
Modifier and Type | Method and Description |
---|---|
Option.Builder |
argName(java.lang.String argName)
Sets the display name for the argument value.
|
Option |
build()
Constructs an Option with the values declared by this
Option.Builder . |
Option.Builder |
desc(java.lang.String description)
Sets the description for this option.
|
Option.Builder |
hasArg()
Indicates that the Option will require an argument.
|
Option.Builder |
hasArg(boolean hasArg)
Indicates if the Option has an argument or not.
|
Option.Builder |
hasArgs()
Indicates that the Option can have unlimited argument values.
|
Option.Builder |
longOpt(java.lang.String longOpt)
Sets the long name of the Option.
|
Option.Builder |
numberOfArgs(int numberOfArgs)
Sets the number of argument values the Option can take.
|
Option.Builder |
optionalArg(boolean isOptional)
Sets whether the Option can have an optional argument.
|
Option.Builder |
required()
Marks this Option as required.
|
Option.Builder |
required(boolean required)
Sets whether the Option is mandatory.
|
Option.Builder |
type(java.lang.Class<?> type)
Sets the type of the Option.
|
Option.Builder |
valueSeparator()
The Option will use '=' as a means to separate argument value.
|
Option.Builder |
valueSeparator(char sep)
The Option will use
sep as a means to
separate argument values. |
private final java.lang.String opt
private java.lang.String description
private java.lang.String longOpt
private java.lang.String argName
private boolean required
private boolean optionalArg
private int numberOfArgs
private java.lang.Class<?> type
private char valuesep
private Builder(java.lang.String opt) throws java.lang.IllegalArgumentException
Builder
with the minimum
required parameters for an Option
instance.opt
- short representation of the optionjava.lang.IllegalArgumentException
- if there are any non valid Option characters in opt
public Option.Builder argName(java.lang.String argName)
argName
- the display name for the argument value.public Option.Builder desc(java.lang.String description)
description
- the description of the option.public Option.Builder longOpt(java.lang.String longOpt)
longOpt
- the long name of the Optionpublic Option.Builder numberOfArgs(int numberOfArgs)
numberOfArgs
- the number of argument valuespublic Option.Builder optionalArg(boolean isOptional)
isOptional
- specifies whether the Option can have
an optional argument.public Option.Builder required()
public Option.Builder required(boolean required)
required
- specifies whether the Option is mandatorypublic Option.Builder type(java.lang.Class<?> type)
type
- the type of the Optionpublic Option.Builder valueSeparator()
public Option.Builder valueSeparator(char sep)
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"
sep
- The value separator.public Option.Builder hasArg()
public Option.Builder hasArg(boolean hasArg)
hasArg
- specifies whether the Option takes an argument or notpublic Option.Builder hasArgs()
public Option build()
Option.Builder
.Option
java.lang.IllegalArgumentException
- if neither opt
or longOpt
has been set