Package org.apache.commons.cli
Class Option
java.lang.Object
org.apache.commons.cli.Option
- All Implemented Interfaces:
Serializable,Cloneable
Describes a single command-line option. It maintains
information regarding the short-name of the option, the long-name,
if any exists, a flag indicating if an argument is required for
this option, and a self-documenting description of the option.
An Option is not created independently, but is created through
an instance of Options. An Option is required to have
at least a short or a long-name.
Note: once an Option has been added to an instance
of Options, it's required flag may not be changed anymore.
- Version:
- $Id: Option.java 1756753 2016-08-18 10:18:43Z britter $
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA nested builder class to createOptioninstances using descriptive methods. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Stringthe name of the argument for this optionprivate Stringdescription of the optionprivate Stringthe long representation of the optionprivate intthe number of argument values this option can haveprivate final Stringthe name of the optionprivate booleanspecifies whether the argument value of this Option is optionalprivate booleanspecifies whether this option is required to be presentprivate static final longThe serial version UID.private Class<?>the type of this Optionstatic final intconstant that specifies the number of argument values has not been specifiedstatic final intconstant that specifies the number of argument values is infinitethe list of argument valuesprivate charthe character that is the value separator -
Constructor Summary
ConstructorsModifierConstructorDescriptionCreates an Option using the specified parameters.Creates an Option using the specified parameters.Creates an Option using the specified parameters.privateOption(Option.Builder builder) Private constructor used by the nested Builder class. -
Method Summary
Modifier and TypeMethodDescription(package private) booleanTells if the option can accept more arguments.private voidAdd the value to this Option.booleanDeprecated.(package private) voidaddValueForProcessing(String value) Adds the specified value to this Option.static Option.Builderbuilder()Returns aOption.Builderto create anOptionusing descriptive methods.static Option.BuilderReturns aOption.Builderto create anOptionusing descriptive methods.(package private) voidClear the Option values.clone()A rather odd clone method - due to incorrect code in 1.0 it is public and in 1.1 rather than throwing a CloneNotSupportedException it throws a RuntimeException so as to maintain backwards compat at the API level.booleanGets the display name for the argument value.intgetArgs()Returns the number of argument values this Option can take.Retrieve the self-documenting description of this OptionintgetId()Returns the id of this Option.(package private) StringgetKey()Returns the 'unique' Option identifier.Retrieve the long name of this Option.getOpt()Retrieve the name of this Option.getType()Retrieve the type of this Option.getValue()Returns the specified value of this Option ornullif there is no value.getValue(int index) Returns the specified value of this Option ornullif there is no value.Returns the value/first value of this Option or thedefaultValueif there is no value.String[]Return the values of this Option as a String array or null if there are no valuescharReturns the value separator character.booleanhasArg()Query to see if this Option requires an argumentbooleanReturns whether the display name for the argument value has been set.booleanhasArgs()Query to see if this Option can take many values.inthashCode()booleanQuery to see if this Option has a long nameprivate booleanReturns whether this Option has any values.booleanbooleanReturn whether this Option has specified a value separator.booleanQuery to see if this Option is mandatoryprivate voidprocessValue(String value) Processes the value.(package private) booleanTells if the option requires more arguments to be valid.voidsetArgName(String argName) Sets the display name for the argument value.voidsetArgs(int num) Sets the number of argument values this Option can take.voidsetDescription(String description) Sets the self-documenting description of this OptionvoidsetLongOpt(String longOpt) Sets the long name of this Option.voidsetOptionalArg(boolean optionalArg) Sets whether this Option can have an optional argument.voidsetRequired(boolean required) Sets whether this Option is mandatory.voidSets the type of this Option.voidDeprecated.voidsetValueSeparator(char sep) Sets the value separator.toString()Dump state, suitable for debugging.
-
Field Details
-
UNINITIALIZED
public static final int UNINITIALIZEDconstant that specifies the number of argument values has not been specified- See Also:
-
UNLIMITED_VALUES
public static final int UNLIMITED_VALUESconstant that specifies the number of argument values is infinite- See Also:
-
serialVersionUID
private static final long serialVersionUIDThe serial version UID.- See Also:
-
opt
the name of the option -
longOpt
the long representation of the option -
argName
the name of the argument for this option -
description
description of the option -
required
private boolean requiredspecifies whether this option is required to be present -
optionalArg
private boolean optionalArgspecifies whether the argument value of this Option is optional -
numberOfArgs
private int numberOfArgsthe number of argument values this option can have -
type
the type of this Option -
values
the list of argument values -
valuesep
private char valuesepthe character that is the value separator
-
-
Constructor Details
-
Option
Private constructor used by the nested Builder class.- Parameters:
builder- builder used to create this option
-
Option
Creates an Option using the specified parameters. The option does not take an argument.- Parameters:
opt- short representation of the optiondescription- describes the function of the option- Throws:
IllegalArgumentException- if there are any non valid Option characters inopt.
-
Option
Creates an Option using the specified parameters.- Parameters:
opt- short representation of the optionhasArg- specifies whether the Option takes an argument or notdescription- describes the function of the option- Throws:
IllegalArgumentException- if there are any non valid Option characters inopt.
-
Option
public Option(String opt, String longOpt, boolean hasArg, String description) throws IllegalArgumentException Creates an Option using the specified parameters.- Parameters:
opt- short representation of the optionlongOpt- the long representation of the optionhasArg- specifies whether the Option takes an argument or notdescription- describes the function of the option- Throws:
IllegalArgumentException- if there are any non valid Option characters inopt.
-
-
Method Details
-
getId
public int getId()Returns the id of this Option. This is only set when the Option shortOpt is a single character. This is used for switch statements.- Returns:
- the id of this Option
-
getKey
String getKey()Returns the 'unique' Option identifier.- Returns:
- the 'unique' Option identifier
-
getOpt
Retrieve the name of this Option. It is this String which can be used withCommandLine.hasOption(String opt)andCommandLine.getOptionValue(String opt)to check for existence and argument.- Returns:
- The name of this option
-
getType
Retrieve the type of this Option.- Returns:
- The type of this option
-
setType
Deprecated.since 1.3, usesetType(Class)insteadSets the type of this Option.Note: this method is kept for binary compatibility and the input type is supposed to be a
Classobject.- Parameters:
type- the type of this Option
-
setType
Sets the type of this Option.- Parameters:
type- the type of this Option- Since:
- 1.3
-
getLongOpt
Retrieve the long name of this Option.- Returns:
- Long name of this option, or null, if there is no long name
-
setLongOpt
Sets the long name of this Option.- Parameters:
longOpt- the long name of this Option
-
setOptionalArg
public void setOptionalArg(boolean optionalArg) Sets whether this Option can have an optional argument.- Parameters:
optionalArg- specifies whether the Option can have an optional argument.
-
hasOptionalArg
public boolean hasOptionalArg()- Returns:
- whether this Option can have an optional argument
-
hasLongOpt
public boolean hasLongOpt()Query to see if this Option has a long name- Returns:
- boolean flag indicating existence of a long name
-
hasArg
public boolean hasArg()Query to see if this Option requires an argument- Returns:
- boolean flag indicating if an argument is required
-
getDescription
Retrieve the self-documenting description of this Option- Returns:
- The string description of this option
-
setDescription
Sets the self-documenting description of this Option- Parameters:
description- The description of this option- Since:
- 1.1
-
isRequired
public boolean isRequired()Query to see if this Option is mandatory- Returns:
- boolean flag indicating whether this Option is mandatory
-
setRequired
public void setRequired(boolean required) Sets whether this Option is mandatory.- Parameters:
required- specifies whether this Option is mandatory
-
setArgName
Sets the display name for the argument value.- Parameters:
argName- the display name for the argument value.
-
getArgName
Gets the display name for the argument value.- Returns:
- the display name for the argument value.
-
hasArgName
public boolean hasArgName()Returns whether the display name for the argument value has been set.- Returns:
- if the display name for the argument value has been set.
-
hasArgs
public boolean hasArgs()Query to see if this Option can take many values.- Returns:
- boolean flag indicating if multiple values are allowed
-
setArgs
public void setArgs(int num) Sets the number of argument values this Option can take.- Parameters:
num- the number of argument values
-
setValueSeparator
public void setValueSeparator(char sep) Sets the value separator. For example if the argument value was a Java property, the value separator would be '='.- Parameters:
sep- The value separator.
-
getValueSeparator
public char getValueSeparator()Returns the value separator character.- Returns:
- the value separator character.
-
hasValueSeparator
public boolean hasValueSeparator()Return whether this Option has specified a value separator.- Returns:
- whether this Option has specified a value separator.
- Since:
- 1.1
-
getArgs
public int getArgs()Returns the number of argument values this Option can take.A value equal to the constant
UNINITIALIZED(= -1) indicates the number of arguments has not been specified. A value equal to the constantUNLIMITED_VALUES(= -2) indicates that this options takes an unlimited amount of values.- Returns:
- num the number of argument values
- See Also:
-
addValueForProcessing
Adds the specified value to this Option.- Parameters:
value- is a/the value of this Option
-
processValue
Processes the value. If this Option has a value separator the value will have to be parsed into individual tokens. When n-1 tokens have been processed and there are more value separators in the value, parsing is ceased and the remaining characters are added as a single token.- Parameters:
value- The String to be processed.- Since:
- 1.0.1
-
add
Add the value to this Option. If the number of arguments is greater than zero and there is enough space in the list then add the value. Otherwise, throw a runtime exception.- Parameters:
value- The value to be added to this Option- Since:
- 1.0.1
-
getValue
Returns the specified value of this Option ornullif there is no value.- Returns:
- the value/first value of this Option or
nullif there is no value.
-
getValue
Returns the specified value of this Option ornullif there is no value.- Parameters:
index- The index of the value to be returned.- Returns:
- the specified value of this Option or
nullif there is no value. - Throws:
IndexOutOfBoundsException- if index is less than 1 or greater than the number of the values for this Option.
-
getValue
Returns the value/first value of this Option or thedefaultValueif there is no value.- Parameters:
defaultValue- The value to be returned if there is no value.- Returns:
- the value/first value of this Option or the
defaultValueif there are no values.
-
getValues
Return the values of this Option as a String array or null if there are no values- Returns:
- the values of this Option as a String array or null if there are no values
-
getValuesList
- Returns:
- the values of this Option as a List or null if there are no values
-
toString
Dump state, suitable for debugging. -
hasNoValues
private boolean hasNoValues()Returns whether this Option has any values.- Returns:
- whether this Option has any values.
-
equals
-
hashCode
public int hashCode() -
clone
A rather odd clone method - due to incorrect code in 1.0 it is public and in 1.1 rather than throwing a CloneNotSupportedException it throws a RuntimeException so as to maintain backwards compat at the API level. After calling this method, it is very likely you will want to call clearValues().- Overrides:
clonein classObject- Returns:
- a clone of this Option instance
- Throws:
RuntimeException- if aCloneNotSupportedExceptionhas been thrown bysuper.clone()
-
clearValues
void clearValues()Clear the Option values. After a parse is complete, these are left with data in them and they need clearing if another parse is done. See: CLI-71 -
addValue
Deprecated.This method is not intended to be used. It was a piece of internal API that was made public in 1.0. It currently throws an UnsupportedOperationException.- Parameters:
value- the value to add- Returns:
- always throws an
UnsupportedOperationException - Throws:
UnsupportedOperationException- always
-
acceptsArg
boolean acceptsArg()Tells if the option can accept more arguments.- Returns:
- false if the maximum number of arguments is reached
- Since:
- 1.3
-
requiresArg
boolean requiresArg()Tells if the option requires more arguments to be valid.- Returns:
- false if the option doesn't require more arguments
- Since:
- 1.3
-
builder
Returns aOption.Builderto create anOptionusing descriptive methods.- Returns:
- a new
Option.Builderinstance - Since:
- 1.3
-
builder
Returns aOption.Builderto create anOptionusing descriptive methods.- Parameters:
opt- short representation of the option- Returns:
- a new
Option.Builderinstance - Throws:
IllegalArgumentException- if there are any non valid Option characters inopt- Since:
- 1.3
-
setType(Class)instead