我有最简单的 groovy 脚本试图找出 CliBuilder。如何让 CliBuilder 为我提供超出选项的命令行参数?我的期望是像...这样的命令行调用
./hello.groovy -u robert Edward
...会产生类似...的输出
ROBERT EDWARD
来 self 的消息来源,例如...
#!/usr/bin/env groovy
cli = new CliBuilder(usage:'hello.groovy [-hu] [name ...]')
cli.with {
h longOpt: 'help', 'Show usage information'
u longOpt: 'upper', 'uppercase eachg name'
}
options = cli.parse(args)
if(!options) {
throw new IllegalStateException("WTF?!?")
}
if(options.h || options.arguments().isEmpty()) {
cli.usage()
return
}
println("$options.arguments()");
..但我无法弄清楚如何获得其余的参数,那些超出选项的参数。
最佳答案
如果 -u
只是一个标志,则不需要 -2
:
#!/usr/bin/env groovy
cli = new CliBuilder(usage:'hello.groovy [-hu] [name ...]')
cli.with {
h longOpt: 'help', 'Show usage information'
u longOpt: 'upper', 'uppercase eachg name'
}
options = cli.parse(args)
if(!options) {
throw new IllegalStateException("WTF?!?")
}
if(options.h || options.arguments().isEmpty()) {
cli.usage()
return
}
if(options.u) {
options.arguments().each { println it.toUpperCase() }
}
else {
options.arguments().each { println it }
}
关于groovy - 如何从 CliBuilder 获取非选项参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28487427/