当选项不是第一个参数时,使用Groovy的CliBuilder获取选项错误

时间:2012-04-17 14:16:58

标签: groovy

我的问题是: 当我通过脚本时: groovy MyScript.groovy -o mtest -f filetest 该脚本可以获取-o选项。 但是当我改变选项的位置时。 groovy MyScript.groovy -f filetest -o mtest 它无法获得-o

的选项

为什么呢? 我会错过什么吗?

groovy代码是:

def cli = new CliBuilder()
            cli.with {
        usage: 'Self'
        h longOpt:'help', 'U should input a analyze script with -o dataFileName!'
        o longOpt:'output', 'The file which should be analyzed.', args:1, required:true
        f longOpt:'file', 'File'
    }
            def opt = cli.parse(args)
            def action
            if( args.length == 0) {
                    cli.usage()
                    return
            }
            if( opt.h ) {
                    cli.usage()
                    return
            }
            println(args);
            println(opt);
            println(opt.o);
groovy MyScript.groovy -f filetest -o mtest 

打印结果是: [-f,filetest,-o,mtest] groovy.util.OptionAccessor@66b51404 假

groovy MyScript.groovy -o mtest -f filetest     打印结果是: [-o,mtest,-f,filetest] groovy.util.OptionAccessor@66b51404 MTEST

1 个答案:

答案 0 :(得分:1)

认为您还需要在args选项上指定-f(因为它需要参数),即:

def cli = new CliBuilder().with {
  usage: 'Self'
  h longOpt:'help', 'U should input a analyze script with -o dataFileName!'
  o longOpt:'output', 'The file which should be analyzed.', args:1, required:true
  f longOpt:'file', 'File', args:1
  it
}

def opt = cli.parse( args )

if( opt ) {
  println args
  println opt
  println opt.o
}
相关问题