public class CmdLineParser extends Object
class Example
{
class Options
{
\@Option(name="t", param="integer", type=Option.OptionType.REQUIRED_ARGUMENT, usage=
"a long description here ...")
public int threshold = 500;
\@option(name="f", param="filename", type=Option.OptionType.REQUIRED_ARGUMENT, usage=
"a long description here ...")
public String file = "std_file.txt";
\@option(name="fs", param="filename-list comma separated", type=Option.OptionType.REQUIRED_ARGUMENT, usage=
"a long description here ...")
public Vector files = new Vector();
\@option(name="d", param="", type=Option.OptionType.NO_ARGUMENT, usage=
"a long description here ...")
public boolean do_something = false;
}
public static void main(String args[])
{
Example m = new Example();
Options options = m.new Options();
CmdLineParser parser = new CmdLineParser(options);
try
{
parser.parse(args);
}
catch(CmdLineException e)
{
parser.printUsage(System.err, e.toString());
}
if (options.do_something)
{
...
}
}
}
TODO Although some support is in place, methods are still not supported. TODO Help-output should be nicely formatted.
TODO Make the program description also an annotated type ?| Constructor and Description |
|---|
CmdLineParser(Object options)
Standard constructor where the options-object and the description are set.
|
| Modifier and Type | Method and Description |
|---|---|
void |
parse(Properties properties)
With this function the parser is invoked for the properties.
|
void |
parse(String[] args)
With this function the parser is invoked for the command-line options.
|
void |
printOptions() |
void |
printUsage(PrintStream stream,
String msg)
With this method the usage description can be printed to a print-stream.
|
String |
toString(Object options)
Pretty print the options.
|
public CmdLineParser(Object options) throws NullPointerException, CmdLineException
o - The options-object with the annotated variables and functions.description - The program description. throws NullPointerException When either of the parameters has been set to
null.CmdLineExceptionNullPointerExceptionpublic void parse(String[] args) throws CmdLineException
args - The command-line options.CmdLineException - When the options cannot be parsed due to insufficient number, etc..public void parse(Properties properties) throws CmdLineException
properties - the properties setCmdLineException - When the options cannot be parsed due to insufficient number, etc..public void printOptions()
public void printUsage(PrintStream stream, String msg)
stream - The print-stream to write the information to.msg - The extra message (when needed) that will be displayed above the usage.public String toString(Object options) throws CmdLineException
CmdLineExceptionCopyright © 2015. All Rights Reserved.