I am trying to use groovy CliBuilder to parse command line options. I am trying to use several long parameters without a short option. I have the following processor:
def cli = new CliBuilder(usage: 'Generate.groovy [options]') cli.with { h longOpt: "help", "Usage information" r longOpt: "root", args: 1, type: GString, "Root directory for code generation" x args: 1, type: GString, "Type of processor (all, schema, beans, docs)" _ longOpt: "dir-beans", args: 1, argName: "directory", type: GString, "Custom location for grails bean classes" _ longOpt: "dir-orm", args: 1, argName: "directory", type: GString, "Custom location for grails domain classes" } options = cli.parse(args) println "BEANS=${options.'dir-beans'}" println "ORM=${options.'dir-orm'}" if (options.h || options == null) { cli.usage() System.exit(0) }
According to the groovy documentation, I can use multiple "_" values โโfor a parameter when I want it to ignore the short parameter name and use only the long parameter name. According to groovy documentation:
Another example showing long options (partial emulation of arg processing for 'curl' command line):
def cli = new CliBuilder(usage:'curl [options] <url>') cli._(longOpt:'basic', 'Use HTTP Basic Authentication') cli.d(longOpt:'data', args:1, argName:'data', 'HTTP POST data') cli.G(longOpt:'get', 'Send the -d data with a HTTP GET') cli.q('If used as the first parameter disables .curlrc') cli._(longOpt:'url', args:1, argName:'URL', 'Set URL to work with') Which has the following usage message: usage: curl [options] <url> --basic Use HTTP Basic Authentication -d,--data <data> HTTP POST data -G,--get Send the -d data with a HTTP GET -q If used as the first parameter disables .curlrc --url <URL> Set URL to work with
This example shows a common convention. When mixing short and long
short names are often the same character in size. Single character parameters with arguments do not require the space between the option and the argument, for example. -DDEBUG = true. the example also shows the use of "_" when a short option is not provided.
Also note that '_' has been used several times. This is supported, but if any other shortOpt or longOpt is repeated, the behavior is undefined.
http://groovy.codehaus.org/gapi/groovy/util/CliBuilder.html
When I use "_", it only accepts the last one from the list (the last one occurs). Am I doing something wrong or is there a way around this problem?
Thanks.