public class Option
extends java.lang.Object
implements java.io.Serializable
Represents a command line option. See Options
to see how options are
processed. This class essentially holds information about each option. The
inner class Option.Instance
holds a specific instance of an option on
the command line. As such, it holds the value or values an option has once it
has been set on the command line.
All setter methods return the object that was used to call them. This allows setter methods to be chained together on one line. For example:
myOpt.setArgNum(1).setRequired(true).setValueSeparator('+');
Options have either a long name, a short name or both. The long name must be
at least two characters long. The short name must be only one character long
although this class enforces this by only allowing the short name to be set
using a char
. Both names can contain only legal characters. The
legal characters are specified by the validateName(java.lang.String)
method
![]() |
![]() |
![]() |
![]() |
Modifier and Type | Class and Description |
---|---|
class |
Option.Instance
An
Instance of an Option contains values assigned
to an option. |
Modifier and Type | Field and Description |
---|---|
static int |
MULTIPLE_ARGUMENTS
Used to indicate the option takes an unlimited number of arguments
(strictly speaking, the upper limit is set by Integer.MAX_VALUE)
|
static char |
UNSET
Used to indicate a character argument (such as short name or value
separator) has no value
|
Constructor and Description |
---|
Option(Options container,
char shortName,
boolean hasArg,
java.lang.String description)
Constructs a new
Option with the specified short name and
description. |
Option(Options container,
char shortName,
java.lang.String description)
Constructs a new
Option with the specified short name and
description. |
Option(Options container,
char shortName,
java.lang.String longName,
boolean hasArg,
java.lang.String description)
Constructs a new
Option with the specified short name, long
name name and description. |
Option(Options container,
char shortName,
java.lang.String longName,
java.lang.String description)
Constructs a new
Option with the specified short name, long
name and description. |
Option(Options container,
java.lang.String longName,
boolean hasArg,
java.lang.String description)
Constructs a new
Option with the specified long name name and
description. |
Option(Options container,
java.lang.String longName,
java.lang.String description)
Constructs a new
Option with the specified long name and
description. |
Modifier and Type | Method and Description |
---|---|
Option |
disableValueSeparator()
Disables the use of a value separator.
|
boolean |
equals(java.lang.Object o)
Two options are considered equal if they have the same long name and short
name
|
java.lang.String |
getDescription() |
java.lang.String |
getId()
Returns the long name for this option if it is set.
|
java.lang.String |
getLongName() |
int |
getNumVals()
Returns the maximum number of values this option can take.
|
java.lang.String |
getShortName() |
char |
getValueSeparator()
Returns the character used to denote a separation in values for this option
in an argument.
|
int |
hashCode() |
boolean |
hasLongName() |
boolean |
hasShortName() |
boolean |
hasValue() |
boolean |
hasValues() |
boolean |
hasValueSeparator()
Returns whether or not this option has a value separator.
|
boolean |
isRequired() |
Option.Instance |
newInstance()
Generates a new instance of this option.
|
Option |
setDescription(java.lang.String description)
Sets a short description for this option
|
Option |
setMultipleArgs()
Allows this option to have multiple arguments.
|
Option |
setNumValues(int num)
Sets the number of values this option can take.
|
Option |
setRequired(boolean required)
Sets whether or not this option must be set on the command line
|
Option |
setValueSeparator(char sep)
Sets the value separator that separates values in an argument assigned to
this option.
|
java.lang.String |
toString()
Returns one of the names of this option.
|
public static final char UNSET
public static final int MULTIPLE_ARGUMENTS
public Option(Options container, char shortName, java.lang.String description) throws java.lang.IllegalArgumentException, java.lang.NullPointerException
Option
with the specified short name and
description. The option will have no long name and take no arguments.container
- An Options
object that this object will be added to
after construction. Can be null
, in which case this
option won't be added to a Options
object after
constructionshortName
- The short name of the option, or Option.UNSET if this option
should not have a short namedescription
- A brief (one line) description of what the option doesjava.lang.IllegalArgumentException
- if shortName
is an illegal character.java.lang.NullPointerException
- if shortName
is the NullPointerException
character or Option.UNSETvalidateName(java.lang.String)
public Option(Options container, char shortName, boolean hasArg, java.lang.String description) throws java.lang.IllegalArgumentException
Option
with the specified short name and
description. The option will have no long name and take one argument if
hasArg
is true
or nor arguments if it is
false
. To allow the option to have more than one argument, use
one of the setter. methods after construction.container
- An Options
object that this object will be added to
after construction. Can be null
, in which case this
option won't be added to a Options
object after
constructionshortName
- The short name of the optionhasArg
- Whether or not the option takes a value.description
- A brief (one line) description of what the option doesjava.lang.IllegalArgumentException
- if shortName
is an illegal character.java.lang.NullPointerException
- if shortName
is the null
character
or Option.UNSETvalidateName(java.lang.String)
public Option(Options container, java.lang.String longName, java.lang.String description) throws java.lang.IllegalArgumentException, java.lang.NullPointerException
Option
with the specified long name and
description. The option will have no short name and take no arguments.container
- An Options
object that this object will be added to
after construction. Can be null
, in which case this
option won't be added to a Options
object after
constructionlongName
- The long name of the option.description
- A brief (one line) description of what the option does.java.lang.IllegalArgumentException
- if longName
contains illegal characters or is
less than two characters long.java.lang.NullPointerException
- if longName
is the null
.validateName(java.lang.String)
public Option(Options container, java.lang.String longName, boolean hasArg, java.lang.String description) throws java.lang.IllegalArgumentException
Option
with the specified long name name and
description. The option will have no short name and take one argument if
hasArg
is true
or nor arguments if it is
false
. To allow the option to have more than one argument, use
one of the setter methods after construction.container
- An Options
object that this object will be added to
after construction. Can be null
, in which case this
option won't be added to a Options
object after
constructionlongName
- The long name of the option.hasArg
- Whether or not the option takes a value.description
- A brief (one line) description of what the option does.java.lang.IllegalArgumentException
- if longName
contains illegal characters or is
less than two characters long.java.lang.NullPointerException
- if longName
is the null
.validateName(java.lang.String)
public Option(Options container, char shortName, java.lang.String longName, java.lang.String description) throws java.lang.IllegalArgumentException
Option
with the specified short name, long
name and description. The option will take no arguments.container
- An Options
object that this object will be added to
after construction. Can be null
, in which case this
option won't be added to a Options
object after
constructionshortName
- The short name of the option, or Option.UNSET if this option
should not have a short name. In this case, longName
cannot be null
.longName
- The long name of the option (can be null
so long as
short name is specified).description
- A brief (one line) description of what the option does.java.lang.IllegalArgumentException
- if shortName
or longName
contain
illegal characters, or if long name is less than two
characters long.java.lang.NullPointerException
- if longName
is the null
or if
shortName
is the null
character or
Option.UNSETvalidateName(java.lang.String)
public Option(Options container, char shortName, java.lang.String longName, boolean hasArg, java.lang.String description) throws java.lang.IllegalArgumentException, java.lang.NullPointerException
Option
with the specified short name, long
name name and description. The option will take one argument if
hasArg
is true
or nor arguments if it is
false
. To allow the option to have more than one argument, use
one of the setter methods after construction.container
- An Options
object that this object will be added to
after construction. Can be null
, in which case this
option won't be added to a Options
object after
constructionshortName
- The short name of the option, or Option.UNSET if this option
should not have a short name In this case, longName
cannot be null
longName
- The long name of the option (can be null
so long as
short name is specified)hasArg
- Whether or not the option takes a value.description
- A brief (one line) description of what the option doesjava.lang.IllegalArgumentException
- if shortName
or longName
contain
illegal characters, or if long name is less than two
characters long.java.lang.NullPointerException
- if longName
is the null
or if
shortName
is the null
character or
Option.UNSETvalidateName(java.lang.String)
public Option disableValueSeparator()
Option
public java.lang.String getDescription()
public java.lang.String getId()
public java.lang.String getLongName()
public int getNumVals()
MULTIPLE_ARGUMENTS
will be
returned.public java.lang.String getShortName()
public char getValueSeparator()
UNSET
is returned. Even if this option only takes one value
or no values at all, a value separator may still be set. However, in these
cases it will never be used.public boolean hasValue()
true
if this option takes one or more values.
false
otherwise.public boolean hasValues()
true
if this option takes more than one value.
false
otherwise.public int hashCode()
hashCode
in class java.lang.Object
public boolean hasLongName()
true
if this option has a long name.
false
otherwise.public boolean hasShortName()
true
if this option has a short name.
false
otherwise.public boolean hasValueSeparator()
true
if this option has a value separator.
false
otherwise.public boolean isRequired()
true
if this option must be set on the command line.
false
otherwisepublic Option.Instance newInstance()
Option.Instance
public Option setNumValues(int num) throws java.lang.IllegalArgumentException
MULTIPLE_ARGUMENTS
as the value.num
- The number of values this option can takeOption
java.lang.IllegalArgumentException
- if num
is less than zeropublic Option setDescription(java.lang.String description)
description
- The descriptionOption
public Option setMultipleArgs()
int
Option
public Option setRequired(boolean required)
required
- Whether or not this option must be set on the command lineOption
public Option setValueSeparator(char sep)
sep
- the value separator to use when assigning an argument to this
optionOption
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
Object.equals(java.lang.Object)
public java.lang.String toString()
toString
in class java.lang.Object
Object.toString()