|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object info.rolandkrueger.roklib.cli.CommandLineOption
public class CommandLineOption
This class constitutes a single command line option that is managed by a
CommandLineArgumentEvaluator
. An option has several properties that
can be configured through this class. An option can have the following
properties:
--server
for the long
option and -s
for the short option. Both variants are configured
through the constructor of class CommandLineOption
. It is possible to
omit one of these variants if, for instance, only a long option should be
available.
CommandLineOptionType
.
CommandLineArgumentEvaluator
will detect this. Note that it makes no
sense to mark flag options as mandatory. Therefore, trying to do so will
raise an unchecked exception.
addDefaultValue(String)
and
addDefaultValues(String[])
.
CommandLineOption
objectsCommandLineOption
is a public inner class, a special syntax has to be
applied in order to create new instances of this class. To do so, you need an
object of CommandLineOption
's enclosing class
CommandLineArgumentEvaluator
. Say such an object is named
evaluator
. A new Option can then be created with Option option = evaluator.new Option (...);
CommandLineOption
's status, i.e. the
user inputCommandLineArgumentEvaluator.evaluate()
, the status of an option can
be queried. That is, you can check whether the user's input is valid, which
values she has provided for list and singular options, and which flag options
she has used. To check if a flag option has been used, you can call method
isSet()
. To obtain the list of values that have
been provided for a list or a singular option, method
getInputList()
can be used. This method will return
a one-element list for singular options. A simple idiom for querying the
user-provided value of a singular option is the following: Option singularOption; CommandLineArgumentEvaluator evaluator; // ... evaluator.evaluate (); // ... String value = singularOption.getInputList ()[0];
CommandLineOptionType
Nested Class Summary | |
---|---|
private static class |
CommandLineOption.EmptyCallback
|
Field Summary | |
---|---|
private Set<CommandLineOption> |
mAssociateOptions
Set of other option objects which must be provided in combination with this option. |
private ICommandLineOptionCallback |
mCallback
|
private boolean |
mDefaultValueSet
|
private String |
mDescription
|
private boolean |
mIsSet
|
private String |
mLongOptionName
|
private boolean |
mMandatory
|
private Set<CommandLineOption> |
mMutuallyExclusiveOptions
List of other options which must not be provided together with this option. |
private boolean |
mRepetionsAllowed
|
private int |
mRepetitionCount
|
private String |
mShortOptionName
|
private CommandLineOptionType |
mType
|
private List<String> |
mValues
|
Constructor Summary | |
---|---|
private |
CommandLineOption()
Private default constructor. |
|
CommandLineOption(Character shortOption,
String longOption,
CommandLineOptionType type,
boolean mandatory)
Constructor for creating a CommandLineOption without a description. |
|
CommandLineOption(Character shortOption,
String longOption,
String description,
CommandLineOptionType type)
|
|
CommandLineOption(Character shortOption,
String longOption,
String description,
CommandLineOptionType type,
boolean mandatory)
|
|
CommandLineOption(Character shortOption,
String longOption,
String description,
CommandLineOptionType type,
boolean mandatory,
ICommandLineOptionCallback callback)
Constructor. |
Method Summary | |
---|---|
void |
addAssociateOptions(CommandLineOption... associates)
|
void |
addDefaultValue(String defaultValue)
Adds a default value for this option. |
void |
addDefaultValues(String... values)
Adds a list of values to the default values of this option. |
void |
addMutuallyExclusiveOptions(CommandLineOption... otherOptions)
|
protected boolean |
addUserInput(String input)
|
void |
allowOptionRepetition(boolean yesNo)
|
boolean |
equals(Object obj)
|
String |
getDescription()
Returns the description of this option as provided through the constructor. |
String[] |
getInputList()
Returns the list of values that have been provided by the user for an option. |
String |
getLongOption()
Returns the option name's long version. |
int |
getRepetitionCount()
|
String |
getShortOption()
Returns the option name's short version. |
String |
getSingleParameterValue()
|
CommandLineOptionType |
getType()
|
int |
hashCode()
|
void |
increaseRepetitions()
|
protected void |
invokeCallback(CommandLineArgumentEvaluator source)
|
boolean |
isAssociateTo(CommandLineOption otherOption)
|
boolean |
isFlag()
Checks whether this option is a flag option. |
boolean |
isMandatory()
Returns whether this option is mandatory or optional. |
boolean |
isMutuallyExclusiveWith(CommandLineOption otherOption)
|
boolean |
isOptionRepetitionAllowed()
|
boolean |
isSet()
Checks whether the option was used on the command line. |
protected void |
set()
Mark this option as set. |
void |
setCallback(ICommandLineOptionCallback callback)
|
void |
setMandatory(boolean mandatory)
|
String |
toString()
|
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private String mShortOptionName
private String mLongOptionName
private String mDescription
private CommandLineOptionType mType
private boolean mIsSet
private boolean mMandatory
private boolean mDefaultValueSet
private List<String> mValues
private boolean mRepetionsAllowed
private int mRepetitionCount
private Set<CommandLineOption> mMutuallyExclusiveOptions
private Set<CommandLineOption> mAssociateOptions
private ICommandLineOptionCallback mCallback
Constructor Detail |
---|
public CommandLineOption(Character shortOption, String longOption, String description, CommandLineOptionType type, boolean mandatory, ICommandLineOptionCallback callback)
shortOption
- short version of the option name. For example, -s
longOption
- long version of the option name. For example,
--server_address
description
- short description for the option. This description will be used
for assembling a help message by method
CommandLineArgumentEvaluator.getOptionDescription()
.type
- type of the option. An option can be a flag option, a singular
option, or a list option.mandatory
- true
if the option is mandatory. Is only allowed for
list and singular options
IllegalArgumentException
- either if an option was both configured to be a flag option and
mandatory or if both short and long option name were left blankpublic CommandLineOption(Character shortOption, String longOption, String description, CommandLineOptionType type)
public CommandLineOption(Character shortOption, String longOption, String description, CommandLineOptionType type, boolean mandatory)
private CommandLineOption()
public CommandLineOption(Character shortOption, String longOption, CommandLineOptionType type, boolean mandatory)
CommandLineOption
without a description.
Method Detail |
---|
public void setCallback(ICommandLineOptionCallback callback)
public void allowOptionRepetition(boolean yesNo)
public void setMandatory(boolean mandatory)
public boolean isOptionRepetitionAllowed()
public boolean isFlag()
true
if this option was configured as a flag optionprotected void invokeCallback(CommandLineArgumentEvaluator source)
public boolean isSet()
CommandLineArgumentEvaluator.getMissingOptions()
.
true
if the option was used by the user on the command
line.public void addAssociateOptions(CommandLineOption... associates)
public void addMutuallyExclusiveOptions(CommandLineOption... otherOptions)
public boolean isMutuallyExclusiveWith(CommandLineOption otherOption)
public boolean isAssociateTo(CommandLineOption otherOption)
public String[] getInputList()
CommandLineOption
object is a singular option, the
returned list has exactly one element.
null
if there aren't any values for this
optionpublic String getSingleParameterValue()
protected void set()
public boolean isMandatory()
true
if this option is marked as mandatoryprotected boolean addUserInput(String input)
public boolean equals(Object obj)
equals
in class Object
public int hashCode()
hashCode
in class Object
public String getDescription()
public void addDefaultValue(String defaultValue)
CommandLineOption
object maintains a list of default values. If the
user has omitted an option on the command line, querying the option with
getInputList()
will yield its default values.
Additionally, each option with default values will return true
wenn invoking its isSet()
method.addDefaultValue(String)
will
add each provided value to the list of default values for list options.
This is not the case for singular options. Here, only the value provided
with the last call to addDefaultValue(String)
will be used as the only default value.CommandLineArgumentEvaluator.evaluate()
. Otherwise the actual data provided by the user will be overwritten for
singular options or the default values will be added to the user's data for
list options, respectively.
defaultValue
- the default value to be added
IllegalStateException
- if this option is either a flag option (which aren't meant to be
provided with a value by definition) or a mandatory option (where
the user is forced to provide a value himself).public void addDefaultValues(String... values)
values
- list of default values
UnsupportedOperationException
- if this option is a flag option or if the passed
values
list has more than one element for singular
optionsaddDefaultValue(String)
public String getLongOption()
public String getShortOption()
public CommandLineOptionType getType()
public void increaseRepetitions()
public int getRepetitionCount()
public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |