Class CmdLineArgumentParser

  • public class CmdLineArgumentParser
    extends Object
    Generic parser for command line arguments in Unix C-Style.

    Short options start with a single dash. The sequence -abc is treated like -a, -b and -c. Just "-" without character is also a valid short option, but very unusual.

    Long options start with a double dash, e.g.: --help.

    All short and long options may be followed by parameters, e.g. "-i input.txt".

    "--" marks the end of options. Everything after that is treated as parameters.

    Usage example:

     public static void main(String args[])
         // for the result
         int verbosity=0;
         boolean help=false;
         String importFile=null;
         CmdLineArgumentParser parser=new CmdLineArgumentParser(args);
         String option=parser.getNextOption();
         while (option!=null)
             if (option.equals("-v") || option.equals("--verbose"))
                 verbosity++; // can be repeated multiple times to increase the verbosity
             else if (option.equals("-h") || option.equals("--help"))
             else if (option.equals("-i") || option.equals("--import"))
                 // after -i we expect a filename
                 if (importFile==null)
                     System.err.println("Missing file name after "+option);
    Stefan Frings,
    • Constructor Detail

      • CmdLineArgumentParser

        public CmdLineArgumentParser​(String[] args)
        args - The arguments of the calling application
    • Method Detail

      • getNextOption

        public String getNextOption()
        Gets the next option from the argument list.
        The next option or null if none exists
      • getNextParameter

        public String getNextParameter()
        Gets the next parameter from the argument list.
        The next parameter or null if none exists