Groovy CliBuilder:只考虑最后的LongOpt

jmq*_*jmq 6 groovy

我正在尝试使用groovy CliBuilder来解析命令行选项.我正在尝试使用多个长选项,而不是一个简短的选项.我有以下处理器:

    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)
    }
Run Code Online (Sandbox Code Playgroud)

根据groovy文档,当我希望它忽略短选项名称并仅使用长选项名称时,我应该能够为选项使用多个"_"值.根据groovy文档:

Another example showing long options (partial emulation of arg 
processing for 'curl' command line):
Run Code Online (Sandbox Code Playgroud)
 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
Run Code Online (Sandbox Code Playgroud)
This example shows a common convention. When mixing short and long
Run Code Online (Sandbox Code Playgroud)

名称,短名称通常是一个字符大小.带参数的一个字符选项不需要选项和参数之间的空格,例如-Ddebug = true.该示例还显示了在没有适用短选项时使用'_'.

另请注意,'_'被多次使用.这是受支持的,但如果重复任何其他shortOpt或任何longOpt,则行为未定义.

http://groovy.codehaus.org/gapi/groovy/util/CliBuilder.html

当我使用"_"时,它只接受列表中的最后一个(遇到最后一个).我做错了什么还是有办法解决这个问题?

谢谢.

jmq*_*jmq 2

我了解到我的原始代码可以正常工作。不起作用的是采用外壳内置的所有选项with并打印详细用法的功能。CliBuilder 中内置的打印用法的函数调用是:

cli.usage()

上面的原始代码打印以下用法行:

usage: Generate.groovy [options]
    --dir-orm <directory>   Custom location for grails domain classes
 -h,--help                  Usage information
 -r,--root                  Root directory for code generation
 -x                         Type of processor (all, schema, beans, docs)
Run Code Online (Sandbox Code Playgroud)

这个用法行让我看起来缺少选项。我犯了一个错误,没有与此使用函数调用分开打印每个单独的项目。这就是为什么它看起来只关心外壳_中的最后一个项目with。我添加了这段代码来证明它正在传递值:

    println "BEANS=${options.'dir-beans'}"
    println "ORM=${options.'dir-orm'}"
Run Code Online (Sandbox Code Playgroud)

我还发现您必须=在长选项和它的值之间使用,否则它将无法正确解析命令行选项(--long-option=some_value