Using Groovy CliBuilder, ideally I would like to have an cmd-line as follows:
./MyProgram.groovy CommandName -arg1 -arg2 -arg3
Is is possible to parse pull out the CommandName as an argument using CliBuilder?
Using Groovy CliBuilder, ideally I would like to have an cmd-line as follows:
./MyProgram.groovy CommandName -arg1 -arg2 -arg3
Is is possible to parse pull out the CommandName as an argument using CliBuilder?
You can do that if you set the property stopAtNonOption
to false so that the parsing does not stop in CommandName
. Then you can get the command from CliBuilder
options. A tiny example below:
def test(args) {
def cli = new CliBuilder(usage: 'testOptions.groovy [command] -r -u', stopAtNonOption: false)
cli.with {
r longOpt: 'reverse', 'Reverse command'
u longOpt: 'upper', 'Uppercase command'
}
def options = cli.parse(args)
def otherArguments = options.arguments()
def command = otherArguments ? otherArguments[0] : 'defaultCommand'
def result = command
if (options.r) {
result = result.reverse()
}
if (options.u) {
result = result.toUpperCase()
}
result
}
assert 'myCommand' == test(['myCommand'])
assert 'MYCOMMAND' == test(['myCommand', '-u'])
assert 'dnammoCym' == test(['myCommand', '-r'])
assert 'DNAMMOCYM' == test(['myCommand', '-r', '-u'])
assert 'defaultCommand' == test([])