|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcommon.cmdline.Parser
public class Parser
Parses options on a command line. This class should not be
instantiated; all its methods are static. The application should
first create options (objects that implement the
Option
interface); the options will automatically
register themselves with the Parser. Then the application should
call Parser.parse
, passing in the argument array that
was given to the application's main
method. The
parse
method uses the Option
objects to
parse all the options on the command line; if the command line is
ill-formed, it prints an error message and exits the program. The
resulting option values are stored by the Option
objects. Any non-option arguments are returned by the
parse
method.
This class supports the same command line syntax as the Unix getopt function. If a command line argument begins with a single dash followed by a non-dash character, the parser looks for an option that has that character as a short form. If such an option exists and does not expect a value, then the parser moves on to the next character in the argument, treating it as a short-form option as well. This continues until the parser reaches an option that expects a value. If there are any characters remaining in the argument at this point, they constitute the option value. Otherwise, the next command line argument is interpreted as the option value. Here are some examples of how short-form options are parsed, assuming that options "d" and "P" take values but "c", "e", and "k" don't.
-c // option "c", no value -d 12 // option "d", value "12" -d12 // option "d", value "12" -cek // options "c", "e", "k", with no values -Pprinter // option "P", value "printer" -cP printer // option "c" with no value, "P" with value "printer" -Palpha=12 // option "P" with value "alpha=12"
If a command line argument begins with two dashes ("--"), then all remaining characters in the argument (up to the first = sign, if any) are interpreted as a long option string. If there is an equals sign in the argument, then all characters after the first equals sign are interpreted as the option value. Otherwise, if the option expects a value, the next command line argument is interpreted as the value. Some examples:
--copy // option "copy", no value --ncopies=12 // option "ncopies", value "12" --ncopies 12 // option "ncopies", value "12" --ncopies12 // option "ncopies12", no value
Method Summary | |
---|---|
static void |
addOption(Option opt)
Adds the given option to the set that this parser will recognize. |
static void |
main(java.lang.String[] args)
Test program. |
static java.util.List |
parse(java.lang.String[] args)
Parses the given array of command line arguments. |
static void |
printUsage(java.io.PrintStream s)
Prints a usage message for the program to the given stream. |
static void |
setProgramDesc(java.lang.String description)
Sets the program description that will be printed by printUsage . |
static void |
setUsageLine(java.lang.String usage)
Sets the basic usage message that will be printed by printUsage . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static void setProgramDesc(java.lang.String description)
printUsage
.
public static void setUsageLine(java.lang.String usage)
printUsage
. This message should include the non-option
arguments that the program expects.
public static void addOption(Option opt)
printUsage
method.
java.lang.IllegalArgumentException
- if any of the short or long forms
of the given option also belong to
an option that was already addedpublic static java.util.List parse(java.lang.String[] args)
public static void printUsage(java.io.PrintStream s)
getUsageString
on the Option objects.
public static void main(java.lang.String[] args)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |