Class 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.
    Author:
    G Todd Miller
    • Constructor Summary

      Constructors 
      Constructor Description
      GetOpt​(java.lang.String[] args, java.lang.String optString)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      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

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • GetOpt

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

      • printOptions

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

        public int getNextOption()
                          throws org.apache.xalan.xsltc.cmdline.getopt.IllegalArgumentException,
                                 org.apache.xalan.xsltc.cmdline.getopt.MissingOptArgException
        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).
        Returns:
        int - the next option found.
        Throws:
        org.apache.xalan.xsltc.cmdline.getopt.IllegalArgumentException
        org.apache.xalan.xsltc.cmdline.getopt.MissingOptArgException
      • getOptionArg

        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 ''.
        Returns:
        String - argument for current parsed option.
      • getCmdArgs

        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}.
        Returns:
        String[] - list of command arguments that may appear after options and option arguments.