public class SimpleCLIParser extends Object
This class can be used on its own, but is normally subclassed. The subclass is
responsible for defining the set of options and presenting a constructor that
just takes the command-line argument vector. The subclass also normally defines
an enum
that corresponds to the available options, as a shorthand
for accessing the option values.
The SimpleCLIParser.OptionDefinition
nested class is how the caller
defines legal options. Options have two forms:
--opt param
"), or as
embedded parameters (eg: "--opt=param
"). Parameterized options
may be repeated (eg: "--opt param1 --opt param2
"), and embedded
parameters may also be repeated (eg: "--opt param1,param2
"). A
parameterized option is considered "enabled" if it appears in the argument
list, with or without parmaeters.
getParameters()
method, or one at a time
using the shift()
method.
Design note: this class is intended for internal use in a narrow phase of program operation. As a result, it makes no attempt to protect its internal state. If you wish to modify the option data after construction, have at it (but beware that some lists are unmodifiable as implementation choices).
Note also that this class does not attempt to perform validation. The caller is responsible for ensuring that all required options are present, and that option parameters are valid.
Modifier and Type | Class and Description |
---|---|
static class |
SimpleCLIParser.OptionDefinition
This class defines a single command-line option.
|
Constructor and Description |
---|
SimpleCLIParser(String[] argv,
SimpleCLIParser.OptionDefinition... optionDefs)
Processes the supplied list of arguments into options, option params,
and non-options.
|
Modifier and Type | Method and Description |
---|---|
List<SimpleCLIParser.OptionDefinition> |
getAllDefinitions()
Returns the complete list of option definitions.
|
SimpleCLIParser.OptionDefinition |
getDefinition(Object key)
Returns a single option definition, by key,
null if
there's no such definition. |
String |
getHelp()
Returns a help message that describes the various options.
|
List<Object> |
getOptions()
Returns the keys for all specified options, in the order that they
appear on the command line.
|
List<String> |
getOptionValues(Object key)
For options that take parameters, returns the parameters.
|
List<String> |
getParameters()
Returns the complete list of non-option parameters.
|
boolean |
isOptionEnabled(Object key)
Indicates whether the option identified by the passed key is enabled
or not.
|
String |
shift()
Returns the next non-option parameter,
null if there are
none remaining. |
public SimpleCLIParser(String[] argv, SimpleCLIParser.OptionDefinition... optionDefs)
argv
- The argument vector passed to main()
.optionDefs
- Definitions for all expected options.public SimpleCLIParser.OptionDefinition getDefinition(Object key)
null
if
there's no such definition.public List<SimpleCLIParser.OptionDefinition> getAllDefinitions()
getHelp()
is
usually a better choice.public String getHelp()
public boolean isOptionEnabled(Object key)
IllegalArgumentException
- if given a key that does not match
any definition.public List<Object> getOptions()
public List<String> getOptionValues(Object key)
public List<String> getParameters()
shift()
. The
returned list is array-backed.public String shift()
null
if there are
none remaining. This method iterates through the list of parameters
once, and may not be reset.