All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----ncsa.horizon.util.CmdLine
An option is specified on the command line by a single letter preceeded by a dash. Several options may be specified in a single argument that is preceeded by a single dash. Option arguments follow the option (assuming the option was configured to take an argument) with or without a space. This class can be used to process command line options supported by a Java application.
The class is usually configured to for a set of supported switches at construction. The configuration is given in the form of a string (using a syntax similar to the Perl package Getopts.pl) in which each letter in the string is a command line switch supported by the application. A character followed by a colon (:) indicates that the switch takes an argument. A character followed by a dash (-) indicates that the option is to be interpreted as a processing-stop switch; all characters beyond this switch (in that word and in all those after it) are taken to make up literal arguments and not additional switches. This allows one to provide literal arguments that begin with a dash.
One can pass the actual argument list to this class either via its constructor or after instantiation via the setCmdLine(String[]) method, which examines the arguments, seperating out the switches from the literal arguments. One can then check which options were set with the isSet(), getValue(), or the options(). One can also enumerate through the arguments in the order they appeared on the command line via the arguments() method.
For example, suppose you would like to support the following command line:
java MyApplication -qu -p -f afile.txt -x -Hello- world
You intend this command line to support 4 basic switches, one of which
takes an argument, plus the -x option as a stop switch. The stop switch
will prevent the "-Hello-" from being interpreted as a string of options.
You can configure your CmdLine object to support this line in this way:
CmdLine cl = new CmdLine("f:puqx-", (CmdLine.RELAX & CmdLine.USRWARN));
Note that it doesn't matter what order the options are list in the
command line. The RELAX flag tells the class not to
throw an exception if an unrecognized option is encountered, while the
the USRWARN flag says to inform the user of such errors
via a message sent to System.err. One can then process the
command line with the following code (where args is the String[] passed
to main()):
// parse the command line
try {
cl.setCmdLine(args);
} catch (UnrecognizedOptionException ex) {
// this exception won't be thrown if RELAX is given as a flag;
// however, if it isn't given, you can put your bail-out code
// here
}
// check for options
if (cl.isSet('q')) ...
if (cl.isSet('f')) filearg = cl.getValue('f');
...
// get arguments
String arg;
for(Enumeration e = cl.options(); e.hasMoreElements();) {
arg = (String) e.nextElement();
...
}
protected String config
protected Hashtable options
protected Vector argList
protected int flags
public final static int NULLFLAG
public final static int RELAX
public final static int USRWARN
public final static int WARN
protected Character stopchar
public CmdLine(String configuration,
int flags,
String args[]) throws UnrecognizedOptionException
public CmdLine(String configuration,
int flags)
public CmdLine(String configuration)
public CmdLine(String configuration,
String args[]) throws UnrecognizedOptionException
public synchronized void setConfig(String configuration)
public synchronized void setCmdLine(String args[]) throws UnrecognizedOptionException
public synchronized String getConfig()
public void setFlags(int flags)
public void addFlags(int flags)
public int getFlags()
public boolean isSet(char c)
public boolean isAnOption(char c)
public synchronized boolean isSwitched(char c)
public synchronized int getNumSet(char c)
public synchronized String getValue(char c)
public synchronized Stack getAllValues(char c)
public synchronized Enumeration options()
public synchronized Enumeration arguments()
public synchronized int getNumArgs()
public static String[] parseStringList(String input,
String delim)
public static void main(String args[])
All Packages Class Hierarchy This Package Previous Next Index