public class Option
extends java.lang.Object
implements java.lang.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 independantly, but is create through
an instance of Options
.
Options
,
CommandLine
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
argName
argName specifies the name of the argument for this option
|
private java.lang.String |
description
description of the option
|
private boolean |
hasArg
hasArg specifies whether this option has an associated argument
|
private char |
id
option char (only valid for single character options)
|
private java.lang.String |
longOpt
longOpt is the long representation of the option
|
private int |
numberOfArgs
numberOfArgs specifies the number of argument values this option
can have
|
private java.lang.String |
opt
opt the single character representation of the option
|
private boolean |
optionalArg
specifies whether the argument value of this Option is optional
|
private boolean |
required
required specifies whether this option is required to be present
|
private java.lang.Object |
type
the type of this Option
|
static int |
UNINITIALIZED
constant that specifies the number of argument values has not been specified
|
static int |
UNLIMITED_VALUES
constant that specifies the number of argument values is infinite
|
private java.util.ArrayList |
values
the list of argument values
|
private char |
valuesep
the character that is the value separator
|
Constructor and Description |
---|
Option(java.lang.String opt,
boolean hasArg,
java.lang.String description)
Creates an Option using the specified parameters.
|
Option(java.lang.String opt,
java.lang.String description)
Creates an Option using the specified parameters.
|
Option(java.lang.String opt,
java.lang.String longOpt,
boolean hasArg,
java.lang.String description)
Creates an Option using the specified parameters.
|
Modifier and Type | Method and Description |
---|---|
boolean |
addValue(java.lang.String value)
Adds the specified value to this Option.
|
java.lang.Object |
clone() |
java.lang.String |
getArgName()
Gets the display name for the argument value.
|
int |
getArgs()
Returns the number of argument values this Option can take.
|
java.lang.String |
getDescription()
Retrieve the self-documenting description of this Option
|
int |
getId()
Returns the id of this Option.
|
java.lang.String |
getLongOpt()
Retrieve the long name of this Option.
|
java.lang.String |
getOpt()
Retrieve the name of this Option.
|
java.lang.Object |
getType()
Retrieve the type of this Option.
|
java.lang.String |
getValue() |
java.lang.String |
getValue(int index) |
java.lang.String |
getValue(java.lang.String defaultValue) |
java.lang.String[] |
getValues() |
char |
getValueSeparator()
Returns the value separator character.
|
java.util.List |
getValuesList() |
boolean |
hasArg()
Query to see if this Option requires an argument
|
boolean |
hasArgName()
Returns whether the display name for the argument value
has been set.
|
boolean |
hasArgs()
Query to see if this Option can take many values
|
boolean |
hasLongOpt()
Query to see if this Option has a long name
|
boolean |
hasOptionalArg() |
boolean |
isRequired()
Query to see if this Option requires an argument
|
private boolean |
isValidChar(char c)
Returns whether the specified character is a valid character.
|
private boolean |
isValidOpt(char c)
Returns whether the specified character is a valid Option.
|
void |
setArgName(java.lang.String argName)
Sets the display name for the argument value.
|
void |
setArgs(int num)
Sets the number of argument values this Option can take.
|
void |
setLongOpt(java.lang.String longOpt)
Sets the long name of this Option.
|
void |
setOptionalArg(boolean optionalArg)
Sets whether this Option can have an optional argument.
|
void |
setRequired(boolean required)
Sets whether this Option is mandatory.
|
void |
setType(java.lang.Object type)
Sets the type of this Option.
|
void |
setValueSeparator(char sep)
Sets the value separator.
|
java.lang.String |
toString()
Dump state, suitable for debugging.
|
private void |
validateOption(java.lang.String opt)
Validates whether
opt is a permissable Option
shortOpt. |
public static final int UNINITIALIZED
public static final int UNLIMITED_VALUES
private java.lang.String opt
private java.lang.String longOpt
private boolean hasArg
private java.lang.String argName
private java.lang.String description
private boolean required
private boolean optionalArg
private int numberOfArgs
private java.lang.Object type
private java.util.ArrayList values
private char id
private char valuesep
public Option(java.lang.String opt, java.lang.String description) throws java.lang.IllegalArgumentException
opt
- short representation of the optionhasArg
- specifies whether the Option takes an argument or notdescription
- describes the function of the optionjava.lang.IllegalArgumentException
public Option(java.lang.String opt, boolean hasArg, java.lang.String description) throws java.lang.IllegalArgumentException
opt
- short representation of the optionhasArg
- specifies whether the Option takes an argument or notdescription
- describes the function of the optionjava.lang.IllegalArgumentException
public Option(java.lang.String opt, java.lang.String longOpt, boolean hasArg, java.lang.String description) throws java.lang.IllegalArgumentException
Creates an Option using the specified 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 optionjava.lang.IllegalArgumentException
private void validateOption(java.lang.String opt) throws java.lang.IllegalArgumentException
Validates whether opt
is a permissable Option
shortOpt. The rules that specify if the opt
is valid are:
opt
is not NULLopt
that is either
' '(special case), '?', '@' or a letteropt
that only contains
letters.opt
- The option string to validatejava.lang.IllegalArgumentException
- if the Option is not valid.private boolean isValidOpt(char c)
Returns whether the specified character is a valid Option.
c
- the option to validatec
is a letter, ' ', '?' or '@', otherwise false.private boolean isValidChar(char c)
Returns whether the specified character is a valid character.
c
- the character to validatec
is a letter.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.
public java.lang.String getOpt()
Retrieve the name of this Option.
It is this String which can be used with
CommandLine.hasOption(String opt)
and
CommandLine.getOptionValue(String opt)
to check
for existence and argument.
public java.lang.Object getType()
Retrieve the type of this Option.
public void setType(java.lang.Object type)
Sets the type of this Option.
type
- the type of this Optionpublic java.lang.String getLongOpt()
Retrieve the long name of this Option.
public void setLongOpt(java.lang.String longOpt)
Sets the long name of this Option.
longOpt
- the long name of this Optionpublic void setOptionalArg(boolean optionalArg)
Sets whether this Option can have an optional argument.
optionalArg
- specifies whether the Option can have
an optional argument.public boolean hasOptionalArg()
public boolean hasLongOpt()
Query to see if this Option has a long name
public boolean hasArg()
Query to see if this Option requires an argument
public java.lang.String getDescription()
Retrieve the self-documenting description of this Option
public boolean isRequired()
Query to see if this Option requires an argument
public void setRequired(boolean required)
Sets whether this Option is mandatory.
required
- specifies whether this Option is mandatorypublic void setArgName(java.lang.String argName)
Sets the display name for the argument value.
argName
- the display name for the argument value.public java.lang.String getArgName()
Gets the display name for the argument value.
public boolean hasArgName()
Returns whether the display name for the argument value has been set.
public boolean hasArgs()
Query to see if this Option can take many values
public void setArgs(int num)
Sets the number of argument values this Option can take.
num
- the number of argument valuespublic void setValueSeparator(char sep)
Sets the value separator. For example if the argument value was a Java property, the value separator would be '='.
sep
- The value separator.public char getValueSeparator()
Returns the value separator character.
public int getArgs()
Returns the number of argument values this Option can take.
public java.lang.String toString()
Dump state, suitable for debugging.
toString
in class java.lang.Object
public boolean addValue(java.lang.String value)
Adds the specified value to this Option.
value
- is a/the value of this Optionpublic java.lang.String getValue()
null
if there are no values.public java.lang.String getValue(int index) throws java.lang.IndexOutOfBoundsException
null
if there are no values.java.lang.IndexOutOfBoundsException
public java.lang.String getValue(java.lang.String defaultValue)
defaultValue
if there are no values.public java.lang.String[] getValues()
public java.util.List getValuesList()
public java.lang.Object clone()
clone
in class java.lang.Object