1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cli = new CliBuilder(usage:'cmdopt -o [-q] [-d{0|1|2}] 文字列 [文字列 ...]')
cli.o(longOpt:'output', required:true, 'set output') 
cli.q(longOpt:'quote', 'set quote') 
cli.d(longOpt:'debug', args:1, argName:'level', 'set debug level') 

opt = cli.parse(args)
if (!opt) System.exit 1
params = opt.arguments()
if (params.size() < 1) die 'parameter must be specified'
level = opt.d ? opt.d.toInteger() : 0
if (!(level in 0..2)) die 'debug level must be in 0-2'

println '[オプション情報]'
println "o(output): ${opt.o ? 'ON' : 'OFF'}"
println "q(quote): ${opt.q ? 'ON' : 'OFF'}"
println "d(debug): $level"
println '\n[パラメータ情報]'
println "指定数: ${params.size()}"
i = 1
params.each { println "${i++}: $it" }

def die(msg) {
  cli.writer.println "error: $msg"
  cli.usage()
  System.exit 1
}