com.mrami.libre.GetOpt
Class GetOpt

java.lang.Object
  extended bycom.mrami.libre.GetOpt.GetOpt

public class GetOpt
extends java.lang.Object

Simple class for doing command line argument processing.

To use:

  1. Add the options to the class using add...()
  2. Take your command line and pass it to processParams()
  3. Read the parameters out of the class using get...Value()
  4. ...
  5. Profit!

An example of the automatic usage statement (as rendered by a standard printStackTrace()):

 
  com.mrami.libre.GetOpt.CommandLineOptionException:
  error: Unknown option: -?
  error: required option --output-dir was not given
  usage:
  com.mrami.libre.ClassGenerator.Output.JavaNoMiddle -d <directory> [ -D <type> ]
  where:
  -d, --output-dir <directory>:
  (required) output the new classes/hierarchy to <directory>
  -D, --date-type <type>:
  the type to use for dates (default: javax.sql.Date)
 
  at com.mrami.libre.GetOpt.GetOpt.processParams(GetOpt.java:459)
  at com.mrami.libre.ClassGenerator.Output.JavaNoMiddle.setParams(JavaNoMiddle.java:80)
  at com.mrami.libre.ClassGenerator.Generate.main(Generate.java:72)
  
 

And the code that generated it:

 GetOpt opts = new GetOpt(getClass().getName());
 
 opts.addParam('d', "output-dir", "directory",
 		"output the new classes/hierarchy to <directory>", true);
 opts.addParam('D', "date-type", "type", "the type to use for dates (default: "
 		+ m_DateType + ")", false);
 opts.processParams(Options);
 


Constructor Summary
GetOpt(java.lang.Class clazz)
          Constructor.
GetOpt(java.lang.String ProgName)
          Constructor.
 
Method Summary
 int addFlag(java.lang.Character ShortOpt, java.lang.String LongOpt, java.lang.String Documentation)
          Add a flag-type option (true or false)
 int addFlag(char ShortOpt, java.lang.String LongOpt, java.lang.String Documentation)
          Add a flag-type option (true or false)
 int addParam(java.lang.Character ShortOpt, java.lang.String LongOpt, java.lang.String ParamMnemonic, java.lang.String Documentation, boolean Required)
          Add a parameter-type option (with a value to be passed in).
 int addParam(char ShortOpt, java.lang.String LongOpt, java.lang.String ParamMnemonic, java.lang.String Documentation, boolean Required)
          Add a parameter-type option (with a value to be passed in).
 boolean getFlagValue(char ch)
          Retrieve a flag.
 boolean getFlagValue(java.lang.String o)
          Retrieve a flag.
 java.lang.String getParamValue(char ch)
          Retrieve a parameter by short option
 java.lang.String getParamValue(char ch, java.lang.String Default)
          Retrieve a parameter by short option (with default value)
 java.lang.String getParamValue(java.lang.String o)
          Retrieve a parameter by long option
 java.lang.String getParamValue(java.lang.String o, java.lang.String Default)
          Retrieve a parameter by long option (with default value)
 java.util.ArrayList processParams(java.util.ArrayList Params)
          Process the command line
 java.util.ArrayList processParams(java.lang.String[] Params)
          Process the command line
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GetOpt

public GetOpt(java.lang.String ProgName)
Constructor.

Parameters:
ProgName - The name of the program (for the usage message)

GetOpt

public GetOpt(java.lang.Class clazz)
Constructor.

Parameters:
clazz - The class name of the program (for the usage message)
Method Detail

addFlag

public int addFlag(char ShortOpt,
                   java.lang.String LongOpt,
                   java.lang.String Documentation)
Add a flag-type option (true or false)

Parameters:
ShortOpt - Optional one-character option value (no dash). May be null.
LongOpt - Optional GNU style long option. DO NOT include the beginning '--'. May be null.
Documentation - Documentation to display when generating the usage message. May not be null.
Returns:
The index number of the option for later retrieval (this can be looked up later with the getIndex... methods)
Throws:
java.lang.IllegalArgumentException - if both ShortOpt and LongOpt are null
java.lang.IllegalArgumentException - if Documentation is null or zero length (Work with me here! I'm building a freakin' usage message for you!)
java.lang.IllegalArgumentException - if either ShortOpt or LongOpt has already been added

addFlag

public int addFlag(java.lang.Character ShortOpt,
                   java.lang.String LongOpt,
                   java.lang.String Documentation)
Add a flag-type option (true or false)

Parameters:
ShortOpt - Optional one-character option value (no dash). May be null.
LongOpt - Optional GNU style long option. DO NOT include the beginning '--'. May be null.
Documentation - Documentation to display when generating the usage message. May not be null.
Returns:
The index number of the option for later retrieval (this can be looked up later with the getIndex... methods)
Throws:
java.lang.IllegalArgumentException - if both ShortOpt and LongOpt are null
java.lang.IllegalArgumentException - if Documentation is null or zero length (Work with me here! I'm building a freakin' usage message for you!)
java.lang.IllegalArgumentException - if either ShortOpt or LongOpt has already been added

