be.ugent.twijug.jclops
Class CLManager

java.lang.Object
  extended by be.ugent.twijug.jclops.CLManager

public class CLManager
extends java.lang.Object

Command line manager. Processes command line options by calling the appropriate (setter) methods in a secondary object, called an option context. The option context class (optionally) contains annotations which configure option names and behaviour.

To process command line options, take the following steps:

  1. Create an object of this class using its default constructor CLManager().
  2. Optionally register some argument parsers with registerArgumentParser(be.ugent.twijug.jclops.ArgumentParser, java.lang.Class). Argument parsers are responsible for converting the command line option arguments to objects of the correct type for the corresponding setter methods. By default, we provide argument parsers for all primitive types and their wrapper classes, for String and for all enum classes.
  3. Register the option context with setContext(java.lang.Object). This will configure the options, and in case of errors a CLDefinitionException will be thrown.
  4. Hand the actual command line arguments to the method parse(java.lang.String[]). If a parsing error occurs, an exception of type CLParseException will be thrown.
  5. Optionally retrieve the remaining (non-option) arguments by means of getRemainingArguments().
If there is no need for special argument parsers, steps 1-3 can be combined by calling the 1-argument constructor CLManager(Object).

The command line manager also automatically generates a 'usage message' which can be obtained from getUsageMessage().

See Also:
ArgumentParser

Constructor Summary
CLManager()
          Create a new command line manager.
CLManager(java.lang.Object context)
          Create a new command line manager for the given option context.
 
Method Summary
 java.lang.String[] getRemainingArguments()
          The command line arguments that remain after all options are processed
 java.lang.String getUsageMessage()
          Generate the usage message from the current option context.
 void parse(java.lang.String[] args)
          Parses the given command line arguments.
<T> void
registerArgumentParser(ArgumentParser<T> parser, java.lang.Class<T> type)
          Install an option argument parser for a specific type.
 void setContext(java.lang.Object context)
          Register the option context object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CLManager

public CLManager(java.lang.Object context)
Create a new command line manager for the given option context. Uses the class of the context object to determine which options are available and what is their behaviour.

Note: if any argument parsers are needed other than those that are provided by default, you must instead use the default constructor, register the argument parsers and then apply setContext(java.lang.Object).

Parameters:
context - Option context object
Throws:
CLDefinitionException - when there is a configuration error

CLManager

public CLManager()
Create a new command line manager. An option context should be installed before parse(java.lang.String[]) is called, but after all argument parsers are registered with registerArgumentParser(be.ugent.twijug.jclops.ArgumentParser, java.lang.Class).

Method Detail

setContext

public void setContext(java.lang.Object context)
Register the option context object. This class of this object is used to configure the names and behaviours of the options. During parsing the manager will call the corresponding methods on this object for each actual command line option.

Throws:
CLDefinitionException - when there is a configuration error

parse

public void parse(java.lang.String[] args)
Parses the given command line arguments. If no error occurs, applies the corresponding option methods to the current option context.

Parameters:
args - Array of command line arguments to be parsed
Throws:
CLParseException - when an error occurred during parsing, i.e., when the user provided a command line which was invalid in some way

getRemainingArguments

public java.lang.String[] getRemainingArguments()
The command line arguments that remain after all options are processed

Returns:
the arguments that remain after parsing

getUsageMessage

public java.lang.String getUsageMessage()
Generate the usage message from the current option context.


registerArgumentParser

public <T> void registerArgumentParser(ArgumentParser<T> parser,
                                       java.lang.Class<T> type)
Install an option argument parser for a specific type. Overrides any previously installed parser for the same type.