org.apache.commons.cli
Class Option

java.lang.Object
  |
  +--org.apache.commons.cli.Option
All Implemented Interfaces:
java.lang.Cloneable

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.

Version:
$Revision: 1.6 $
Author:
bob mcwhirter (bob @ werken.com), James Strachan
See Also:
Options, CommandLine

Field Summary
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
 
Constructor Summary
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.
 
Method Summary
 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
 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.
 
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

UNLIMITED_VALUES

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

Option

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

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

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

Returns:
The name of this option

getType

public java.lang.Object getType()

Retrieve the type of this Option.

Returns:
The type of this option

setType

public void setType(java.lang.Object type)

Sets the type of this Option.

Parameters:
type - the type of this Option

getLongOpt

public java.lang.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(java.lang.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 java.lang.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(java.lang.String argName)

Sets the display name for the argument value.

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

getArgName

public java.lang.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 java.lang.String toString()

Dump state, suitable for debugging.

Overrides:
toString in class java.lang.Object
Returns:
Stringified form of this object

addValue

public boolean addValue(java.lang.String value)

Adds the specified value to this Option.

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

getValue

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

getValue

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

getValue

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

getValues

public java.lang.String[] getValues()
Returns:
the values of this Option as a String array or null if there are no values

getValuesList

public java.util.List getValuesList()
Returns:
the values of this Option as a List or null if there are no values

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object
Returns:
a copy of this Option


Copyright © 2002-2002 Apache Software Foundation. All Rights Reserved.