org.jargp
Class ArgumentProcessor

java.lang.Object
  |
  +--org.jargp.ArgumentProcessor

public class ArgumentProcessor
extends java.lang.Object

Command line parameter processing handler. Organizes all the parameter information, including the data object to which parameter values defined by the command line are stored. Provides specialized processing for the argument strings, including recognizing the '-' character at the start of an argument as indicating that the argument provides control information (flags and possibly embedded values) as opposed to data.

Author:
Dennis M. Sosnoski

Field Summary
private  org.jargp.CharTracker m_currentArg
          Character tracker for current argument.
private  int m_currentIndex
          Current argument position in list.
private  org.jargp.ParameterSet m_parameterSet
          Head of parameter set chain.
private  org.jargp.StringTracker m_remainingArgs
          String tracker for full set of arguments.
private  java.lang.Object m_targetObject
          Argument data object.
 
Constructor Summary
ArgumentProcessor(org.jargp.ParameterDef[] defs)
          Constructor from array of parameter definitions.
ArgumentProcessor(org.jargp.ParameterSet set)
          Constructor from parameter set definition.
 
Method Summary
private  void bindDefinitions(java.lang.Object parm)
          Bind parameter definitions to target object class.
 org.jargp.StringTracker getArgs()
          Get argument list information.
(package private)  org.jargp.CharTracker getChars()
          Get current control argument character information.
(package private)  int getIndex()
          Get current argument position in list.
 void listParameters(int width, java.io.PrintStream print)
          List known parameter definitions.
 java.lang.Object processArgs(java.lang.String[] args, java.lang.Object target)
          Process argument list control information.
static int processArgs(java.lang.String[] args, org.jargp.ParameterDef[] parms, java.lang.Object target)
          Process argument list directly.
 void reportArgumentError(char flag, java.lang.String text)
          Report argument error.
(package private)  void setValue(java.lang.Object value, java.lang.reflect.Field field)
          Set parameter value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_parameterSet

private final org.jargp.ParameterSet m_parameterSet
Head of parameter set chain.


m_currentArg

private org.jargp.CharTracker m_currentArg
Character tracker for current argument.


m_currentIndex

private int m_currentIndex
Current argument position in list.


m_remainingArgs

private org.jargp.StringTracker m_remainingArgs
String tracker for full set of arguments.


m_targetObject

private java.lang.Object m_targetObject
Argument data object.

Constructor Detail

ArgumentProcessor

public ArgumentProcessor(org.jargp.ParameterSet set)
Constructor from parameter set definition.

Parameters:
set - head parameter set in possible chain of sets defined

ArgumentProcessor

public ArgumentProcessor(org.jargp.ParameterDef[] defs)
Constructor from array of parameter definitions.

Parameters:
defs - head parameter set in possible chain of sets defined
Method Detail

bindDefinitions

private void bindDefinitions(java.lang.Object parm)
Bind parameter definitions to target object class. This goes through the set of defined parameters, binding each to the class of the supplied target object and finding the associated field information. Rather than doing this binding of all parameter definitions prior to processing the command line information, it'd also be possible (and even more efficient) to just lookup the field information for each parameter actually present in the list. The only advantage of doing this lookup in advance is that it insures that configuration errors are reported whether the parameter in error is used or not.

Parameters:
parm - data object for parameter values
Throws:
ArgumentErrorException - on error in data
java.lang.IllegalArgumentException - on error in processing

processArgs

public java.lang.Object processArgs(java.lang.String[] args,
                                    java.lang.Object target)
Process argument list control information. Processes control flags present in the supplied argument list, setting the associated parameter values. Arguments not consumed in the control flag processing are available for access using other methods after the return from this call.

Parameters:
args - command line argument string array
target - application object defining parameter fields
Throws:
ArgumentErrorException - on error in data
java.lang.IllegalArgumentException - on error in processing

getChars

org.jargp.CharTracker getChars()
Get current control argument character information. The caller can consume characters from the current argument as needed.

Returns:
argument string tracking information

getIndex

int getIndex()
Get current argument position in list.

Returns:
offset in argument list of current flag argument

getArgs

public org.jargp.StringTracker getArgs()
Get argument list information. The caller can comsume arguments from the list as needed.

Returns:
argument list information

setValue

void setValue(java.lang.Object value,
              java.lang.reflect.Field field)
Set parameter value. Uses reflection to set a value within the target data object.

Parameters:
value - value to be set for parameter
field - target field for parameter value
Throws:
java.lang.IllegalArgumentException - on error in setting parameter value

reportArgumentError

public void reportArgumentError(char flag,
                                java.lang.String text)
Report argument error. Generates an exception with information about the argument causing the problem.

Parameters:
flag - argument flag character
text - error message text
Throws:
ArgumentErrorException - reporting the error

listParameters

public void listParameters(int width,
                           java.io.PrintStream print)
List known parameter definitions. This lists all known parameter definitions in fixed maximum width format.

Parameters:
width - maximum number of columns in listing
print - print stream destination for listing definitions

processArgs

public static int processArgs(java.lang.String[] args,
                              org.jargp.ParameterDef[] parms,
                              java.lang.Object target)
Process argument list directly. Creates and initializes an instance of this class, then processes control flags present in the supplied argument list, setting the associated parameter values in the target object. Arguments not consumed in the control flag processing are available for access using other methods after the return from this call.

Parameters:
args - command line argument string array
parms - data object for parameter values
target - application object defining parameter fields
Returns:
index of first command line argument not consumed by processing
Throws:
ArgumentErrorException - on error in data
java.lang.IllegalArgumentException - on error in processing