2012-04-17 7 views
0

オプションのエラーを取得するために、GroovyのCliBuilderを使用して、私の質問は: 私はスクリプトを渡すとき: グルーヴィーMyScript.groovy -o MTEST -fファイルテスト スクリプトは、-oオプションを得ることができます。 しかし、私はオプションの場所を変更します。 groovy MyScript.groovy -fファイルテスト-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、ファイルテスト、-o、MTEST] [email protected]

グルービーMyScript.groovy -o MTEST -fファイルテスト 印刷結果である: [-o、MTEST、-F、ファイルテスト] [email protected] MTEST

+0

i'amが起こる何を混乱させる:私はいくつかのオプションを渡す場合は、 gammachen

答えて

1

は、あなたが(それは引数を取ると)同様にあなたの-fオプションにargsを指定する必要があると思うつまり:

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 
} 
関連する問題