package com.e4net.authentibot; import gnu.getopt.Getopt; import gnu.getopt.LongOpt; public class Test { private static void usage() { System.out.println("Usage: myapp [-x filename] [--command] [args...]"); System.out.println("\twhere commands is one of:"); System.out.println("\tfroboz param1 param2 param3"); System.out.println("\tbazznot param1 param2"); System.exit(1); } // The various supported command line commands // code is the short form command argument // size is the number of arguments needed for this command private enum Commands { froboz('f', 3) { boolean doit(int off, String [] argv){ // do whatever froboz does with three arguments System.out.println("In froboz with: " + argv[off] + ", " + argv[off+1] + ", " + argv[off+2]); return true; } }, bazznot('b', 2) { boolean doit(int off, String [] argv){ // do whatever bazznot does with two arguments System.out.println("In bazznot with: " + argv[off] + ", " + argv[off+1]); return true; } }; private final int code, size; Commands(int code, int size){ this.code= code; this.size= size; } public int code() { return code; } public int size() { return size; } // Do the command, off is the offset within argv the commands // parameters start abstract boolean doit(int off, String [] argv); }; public static void main(String[] args) { // build the long commands from the enum LongOpt[] longopts = new LongOpt[Commands.values().length+1]; longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'); // loops through each enum and generates the long command // based on the enums value, the code() is the short form of // the command so either --froboz can be used or -f int off= 1; for(Commands c : Commands.values()){ longopts[off++] = new LongOpt(c.toString(), LongOpt.NO_ARGUMENT, null, c.code()); } Getopt g = new Getopt("myprog", args, "x:", longopts); String file= "default.file"; int c; boolean found= false; while ((c = g.getopt()) != -1 && !found) { int i= g.getOptind(); switch (c) { case 'x': // get some file name with the -x // option file = g.getOptarg(); break; // this is the help which runs with --help or -h case '?': case 'h': usage(); break; // this will lookup the command and execute it default: // see if in commands String cmd= longopts[g.getLongind()].getName(); Commands cm= null; try { // lookup the text command in the enum cm= Commands.valueOf(cmd); } catch (IllegalArgumentException e) { usage(); } // check we have enough arguments for this command if(args.length - i == cm.size){ // do the command cm.doit(g.getOptind(), args); found= true; }else { // not enough arguments so show usage System.err.println("Not enough arguments for: " + cmd); usage(); } } } Test t= new Test(); t.handle("froboz", new String[]{"arg1", "arg2", "arg3"}); } // another way to do it private Commands getCommand(String command) { Commands cmd; try { cmd= Commands.valueOf(command.toLowerCase()); } catch (IllegalArgumentException e) { cmd= null; } return cmd; } public boolean handle(String command, String [] args) { // lookup and execute the command Commands cmd= getCommand(command); if(cmd != null) { return cmd.doit(0, args); }else{ System.err.println("handle() - Unknown command: " + command); return false; } } }