Table of contents

Hello world example

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 classes instances 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);
	}
}

Overall view

List of annotations

Here is the list of annotations that can be used with a pointer to the relevant documentation

  1. @Argument to define a new named command line argument
  2. @ArgumentClass for a variable member will parse the Class for more arguments
  3. @ArgumentRegexp Defines a regular expression that should be applied to decompose one or more arguments
  4. @ArgumentHandler
  5. @ArgumentPostProcessor Defines a method that is called when the command line has been processed
  6. @CheckNumericIntervals to specify bounds for a numerical value
  7. @EnumValue Defines a possible value in an enumeration
  8. @OrderedArgument to define a new unnamed command line argument

Handling variables

In order to handle a variable, ArgParser depends on a handler. For example, an Integer object can be handled by an IntegerHandler that will automatically set its value if the corresponding argument is found in the command line.

To choose a handler, ArgParser follows the following steps and stops whenever a matching handler has been found:

  1. There is a handler defined in the annotation (handler of type Class<? extends Handler> )
  2. The class of the object has a @ArgumentHandler annotation
  3. The class of the object can be handled by one of the predefined handlers:

The following types are automatically detected and what they expect:

  • String Expects a string
  • Pattern Expect a string that is a valid regular expression
  • A collection of strings: This handler expects a comma separated list of strings
  • Boxed or unboxed numerical types (Integer, int, Long, long, Float, float, etc.)
  • boolean or Boolean
  • File
  • Object with a String constructor
  • A method can be

Here is a list of handlers that are pre-defined but that need to be explicitely given to be used:

  • CharSetHandler The argument is a String that is decomposed into a set of characters
  • XStreamHandler The argument is an XML file or an XML string