public final class CmdLineParser extends Object
Constructor and Description |
---|
CmdLineParser() |
Modifier and Type | Method and Description |
---|---|
CmdLineOption |
addOption(CmdLineOption opt)
Adds a new option to the parser.
|
void |
addOptionClass(Object obj)
Adds all the
CmdLineOption -derived fields on
this object. |
List |
getArguments()
Returns the list of non-option arguments.
|
void |
parse(String[] args)
Parse the arguments.
|
public CmdLineOption addOption(CmdLineOption opt)
opt
parameter.public void addOptionClass(Object obj)
CmdLineOption
-derived fields on
this object.
This method uses Java reflection to the specified object,
and looks for fields whose types derive from CmdLineOption
.
All such fields are added through addOption(CmdLineOption)
method.
This method would be convenient if you have a class that
defines a bunch of CmdLineOption
s as its fields.
For example,
class MyMain { BooleanOption opt1 = ...; StringOption opt2 = ...; IntOption opt3 = ...; ... void doMain( String[] args ) { CmdLineParser parser = new CmdLineParser(); parser.addOptionClass(this); parser.parse(args); .... } }Note that because of the access modifier restriction, only public fields of a clss can be registered through this method.
IllegalArgumentException
- If the specified object doesn't contain any
CmdLineOption
fields. Given the typical
use case, this is more likely to be a bug of the
caller, but I appreciate your input on this behavior.public void parse(String[] args) throws CmdLineException
CmdLineException
public List getArguments()
For example, if this parser is being used by javac and the command line is like "-d x abc.java def.java -target 1.1", then this method will return ["abc.java","def.java"].
Copyright © 2003-2013 Kohsuke Kawaguchi. All Rights Reserved.