org.eclipse.actf.util.command
Class CLArgumentProcessor

java.lang.Object
  extended by org.eclipse.actf.util.command.CLArgumentProcessor

public class CLArgumentProcessor
extends Object

The CLArgumentProcessor can be used to process arguments on the command line for any application. A set of CLSwitches are associated with the processor upon its instantiation and the resolved values of these switches can be obtained via the set of getXXXArgument methods of this class. The type of the value to which the argument for a given switch is resolved is determined by the IArgumentResolver for that switch.

Author:
Mike Squillace
See Also:
CLSwitch

Constructor Summary
CLArgumentProcessor()
          construct a new command line argument processor with no switches associated with it
CLArgumentProcessor(boolean preserveCase)
          construct a new command line argument processor with no switches associated with it
CLArgumentProcessor(CLSwitch[] switches)
          construct a new command line argument processor with the given switches.
CLArgumentProcessor(CLSwitch[] switches, boolean preserveCase)
          construct a new command line argument processor with the given switches.
 
Method Summary
 void addSwitch(CLSwitch s)
          add a switch to the set of switches processed by this processor
 void appendToUsage(String post)
          append the given string to the usage.
 Object getArgument(String name)
          get the resolved argument for the switch with the given name
 boolean getBooleanArgument(String name)
          get the boolean argument value of the switch with the given name
 double getDoubleArgument(String name)
          get the double argument value of the switch with the given name
 float getFloatArgument(String name)
          get the float argument value of the switch with the given name
 int getIntArgument(String name)
          get the int argument value of the switch with the given name
 long getLongArgument(String name)
          get the long argument value of the switch with the given name
 CLSwitch getSwitch(String name)
          return the command line switch with the given name
 CLSwitch[] getSwitches()
          return the switches that are currently being handled by this processor
 String nameIfValidSwitch(String sw)
          determines whether or not the given string is a valid switch.
 void prependToUsage(String pre)
          prepend the given string to the usage statement.
protected  void printUsage()
           
 void processArguments(String[] args)
          process the given arguments from the command line.
 CLSwitch removeSwitch(String name)
          remove the switch with the given name from this processor
 void setSwitches(CLSwitch[] switches)
          set the command line switches to be associated with this command line processor.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CLArgumentProcessor

public CLArgumentProcessor()
construct a new command line argument processor with no switches associated with it


CLArgumentProcessor

public CLArgumentProcessor(CLSwitch[] switches)
construct a new command line argument processor with the given switches.

Parameters:
switches - - valid switches for this processor

CLArgumentProcessor

public CLArgumentProcessor(boolean preserveCase)
construct a new command line argument processor with no switches associated with it

Parameters:
preserveCase -

CLArgumentProcessor

public CLArgumentProcessor(CLSwitch[] switches,
                           boolean preserveCase)
construct a new command line argument processor with the given switches.

Parameters:
switches - - valid switches for this processor
preserveCase - - boolean to process arguments as case sensitive or not
Method Detail

getSwitch

public CLSwitch getSwitch(String name)
return the command line switch with the given name

Parameters:
name - - name of desired switch (Without prefix)
Returns:
the desired switch

getSwitches

public CLSwitch[] getSwitches()
return the switches that are currently being handled by this processor

Returns:
switches which are currently being processed

setSwitches

public void setSwitches(CLSwitch[] switches)
set the command line switches to be associated with this command line processor. This method will automatically clear any previous switches associated with this processor.

Parameters:
switches - - valid switches for this command line argument processor

addSwitch

public void addSwitch(CLSwitch s)
add a switch to the set of switches processed by this processor

Parameters:
s - - switch to be added

removeSwitch

public CLSwitch removeSwitch(String name)
remove the switch with the given name from this processor

Parameters:
name - - name of switch to be removed
Returns:
command line switch that was removed

processArguments

public void processArguments(String[] args)
                      throws CLArgumentException
process the given arguments from the command line. Resolved arguments can be retreaved with one of the getXXXArgument methods of this class.

If the argument array is empty, the printAUsage() method is invoked. Usage for a switch consists of the name of that switch along with its description.

Parameters:
args - - arguments from command line
Throws:
CLArgumentException

getIntArgument

public int getIntArgument(String name)
                   throws CLArgumentException
get the int argument value of the switch with the given name

Parameters:
name - - name of switch for which value is desired
Returns:
value of desired switch or -1 if switch is not found
Throws:
CLArgumentException - - if switch value is not of appropriate type

getLongArgument

public long getLongArgument(String name)
                     throws CLArgumentException
get the long argument value of the switch with the given name

Parameters:
name - - name of switch for which value is desired
Returns:
value of desired switch or -1 if switch is not found
Throws:
CLArgumentException - - if switch value is not of appropriate type

getFloatArgument

public float getFloatArgument(String name)
                       throws CLArgumentException
get the float argument value of the switch with the given name

Parameters:
name - - name of switch for which value is desired
Returns:
value of desired switch or -1 if switch is not found
Throws:
CLArgumentException - - if switch value is not of appropriate type

getDoubleArgument

public double getDoubleArgument(String name)
                         throws CLArgumentException
get the double argument value of the switch with the given name

Parameters:
name - - name of switch for which value is desired
Returns:
value of desired switch or -1 if switch is not found
Throws:
CLArgumentException - - if switch value is not of appropriate type

getBooleanArgument

public boolean getBooleanArgument(String name)
                           throws CLArgumentException
get the boolean argument value of the switch with the given name

Parameters:
name - - name of switch for which value is desired
Returns:
value of desired switch or -1 if switch is not found
Throws:
CLArgumentException - - if switch value is not of appropriate type

getArgument

public Object getArgument(String name)
get the resolved argument for the switch with the given name

Parameters:
name - - name of switch for which argument is desired
Returns:
- the resolved value of that switch or null if switch with the given name is not found

nameIfValidSwitch

public String nameIfValidSwitch(String sw)
determines whether or not the given string is a valid switch. The proposed switch will be valid if it contains a switch name recognized by this processor and is preceded by the prefix associated with that switch. If the proposed string is a valid switch, its name is returned.

Parameters:
sw - - string to examine for validity
Returns:
name of the switch if valid or null if not

prependToUsage

public void prependToUsage(String pre)
prepend the given string to the usage statement. This will be printed first when the printUsage method is called.

Parameters:
pre - - first message to be printed for usage

appendToUsage

public void appendToUsage(String post)
append the given string to the usage. After switch names and descriptions are printed, this message will be output.

Parameters:
post - - to be printed after the usage

printUsage

protected void printUsage()

toString

public String toString()
Overrides:
toString in class Object