Top > Blog Index > コマンドラインオプションの処理コード

コマンドラインオプションの処理コード

基本は、args を使えばよい。 ここに引数が配列になって入っている。

println args

さらに複雑なオプションを管理したい場合は、 CliBuilderを使う。

CliBuilder

test.groovy

def cli = new CliBuilder()
cli.i(argName:'input',    required:true  ,args:1 , 'input file')
cli.o(argName:'output',   required:true  ,args:1 , 'output file')

def options=cli.parse(args)
def inputf = new File(options.i)
def outputf = new File(options.o)

println inputf
println outputf

実行

$ groovy test -i input.txt -o output.txt