Reputation: 3216
I use CliBuilder to parse some named arguments (h, t, c, n, s):
static main(args) {
// http://mrhaki.blogspot.com/2009/09/groovy-goodness-parsing-commandline.html
// http://docs.groovy-lang.org/latest/html/gapi/groovy/util/CliBuilder.html
def cli = new CliBuilder(usage: 'hl7-benchmark -[t] type -[c] concurrency -[n] messages -[s] ip:port')
cli.with {
h longOpt: 'help', 'Show usage information'
t longOpt: 'type', args: 1, argName: 'type', 'mllp|soap'
c longOpt: 'concurrency', args: 1, argName: 'concurrency', 'number of processes sending messages'
n longOpt: 'messages', args: 1, argName: 'messages', 'number of messages to be send by each process'
s longOpt: 'ip:port', args: 2, argName: 'ip:port', 'server IP address:server port', valueSeparator: ':'
}
def options = cli.parse(args)
The invocation command line looks like: hl7-benchmark -t xxx -c yyy -n zzz -s aaa:bbb
I need to add an optional attribute at the end but I don't want it to be named, like:
hl7-benchmark -t xxx -c yyy -n zzz -s aaa:bbb final_value_without_name
Is that possible with CliBuilder? I couldn't find any examples of this.
Thanks!
Upvotes: 1
Views: 736
Reputation: 8119
You can simply get it as the "arguments" part of the command line:
def test(args) {
def cli = new CliBuilder(usage: 'hl7-benchmark -[t] type -[c] concurrency -[n] messages -[s] ip:port')
cli.with {
h longOpt: 'help', 'Show usage information'
t longOpt: 'type', args: 1, argName: 'type', 'mllp|soap'
c longOpt: 'concurrency', args: 1, argName: 'concurrency', 'number of processes sending messages'
n longOpt: 'messages', args: 1, argName: 'messages', 'number of messages to be send by each process'
s longOpt: 'ip:port', args: 2, argName: 'ip:port', 'server IP address:server port', valueSeparator: ':'
}
def options = cli.parse(args)
def otherArguments = options.arguments()
println options.t
println options.c
println options.n
println options.ss // http://www.kellyrob99.com/blog/2009/10/04/groovy-clibuilder-with-multiple-arguments/#hide
println otherArguments
}
test(['-t', 'xxx', '-c', 'yyy', '-n', 'zzz', '-s', 'aaa:bbb', 'final_value_without_name'])
The above gives:
xxx
yyy
zzz
[aaa, bbb]
[final_value_without_name]
If you want that the argument is also correctly parsed when placed before the options, you can set the stopAtNonOption
to false, like in CliBuilder argument without dash
Upvotes: 2