gpt4 book ai didi

Groovy CliBuilder : only last LongOpt is taken in account

转载 作者:行者123 更新时间:2023-12-01 22:54:03 26 4
gpt4 key购买 nike

我正在尝试使用 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)
}

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

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

names, the short names are often one character in size. One character options with arguments don't require a space between the option and the argument, e.g. -Ddebug=true. The example also shows the use of '_' when no short option is applicable.

Also note that '_' was used multiple times. This is supported but if any other shortOpt or any longOpt is repeated, then the behavior is undefined.

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

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

谢谢。

最佳答案

不知道你的意思它只接受最后一个。但这应该有效...

def cli = new CliBuilder().with {
x 'something', args:1
_ 'something', args:1, longOpt:'dir-beans'
_ 'something', args:1, longOpt:'dir-orm'
parse "-x param --dir-beans beans --dir-orm orm".split(' ')
}
assert cli.x == 'param'
assert cli.'dir-beans' == 'beans'
assert cli.'dir-orm' == 'orm'

关于Groovy CliBuilder : only last LongOpt is taken in account,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5798842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com