It is very much similar to Java and you can use the same java syntax. For eg.
class TestExecutor {
public static void main(def args) {
println("Printing arguments");
for(String arguments : args) {
println (arguments);
}
}
}
Run it and you should see the arguments printed
C:\Users\athakur\Desktop>groovy TestExecutor.groovy test1 test2 test3
Aug 16, 2014 11:47:56 AM org.codehaus.groovy.runtime.m12n.MetaInfExtensionModule
newModule
WARNING: Module [groovy-nio] - Unable to load extension class [org.codehaus.groo
vy.runtime.NioGroovyMethods]
Printing arguments
test1
test2
test3
Also note if you do not provide main method or provide one like in above example then you can get arguments as args[i]
but you can change the name of the array (again same as java). So you can have something like -
public static void main(def argsNew) {
println("Printing arguments");
for(String arguments : argsNew) {
//using args in above for loop will throw error
println (arguments);
}
}
Point being it's not something that is hard-coded. Finally as suggested in other answer you can always use CliBuilder for smart parsing. But again in that too it internally used def options = cli.parse(args)
.