0

I have a groovy script used in conjunction with GroovyScriptEngine:

public static void main(String[] args) {
    GroovyScriptEngine gse = new GroovyScriptEngine(new String[] {"/home/user/tmp"});
    Binding varSet = new Binding();
    varSet.setVariable("testVar", "Hello World");
    gse.run("printHello.groovy", varSet);
}

This is running just fine from java. The printHello.groovy starts keeping as already defined all the bound variables. The script "/home/user/tmp/printHello.groovy" is something like this:

println("${testVar} !!!")

What I want is to be able to test this script calling it from command line, but I haven't found a way to pass the binding variables to my script.

$ groovy printHello.groovy [???]

That could be very useful for testing.

mizar
  • 355
  • 4
  • 11

1 Answers1

1

You can just pass the arguments You need after the script invocation:

$ groovy groovyAuthDefault.groovy user pass

In the script all the parameters are accessible via args variable. More info.

Is that what You were looking for?

UPDATE

Found solution but it has some limitations, maybe it's possible to bypass them but don't know exactly how. As I wrote above when You invoke script from command line You can pass arguments that are kept in args list. The problem lies in the fact that GroovyScriptEngine doesn't invoke the external script with it's main method - there's no args list so it fails with an MissingPropertyException. The idea is to set fake args.

java:

public static void main(String[] args) {
    GroovyScriptEngine gse = new GroovyScriptEngine(new String[] {"/home/user/tmp"});
    Binding varSet = new Binding();
    varSet.setVariable("testVar", "Hello World");
    varSet.setVariable("args", null); //null, empty string, whatever evaluates to false in groovy
    gse.run("printHello.groovy", varSet);
}

printHello.groovy:

if(args) {
    setBinding(new Binding(Eval.me(args[0])))
}
println("${testVar} !!!")

In printHello.groovy args is checked. If it evaluates to true it means that script was invoked from command line with arguments and a new Binding is set - evaluated from first element of arguments passed (plain groovy script extends groovy.lang.Script. If args evaluates to false it means that script was run with GroovyScriptEngine.

Command line invocation:

groovy printHello.groovy [testVar:\'hi\']

Exception handling might be added with other improvements as well. Hope that helps.

Community
  • 1
  • 1
Opal
  • 81,889
  • 28
  • 189
  • 210
  • Sorry Opal, maybe that's the only answer but I expected to find a real command line way to bind variables. The script does not define in any way these variables, even through args[], it simply knows about them trough Binding. Binding is used in my web app to bind some objects and let them to be managed by a dynamically loaded groovy script. A very similar but, as far as I know,not the same thing as using args. And if I decide to use args for testing the script, what can be the way I use args with GroovyScriptEngine ? – mizar Jul 03 '14 at 23:46
  • Can You provide a minimal working example just to know exactly how it works? It seems that something like `JCommander` or `CliBuilder` might be needed. – Opal Jul 04 '14 at 14:26
  • 1
    I edit the original question in order to appear minimal and easily reproducible. You must have the groovy embeddable jars in your class path to compile it. – mizar Jul 04 '14 at 15:29
  • That's a viable way. The script must take into account both ways of being executed. I thought about that already but I hoped for a faster emulation of binding. Anyway your example is pretty clear and adoptable. Thanks. – mizar Jul 07 '14 at 09:13