Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture arguments passed to a Groovy script?

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") 
like image 908
djangofan Avatar asked Jun 16 '11 05:06

djangofan


People also ask

How do I print a statement in Groovy?

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.

Does Groovy have named parameters?

Groovy collects all named parameters and puts them in a Map. The Map is passed on to the method as the first argument.

What is def in Groovy script?

The def keyword is used to define an untyped variable or a function in Groovy, as it is an optionally-typed language.


1 Answers

Sorry about asking the question. I just figured it out:

println args[0] println args[1] println args[2] 
like image 77
djangofan Avatar answered Oct 02 '22 17:10

djangofan