-
-
Save duckpuppy/a9fe87762d8d84783ce0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Base class for command line applications. | |
* | |
* Children can provide functionality in the form of | |
* <command name> <arguments...> | |
* | |
* @author Ben Fagin | |
* @author Patrick Aikens | |
*/ | |
class GroovyCLI implements Runnable { | |
private String[] args; | |
// invoked at the command line | |
GroovyCLI(String[] args) { | |
this.args = args | |
} | |
void run() { | |
if (args) { | |
def cmd = args[0].toLowerCase().capitalize() | |
// run the command | |
try { | |
"_do${cmd}"(args.tail()) | |
} catch (MissingMethodException ex) { | |
throw new RuntimeException("Command '${cmd}' not recognized.") | |
} | |
} else { | |
throw new RuntimeException("usage <command name :: 1> <args :: 0+>") | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.InheritConstructors | |
/** | |
* Example application. Pass this as the main class, and Groovy will | |
* construct the object with the arguments, and then invoke the | |
* specified command as per the run() method in {@link GroovyCLI}. | |
* | |
* @author Ben Fagin | |
*/ | |
@InheritConstructors | |
class MyApp extends GroovyCLI { | |
// a single command called 'test' | |
// expects any number of arguments | |
void _doTest(def args) { | |
println("${args.size()} arguments") | |
println(args) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment