|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | CONST | FIELD | CONSTR | METHOD | DETAIL: CONST | FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--satin.common.CmdLineParser
public class CmdLineParser
extends java.lang.Object
Parses command lines that are passed to a Java main method.
This class allows you to define the syntax for the command-line arguments
that are passed to a Java program when it is launched, and made available
through the argument to the main method.
Java treats a command line as a series of tokens which are delimited by
whitespace or enclosed by double quotes.
This class recognizes three fundamental kinds of tokens:
"-verbose" switch for the Java compiler "javac".
"-d" for specifying the output directory; the name of
the output directory following the switch is a switch argument.
command_line
| := [element [whitespace element]...]
|
element
| := non_switch | (switch [arg_list])
|
arg_list
| := (whitespace switch_arg)...
|
switch_arg
| := token
|
switch
| := "-" token
|
non_switch
| := token
|
token
| := any sequence of non-space characters, not beginning with "-"
|
whitespace
| := space | tab
|
Each defined switch may appear at most once in a command line; including the same switch twice will result in a run-time exception being thrown. The following kinds of switches are currently supported:
CmdLineParser class. If one of these switches is present
on the commmand line, the parser displays a help message and then
terminates the program.
"foobar", the parser will
recognize "-foo" as matching that switch, as long as there are no
other switches beginning with "-foo".
Switches are not case-sensitive.
CmdLineParser object.
BooleanHolder, IntHolder, StringBuffer,
and/or Vector objects you will need to receive the results
of parsing the command line. Initialize them with whatever default values
you want to use if the corresponding switch doesn't appear in the command line.
define***
methods, passing them the appropriate value objects you instantiated
in the previous step.
parse to parse
the command line arguments. If any errors are encountered, parse
will throw a SyntaxError.
public class MyProgram
{
public static void main (String[] args)
{
CmdLineParser clp = new CmdLineParser();
// create the value objects
BooleanHolder simple = new BooleanHolder(false);
IntHolder oneInt = new IntHolder(0);
StringBuffer oneString = new StringBuffer();
// define the switches
clp.defineSwitch("simple", simple, "A simple switch.");
clp.defineIntSwitch("oneInt", oneInt, "One integer.");
clp.defineStringSwitch("oneString", oneString,
"This is a switch that takes a single string as an argument.");
// parse the command line
clp.parse(args);
// display the results
System.out.println("simple" + (simple.value ? " " : " NOT ") + "specified");
System.out.println("oneInt = " + oneInt.value);
System.out.println("oneString = " + oneString.toString());
}
}
At runtime, you might invoke this program as follows:
java MyProgram -simple
java MyProgram -oneInt 4
java MyProgram -oneString Hello
java MyProgram -oneStr "hello, world"
java MyProgram -simple -oneInt 10 -oneString foobar
| Inner Class Summary | |
static class |
CmdLineParser.SyntaxError
Indicates a syntax error in a command line. |
| Constructor Summary | |
CmdLineParser()
Constructs a command line parser. |
|
| Method Summary | |
void |
defineHelp(java.lang.String help)
Defines a help overview message. |
void |
defineIntSwitch(java.lang.String switchName,
int[] values,
java.lang.String help)
Defines an integer list switch. |
void |
defineIntSwitch(java.lang.String switchName,
IntHolder valueRcvr,
java.lang.String help)
Defines an integer switch. |
void |
defineIntSwitch(java.lang.String switchName,
java.util.Vector values,
java.lang.String help)
Defines an integer list switch. |
void |
defineNonSwitch(java.lang.String[] values,
java.lang.String help)
Defines where to put non-switch arguments. |
void |
defineNonSwitch(java.util.Vector values,
java.lang.String help)
Defines where to put non-switch arguments. |
void |
defineStringSwitch(java.lang.String switchName,
java.lang.String[] values,
java.lang.String help)
Defines a string list switch. |
void |
defineStringSwitch(java.lang.String switchName,
java.lang.StringBuffer valueRcvr,
java.lang.String help)
Defines a string switch. |
void |
defineStringSwitch(java.lang.String switchName,
java.util.Vector values,
java.lang.String help)
Defines a string list switch. |
void |
defineSwitch(java.lang.String switchName,
BooleanHolder valueRcvr,
java.lang.String help)
Defines a simple switch. |
void |
parse(java.lang.String[] cmdLineArgs)
Parses the command line arguments, and places any switch values in the value objects defined by the define***
methods. |
boolean |
switchWasFound(java.lang.String switchName)
Returns whether a switch was found by parse. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public CmdLineParser()
"-help" and "-?"
are defined automatically.| Method Detail |
public void defineSwitch(java.lang.String switchName,
BooleanHolder valueRcvr,
java.lang.String help)
valueRcvr.value
is set to true if the switch is encountered.
You should initialize the value object's value to
false sometime before parsing the command line.
Fails an assertion if the switch name has already been defined.
switchName"foo",
not "-foo").valueRcvrhelp
public void defineIntSwitch(java.lang.String switchName,
IntHolder valueRcvr,
java.lang.String help)
SyntaxError.
The value of the integer will be placed in valueRcvr.value.
Fails an assertion if the switch name has already been defined.
switchName"foo",
not "-foo").valueRcvrhelp
public void defineStringSwitch(java.lang.String switchName,
java.lang.StringBuffer valueRcvr,
java.lang.String help)
SyntaxError.
The string will be placed in the StringBuffer.
Fails an assertion if the switch name has already been defined.
switchName"foo",
not "-foo").valueRcvrhelp
public void defineIntSwitch(java.lang.String switchName,
java.util.Vector values,
java.lang.String help)
SyntaxError.
Fails an assertion if the switch name has already been defined.
switchName"foo",
not "-foo").valuesInteger object.help
public void defineIntSwitch(java.lang.String switchName,
int[] values,
java.lang.String help)
SyntaxError.
Fails an assertion if the switch name has already been defined.
switchName"foo",
not "-foo").valuesSyntaxError.help
public void defineStringSwitch(java.lang.String switchName,
java.util.Vector values,
java.lang.String help)
SyntaxError.
Fails an assertion if the switch name has already been defined.
switchName"foo",
not "-foo").valuesString object.help
public void defineStringSwitch(java.lang.String switchName,
java.lang.String[] values,
java.lang.String help)
SyntaxError.
Fails an assertion if the switch name has already been defined.
switchName"foo",
not "-foo").valuesSyntaxError.help
public void defineNonSwitch(java.util.Vector values,
java.lang.String help)
SyntaxError.
This method may be called at most once during the lifetime of each
CmdLineParser object. If it is called more than once,
an assertion will fail.
valuesString object.help
public void defineNonSwitch(java.lang.String[] values,
java.lang.String help)
SyntaxError.
This method may be called at most once during the lifetime of each
CmdLineParser object. If it is called more than once,
an assertion will fail.
valuesSyntaxError.helppublic void defineHelp(java.lang.String help)
help
public void parse(java.lang.String[] cmdLineArgs)
throws CmdLineParser.SyntaxError
define***
methods. If any syntax errors are encountered it throws a
SyntaxError.
This method may be called at most once during the lifetime of each
CmdLineParser object. If it is called more than once,
an assertion will fail.
cmdLineArgsmain().public boolean switchWasFound(java.lang.String switchName)
parse.
You can use this method if you need to know whether a switch was
actually found. In most cases you shouldn't need to call this
method. You may only call this after parse has
been called; if you call it before calling parse,
it will fail an assertion.
switchName| Revision History |
7/15/01 Ed Stauff - added defineHelp.
6/15/01 Ed Stauff - made changes per code review.
6/12/01 Ed Stauff - made changes per API review.
6/11/01 - Initial review by Satin team.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | CONST | FIELD | CONSTR | METHOD | DETAIL: CONST | FIELD | CONSTR | METHOD | ||||||||
CmdLineParser.java (PRIVATE SOURCES)