org.apache.commons.cli

Class Option

public class Option extends Object implements 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.

Version: $Revision: 551821 $

Author: bob mcwhirter (bob @ werken.com) James Strachan

See Also: Options

Field Summary
StringargName
argName specifies the name of the argument for this option
Stringdescription
description of the option
booleanhasArg
hasArg specifies whether this option has an associated argument
StringlongOpt
longOpt is the long representation of the option
intnumberOfArgs
numberOfArgs specifies the number of argument values this option can have
Stringopt
opt the name of the option
booleanoptionalArg
specifies whether the argument value of this Option is optional
booleanrequired
required specifies whether this option is required to be present
Objecttype
the type of this Option
static intUNINITIALIZED
constant that specifies the number of argument values has not been specified
static intUNLIMITED_VALUES
constant that specifies the number of argument values is infinite
ArrayListvalues
the list of argument values *
charvaluesep
the character that is the value separator
Constructor Summary
Option(String opt, String description)
Creates an Option using the specified parameters.
Option(String opt, boolean hasArg, String description)
Creates an Option using the specified parameters.
Option(String opt, String longOpt, boolean hasArg, String description)
Creates an Option using the specified parameters.
Method Summary
voidadd(String value)
Add the value to this Option.
booleanaddValue(String value)
This method is not intended to be used.
voidaddValueForProcessing(String value)
Adds the specified value to this Option.
voidclearValues()

Clear the Option values.

