Package org.apache.commons.cli
Class Option.Builder
- java.lang.Object
-
- org.apache.commons.cli.Option.Builder
-
- Enclosing class:
- Option
public static final class Option.Builder extends java.lang.ObjectA nested builder class to createOptioninstances using descriptive methods.Example usage:
Option option = Option.builder("a") .required(true) .longOpt("arg-name") .build();- Since:
- 1.3
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringargNamethe name of the argument for this optionprivate java.lang.Stringdescriptiondescription of the optionprivate java.lang.StringlongOptthe long representation of the optionprivate intnumberOfArgsthe number of argument values this option can haveprivate java.lang.Stringoptthe name of the optionprivate booleanoptionalArgspecifies whether the argument value of this Option is optionalprivate booleanrequiredspecifies whether this option is required to be presentprivate java.lang.Class<?>typethe type of this Optionprivate charvaluesepthe character that is the value separator
-
Constructor Summary
Constructors Modifier Constructor Description privateBuilder(java.lang.String opt)Constructs a newBuilderwith the minimum required parameters for anOptioninstance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Option.BuilderargName(java.lang.String argName)Sets the display name for the argument value.Optionbuild()Constructs an Option with the values declared by thisOption.Builder.Option.Builderdesc(java.lang.String description)Sets the description for this option.Option.BuilderhasArg()Indicates that the Option will require an argument.Option.BuilderhasArg(boolean hasArg)Indicates if the Option has an argument or not.Option.BuilderhasArgs()Indicates that the Option can have unlimited argument values.Option.BuilderlongOpt(java.lang.String longOpt)Sets the long name of the Option.Option.BuildernumberOfArgs(int numberOfArgs)Sets the number of argument values the Option can take.Option.BuilderoptionalArg(boolean isOptional)Sets whether the Option can have an optional argument.Option.Builderrequired()Marks this Option as required.Option.Builderrequired(boolean required)Sets whether the Option is mandatory.Option.Buildertype(java.lang.Class<?> type)Sets the type of the Option.Option.BuildervalueSeparator()The Option will use '=' as a means to separate argument value.Option.BuildervalueSeparator(char sep)The Option will usesepas a means to separate argument values.
-
-
-
Field Detail
-
opt
private final java.lang.String opt
the name of the option
-
description
private java.lang.String description
description of the option
-
longOpt
private java.lang.String longOpt
the long representation of the option
-
argName
private java.lang.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
-
numberOfArgs
private int numberOfArgs
the number of argument values this option can have
-
type
private java.lang.Class<?> type
the type of this Option
-
valuesep
private char valuesep
the character that is the value separator
-
-
Constructor Detail
-
Builder
private Builder(java.lang.String opt) throws java.lang.IllegalArgumentExceptionConstructs a newBuilderwith the minimum required parameters for anOptioninstance.- Parameters:
opt- short representation of the option- Throws:
java.lang.IllegalArgumentException- if there are any non valid Option characters inopt
-
-
Method Detail
-
argName
public Option.Builder argName(java.lang.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
-
desc
public Option.Builder desc(java.lang.String description)
Sets the description for this option.- Parameters:
description- the description of the option.- Returns:
- this builder, to allow method chaining
-
longOpt
public Option.Builder longOpt(java.lang.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
-
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(java.lang.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 usesepas 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
-
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
-
build
public Option build()
Constructs an Option with the values declared by thisOption.Builder.- Returns:
- the new
Option - Throws:
java.lang.IllegalArgumentException- if neitheroptorlongOpthas been set
-
-