加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Groovy CliBuilder:只考虑最后的LongOpt

发布时间:2020-12-14 16:29:14 所属栏目:大数据 来源:网络整理
导读:我正在尝试使用groovy CliBuilder来解析命令行选项.我正在尝试使用多个长选项,而不是一个简短的选项. 我有以下处理器: def cli = new CliBuilder(usage: 'Generate.groovy [options]') cli.with { h longOpt: "help","Usage information" r longOpt: "root"
我正在尝试使用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 of processor (all,schema,beans,docs)"
        _ longOpt: "dir-beans",argName: "directory","Custom location for grails bean classes"
        _ longOpt: "dir-orm","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文档:

06001

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',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

06003

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',longOpt:'dir-beans'
  _ 'something',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'

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读