Reputation: 2336
With Groovy's CliBuilder it's possible to supply multiple arguments as described e.g. here:
Sample from the above link:
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)
}
However, this means the script has to be called like this:
groovy script.groovy -d folder1,folder2,folder3
instead of the more usual (at least in the Unix world):
groovy script.groovy -d folder1 -d folder2 -d folder3
Is there a way to make it work like in the second example?
Upvotes: 3
Views: 1669
Reputation: 11
Yes, append the letter 's' to the name of the argument. This will, by convention, return an ArrayList that you can iterate through to access all of the values.
println "The first directory value: ${option.d}"
println "The directories as a list ${option.ds}"
options.ds.each{ it->
println "directory: ${it}"
}
Upvotes: 1