org.brains2b.console
Class Main

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

public class Main
extends java.lang.Object

Main is a class for applications that are depending on commandline argument, like console applications.

Main together with ArgumentMap supply a convenient way to define arguments, their relation and optionality and report if the supplied arguments when starting the application are meet the defined argument requirements.

    The way to use this class is to extend your own class that contains a main method.
  1. Before you do anything to start the application create an ArgumentMap and add arguments and options to it.
  2. Call the setArgumentMap(ActionMap) supplying your argument map to it.
  3. Afiter this you can call makeArgumentList(String[] args) with the arguments passed from main.
  4. Alle recognized arguments are now available through getArgument(String name), supplying your defined name for the argument.

    Note:If no ArgumentMap is supplied to setArgumentMap(ArgumentMap) an argument list is created with each argument supplied to makeArgumentList(String[]) with as name the numeric location of the argument starting at 0 and the value of the argument from that location in the array.

    All exceptions thrown by the ArgumentMap are processed through handleException(ArgumentException) if you want to do anything other then writing it to System.err overwrite this function.

    The way I usually apply Main is:

     public class AConsole {
     	public AConsole() {
     		super();
     		init();
     		//other code
     	}
     	
     	public static void main	(String[] args) {
     		AConsole a=new AConsole();
     		a.makeArguments(args);
     		a.start();
     	}
     	
     	private void init() {
     		ArgumentMap arg=new ArgumentMap();
     		//add arguments
     		setArgumentMap(arg);
     	}
     	
     	private void start() {
     		//do your thing
     	}
     }
     

    Version:
    0.10.1 [25-11-2006]
    Author:
    dennis@brains2b.nl
    See Also:
    ArgumentMap

    Constructor Summary
    Main()
              Constructor for Main.
     
    Method Summary
     java.lang.String getArgument(java.lang.String name)
              gets the value of an argument defined by name.
    protected  ArgumentMap getArgumentMap()
              gets the ArgumentMap.
    protected  void handleException(ArgumentException ex)
              Default function for handling ArgumentException(s).
     void makeArgumentList(java.lang.String[] args)
              Makes the argument list on the basis of a defined ArgumentMap and the supplied array of arguments
     void setArgument(java.lang.String name, java.lang.String value)
              set arguments to the list of arguments.
    protected  void setArgumentMap(ArgumentMap argumentMap)
              Sets the ArgumentMap.
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Constructor Detail

    Main

    public Main()
    Constructor for Main.

    Method Detail

    getArgumentMap

    protected ArgumentMap getArgumentMap()
    gets the ArgumentMap.

    Returns:
    ArgumentMap

    setArgumentMap

    protected void setArgumentMap(ArgumentMap argumentMap)
    Sets the ArgumentMap.

    Parameters:
    argumentMap - ArgumentMap, the created ArgumentMap

    getArgument

    public java.lang.String getArgument(java.lang.String name)
    gets the value of an argument defined by name.

    Returns:
    String, the value belonging to argument name or null if argument was not defined
    Throws:
    NullPointerException, - thrown if there was no argument list made (forgot to call makeArgumentList()?)

    setArgument

    public void setArgument(java.lang.String name,
                            java.lang.String value)
    set arguments to the list of arguments.

    This method does not check for validity of the argument and can possibly overwrite arguments set with makeArgumentList. Checking your arguments should be done by your own code.

    Parameters:
    name -
    value -

    makeArgumentList

    public void makeArgumentList(java.lang.String[] args)
    Makes the argument list on the basis of a defined ArgumentMap and the supplied array of arguments

    If an ArgumentMap exists the arguments are passed to ArgumentMap.getList(String[])

    If no ArgumentMap exists a List is made with the numeric location of the argument as name and the corresponding value of the Array as value.

    Parameters:
    args - String[] An array of arguments
    See Also:
    ArgumentMap.getList(java.lang.String[])

    handleException

    protected void handleException(ArgumentException ex)
    Default function for handling ArgumentException(s).

    It writes the argument that caused the exception and the exception message to the System.err outstream and exits the VM with code 1.

    Overwrite if you want it to do something else.
    Be sure to break normal operation if not exiting while arguments and options you set to required are not actually there and you have not set default values.

    Parameters:
    ex - ArgumentException