Class GetOpt

Object
  extended by GetOpt

public class GetOpt
extends Object

Java version of GNU getopt.


Nested Class Summary
static class GetOpt.LongOption
          A record used in a table of option descriptions passed to a constructor of GetOpt.
 
Field Summary
private  String arg
          The flags argument currently in use.
private  String[] argv
          The arguments to main (note that argv[0] is the first arg, not the class name.
 int longind
          Index in longopts of a long-named option found.
private  boolean longonly
          Flag to indicate that only long options should be recognized.
private  GetOpt.LongOption[] longopts
          Table of long options.
private  int nextchar
          The index in arg of the next option flag to be examined.
static int NO_ARG
          A value for LongOption.has Arg meaning "option has no argument".
static int OPT_ARG
          A value for LongOption.has Arg meaning "option has an optional argument".
 String optarg
          For communication from nextOpt() to the caller.
 boolean opterr
          If set to false, do not print messages to System.err for unrecognized options.
 int optind
          Index in argv of the next element to be scanned.
 char optopt
          Set to an option character which was unrecognized.
private  String progname
          The "program" (class) name (counterpart of argv[0] in Unix).
static int REQ_ARG
          A value for LongOption.has Arg meaning "option has a required argument".
private  String shortopts
          Short option flags.
private static String VERSION
          Source code version.
 
Constructor Summary
GetOpt(String progname, String[] argv, String shortopts)
          Create a new option parser.
GetOpt(String progname, String[] argv, String shortopts, GetOpt.LongOption[] longopts)
          Create a new option parser.
GetOpt(String progname, String[] argv, String shortopts, GetOpt.LongOption[] longopts, boolean longonly)
          Create a new option parser.
 
Method Summary
static void main(String[] ignore)
          Main program for testing.
 int nextOpt()
          Return the next option.
private static void pl(Object o)
          Helper function to main: prints a message to System.err.
private  void printErr(String a, String b, String c)
          A convenience routine to print an error to System.err if opterr is true.
private static String[] tokenize(String str)
          Helper procedure for main.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

private static String VERSION
Source code version.


NO_ARG

public static final int NO_ARG
A value for LongOption.has Arg meaning "option has no argument".

See Also:
Constant Field Values

REQ_ARG

public static final int REQ_ARG
A value for LongOption.has Arg meaning "option has a required argument".

See Also:
Constant Field Values

OPT_ARG

public static final int OPT_ARG
A value for LongOption.has Arg meaning "option has an optional argument".

See Also:
Constant Field Values

progname

private String progname
The "program" (class) name (counterpart of argv[0] in Unix).


argv

private String[] argv
The arguments to main (note that argv[0] is the first arg, not the class name.


arg

private String arg
The flags argument currently in use. Normally argv[optind-1], but for flags that take separate arguments, optind may be larger. For example, if flags a and b take arguments and argv is { "-abc", "one", "two", "three" }, then after returning option 'b' (with optarg "two"), we have optind==3, arg=="-abc"=argv[0], and nextchar==2. Also, if nextchar == -1, then arg will be reset to another element of argv before being examined.

See Also:
optind

shortopts

private String shortopts
Short option flags.


longopts

private GetOpt.LongOption[] longopts
Table of long options.


longonly

private boolean longonly
Flag to indicate that only long options should be recognized. If true, '-' as well as '--' can introduce long-named options.


optind

public int optind
Index in argv of the next element to be scanned. When nextOption() returns -1, this is the index of the first of the non-option elements that the caller should itself scan (argv.length, if there are no non-option arguments). Otherwise, optind communicates from one call to the next how much of argv has been scanned thus far.


nextchar

private int nextchar
The index in arg of the next option flag to be examined. If nextchar < 0 or nextchar >= arg.length(), then arg has been completely examined and argv[optind] should be examined next.


longind

public int longind
Index in longopts of a long-named option found. It is only valid when long-named option has been found in the most recent call to nextOpt().


optarg

public String optarg
For communication from nextOpt() to the caller. When nextOpt() finds an option that takes an argument, the argument value is returned here.


opterr

public boolean opterr
If set to false, do not print messages to System.err for unrecognized options. By default, it is set to true.


optopt

public char optopt
Set to an option character which was unrecognized.

Constructor Detail

GetOpt

public GetOpt(String progname,
              String[] argv,
              String shortopts,
              GetOpt.LongOption[] longopts,
              boolean longonly)
Create a new option parser.

Parameters:
progname - the name of the program (for error messages)
argv - an array of command-line arguments
shortopts - a string of short option flags. A single colon following a flag indicates it has a mandatory argument; a double colon indicates an optional argument.
longopts - a table of long option descriptors
longonly - if true, indicates athat -flag should be treated as a long flag, like --flag, rather than four short flags.

GetOpt

public GetOpt(String progname,
              String[] argv,
              String shortopts)
Create a new option parser. Equivalent to GetOpt(progname, argv, shortopts, null, false)

Parameters:
progname - the name of the program (for error messages)
argv - an array of command-line arguments
shortopts - a string of short option flags. A single colon following a flag indicates it has a mandatory argument; a double colon indicates an optional argument.

GetOpt

public GetOpt(String progname,
              String[] argv,
              String shortopts,
              GetOpt.LongOption[] longopts)
Create a new option parser. Equivalent to GetOpt(progname, argv, shortopts, longopts, false)

Parameters:
progname - the name of the program (for error messages)
argv - an array of command-line arguments
shortopts - a string of short option flags. A single colon following a flag indicates it has a mandatory argument; a double colon indicates an optional argument.
longopts - a table of long option descriptors
Method Detail

nextOpt

public int nextOpt()
Return the next option. If there are no more options, return -1. Other information may be returned in the fields optind, longind, optarg, and optopt.

Returns:
-1 if there are no more options; otherwise a character indicating which option was found (longopts[longind].val for long options) or '?' for errors.

printErr

private void printErr(String a,
                      String b,
                      String c)
A convenience routine to print an error to System.err if opterr is true. The message has the form "progname: first-part `second-part' third-part".

Parameters:
a - the first part of the message
b - the middle part of the message
c - the last part of the message

pl

private static void pl(Object o)
Helper function to main: prints a message to System.err.

Parameters:
o - the message

main

public static void main(String[] ignore)
Main program for testing.

Parameters:
ignore - ignored

tokenize

private static String[] tokenize(String str)
Helper procedure for main.

Parameters:
str - a command-line-like string.
Returns:
the result of breaking the line into an array of words