|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--net.sf.saffron.util.OptionsList
Command-line option parser.
For example, given the options
Short name | Long name | Type | Default | Anonymous |
---|---|---|---|---|
v | verbose | Boolean | true | false |
f | String | true | ||
c | count | Number | false |
and the command line
-v count=5 Foo.txtthe parser will set verbose = true, count = 5, and file = Foo.txt.
Options can generally be specified using flag syntax (for example -v or -count 5) or property synax (for example verbose=true or count=5).
Boolean options do not have a value following the flag. -v means the same as verbose=true, and +v means the same as verbose=false.
One of the options in a list can be anonymous. Arguments which are
not flagged with an option name are assumed to be for this option.
Defining options
You first define what options are available. Options all implement
interface OptionsList.Option
. You can use one of the built-in option types
(OptionsList.StringOption
, OptionsList.NumberOption
, OptionsList.BooleanOption
, OptionsList.EnumeratedOption
) or write one of your own.
Parsing options
Once you have defined the options, you can parse an argument list by
calling parse(java.lang.String[])
.
There are two ways of handling options. By default, when you parse an array
of command-line parameters, the values of those parameters are stored in
the options themselves. Alternatively, you can specify a handler
.
Nested Class Summary | |
class |
OptionsList.BasicOptionHandler
|
static class |
OptionsList.BooleanOption
|
static class |
OptionsList.EnumeratedOption
|
private static class |
OptionsList.Group
|
static class |
OptionsList.NumberOption
|
static class |
OptionsList.Option
Definition of a command-line option, including its short and long names, description, default value, and whether it is mandatory. |
(package private) static interface |
OptionsList.OptionHandler
Handles the event of setting options. |
static class |
OptionsList.StringOption
|
Field Summary | |
private ArrayList |
optionGroups_
|
private ArrayList |
options_
|
Constructor Summary | |
OptionsList()
Creates an options list with no options. |
|
OptionsList(OptionsList.Option[] options)
Creates an options list with an array of options. |
Method Summary | |
void |
add(OptionsList.Option option)
|
void |
constrain(OptionsList.Option[] options,
int minCount,
int maxCount)
Tells the options list that the given options are mutually exclusive. |
void |
parse(String[] args)
|
OptionsList.Option[] |
toArray()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private final ArrayList optionGroups_
private final ArrayList options_
Constructor Detail |
public OptionsList()
public OptionsList(OptionsList.Option[] options)
Method Detail |
public void add(OptionsList.Option option)
public void constrain(OptionsList.Option[] options, int minCount, int maxCount)
To create a set mutually exclusive options, specify minCount = 0 or 1, maxCount = 1. To create a set of mutually inclusive options, specify minCount = 1, maxCount = -1.
options
- List of mutually exclusive optionsminCount
- Minimum number of these options which must be
specified.maxCount
- Maximum number of these options which must be
specified.public void parse(String[] args)
public OptionsList.Option[] toArray()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |