Package org.apache.commons.cli
Class Option.Builder
- java.lang.Object
-
- org.apache.commons.cli.Option.Builder
-
- All Implemented Interfaces:
java.util.function.Supplier<Option>
- Enclosing class:
- Option
public static final class Option.Builder extends java.lang.Object implements java.util.function.Supplier<Option>
BuildsOptioninstances 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 intargCountThe number of argument values this option can have.private java.lang.StringargNameThe name of the argument for this option.private Converter<?,?>converterThe converter to convert to type.private static java.lang.Class<java.lang.String>DEFAULT_TYPEThe default type.private DeprecatedAttributesdeprecatedSpecifies whether this option is deprecated.private java.lang.StringdescriptionDescription of the option.private java.lang.StringlongOptionThe long representation of the option.private java.lang.StringoptionThe name of the option.private booleanoptionalArgSpecifies whether the argument value of this Option is optional.private booleanrequiredSpecifies whether this option is required to be present.private java.lang.StringsinceSpecifies the version when this option was added.private java.lang.Class<?>typeThe type of this Option.private charvalueSeparatorThe character that is the value separator.
-
Constructor Summary
Constructors Modifier Constructor Description privateBuilder(java.lang.String option)Constructs a newBuilderwith the minimum required parameters for anOptioninstance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Option.BuilderargName(java.lang.String argName)Sets the display name for the argument value.Optionbuild()Deprecated.Useget().Option.Builderconverter(Converter<?,?> converter)Sets the converter for the option.Option.Builderdeprecated()Marks this Option as deprecated.Option.Builderdeprecated(DeprecatedAttributes deprecated)Sets whether the Option is deprecated.Option.Builderdesc(java.lang.String description)Sets the description for this option.Optionget()Constructs an Option with the values declared by thisOption.Builder.Option.BuilderhasArg()Tests whether the Option will require an argument.Option.BuilderhasArg(boolean hasArg)Tests whether the Option has an argument or not.Option.BuilderhasArgs()Tests whether the Option can have unlimited argument values.Option.BuilderlongOpt(java.lang.String longOption)Sets the long name of the Option.Option.BuildernumberOfArgs(int argCount)Sets the number of argument values the Option can take.Option.Builderoption(java.lang.String option)Sets the name of the Option.Option.BuilderoptionalArg(boolean optionalArg)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 required.Option.Buildersince(java.lang.String since)Sets the version number when this option was first defined."private static java.lang.Class<?>toType(java.lang.Class<?> type)Returns the input Class or the default type (String) if null.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 valueSeparator)The Option will usesepas a means to separate argument values.
-
-
-
Field Detail
-
DEFAULT_TYPE
private static final java.lang.Class<java.lang.String> DEFAULT_TYPE
The default type.
-
argCount
private int argCount
The number of argument values this option can have.
-
argName
private java.lang.String argName
The name of the argument for this option.
-
converter
private Converter<?,?> converter
The converter to convert to type.
-
deprecated
private DeprecatedAttributes deprecated
Specifies whether this option is deprecated.
-
description
private java.lang.String description
Description of the option.
-
longOption
private java.lang.String longOption
The long representation of the option.
-
option
private java.lang.String option
The name of the option.
-
optionalArg
private boolean optionalArg
Specifies whether the argument value of this Option is optional.
-
required
private boolean required
Specifies whether this option is required to be present.
-
since
private java.lang.String since
Specifies the version when this option was added. May be null
-
type
private java.lang.Class<?> type
The type of this Option.
-
valueSeparator
private char valueSeparator
The character that is the value separator.
-
-
Constructor Detail
-
Builder
private Builder(java.lang.String option) throws java.lang.IllegalArgumentExceptionConstructs a newBuilderwith the minimum required parameters for anOptioninstance.- Parameters:
option- short representation of the option.- Throws:
java.lang.IllegalArgumentException- if there are any non valid Option characters inopt.
-
-
Method Detail
-
toType
private static java.lang.Class<?> toType(java.lang.Class<?> type)
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
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:
thisinstance..
-
build
@Deprecated public Option build()
Deprecated.Useget().Constructs an Option with the values declared by thisOption.Builder.- Returns:
- the new
Option. - Throws:
java.lang.IllegalArgumentException- if neitheroptorlongOpthas been set.
-
converter
public Option.Builder converter(Converter<?,?> converter)
Sets the converter for the option.Note: See
TypeHandlerfor serialization discussion.- Parameters:
converter- the Converter to use.- Returns:
thisinstance..- Since:
- 1.7.0
-
deprecated
public Option.Builder deprecated()
Marks this Option as deprecated.- Returns:
- this builder.
- Since:
- 1.7.0
-
deprecated
public Option.Builder deprecated(DeprecatedAttributes deprecated)
Sets whether the Option is deprecated.- Parameters:
deprecated- specifies whether the Option is deprecated.- Returns:
- this builder.
- Since:
- 1.7.0
-
desc
public Option.Builder desc(java.lang.String description)
Sets the description for this option.- Parameters:
description- the description of the option.- Returns:
thisinstance..
-
get
public Option get()
Constructs an Option with the values declared by thisOption.Builder.
-
hasArg
public Option.Builder hasArg()
Tests whether the Option will require an argument.- Returns:
thisinstance..
-
hasArg
public Option.Builder hasArg(boolean hasArg)
Tests whether the Option has an argument or not.- Parameters:
hasArg- specifies whether the Option takes an argument or not.- Returns:
thisinstance..
-
hasArgs
public Option.Builder hasArgs()
Tests whether the Option can have unlimited argument values.- Returns:
- this builder.
-
longOpt
public Option.Builder longOpt(java.lang.String longOption)
Sets the long name of the Option.- Parameters:
longOption- the long name of the Option- Returns:
- this builder.
-
numberOfArgs
public Option.Builder numberOfArgs(int argCount)
Sets the number of argument values the Option can take.- Parameters:
argCount- the number of argument values- Returns:
- this builder.
-
option
public Option.Builder option(java.lang.String option) throws java.lang.IllegalArgumentException
Sets the name of the Option.- Parameters:
option- the name of the Option.- Returns:
- this builder.
- Throws:
java.lang.IllegalArgumentException- if there are any non valid Option characters inopt.- Since:
- 1.5.0
-
optionalArg
public Option.Builder optionalArg(boolean 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
public Option.Builder required()
Marks this Option as required.- Returns:
- this builder.
-
required
public Option.Builder required(boolean required)
Sets whether the Option is required.- Parameters:
required- specifies whether the Option is required.- Returns:
- this builder.
-
since
public Option.Builder since(java.lang.String since)
Sets the version number when this option was first defined."- Parameters:
since- the version number when this option was first defined.- Returns:
- this builder.
-
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.
-
valueSeparator
public Option.Builder valueSeparator()
The Option will use '=' as a means to separate argument value.- Returns:
- this builder.
-
valueSeparator
public Option.Builder valueSeparator(char valueSeparator)
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:
valueSeparator- The value separator.- Returns:
- this builder.
-
-