|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--ucb.util.CommandArgs
A CommandArgs object is a sequence of arguments (Strings), typically extracted from the command-line arguments to a main procedure. It expects such arguments to conform to Sun's standard guidelines, according to which, a command (issued to a shell) has the following general form:
COMMAND [ OPTION ... ] [ -- ] [ OTHER_ARGUMENT ... ]
([]'s indicate optional parts; ... indicates one or more). Each
OPTION has one of the following forms (x, y, etc. denote
non-blank characters):
Single parameterless short option:
-x
Several parameterless short options:
-xyz...
Single short option with parameter:
-x OPTARG
or
-xOPTARG
Long parameterless option:
--opt
Long option with parameter:
--opt=foo
If a short option takes an additional argument, that argument is
always required to follow; it cannot be omitted. When a long argument
takes an argument, it is optional.
The '--' before the first OTHER_ARGUMENT is optional unless that OTHER_ARGUMENT starts with '-'.
One creates a CommandArgs object by supplying an array of legal option names (without leading "-"s or "--"s), and an array of command-line argument strings (as sent to the main function). To indicate options that take an additional parameter value, affix a trailing '=' to the option name. If all options are single-letter, you can use the alternative form of the constructor, which takes as its option a single string with all option letters, with those that take arguments followed by a colon (:).
The CommandArgs object then parses the command-line arguments according to the specification, and presents the options and other arguments by means of a set of access methods.
Any short option is considered equivalent to a one-character long option, and vice-versa.
For example, suppose that we have a program whose usage is
foo [ -c ] [ -h ] [ -o FILE ] ARG1
where []'s indicate optional arguments. It's main program would
begin
import ucb.util.CommandArgs;
class foo {
public static void main (String[] args0) {
boolean cOptionSpecified;
boolean hOptionSpecified;
String oOptionValue;
String arg1;
CommandArgs args = new CommandArgs ("cho:", args0);
if (! args.ok ()
|| args.number ('o') > 1 // multiple o options
|| args.numOtherArgs () != 1)
ERROR ();
cOptionSpecified = args.present ('c');
hOptionSpecified = args.present ('h');
oOptionValue = args.optionValue ('o', 0); // null if absent.
arg1 = args.otherArg (0);
...
For a program whose usage is
bar [ -c ] [ -o FILE ] [ --dry-run ] [ --form=NAME ] ARG1
we can write
import ucb.util.CommandArgs;
class foo {
public static void main (String[] args0) {
...
String[] options = { "c", "o=", "dry-run", "form=" };
CommandArgs args = new CommandArgs (options, args0);
...
formValue = args.optionValue ("form", 0);
| Constructor Summary | |
CommandArgs(java.lang.String[] longOptions,
java.lang.String[] rawArgs)
|
|
CommandArgs(java.lang.String shortOptions,
java.lang.String[] rawArgs)
A new stream of arguments conforming to SHORTOPTIONS and LONGOPTIONS, extracted from RAWARGS (typically the argument to the main program). |
|
| Method Summary | |
java.lang.String |
name(int i)
The option name associated with the Ith option occurrence (0 <= I < numOptions ()). |
int |
number(char c)
The number of occurrences of option C in the arguments to the constructor. |
int |
number(java.lang.String s)
The number of occurrences of option S in the arguments to the constructor. |
int |
numOptions()
The number of option occurrences in the arguments. |
int |
numOtherArgs()
The number of other (non-option) arguments (does not include --). |
boolean |
ok()
True iff all options provided to the constructor were correct. |
boolean |
ok(int i)
True iff the Ith option occurrence was legal, 0 <= I < numOptions (). |
char |
option(int i)
The option letter associated with the Ith option occurrence (0 <= I < numOptions ()). |
java.lang.String |
optionValue(char c,
int i)
The argument value of the Ith occurrence of option C, where I >= 0. |
java.lang.String |
optionValue(int i)
The argument value associated with the Ith option occurrence (0 <= I < numOptions ()). |
java.lang.String |
optionValue(java.lang.String s,
int i)
The argument value of the Ith occurrence of option S, where I >= 0. |
java.lang.String |
otherArg(int i)
The Ith other (non-option) argument, 0 <= I < numOtherArgs (). |
boolean |
present(char c)
True iff at least one instance of option C is present. |
boolean |
present(java.lang.String s)
True iff at least one instance of option S is present. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public CommandArgs(java.lang.String shortOptions,
java.lang.String[] rawArgs)
public CommandArgs(java.lang.String[] longOptions,
java.lang.String[] rawArgs)
| Method Detail |
public int number(char c)
public int number(java.lang.String s)
public boolean present(char c)
public boolean present(java.lang.String s)
public java.lang.String optionValue(char c,
int i)
public java.lang.String optionValue(java.lang.String s,
int i)
public int numOptions()
public boolean ok(int i)
public boolean ok()
public char option(int i)
public java.lang.String name(int i)
public java.lang.String optionValue(int i)
public int numOtherArgs()
public java.lang.String otherArg(int i)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||