CliBuilder参数没有破折号

时间:2014-12-12 00:44:04

标签: java groovy command-line-interface

使用Groovy CliBuilder,理想情况下我希望有一个cmd-line,如下所示:

./MyProgram.groovy CommandName -arg1 -arg2 -arg3

是否可以解析使用CliBuilder将CommandName拉出作为参数?

1 个答案:

答案 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([])