org.brains2b.console
Class ArgumentMap

java.lang.Object
  extended byorg.brains2b.console.ArgumentMap

public class ArgumentMap
extends java.lang.Object

An ArgumentMap is a 'road map' to the arguments that can be supplied to Main and the expected follow up.

arguments and options are read in the order they where supplied in by static void main(String[] args)

The arguments and their key are stored in a HashMap fur future retrieval.

You can either add options, which could be translated as switches or add arguments where the entire argument which is supplied by static void main(String[] args) is supplied to Main.

You can add options to an argument. Which means that the value of an argument will have to be one of the options, otherwise the argument will be ignored if it is optional or throw an ArgumentException if it is not optional.

You can add an argument to an option. Which means that main after finding the option looks for an argument belonging to it. This argument has its own name and can be later referenced by this name.

Options can have next to the required long form, an alternative optional one character short form. supplying options to Java will either be --longform or -shortform in good Unix fashion. Both forms will signify the same option.

Version:
$Revision: 1.5 $ $Date: 2009/09/15 08:09:58 $
Author:
dennis@brains2b.nl

Constructor Summary
ArgumentMap()
          Constructor for ArgumentMap.
 
Method Summary
 void addArgument(java.lang.String name, boolean required)
          adds an Argument to the root of this map.
 void addArgument(java.lang.String name, java.lang.String defaultValue)
          adds an Argument to the root of this map.
 void addArgumentTo(java.lang.String option, java.lang.String name, boolean required)
          Adds an argument to an option
 void addOption(java.lang.String longKey, char shortKey, boolean required)
          adds an Option to the root of this map.
 void addOptionTo(java.lang.String argument, java.lang.String longKey, char shortKey)
          adds an option to an argument
 java.util.Map getList(java.lang.String[] args)
          gets the Map of all arguments supplied through String[] args mapped to their argument name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArgumentMap

public ArgumentMap()
Constructor for ArgumentMap.

Method Detail

addArgument

public void addArgument(java.lang.String name,
                        boolean required)
adds an Argument to the root of this map.

A root can have multiple arguments and options

Parameters:
name - String, the name for this argument
required - boolean, is this argument required

addArgument

public void addArgument(java.lang.String name,
                        java.lang.String defaultValue)
adds an Argument to the root of this map.

A root can have multiple arguments and options

Parameters:
name - String, the name for this argument
defaultValue - String, value of this argument when non is supplied.

addOption

public void addOption(java.lang.String longKey,
                      char shortKey,
                      boolean required)
adds an Option to the root of this map.

A root can have multiple arguments and options

Parameters:
longKey - String , the long form of the name, used as argument name, must be unique
shortKey - char, the one character short key, must be unique
required - boolean, is the option required.

addArgumentTo

public void addArgumentTo(java.lang.String option,
                          java.lang.String name,
                          boolean required)
Adds an argument to an option

This will typically take the form of -o argument or --option argument

Parameters:
option - String, the option to add the argument to.
name - String, the name of the argument.
required - boolean, is the argument required, if the option is supplied. If the option is optional leaving the option out will not check for the required argument. However if the option is supplied it must be followed by a value for this argument.

addOptionTo

public void addOptionTo(java.lang.String argument,
                        java.lang.String longKey,
                        char shortKey)
adds an option to an argument

This means that if we defined an argument argument and added options one, two, three to it, finding any of these options will be set as value for argument. If the argument is required and none of the defined options is supplied an ArgumentException is thrown when processing the input.

Parameters:
argument -
longKey -
shortKey -

getList

public java.util.Map getList(java.lang.String[] args)
                      throws ArgumentException
gets the Map of all arguments supplied through String[] args mapped to their argument name.

Parameters:
args - String[], the array of arguments typically supplied from static void main(String[] args)
Returns:
Map. the Map containing the argument name and argument value as defined through arguments and options. The value of the option is equivalent to its name and can be ignored.
Throws:
ArgumentException, - ArgumentException is thrown when a required argument is missing either in the root or from an option, or when a non-defined argument or option is found when not in the root.
ArgumentException