opendap.util
Class Getopts

java.lang.Object
  extended by opendap.util.Getopts

public class Getopts
extends java.lang.Object

A class for achieving getopts() functionality.

Loosely based on perl5's getopt/Std.pm.

The object is instantiated with a 'flags' String that is the composed of the set of switches allowed to be passed, and the 'args' String array that has to be parsed opposite the 'flags' string.

 new Getopts("oif:", args)      -o, -i are boolean flags,
 -f takes an argument
 
The class processes single-character switches with switch clustering.

The list of valid switches is accessible through the 'swList()' method, which returns an Enumeration of the switch names.

A local array including the arguments from the 'args' array that was passed as an argument but are the actual command line arguments is generated and is accessible through the 'argList()' method.

Options switch content fields can be accessible through the 'OptSwitch' class.

Version:
1.1 6/14/98 Updated evaluation following Mark's remarks., 1.2 4/10/10 Added some code to provide a gnu style getopts.
Author:
Arieh Markel (arieh@selectjobs.com) thanks to Mark Skipper (mcs@dmu.ac.uk) for bug fix
See Also:
OptSwitch, Enumeration

Constructor Summary
Getopts(java.lang.String flags, java.lang.String[] args)
          Wrapper Constructor
Getopts(java.lang.String progname, java.lang.String[] args, java.lang.String flags)
          Wrapper Constructor
Getopts(java.lang.String progname, java.lang.String flags, java.lang.String[] args)
          Basic class constructor.
 
Method Summary
 java.lang.String[] argList()
          Method to return an array of the actual arguments of the command line invocation.
 int getopt()
           
 java.lang.String getOptarg()
           
 int getOptind()
           
 java.lang.String getOption(java.lang.Character sw)
          getOption
 java.lang.String getOption(int sw)
          getOption
 int getOptopt()
           
 OptSwitch getSwitch(java.lang.Character sw)
          method to return the OptSwitch object associated with the 'sw' argument.
protected  void initialize(java.lang.String progname, java.lang.String flags, java.lang.String[] args)
           
static void main(java.lang.String[] args)
          method for class testing.
 void setOpterr(boolean b)
           
 java.util.Enumeration swList()
          Method to return an Enumeration of the switches that the Getopts object is able to parse (according to its initialization).
 
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 flags,
               java.lang.String[] args)
        throws InvalidSwitch
Wrapper Constructor

Parameters:
flags - a string with the valid switch names
args - array of strings (usually args)

Throws:
InvalidSwitch - thrown when invalid options are found


Getopts

public Getopts(java.lang.String progname,
               java.lang.String flags,
               java.lang.String[] args)
        throws InvalidSwitch
Basic class constructor. Gets the flags passed, in a notation similar to the one used by the sh, ksh, bash, and perl getopts.

String array 'args' is passed and is parsed according to the flags.

Parameters:
progname - program name for producing error messages
flags - a string with the valid switch names
args - array of strings (usually args)

Throws:
InvalidSwitch - thrown when invalid options are found


Getopts

public Getopts(java.lang.String progname,
               java.lang.String[] args,
               java.lang.String flags)
Wrapper Constructor

Parameters:
progname - program name for producing error messages
flags - a string with the valid switch names
args - array of strings (usually args)

Method Detail

getSwitch

public OptSwitch getSwitch(java.lang.Character sw)
method to return the OptSwitch object associated with the 'sw' argument.

Parameters:
sw - switch whose class is requested


getOption

public java.lang.String getOption(java.lang.Character sw)
getOption

Parameters:
sw - Character switch whose option is requested

getOption

public java.lang.String getOption(int sw)
getOption

Parameters:
sw - int value switch whose option is requested

swList

public java.util.Enumeration swList()
Method to return an Enumeration of the switches that the Getopts object is able to parse (according to its initialization).

May be later used to step through the OptSwitch objects.


argList

public java.lang.String[] argList()
Method to return an array of the actual arguments of the command line invocation.


initialize

protected void initialize(java.lang.String progname,
                          java.lang.String flags,
                          java.lang.String[] args)
                   throws InvalidSwitch
Throws:
InvalidSwitch

main

public static void main(java.lang.String[] args)
method for class testing.

Invocation:

 java Getopts "option set" arg0 arg1 ... argn
 

Parameters:
args - arguments passed

getopt

public int getopt()

getOptarg

public java.lang.String getOptarg()

setOpterr

public void setOpterr(boolean b)

getOptind

public int getOptind()

getOptopt

public int getOptopt()