使用Groovy的CliBuilder,可以提供多个参数,例如:这里:
以上链接中的示例:
def cli = new CliBuilder(
usage: 'findClassesInJars.groovy -d <root_directories> -s <strings_to_search_for>',
header: '\nAvailable options (use -h for help):\n',
footer: '\nInformation provided via above options is used to generate printed string.\n')
import org.apache.commons.cli.Option
cli.with
{
h(longOpt: 'help', 'Help', args: 0, required: false)
d(longOpt: 'directories', 'Two arguments, separated by a comma', args: Option.UNLIMITED_VALUES, valueSeparator: ',', required: true)
s(longOpt: 'strings', 'Strings (class names) to search for in JARs', args: Option.UNLIMITED_VALUES, valueSeparator: ',', required: true)
}
但是,这意味着必须像这样调用脚本:
groovy script.groovy -d folder1,folder2,folder3
而不是更常见的(至少在Unix世界中):
groovy script.groovy -d folder1 -d folder2 -d folder3
有没有办法让它像第二个例子一样工作?
答案 0 :(得分:1)
是的,将字母's'附加到参数的名称。按照惯例,这将返回一个ArrayList,您可以迭代它以访问所有值。
println "The first directory value: ${option.d}"
println "The directories as a list ${option.ds}"
options.ds.each{ it->
println "directory: ${it}"
}