public class ArgOptions extends Object
parseOptions
must be called, which will disect an array of Strings and create values for
each option. For all option types, a default value may be specified when
requesting the option value. In the case of options corresponding to
classnames, the accessor assumes that no argument constructor exists.
Below are some common usages of this class:
ArgOptions opts = new ArgOptions(); opts.addOption('c', "numClusters", "the number of clusters", true, "INT", "Required"); opts.addOption('C', "clustering", "the clustering algorithm", true, "CLASSNAME", "Required"); opts.addOption('v', "verbose", "true if logs should be emitted", false, null, "Optional"); opts.parseOptions(args); int numClusters = opts.getIntOption('c'); boolean verbose = opts.hasOption('v'); String clusterName = opts.getStringOption('C');
Constructor and Description |
---|
ArgOptions()
Constructs an empty
ArgOptions with no options available. |
Modifier and Type | Method and Description |
---|---|
void |
addOption(char shortName,
String longName,
String description)
Creates an option that is in the default group and takes no values and
adds to the set of possible options.
|
void |
addOption(char shortName,
String longName,
String description,
boolean hasValue,
String valueName)
Creates an option in the default group using the provided parameters and
adds it to the set of possible options.
|
void |
addOption(char shortName,
String longName,
String description,
boolean hasValue,
String valueName,
String optionGroupName)
Creates an option based on the provided parameters and adds the option to
the set of possible options.
|
boolean |
getBooleanOption(char shortName)
Returns the boolean value associated with the
shortName option. |
boolean |
getBooleanOption(char shortName,
boolean defaultValue)
Returns the value associated with the
shortName , or defaultValue if no value was found during parsing. |
boolean |
getBooleanOption(String optionName)
Returns the boolean value associated with the
optionName option. |
boolean |
getBooleanOption(String optionName,
boolean defaultValue)
Returns the value associated with the
optionName , or defaultValue if no value was found during parsing. |
double |
getDoubleOption(char shortName)
Returns the double value associated with the
shortName option. |
double |
getDoubleOption(char shortName,
double defaultValue)
Returns the value associated with the
shortName , or defaultValue if no value was found during parsing. |
double |
getDoubleOption(String optionName)
Returns the double value associated with the
optionName option. |
double |
getDoubleOption(String optionName,
double defaultValue)
Returns the value associated with the
shortName , or defaultValue if no value was found during parsing. |
int |
getIntOption(char shortName)
Returns the int value associated with the
shortName option. |
int |
getIntOption(char shortName,
int defaultValue)
Returns the value associated with the
shortName , or defaultValue if no value was found during parsing. |
int |
getIntOption(String optionName)
Returns the int value associated with the
optionName option. |
int |
getIntOption(String optionName,
int defaultValue)
Returns the value associated with the
shortName , or defaultValue if no value was found during parsing. |
long |
getLongOption(char shortName)
Returns the long value associated with the
shortName option. |
long |
getLongOption(char shortName,
long defaultValue)
Returns the value associated with the
shortName , or defaultValue if no value was found during parsing. |
long |
getLongOption(String optionName)
Returns the long value associated with the
optionName option. |
long |
getLongOption(String optionName,
long defaultValue)
Returns the value associated with the
shortName , or defaultValue if no value was found during parsing. |
<T> T |
getObjectOption(char optionName,
T defaultValue)
Returns a class instance of the class specified associated with
optionName , or defaultValue if there is no value for the
argument. |
<T> T |
getObjectOption(String optionName,
T defaultValue)
Returns a class instance of the class specified associated with
optionName , or defaultValue if there is no value for the
argument. |
String |
getPositionalArg(int argNum)
Returns the argument that was provided at the position based on the
original ordering.
|
List<String> |
getPositionalArgs()
Returns the positional arguments in the order they were provided.
|
String |
getStringOption(char shortName)
Returns the String value associated with the
shortName option. |
String |
getStringOption(char shortName,
String defaultValue)
Returns the value associated with the
shortName , or defaultValue if no value was found during parsing. |
String |
getStringOption(String optionName)
Returns the boolean value associated with the
optionName option. |
String |
getStringOption(String optionName,
String defaultValue)
Returns the value associated with the
optionName , or defaultValue if no value was found during parsing. |
boolean |
hasOption(char shortName)
Returns true if there is a value for the short option name.
|
boolean |
hasOption(String optionName)
Returns true if there is a value for the long option name.
|
int |
numPositionalArgs()
Returns the number of positional arguments specified on the command line.
|
int |
numProvidedOptions()
Returns the number of options that were specified on the command line.
|
void |
parseOptions(String[] commandLine) |
String |
prettyPrint()
Returns a pretty-print formated string describing all of the options and
their arguments.
|
void |
removeOption(char shortName)
Removes the
Option mapped by the given shortName . |
void |
removeOption(String longName)
Removes the
Option mapped by the given longName . |
String |
toString()
Returns a string description of the options that are currently set and
their values.
|
public ArgOptions()
ArgOptions
with no options available.public void addOption(char shortName, String longName, String description)
shortName
- a one character short name for specify this option.longName
- a string name for specifying this option.description
- an optional descritpion for this option, or null
if no description is to be provided.IllegalArgumentException
- if shortName
or the same longName
has been already been added. public void addOption(char shortName, String longName, String description, boolean hasValue, String valueName)
shortName
- a one character short name for specify this option.longName
- a string name for specifying this option.description
- an optional descritpion for this option, or null
if no description is to be provided.hasValue
- whether this option takes a value.valueName
- if this option takes a value, the name of that valueIllegalArgumentException
- if shortName
or the same longName
has been already been added. hasArg
is true
but valueName
is null
. public void addOption(char shortName, String longName, String description, boolean hasValue, String valueName, String optionGroupName)
shortName
- a one character short name for specify this option.longName
- a string name for specifying this option.description
- an optional descritpion for this option, or null
if no description is to be provided.hasValue
- whether this option takes a value.valueName
- if this option takes a value, the name of that valueoptionGroupName
- the name of a group if this option is part of a
specific subset of the options, or null
if this is a
generic optionIllegalArgumentException
- if shortName
or the same longName
has been already been added. hasArg
is true
but valueName
is null
. public void removeOption(char shortName)
Option
mapped by the given shortName
. This
also removes the mapping from the long name if one exists.shortName
- The short name key of the option to remove.public void removeOption(String longName)
Option
mapped by the given longName
. This
also removes the mapping from the short name if one exists.longName
- The long name key of the option to remove.public void parseOptions(String[] commandLine)
commandLine
- the set of string arguments provided on the command
linepublic int numPositionalArgs()
public int numProvidedOptions()
public String getPositionalArg(int argNum)
public List<String> getPositionalArgs()
public double getDoubleOption(char shortName)
shortName
option.IllegalArgumentException
- If no value was provided for shortName
public double getDoubleOption(char shortName, double defaultValue)
shortName
, or defaultValue
if no value was found during parsing.public double getDoubleOption(String optionName)
optionName
option.IllegalArgumentException
- If no value was provided for shortName
public double getDoubleOption(String optionName, double defaultValue)
shortName
, or defaultValue
if no value was found during parsing.public long getLongOption(char shortName)
shortName
option.IllegalArgumentException
- If no value was provided for shortName
public long getLongOption(char shortName, long defaultValue)
shortName
, or defaultValue
if no value was found during parsing.public long getLongOption(String optionName)
optionName
option.IllegalArgumentException
- If no value was provided for shortName
public long getLongOption(String optionName, long defaultValue)
shortName
, or defaultValue
if no value was found during parsing.public int getIntOption(char shortName)
shortName
option.IllegalArgumentException
- If no value was provided for shortName
public int getIntOption(char shortName, int defaultValue)
shortName
, or defaultValue
if no value was found during parsing.public int getIntOption(String optionName)
optionName
option.IllegalArgumentException
- If no value was provided for shortName
public int getIntOption(String optionName, int defaultValue)
shortName
, or defaultValue
if no value was found during parsing.public boolean getBooleanOption(char shortName)
shortName
option.IllegalArgumentException
- If no value was provided for shortName
public boolean getBooleanOption(char shortName, boolean defaultValue)
shortName
, or defaultValue
if no value was found during parsing.public boolean getBooleanOption(String optionName)
optionName
option.IllegalArgumentException
- If no value was provided for shortName
public boolean getBooleanOption(String optionName, boolean defaultValue)
optionName
, or defaultValue
if no value was found during parsing.public String getStringOption(char shortName)
shortName
option.IllegalArgumentException
- If no value was provided for shortName
public String getStringOption(char shortName, String defaultValue)
shortName
, or defaultValue
if no value was found during parsing.public String getStringOption(String optionName)
optionName
option.IllegalArgumentException
- If no value was provided for shortName
public <T> T getObjectOption(char optionName, T defaultValue)
optionName
, or defaultValue
if there is no value for the
argument. This method assumes that the class has a no argument
constructor.public <T> T getObjectOption(String optionName, T defaultValue)
optionName
, or defaultValue
if there is no value for the
argument. This method assumes that the class has a no argument
constructor.public String getStringOption(String optionName, String defaultValue)
optionName
, or defaultValue
if no value was found during parsing.public boolean hasOption(String optionName)
public boolean hasOption(char shortName)
public String prettyPrint()
Copyright © 2012. All Rights Reserved.