addParam

public int addParam(char ShortOpt,
                    java.lang.String LongOpt,
                    java.lang.String ParamMnemonic,
                    java.lang.String Documentation,
                    boolean Required)
Add a parameter-type option (with a value to be passed in). The wrapper method is usually handier, except when the short opt is null .

Parameters:
ShortOpt - Optional one-character option value (no dash). May be null.
LongOpt - Optional GNU style long option. DO NOT include the beginning '--'. May be null.
ParamMnemonic - A short name for the parameter. May not be null.
Documentation - Documentation to display when generating the usage message. May not be null.
Required - Whether the flag is required.
Returns:
The index number of the option for later retrieval (this can be looked up later with the getIndex... methods)
Throws:
java.lang.IllegalArgumentException - if both ShortOpt and LongOpt are null
java.lang.IllegalArgumentException - if Documentation is null or zero length
java.lang.IllegalArgumentException - if ParamMnemonic is null or zero length
java.lang.IllegalArgumentException - if either ShortOpt or LongOpt has already been added

addParam

public int addParam(java.lang.Character ShortOpt,
                    java.lang.String LongOpt,
                    java.lang.String ParamMnemonic,
                    java.lang.String Documentation,
                    boolean Required)
Add a parameter-type option (with a value to be passed in). The wrapper method is usually handier, except when the short opt is null .

Parameters:
ShortOpt - Optional one-character option value (no dash). May be null.
LongOpt - Optional GNU style long option. DO NOT include the beginning '--'. May be null.
ParamMnemonic - A short name for the parameter. May not be null.
Documentation - Documentation to display when generating the usage message. May not be null.
Required - Whether the flag is required.
Returns:
The index number of the option for later retrieval (this can be looked up later with the getIndex... methods)
Throws:
java.lang.IllegalArgumentException - if both ShortOpt and LongOpt are null
java.lang.IllegalArgumentException - if Documentation is null or zero length
java.lang.IllegalArgumentException - if ParamMnemonic is null or zero length
java.lang.IllegalArgumentException - if either ShortOpt or LongOpt has already been added

processParams

public java.util.ArrayList processParams(java.lang.String[] Params)
                                  throws CommandLineOptionException
Process the command line

Parameters:
Params - The command line parameters
Returns:
The remaining parameters after processing is over
Throws:
CommandLineOptionException - if any of the command line processing semantics are violated. The message will contain a usage message in traditional unix style.

processParams

public java.util.ArrayList processParams(java.util.ArrayList Params)
                                  throws CommandLineOptionException
Process the command line

Parameters:
Params - The command line parameters
Returns:
The remaining parameters after processing is over
Throws:
CommandLineOptionException - if any of the command line processing semantics are violated

getParamValue

public java.lang.String getParamValue(char ch)
                               throws OptionProcessingException
Retrieve a parameter by short option

Parameters:
ch - The short option version of the parameter to fetch
Returns:
The value from the command line (or null if the parameter was not passed)
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index does not exist
OptionProcessingException - if the index was a flag and not a parameter

getParamValue

public java.lang.String getParamValue(char ch,
                                      java.lang.String Default)
                               throws OptionProcessingException
Retrieve a parameter by short option (with default value)

Parameters:
ch - The short option version of the parameter to fetch
Default - The value to return if the parameter was not passed
Returns:
The value from the command line (or Default )
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index does not exist
OptionProcessingException - if the index was a flag and not a parameter

getParamValue

public java.lang.String getParamValue(java.lang.String o)
                               throws OptionProcessingException
Retrieve a parameter by long option

Parameters:
o - The long option version of the parameter to fetch
Returns:
The value from the command line (or null if the parameter was not passed)
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index does not exist
OptionProcessingException - if the index was a flag and not a parameter

getParamValue

public java.lang.String getParamValue(java.lang.String o,
                                      java.lang.String Default)
                               throws OptionProcessingException
Retrieve a parameter by long option (with default value)

Parameters:
o - The long option version of the parameter to fetch
Default - The value to return if the parameter was not passed
Returns:
The value from the command line (or Default )
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index does not exist
OptionProcessingException - if the index was a flag and not a parameter

getFlagValue

public boolean getFlagValue(char ch)
                     throws OptionProcessingException
Retrieve a flag.

Parameters:
ch - The short option version of the parameter to fetch
Returns:
Whether the flag was set (or null if the parameter was not passed)
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index does not exist
OptionProcessingException - if the index was a parameter and not a flag

getFlagValue

public boolean getFlagValue(java.lang.String o)
                     throws OptionProcessingException
Retrieve a flag.

Parameters:
o - The long option version of the parameter to fetch
Returns:
Whether the flag was set (or null if the parameter was not passed)
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index does not exist
OptionProcessingException - if the index was a parameter and not a flag