de.uni_hamburg.eggink.autojar
Class Getopt

java.lang.Object
  extended by de.uni_hamburg.eggink.autojar.Getopt

public class Getopt
extends Object

Handles program arguments like Unix getopt(). Example:

  void main(String[] argv)
  {
      int     opt;
      boolean x = false;
      Getopt  getopt = new Getopt(argv, "a:hx");
      String  argA = "";

      // get Options
      
      while ((opt = getopt.getOption()) != -1)
      {
          switch (opt)
          {
              case 'a':   argA = getopt.getOptarg();
                          break;
              case 'h':   help();
                          break;
              case 'x':   x = true;
                          break;
              default:    System.out.println("wrong option: " + getOptopt());
          }
      }
      
      // handle non-option parameters

      String[]   files = getopt.getParms();
      
      for (int i = 0; i < files.length; ++i)
          doSomethingWith(files[i]);
  }

  Legal calls:
      java Mainclass -hx file1 file2
      java Mainclass -xa blurp -h -- file3
      
  Illegal calls:      
      java Mainclass -y f
      java Mainclass -a

  The special argument -- denotes the end of the options. All Arguments 
  after this will be considered as non-options, even if starting with -.

  If any command line argument is of the form "@filename", this file
  is read and each line is considered a program parameter.
  
  

Author:
Bernd Eggink, RRZ Uni Hamburg (Bernd.Eggink@rrz.uni-hamburg.de)

Constructor Summary
Getopt(String[] argv, String opts)
          Like Getopt(argv, opts, true).
Getopt(String[] argv, String opts, boolean opterr)
          Constructs a Getopt object using the main() parameter list.
Getopt(String filename, String opts)
          Like Getopt(filename, opts, true)
Getopt(String filename, String opts, boolean opterr)
          Constructs a Getopt object by reading all arguments from a file.
 
Method Summary
 String getOptarg()
          Returns the current argument or null.
 int getOption()
          Returns the next option as int value, -1 if no more options are available, '?' if the option is illegal.
 char getOptopt()
          Returns the unrecognized option character.
 String[] getParms()
          Returns parameters not handled by getOption() as array of Strings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Getopt

public Getopt(String filename,
              String opts,
              boolean opterr)
       throws IOException
Constructs a Getopt object by reading all arguments from a file.

Parameters:
filename - Name of the file to read.
opts - The possible options.
opterr - If true, an error message will be printed if an illegal option character is found.
Throws:
IOException

Getopt

public Getopt(String filename,
              String opts)
       throws IOException
Like Getopt(filename, opts, true)

Throws:
IOException

Getopt

public Getopt(String[] argv,
              String opts,
              boolean opterr)
       throws IOException
Constructs a Getopt object using the main() parameter list.

Parameters:
argv - The arguments of main()
opts - The possible options. Each option is a single character. If followed by a colon, the option given in the command line must be followed by an argument.
opterr - If true, an error message will be printed if an illegal option character is encountered.
Throws:
IOException

Getopt

public Getopt(String[] argv,
              String opts)
       throws IOException
Like Getopt(argv, opts, true).

Throws:
IOException
Method Detail

getOptarg

public String getOptarg()
Returns the current argument or null.


getOption

public int getOption()
Returns the next option as int value, -1 if no more options are available, '?' if the option is illegal.


getOptopt

public char getOptopt()
Returns the unrecognized option character.


getParms

public String[] getParms()
Returns parameters not handled by getOption() as array of Strings.