Table of contents
Using ArgParser is very simple. You have to define one (or more) classes that contain the options, create an ArgParser object, give it the option object instance(s) and analyse the command line.
Here is how ArgParser is initialised and called:
ArgParser p = new ArgParser("synopsis"); final Options options = new Options(); p.addOptions(options); p.matchAllArgs(args);
where Options is an object that contains annotated members. Here is an example:
@Argument(name = "name", help = "The name of the component") String name;
After that, you can access the values that have been set in the instance of the Options class.
A complete "Hello World" example is:
public class HelloWorld { @Argument(name = "name", help = "The name of the component") String name; public static void main(String[] args) throws ArgParserException { new HelloWorld().run(args); } private void run(String[] args) throws ArgParserException { ArgParser argParser = new ArgParser("java HelloWorld [options]"); argParser.addOptions(this); argParser.matchAllArgs(args); System.out.format("The name of the component is %s", name); } }
Below are the different ways to declare command line arguments:
Exceptions can either be thrown when analyzing the code, or when parsing the command line.
Here is the list of annotations that can be used with a pointer to the relevant documentation