H C   S O F T W A R E   L T D

commanderbond cryptosaurus image jdep spambuddy superstopwatch textscape textscape2 utilities xmogrify xql yago

YAGO yet another get opt

A high-quality commandline options parser:

Parsing and validating
of options and arguments
Declarative style
Manage the parameters for your programme in one place.
Easily extendable
different parsing styles (GNU, POSIX etc.), flexible rules, groupings ('these options are mutually exclusive' 'if one then all', 'at least one required' etc.)
Typesafety
Use Java Generics to parse options into Java types up front.

For Example..

A quick helloworld programme:

public class HelloWorld {
     public static void main(String[] args) {
         Option m=new Option(
                 "m"                 //name (short-id defaults to this)
                 ,true               //is required
                 ,DefaultArgReaders.createStringReader()  //treat the arg as a string
                 ,"print message"    //description for the usage
         );  
 
         new GnuParser(m).parseOrDie("HelloWorld",args);
         System.out.println(m.getArgValue());
     }
 }
called without arguments, produces:
 invocation: 
 Option m is required but was not found
 usage: HelloWorld options 
  -m <arg> *  prints the message
 
 * required options

A more complex example:

public class LsExample {
     public static void main(String[] args) {
         Option a = new Option("a",false,'a',"all",null,"do not hide entries starting with .");
         Option A = new Option("almostall",false,'A',"almost-all",null,"do not list implied . and ..");
         Option b = new Option("escape",false,'b',"escape",null,"print octal escapes for nongraphic characters");
         Option<Integer> blockSize = new Option<Integer>("block-size",false,DefaultArgReaders.createIntReader(),"use SIZE-byte blocks");
         blockSize.setArgName("SIZE");
         Option ignoreBackups = new Option("escape",false,'B',"ignore-backups",null,"do not list implied entries ending with ~");
         Option c = new Option("c",false,null,"with -lt: sort by, and show, ctime (time of last\n" +
                 "                           modification of file status information)\n" +
                 "                           with -l: show ctime and sort by name\n" +
                 "                           otherwise: sort by ctime");
         Option C = new Option("C",false,null,"list entries by columns");
         Option help = new Option("help",false,'h',"help",null,"print this message");
 
         GnuParser gp=new GnuParser(a,A,b,blockSize,ignoreBackups,c,C,help);
         gp.parseOrDie("LsExample",args);
         if(help.isSet()){
             System.out.println(gp.getUsage("LsExample"));
         }
         if(blockSize.isSet()){
             int i=blockSize.getArgValue(); //NOTE: no need to cast/parse/validate!
         }
     }
 }
uk.co.hcsoftware.yago.demo.LsExample --help
produces:
usage: LsExample [options] 
  -C                   list entries by columns
  -a,--all             do not hide entries starting with .
  -A,--almost-all      do not list implied . and ..
  --block-size <SIZE>  use SIZE-byte blocks
  -c                   with -lt: sort by, and show, ctime (time of last       
                        modification of file status information)               
                        with -l: show ctime and sort by name                   
                        otherwise: sort by ctime
  -b,--escape          print octal escapes for nongraphic characters
  -B,--ignore-backups  do not list implied entries ending with ~
  -h,--help            print this message

Define your own arg parser:

public class Custom {
     public static void main(String[] args) {
         new Custom(args);
     }
 
     enum Answer{
         yes,no,maybe;
     }
     Option<Answer> answer = new Option<Answer>("answer"
             ,true
             ,new ArgReader<Answer>() {
                 public Answer readArg(String argValue) throws InvalidArgException {
                     if("y".equals(argValue)) return Answer.yes;
                     else if("n".equals(argValue)) return Answer.no;
                     else if("m".equals(argValue)) return Answer.maybe;
                     throw new InvalidArgException("answer must be one of y,n,m",argValue);
                 }
             }
             ,"the answer value");
 
 
     public Custom(String[] argv) {
         new GnuParser(answer).parseOrDie(Custom.class.getName(),argv);
         
         Answer a = answer.getArgValue();
         if(Answer.yes==a) System.out.println("YES");
         if(Answer.no==a) System.out.println("NO");
         else System.out.println("MAYBE"); //must be maybe
         //note how the main programme is not cluttered with options parsing and validation.
         //That is all done in the declaration/setup code.
     }
 }
The value of a is guaranteed to be a valid Answer.