84

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")
lospejos
  • 1,976
  • 3
  • 19
  • 35
djangofan
  • 28,471
  • 61
  • 196
  • 289

6 Answers6

119

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

println args[0]
println args[1]
println args[2]
djangofan
  • 28,471
  • 61
  • 196
  • 289
  • Works perfectly. I didn't know about that. Here is good page where are listed all groovy script related capabilities (inlcluding args variable) - http://groovy.codehaus.org/Groovy+CLI – Michal Bernhard Sep 25 '14 at 08:42
  • 3
    @MichalBernhard here's a copy: https://web.archive.org/web/20150317165438/http://groovy.codehaus.org/Groovy+CLI – pennstatephil Mar 04 '16 at 01:38
38

If you want more advanced parsing than just getting the arguments you can use the Groovy CliBuilder to help you. It helps you with commandline flags, optional arguments and printing the usage instruction.

Checkout CliBuilder's Javadoc or MrHakis post about it.

user11171
  • 3,821
  • 5
  • 26
  • 35
xlson
  • 2,677
  • 1
  • 20
  • 11
24

The simplest is just to use this.args as an array e.g.:

test.groovy

println this.args[0]

Call:

C:>groovy test this

Output:

this
Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
paulkav1
  • 449
  • 3
  • 4
8

try this:

args.each{println it}
Buddy
  • 10,874
  • 5
  • 41
  • 58
Zak
  • 91
  • 1
  • 3
2

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).

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • "//using args in above for loop will throw error" - what args ? – Blessed Geek Jul 28 '15 at 23:02
  • "if you do not provide main method or provide one like in above example then... but you can change the name of the array" - don't understand. – Blessed Geek Jul 28 '15 at 23:06
  • @BlessedGeek that means if you do not provide main method or provide one with array name in arguments as `args` you can access the arguments passed with args[i]. But if you change the array name in arguments from `main(def args)` to `main(def argsNew)` which is allowed you can no longer access arguments passed as `args[i]` you will have to use `argsNew[i]` as mentioned in code snipped above. – Aniket Thakur Jul 29 '15 at 06:11
0

If you run your script with --compile-static or --type-checked option, the args variable won't be visible and therefore the compile throws an error:

[Static type checking] - The variable [args] is undeclared.

I like to do the following at the top of the script:

import groovy.transform.Field

@Field String[] args = binding.getVariable('args') as String[]

This will expose args as a global variable which can be used anywhere in the script.


References:

https://stackoverflow.com/a/53783178/2089675

https://groovy-lang.org/structure.html#_variables

smac89
  • 39,374
  • 15
  • 132
  • 179