public class CmdLineParser
extends java.lang.Object
For a typical usage, see this example.
| Constructor and Description |
|---|
CmdLineParser(java.lang.Object bean)
Creates a new command line owner that
parses arguments/options and set them into
the given object.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addArgument(Setter setter,
Argument a)
Programmatically defines an argument (instead of reading it from annotations like you normally do.)
|
void |
addOption(Setter setter,
Option o)
Programmatically defines an option (instead of reading it from annotations like you normally do.)
|
protected OptionHandler |
createOptionHandler(OptionDef o,
Setter setter)
|
java.util.List<OptionHandler> |
getArguments() |
protected boolean |
isOption(java.lang.String arg)
Returns true if the given token is an option
(as opposed to an argument.)
|
void |
parseArgument(java.util.Collection<java.lang.String> args)
Same as
parseArgument(String[]) |
void |
parseArgument(java.lang.String... args)
Parses the command line arguments and set them to the option bean
given in the constructor.
|
java.lang.String |
printExample(ExampleMode mode)
Formats a command line example into a string.
|
java.lang.String |
printExample(ExampleMode mode,
java.util.ResourceBundle rb)
Formats a command line example into a string.
|
void |
printSingleLineUsage(java.io.OutputStream out)
Prints a single-line usage to the screen.
|
void |
printSingleLineUsage(java.io.Writer w,
java.util.ResourceBundle rb)
Prints a single-line usage to the screen.
|
void |
printUsage(java.io.OutputStream out)
Prints the list of options and their usages to the screen.
|
void |
printUsage(java.io.Writer out,
java.util.ResourceBundle rb)
Prints the list of options and their usages to the screen.
|
static void |
registerHandler(java.lang.Class valueType,
java.lang.Class<? extends OptionHandler> handlerClass)
Registers a user-defined
OptionHandler class with args4j. |
void |
setUsageWidth(int usageWidth) |
void |
stopOptionParsing() |
public CmdLineParser(java.lang.Object bean)
bean - instance of a class annotated by Option and Argument.
this object will receive values. If this is null, the processing will
be skipped, which is useful if you'd like to feed metadata from other sources.IllegalAnnotationError - if the option bean class is using args4j annotations incorrectly.public void addArgument(Setter setter, Argument a)
setter - the setter for the typea - the Argumentpublic void addOption(Setter setter, Option o)
setter - the setter for the typeo - the Optionpublic java.util.List<OptionHandler> getArguments()
protected OptionHandler createOptionHandler(OptionDef o, Setter setter)
public java.lang.String printExample(ExampleMode mode)
printExample(ExampleMode, ResourceBundle) for more details.mode - must not be null.public java.lang.String printExample(ExampleMode mode, java.util.ResourceBundle rb)
This method produces a string like " -d <dir> -v -b", which is useful for printing a command line example, perhaps as a part of the usage screen.
mode - One of the ExampleMode constants. Must not be null.
This determines what option should be a part of the returned string.rb - If non-null, meta variables (<dir> in the above example)
is treated as a key to this resource bundle, and the associated
value is printed. See Option.metaVar(). This is to support
localization.
Passing null would print Option.metaVar() directly.System.err.println("java -jar my.jar"+parser.printExample(REQUIRED)+" arg1 arg2");public void printUsage(java.io.OutputStream out)
This is a convenience method for calling printUsage(new OutputStreamWriter(out),null)
so that you can do printUsage(System.err).
public void printUsage(java.io.Writer out,
java.util.ResourceBundle rb)
rb - if this is non-null, Option.usage() is treated
as a key to obtain the actual message from this resource bundle.public void parseArgument(java.util.Collection<java.lang.String> args)
throws CmdLineException
parseArgument(String[])CmdLineExceptionpublic void parseArgument(java.lang.String... args)
throws CmdLineException
args - arguments to parseCmdLineException - if there's any error parsing arguments, or if
required option was not given.protected boolean isOption(java.lang.String arg)
public static void registerHandler(java.lang.Class valueType,
java.lang.Class<? extends OptionHandler> handlerClass)
OptionHandler class with args4j.
This method allows users to extend the behavior of args4j by writing
their own OptionHandler implementation.
valueType - The specified handler is used when the field/method annotated by Option
is of this type.handlerClass - This class must have the constructor that has the same signature as
OptionHandler.OptionHandler(CmdLineParser, OptionDef, Setter)public void setUsageWidth(int usageWidth)
public void stopOptionParsing()
public void printSingleLineUsage(java.io.OutputStream out)
This is a convenience method for calling printUsage(new OutputStreamWriter(out),null)
so that you can do printUsage(System.err).
public void printSingleLineUsage(java.io.Writer w,
java.util.ResourceBundle rb)
rb - if this is non-null, Option.usage() is treated
as a key to obtain the actual message from this resource bundle.Copyright © 2003-2011 Kohsuke Kawaguchi. All Rights Reserved.