jas.util
Class GetOptions

java.lang.Object
  extended by jas.util.GetOptions

public class GetOptions
extends 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.

Author:
Peter Armstrong

Nested Class Summary
static class GetOptions.BadArguments
          Exception to throw when an invalid argument is encountered.
 
Field Summary
protected  Hashtable longToComment
          Maps a long option to its comment.
protected  Hashtable longToIsFlagMap
          Maps a long option to whether it is a flag (takes no parameters).
protected  Hashtable longToOptionsMap
          The storage of options.
protected  String[] params
          The list of arguments that followed the options.
protected  Vector pVector
          Vector used to dynamically build options.
protected  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, String comment)
          Adds an option to be recognized by parseArgs.
 void addOption(String longName, boolean flag)
          Adds an option to be recognized by parseArgs.
 void addOption(String longName, boolean flag, String comment)
          Adds an option to be recognized by parseArgs.
 void addOption(String longName, char shortName, boolean flag)
          Adds an option to be recognized by parseArgs.
 void addOption(String longName, char shortName, boolean flag, String comment)
          Adds an option to be recognized by parseArgs.
 void addParam(String param)
          Add a parmeter
 void dumpOptions()
           
 String getComment(String longName)
          Returns the comment given for the option with the long name longName.
 String getOption(String longName)
          Returns the value given for the option with the long name longName.
 String getParam(int pNum)
          Returns a specific parameter
 String[] getParams()
          Returns list of parameters
 boolean hasOption(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(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(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 String[] params
The list of arguments that followed the options.


pVector

protected Vector pVector
Vector used to dynamically build options.


shortToLongMap

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


longToIsFlagMap

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


longToComment

protected Hashtable longToComment
Maps a long option to its comment.


longToOptionsMap

protected 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(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(String longName,
                      boolean flag,
                      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,
                      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(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(String longName,
                      char shortName,
                      boolean flag,
                      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(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(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 String getOption(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 String getComment(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(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 String[] getParams()
Returns list of parameters


getParam

public String getParam(int pNum)
Returns a specific parameter


numParams

public int numParams()
Returns number of parameters


addParam

public void addParam(String param)
Add a parmeter



Copyright © 2000-2009 FreeHEP. All Rights Reserved.