|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object util.CommandLineParser<Opt,Cxt>
Opt
- The enum specifying the command-line options accepted. Must
implement CommandLineParser.OptionSpec
, and each field must
have a CommandLineParser.OptionSpec.Option
attribute.Cxt
- The type of the context in which the members of Opt
run the CommandLineParser.OptionSpec.execute(String, Object)
handler method.public final class CommandLineParser<Opt extends Enum<Opt> & CommandLineParser.OptionSpec<? super Cxt>,Cxt>
Parses the command line arguments and options passed to a program. The
specifications for the options are given in an enum that implements
CommandLineParser.OptionSpec
at construction; the parser can then
be passed the array of arguments (typically the argument to the main()
method), and it will sort out which of them are options to the program,
perform actions for each, (see
CommandLineParser.OptionSpec.execute(String, Object)
), and return
the other arguments.
Nested Class Summary | |
---|---|
static class |
CommandLineParser.InvalidCommandLineException
Exception thrown by parse(Object, String[]) on
invalid command-line input. |
static interface |
CommandLineParser.OptionSpec<Cxt>
The interface which the enum passed to buildParser(Class) must implement. |
static interface |
CommandLineParser.Usage
Annotates an CommandLineParser.OptionSpec enum with the usage message to be
displayed by the "--help" output or when an error occurs. |
Method Summary | ||
---|---|---|
static
|
buildParser(Class<Opt> specClass)
Construct a parser for the given command-line option specifications. |
|
String[] |
extractArguments(String... cmdLine)
Look for the plain arguments (i.e. |
|
String[] |
getArgs()
Retrieve the arguments found by the last call to parse(Object, String[]) . |
|
String |
getHelp()
Format a help message, suitable for output by a "--help" option. |
|
Map<Opt,String[]> |
getOptMap()
Retrieve the parsed options, as a map from the option enum constants to the arguments given. |
|
Pair<Opt,String>[] |
getOpts()
Retrieve the options found by the last call to parse(Object, String[]) . |
|
String |
getUsage()
Get the usage line for the given CommandLineParser.OptionSpec enum, or the empty
string if none was specified. |
|
String[] |
parse(Cxt context,
String... cmdLine)
Process the options in the command line, and return the other arguments. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public Map<Opt,String[]> getOptMap()
getOpts()
instead.
parse(Object, String[])
to an array of the arguments
passed to the option (or null
for options without
arguments) each time it occurred.public String getHelp()
getUsage()
), followed by a list of
all options with their names, abbreviations, and descriptions.
public String[] extractArguments(String... cmdLine) throws CommandLineParser.InvalidCommandLineException
This is something of a hack; it may go away, as I found a better way to do what I wrote this method for.
cmdLine
- The command line. This will typically be whatever the
argument to the main() method was.
CommandLineParser.InvalidCommandLineException
- If the command line fails to parse;
for instance, if no argument is given to an option that requires
one.public String[] parse(Cxt context, String... cmdLine) throws CommandLineParser.InvalidCommandLineException
context
- The context to pass to the
CommandLineParser.OptionSpec.execute(String, Object)
method of the OptionSpec
enum for this parser.cmdLine
- The command line. This will typically be whatever the
argument to the main() method was.
getOpts()
or getOptMap()
method if necessary,
and this return value will also be available as getArgs()
.
CommandLineParser.InvalidCommandLineException
- If there is an error in the command
line.public String[] getArgs()
parse(Object, String[])
.
null
if parse() has not yet been called.public Pair<Opt,String>[] getOpts()
parse(Object, String[])
.
null
if parse() has not yet been called.public String getUsage()
CommandLineParser.OptionSpec
enum, or the empty
string if none was specified.
CommandLineParser.Usage
public static <Opt extends Enum<Opt> & CommandLineParser.OptionSpec<? super Cxt>,Cxt> CommandLineParser<Opt,Cxt> buildParser(Class<Opt> specClass)
Opt
- The CommandLineParser.OptionSpec
-implementing enum type specifying
the command-line options recognized.Cxt
- The context type for Opt
.specClass
- The Class object for Opt
.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |