使用Groovy CliBuilder,理想情况下我希望有一个cmd-line,如下所示:
./MyProgram.groovy CommandName -arg1 -arg2 -arg3
是否可以解析使用CliBuilder将CommandName拉出作为参数?
答案 0 :(得分:1)
如果将属性stopAtNonOption
设置为false,则可以执行此操作,以便解析不会在CommandName
中停止。然后,您可以从CliBuilder
options获取命令。下面是一个小例子:
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([])