org.apache.commons.cli
Class Option

java.lang.Object
  extended byorg.apache.commons.cli.Option
All Implemented Interfaces:
Cloneable

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: 1.6 $
Author:
bob mcwhirter (bob @ werken.com), James Strachan
See Also:
Options, CommandLine

Field Summary
private  String argName
          argName specifies the name of the argument for this option
private  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  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  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  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  ArrayList values
          the list of argument values
private  char valuesep
          the character that is the value separator
 
Constructor Summary
Option(String opt, boolean hasArg, String description)
          Creates an Option using the specified parameters.
Option(String opt, 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
 boolean addValue(String value)
          Adds the specified value to this Option.
 Object clone()
           
 String getArgName()
          Gets the display name for the argument value.
 int getArgs()
          Returns the number of argument values this Option can take.
 String getDescription()
          Retrieve the self-documenting description of this Option
 int getId()
          Returns the id of this Option.
 String getLongOpt()
          Retrieve the long name of this Option.
 String getOpt()
          Retrieve the name of this Option.
 Object getType()
          Retrieve the type of this Option.
 String getValue()
           
 String getValue(int index)
           
 String getValue(String defaultValue)
           
 String[] getValues()
           
 char getValueSeparator()
          Returns the value separator character.
 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(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(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(Object type)
          Sets the type of this Option.
 void setValueSeparator(char sep)
          Sets the value separator.
 String toString()
          Dump state, suitable for debugging.
private  void validateOption(String opt)
          Validates whether opt is a permissable Option shortOpt.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

UNINITIALIZED

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

See Also:
Constant Field Values

UNLIMITED_VALUES

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

See Also:
Constant Field Values

opt

private String opt
opt the single character representation of the option


longOpt

private String longOpt
longOpt is the long representation of the option


hasArg

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


argName

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


description

private String description
description of the option


required

private boolean required
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
numberOfArgs specifies the number of argument values this option can have


type

private Object type
the type of this Option


values

private ArrayList values
the list of argument values


id

private char id
option char (only valid for single character options)


valuesep

private char valuesep
the character that is the value separator

Constructor Detail

Option

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

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

Option

public Option(String opt,
              boolean hasArg,
              String description)
       throws IllegalArgumentException
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

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 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
Method Detail

validateOption

private void validateOption(String opt)
                     throws IllegalArgumentException

Validates whether opt is a permissable Option shortOpt. The rules that specify if the opt is valid are:

Parameters:
opt - The option string to validate
Throws:
IllegalArgumentException - if the Option is not valid.

isValidOpt

private boolean isValidOpt(char c)

Returns whether the specified character is a valid Option.

Parameters:
c - the option to validate
Returns:
true if c is a letter, ' ', '?' or '@', otherwise false.

isValidChar

private boolean isValidChar(char c)

Returns whether the specified character is a valid character.

Parameters:
c - the character to validate
Returns:
true if c is a letter.

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

getOpt

public 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.

Returns:
The name of this option

getType

public Object getType()

Retrieve the type of this Option.

Returns:
The type of this option

setType

public void setType(Object type)

Sets the type of this Option.

Parameters:
type - the type of this Option

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

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.

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

public String getDescription()

Retrieve the self-documenting description of this Option

Returns:
The string description of this option

isRequired

public boolean isRequired()

Query to see if this Option requires an argument

Returns:
boolean flag indicating if an argument is required

setRequired

public void setRequired(boolean required)

Sets whether this Option is mandatory.

Parameters:
required - specifies whether this Option is mandatory

setArgName

public void setArgName(String argName)

Sets the display name for the argument value.

Parameters:
argName - the display name for the argument value.

getArgName

public String 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.

getArgs

public int getArgs()

Returns the number of argument values this Option can take.

Returns:
num the number of argument values

toString

public String toString()

Dump state, suitable for debugging.

Returns:
Stringified form of this object

addValue

public boolean addValue(String value)

Adds the specified value to this Option.

Parameters:
value - is a/the value of this Option

getValue

public String getValue()
Returns:
the value/first value of this Option or null if there are no values.

getValue

public String getValue(int index)
                throws IndexOutOfBoundsException
Returns:
the specified value of this Option or null if there are no values.
Throws:
IndexOutOfBoundsException

getValue

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

getValues

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

clone

public Object clone()
Returns:
a copy of this Option