/** * Base class for command line applications. * * Children can provide functionality in the form of * * * @author Ben Fagin */ class GroovyCLI implements Runnable { private String[] args; // invoked at the command line GroovyCLI(String[] args) { this.args = args } void run() { // argument parsing def newArgs if (args.length < 1) { throw new RuntimeException("usage ") } else if (args.length == 1) { newArgs = {} } else { newArgs = args[1..args.length-1] } // make the command name def cmd = args[0].toLowerCase() if (cmd.length() > 0) { def firstChar = Character.toUpperCase(cmd.charAt(0)) firstChar = Character.toString(firstChar) cmd = firstChar + cmd.substring(1) } // run the command try { "_do${cmd}"(newArgs) } catch (MissingMethodException ex) { throw new RuntimeException("Command '${cmd}' not recognized.") } } }