jimm.twice.util
Class Getopts

java.lang.Object
  extended byjimm.twice.util.Getopts

public class Getopts
extends java.lang.Object

Getopts is similar to the UN*X getopt() system call. It parses an array of Strings (usually the command line), looking for specified option flags and values. An instance of Getopts parses the whole args list at once, and stores the option flags and values that it finds.

Author:
Jim Menard, jimm@io.com

Constructor Summary
Getopts(java.lang.String optionListString, java.lang.String[] args)
          This constructor takes a list of legal options and a list of (usually command line) arguments.
 
Method Summary
 int argc()
          Return the number of non-option args.
 java.lang.String[] args()
          Return the remaining command-line arguments.
 java.lang.String argv(int index)
          Return a command line argument.
 boolean error()
          Return true if there was an error while parsing the command line.
 boolean hasOption(char c)
          Returns existence of an option.
 java.lang.String option(char c)
          Return an option or, if missing, the empty string.
 java.lang.String option(char c, java.lang.String defaultValue)
          Return an option or, if missing, a default value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Getopts

public Getopts(java.lang.String optionListString,
               java.lang.String[] args)
This constructor takes a list of legal options and a list of (usually command line) arguments. Each option in optionListString may be followed by a ':' to signify that option takes an argument.

Parameters:
optionListString - option chars with optional ':' specifying arg. For example, "ab:c" specifies three options, a, b, and c. Option b takes a (required) argument.
args - array of command line arguments
Method Detail

error

public boolean error()
Return true if there was an error while parsing the command line.


hasOption

public boolean hasOption(char c)
Returns existence of an option.

Parameters:
c - any character
Returns:
true of option 'c' exists, else return false.

option

public java.lang.String option(char c)
Return an option or, if missing, the empty string.

Parameters:
c - the option whose value is returned
Returns:
option string, or "" if error or option has no argument

option

public java.lang.String option(char c,
                               java.lang.String defaultValue)
Return an option or, if missing, a default value.

Parameters:
c - the option whose value is returned
defaultValue - the value to return if there is no such option
Returns:
option string, or defaultValue if error or option has no argument

args

public java.lang.String[] args()
Return the remaining command-line arguments.

Returns:
an array of Strings
See Also:
argc(), argv(int)

argc

public int argc()
Return the number of non-option args.


argv

public java.lang.String argv(int index)
Return a command line argument.

Parameters:
index - which argument to return
Returns:
the index'th arg, or "" if error. Index starts at 0.