jas.util
Class GetOptions

java.lang.Object
  |
  +--jas.util.GetOptions

public class GetOptions
extends java.lang.Object

GetOptions is used to help parse command line arguments. It is loosely based on Paul Raines' GetOpt module with modifications to handle multiple classes wanting access to the same arguments. The main design difference is that instead of doing all the work in the constructor we do it with addOption() and parseArgs(). Furthermore, the Hashtables are different, etc.


Inner Class Summary
static class GetOptions.BadArguments
          Exception to throw when an invalid argument is encountered.
 
Field Summary
protected  java.util.Hashtable longToComment
          Maps a long option to its comment.
protected  java.util.Hashtable longToIsFlagMap
          Maps a long option to whether it is a flag (takes no parameters).
protected  java.util.Hashtable longToOptionsMap
          The storage of options.
protected  java.lang.String[] params
          The list of arguments that followed the options.
protected  java.util.Vector pVector
          Vector used to dynamically build options.
protected  java.util.Hashtable shortToLongMap
          Maps a short option to its equivalent long one.
 
Constructor Summary
GetOptions()
          Creates an empty GetOptions object.
 
Method Summary
 void addOption(char shortName, boolean flag)
          Adds an option to be recognized by parseArgs.
 void addOption(char shortName, boolean flag, java.lang.String comment)
          Adds an option to be recognized by parseArgs.
 void addOption(java.lang.String longName, boolean flag)
          Adds an option to be recognized by parseArgs.
 void addOption(java.lang.String longName, boolean flag, java.lang.String comment)
          Adds an option to be recognized by parseArgs.
 void addOption(java.lang.String longName, char shortName, boolean flag)
          Adds an option to be recognized by parseArgs.
 void addOption(java.lang.String longName, char shortName, boolean flag, java.lang.String comment)
          Adds an option to be recognized by parseArgs.
 void addParam(java.lang.String param)
          Add a parmeter
 void dumpOptions()
           
 java.lang.String getComment(java.lang.String longName)
          Returns the comment given for the option with the long name longName.
 java.lang.String getOption(java.lang.String longName)
          Returns the value given for the option with the long name longName.
 java.lang.String getParam(int pNum)
          Returns a specific parameter
 java.lang.String[] getParams()
          Returns list of parameters
 boolean hasOption(java.lang.String longName)
          Returns true or false depending on if the option with the long name longName was given in either the short (if one exists) or the long form.
 boolean isLegalOption(java.lang.String longName)
          Returns true or false depending on if the option with the long name longName is in fact a real option.
 int numParams()
          Returns number of parameters
 void parseArgs(java.lang.String[] argv)
          Parses the command line arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

params

protected java.lang.String[] params
The list of arguments that followed the options.

pVector

protected java.util.Vector pVector
Vector used to dynamically build options.

shortToLongMap

protected java.util.Hashtable shortToLongMap
Maps a short option to its equivalent long one.

longToIsFlagMap

protected java.util.Hashtable longToIsFlagMap
Maps a long option to whether it is a flag (takes no parameters).

longToComment

protected java.util.Hashtable longToComment
Maps a long option to its comment.

longToOptionsMap

protected java.util.Hashtable longToOptionsMap
The storage of options. An option can have either no value or a String value of colon-delimited arguments.
Constructor Detail

GetOptions

public GetOptions()
Creates an empty GetOptions object. Options are added to the object using the addOption method, unlike GetOpt which adds them in the constructor.
Method Detail

addOption

public void addOption(java.lang.String longName,
                      boolean flag)
Adds an option to be recognized by parseArgs.
Parameters:
longName - the full name of the option
flag - true if the option is a flag (takes 0 parameters), false otherwise

addOption

public void addOption(java.lang.String longName,
                      boolean flag,
                      java.lang.String comment)
Adds an option to be recognized by parseArgs.
Parameters:
longName - the full name of the option
flag - true if the option is a flag (takes 0 parameters), false otherwise
comment - description of the arguments to the option

addOption

public void addOption(char shortName,
                      boolean flag)
Adds an option to be recognized by parseArgs.
Parameters:
shortName - the single character name of the option
flag - true if the option is a flag (takes 0 parameters), false otherwise

addOption

public void addOption(char shortName,
                      boolean flag,
                      java.lang.String comment)
Adds an option to be recognized by parseArgs.
Parameters:
shortName - the single character name of the option
flag - true if the option is a flag (takes 0 parameters), false otherwise
comment - description of the arguments to the option

addOption

public void addOption(java.lang.String longName,
                      char shortName,
                      boolean flag)
Adds an option to be recognized by parseArgs.
Parameters:
longName - the full name of the option
shortName - the single character name of the option
flag - true if the option is a flag (takes 0 parameters), false otherwise

addOption

public void addOption(java.lang.String longName,
                      char shortName,
                      boolean flag,
                      java.lang.String comment)
Adds an option to be recognized by parseArgs.
Parameters:
longName - the full name of the option
shortName - the single character name of the option
flag - true if the option is a flag (takes 0 parameters), false otherwise
comment - description of the arguments to the option

isLegalOption

public boolean isLegalOption(java.lang.String longName)
Returns true or false depending on if the option with the long name longName is in fact a real option.
Parameters:
longName - a string describing the option to query for.
Returns:
true or false.

hasOption

public boolean hasOption(java.lang.String longName)
Returns true or false depending on if the option with the long name longName was given in either the short (if one exists) or the long form.
Parameters:
longName - a string describing the option to query for.
Returns:
true or false.

getOption

public java.lang.String getOption(java.lang.String longName)
Returns the value given for the option with the long name longName. Returns "FLAG" if the option was given but is a flag (takes no arguments).
Parameters:
longName - a string describing the option to query for.
Returns:
value of option, "FLAG" or null.

getComment

public java.lang.String getComment(java.lang.String longName)
Returns the comment given for the option with the long name longName.
Parameters:
longName - a string describing the option to query for.
Returns:
value of comment or null if no comment exists.

dumpOptions

public void dumpOptions()

parseArgs

public void parseArgs(java.lang.String[] argv)
               throws GetOptions.BadArguments
Parses the command line arguments.
Parameters:
argv - the array of arguments (eg. args as given to main())
Throws:
GetOptions.BadArguments - an invalid arg or wrong # of args is encountered

getParams

public java.lang.String[] getParams()
Returns list of parameters

getParam

public java.lang.String getParam(int pNum)
Returns a specific parameter

numParams

public int numParams()
Returns number of parameters

addParam

public void addParam(java.lang.String param)
Add a parmeter