I am just starting out with Groovy. I couldn't find any examples anywhere of how to handle arguments to a Groovy script and so I hacked this method myself. There must be a better way of doing this? If so, I am looking for this better way, since I am probably overlooking the obvious.
import groovy.lang.Binding; Binding binding = new Binding(); int x = 1 for (a in this.args) { println("arg$x: " + a) binding.setProperty("arg$x", a); x=x+1 } println binding.getProperty("arg1") println binding.getProperty("arg2") println binding.getProperty("arg3")
You can use the print function to print a string to the screen. You can include \n to embed a newline character. There is no need for semi-colon ; at the end of the statement. Alternatively you can use the println function that will automatically append a newline to the end of the output.
Groovy collects all named parameters and puts them in a Map. The Map is passed on to the method as the first argument.
The def keyword is used to define an untyped variable or a function in Groovy, as it is an optionally-typed language.
Sorry about asking the question. I just figured it out:
println args[0] println args[1] println args[2]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With