Objectclone()
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.
booleanequals(Object o)
StringgetArgName()
Gets the display name for the argument value.
intgetArgs()
Returns the number of argument values this Option can take.
StringgetDescription()
Retrieve the self-documenting description of this Option
intgetId()
Returns the id of this Option.
StringgetKey()
Returns the 'unique' Option identifier.
StringgetLongOpt()
Retrieve the long name of this Option.
StringgetOpt()
Retrieve the name of this Option.
ObjectgetType()
Retrieve the type of this Option.
StringgetValue()
Returns the specified value of this Option or null if there is no value.
StringgetValue(int index)
Returns the specified value of this Option or null if there is no value.
StringgetValue(String defaultValue)
Returns the value/first value of this Option or the defaultValue if there is no value.
String[]getValues()
Return the values of this Option as a String array or null if there are no values
ListgetValuesList()
chargetValueSeparator()
Returns the value separator character.
booleanhasArg()
Query to see if this Option requires an argument
booleanhasArgName()
Returns whether the display name for the argument value has been set.
booleanhasArgs()
Query to see if this Option can take many values.
inthashCode()
booleanhasLongOpt()
Query to see if this Option has a long name
booleanhasNoValues()
Returns whether this Option has any values.
booleanhasOptionalArg()
booleanhasValueSeparator()
Return whether this Option has specified a value separator.
booleanisRequired()
Query to see if this Option requires an argument
voidprocessValue(String value)
Processes the value.
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 Option
voidsetLongOpt(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.
voidsetType(Object type)
Sets the type of this Option.
voidsetValueSeparator(char sep)
Sets the value separator.
StringtoString()
Dump state, suitable for debugging.

Field Detail

argName

private String argName
argName specifies the name of the argument for this option

description

private String description
description of the option

hasArg

private boolean hasArg
hasArg specifies whether this option has an associated argument

longOpt

private String longOpt
longOpt is the long representation of the option

numberOfArgs

private int numberOfArgs
numberOfArgs specifies the number of argument values this option can have

opt

private String opt
opt the name of the option

optionalArg

private boolean optionalArg
specifies whether the argument value of this Option is optional

required

private boolean required
required specifies whether this option is required to be present

type

private Object type
the type of this Option

UNINITIALIZED

public static final int UNINITIALIZED
constant that specifies the number of argument values has not been specified

UNLIMITED_VALUES

public static final int UNLIMITED_VALUES
constant that specifies the number of argument values is infinite

values

private ArrayList values
the list of argument values *

valuesep

private char valuesep
the character that is the value separator

Constructor Detail

Option

public Option(String opt, String description)
Creates an Option using the specified parameters.

Parameters: opt short representation of the option description describes the function of the option

Throws: IllegalArgumentException if there are any non valid Option characters in opt.

Option

public Option(String opt, boolean hasArg, String description)
Creates an Option using the specified parameters.

Parameters: opt short representation of the option hasArg specifies whether the Option takes an argument or not description describes the function of the option

Throws: IllegalArgumentException if there are any non valid Option characters in opt.

Option

public Option(String opt, String longOpt, boolean hasArg, String description)
Creates an Option using the specified parameters.

Parameters: opt short representation of the option longOpt the long representation of the option hasArg specifies whether the Option takes an argument or not description describes the function of the option

Throws: IllegalArgumentException if there are any non valid Option characters in opt.

Method Detail

add

private void add(String value)
Add the value to this Option.

Parameters: value The value to be added to this Option

Since: 1.0.1

addValue

public boolean addValue(String value)

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.

Throws: UnsupportedOperationException

addValueForProcessing

void addValueForProcessing(String value)
Adds the specified value to this Option.

Parameters: value is a/the value of this Option

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

clone

public Object 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().

Throws: RuntimeException

equals

public boolean equals(Object o)

getArgName

public String getArgName()
Gets the display name for the argument value.

Returns: the display name for the argument value.

getArgs

public int getArgs()
Returns the number of argument values this Option can take.

Returns: num the number of argument values

getDescription

public String getDescription()
Retrieve the self-documenting description of this Option

Returns: The string description of this option

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

getLongOpt

public String getLongOpt()
Retrieve the long name of this Option.

Returns: Long name of this option, or null, if there is no long name

getOpt

public String getOpt()
Retrieve the name of this Option. It is this String which can be used with CommandLine and CommandLine to check for existence and argument.

Returns: The name of this option

getType

public Object getType()
Retrieve the type of this Option.

Returns: The type of this option

getValue

public String getValue()
Returns the specified value of this Option or null if there is no value.

Returns: the value/first value of this Option or null if there is no value.

getValue

public String getValue(int index)
Returns the specified value of this Option or null if there is no value.

Parameters: index The index of the value to be returned.

Returns: the specified value of this Option or null if there is no value.

Throws: IndexOutOfBoundsException if index is less than 1 or greater than the number of the values for this Option.

getValue

public String getValue(String defaultValue)
Returns the value/first value of this Option or the defaultValue if there is no value.

Parameters: defaultValue The value to be returned if ther is no value.

Returns: the value/first value of this Option or the defaultValue if there are no values.

getValues

public String[] 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

public List getValuesList()

Returns: the values of this Option as a List or null if there are no values

getValueSeparator

public char getValueSeparator()
Returns the value separator character.

Returns: the value separator character.

hasArg

public boolean hasArg()
Query to see if this Option requires an argument

Returns: boolean flag indicating if an argument is required

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

hashCode

public int hashCode()

hasLongOpt

public boolean hasLongOpt()
Query to see if this Option has a long name

Returns: boolean flag indicating existence of a long name

hasNoValues

private boolean hasNoValues()
Returns whether this Option has any values.

Returns: whether this Option has any values.

hasOptionalArg

public boolean hasOptionalArg()

Returns: whether this Option can have an optional argument

hasValueSeparator

public boolean hasValueSeparator()
Return whether this Option has specified a value separator.

Returns: whether this Option has specified a value separator.

isRequired

public boolean isRequired()
Query to see if this Option requires an argument

Returns: boolean flag indicating if an argument is required

processValue

private void processValue(String value)
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

setArgName

public void setArgName(String argName)
Sets the display name for the argument value.

Parameters: argName the display name for the argument value.

setArgs

public void setArgs(int num)
Sets the number of argument values this Option can take.

Parameters: num the number of argument values

setDescription

public void setDescription(String description)
Sets the self-documenting description of this Option

Parameters: description The description of this option

setLongOpt

public void setLongOpt(String longOpt)
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.

setRequired

public void setRequired(boolean required)
Sets whether this Option is mandatory.

Parameters: required specifies whether this Option is mandatory

setType

public void setType(Object type)
Sets the type of this Option.

Parameters: type the type of this Option

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.

toString

public String toString()
Dump state, suitable for debugging.

Returns: Stringified form of this object