0

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

为什么?我想念什么吗?

常规代码是:

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 false

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

4

1 回答 1

1

Think you need to specify args on your -f option as well (as it takes an argument) ie:

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
}
于 2012-04-18T16:45:43.443 回答