Class GetOpt

  extended by org.apache.xalan.xsltc.cmdline.getopt.GetOpt

public class GetOpt
extends java.lang.Object

GetOpt is a Java equivalent to the C getopt() library function discussed in man page getopt(3C). It provides command line parsing for Java applications. It supports the most rules of the command line standard (see man page intro(1)) including stacked options such as '-sxm' (which is equivalent to -s -x -m); it handles special '--' option that signifies the end of options. Additionally this implementation of getopt will check for mandatory arguments to options such as in the case of '-d ' it will throw a MissingOptArgException if the option argument '' is not included on the commandline. getopt(3C) does not check for this.

Nested Class Summary
(package private) static class GetOpt.Option
(package private) static class GetOpt.OptionMatcher
Field Summary
private  java.util.List theCmdArgs
private  GetOpt.Option theCurrentOption
private  GetOpt.OptionMatcher theOptionMatcher
private  java.util.List theOptions
private  java.util.ListIterator theOptionsIterator
Constructor Summary
GetOpt(java.lang.String[] args, java.lang.String optString)
Method Summary
 java.lang.String[] getCmdArgs()
          gets list of the commandline arguments.
 int getNextOption()
          gets the next option found in the commandline.
 java.lang.String getOptionArg()
          gets the argument for the current parsed option.
 void printOptions()
          debugging routine to print out all options collected
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Detail


private GetOpt.Option theCurrentOption


private java.util.ListIterator theOptionsIterator


private java.util.List theOptions


private java.util.List theCmdArgs


private GetOpt.OptionMatcher theOptionMatcher
Constructor Detail


public GetOpt(java.lang.String[] args,
              java.lang.String optString)
Method Detail


public void printOptions()
debugging routine to print out all options collected


public int getNextOption()
                  throws IllegalArgumentException,
gets the next option found in the commandline. Distinguishes between two bad cases, one case is when an illegal option is found, and then other case is when an option takes an argument but no argument was found for that option. If the option found was not declared in the optString, then an IllegalArgumentException will be thrown (case 1). If the next option found has been declared to take an argument, and no such argument exists, then a MissingOptArgException is thrown (case 2).

int - the next option found.
IllegalArgumentException, - MissingOptArgException.


public java.lang.String getOptionArg()
gets the argument for the current parsed option. For example, in case of '-d ', if current option parsed is 'd' then getOptionArg() would return ''.

String - argument for current parsed option.


public java.lang.String[] getCmdArgs()
gets list of the commandline arguments. For example, in command such as 'cmd -s -d file file2 file3 file4' with the usage 'cmd [-s] [-d ] ...', getCmdArgs() would return the list {file2, file3, file4}.

String[] - list of command arguments that may appear after options and option arguments.