Package ml.options
Class OptionSet
java.lang.Object
ml.options.OptionSet
- All Implemented Interfaces:
Constrainable
This class holds the information for a set of options. A set can hold any number of
OptionData
instances which are checked together to determine success or failure.
The approach to use this class looks like this:
- The user uses any of the
Options.addSet()
methods (e. g.Options.addSet(String)
) to create any number of sets required (or just relies on the default set, if only one set is required) - The user adds all required option definitions to each set
- Using any of the
Options.check()
methods, each set can be checked whether the options that were specified on the command line satisfy its requirements - If the check was successful for a given set, several data items are available from this class:
- All options defined for the set (through which e. g. values, details, and multiplicity are available)
- All data items found (these are the items on the command line which do not start with the prefix, i. e. non-option arguments)
- All unmatched arguments on the command line (these are the items on the command line which start with the prefix, but do not match to one of the options). Programs can elect to ignore these, or react with an error
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
A constant indicating an unlimited number of supported data items -
Method Summary
Modifier and TypeMethodDescriptionboolean
Indicate whether this set accepts data (which means thatmaxData
is 1 or larger).void
addConstraint
(Constraint constraint) Add a constraint for this option setaddOption
(OptionData.Type type, String key) Add the given option to the set.addOption
(OptionData.Type type, String key, String altKey) Add the given option to the set.addOption
(OptionData.Type type, String key, String altKey, Options.Multiplicity multiplicity) Add the given option to the set.addOption
(OptionData.Type type, String key, Options.Multiplicity multiplicity) Add the given option to the set.Get the constraints defined for this option setgetData()
Return the data items found (these are the items on the command line which do not start with the prefix, i.getData
(int index) Return a specific data item.int
Return the number of data items found (these are the items on the command line which do not start with the prefix, i.getDataText
(int index) Get the data text for a data item on the command line.getHelpText
(int index) Get the help text for a data item on the command line.int
Getter method formaxData
propertyint
Getter method forminData
propertygetName()
Return the name of the setGet the data for a specific option, identified by its key name (which is unique)Get a list of all the options defined for this setReturn all unmatched items found (these are the items on the command line which start with the prefix, but do not match to one of the options)getUnmatched
(int index) Return a specific unmatched item.int
Return the number of unmatched items found (these are the items on the command line which start with the prefix, but do not match to one of the options)boolean
Indicate whether this set has no upper limit for the number of allowed data itemsboolean
Indicate whether this set is the default set or notboolean
Check whether a specific option is set, i.void
A convenience method that prints all the results obtained for this option set toSystem.out
.setDataText
(int index, String text) Set the data text for a data item on the command line.setHelpText
(int index, String text) Set the help text for a data item on the command line.
-
Field Details
-
INF
public static final int INFA constant indicating an unlimited number of supported data items- See Also:
-
-
Method Details
-
hasUnlimitedData
public boolean hasUnlimitedData()Indicate whether this set has no upper limit for the number of allowed data items- Returns:
- A boolean indicating whether this set has no upper limit for the number of allowed data items
-
isDefault
public boolean isDefault()Indicate whether this set is the default set or not- Returns:
- A boolean indicating whether this set is the default set or not
-
addConstraint
Add a constraint for this option set- Specified by:
addConstraint
in interfaceConstrainable
- Parameters:
constraint
- TheConstraint
to add
-
getConstraints
Get the constraints defined for this option set- Specified by:
getConstraints
in interfaceConstrainable
- Returns:
- The defined constraints for this option (or
null
if no constraints have been defined)
-
setDataText
Set the data text for a data item on the command line. This is exploited e. g. inHelpPrinter
instances.- Parameters:
index
- The index for this data item on the command line. Must be within the allowed range of0 ... maxData - 1
for this set. If this set supports an unlimited number of data items, the allowed range is0 ... minData
.text
- The text to use for this data item in the command line syntax- Returns:
- This set to allow for invocation chaining
-
setHelpText
Set the help text for a data item on the command line. This is exploited e. g. inHelpPrinter
instances.- Parameters:
index
- The index for this data item on the command line. Must be within the allowed range of0 ... maxData - 1
for this set. If this set supports an unlimited number of data items, the allowed range is0 ... minData
.text
- The help text to use to describe the purpose of the data item- Returns:
- This set to allow for invocation chaining
-
getDataText
Get the data text for a data item on the command line. This is only useful if such a data text is used.- Parameters:
index
- The index for this data item on the command line. Must be within the allowed range of0 ... maxData - 1
for this set. If this set supports an unlimited number of data items, the allowed range is0 ... minData
.- Returns:
- The text used for this data item in the command line syntax
-
getHelpText
Get the help text for a data item on the command line. This is only useful if such a help text is used.- Parameters:
index
- The index for this data item on the command line. Must be within the allowed range of0 ... maxData - 1
for this set. If this set supports an unlimited number of data items, the allowed range is0 ... minData
.- Returns:
- The help text used to describe the purpose of the data item
-
getOptionData
Get a list of all the options defined for this set- Returns:
- A list of
OptionData
instances defined for this set
-
getOption
Get the data for a specific option, identified by its key name (which is unique)- Parameters:
key
- The key for the option- Returns:
- The
OptionData
instance
-
isSet
Check whether a specific option is set, i. e. whether it was specified at least once on the command line.- Parameters:
key
- The key for the option- Returns:
true
orfalse
, depending on the outcome of the check
-
getName
Return the name of the set- Returns:
- The name of the set
-
getMinData
public int getMinData()Getter method forminData
property- Returns:
- The value for the
minData
property
-
getMaxData
public int getMaxData()Getter method formaxData
property- Returns:
- The value for the
maxData
property
-
acceptsData
public boolean acceptsData()Indicate whether this set accepts data (which means thatmaxData
is 1 or larger).- Returns:
- A boolean indicating whether this set accepts data
-
getData
Return the data items found (these are the items on the command line which do not start with the prefix, i. e. non-option arguments)- Returns:
- A list of strings with all data items found
-
getDataCount
public int getDataCount()Return the number of data items found (these are the items on the command line which do not start with the prefix, i. e. non-option arguments)- Returns:
- The number of all data items found
-
getData
Return a specific data item.- Parameters:
index
-- Returns:
- The requested data item
-
getUnmatched
Return all unmatched items found (these are the items on the command line which start with the prefix, but do not match to one of the options)- Returns:
- A list of strings with all unmatched items found
-
getUnmatchedCount
public int getUnmatchedCount()Return the number of unmatched items found (these are the items on the command line which start with the prefix, but do not match to one of the options)- Returns:
- The number of all unmatched items found
-
getUnmatched
Return a specific unmatched item.- Parameters:
index
-- Returns:
- The requested unmatched item
-
addOption
Add the given option to the set.- Parameters:
type
- The type of the optionkey
- The name of the option- Returns:
- The newly created option (to support invocation chaining)
-
addOption
Add the given option to the set.- Parameters:
type
- The type of the optionkey
- The name of the optionmultiplicity
- The multiplicity of the option- Returns:
- The newly created option (to support invocation chaining)
-
addOption
Add the given option to the set.- Parameters:
type
- The type of the optionkey
- The name of the optionaltKey
- The alternate name of the option- Returns:
- The newly created option (to support invocation chaining)
-
addOption
public OptionData addOption(OptionData.Type type, String key, String altKey, Options.Multiplicity multiplicity) Add the given option to the set.- Parameters:
type
- The type of the optionkey
- The name of the optionaltKey
- The alternate name of the optionmultiplicity
- The multiplicity of the option- Returns:
- The newly created option (to support invocation chaining)
-
printResults
public void printResults()A convenience method that prints all the results obtained for this option set toSystem.out
. This is quite handy to quickly check whether a set definition yields the expected results for a given set of command line arguments.
-