Comment puis-je passer des arguments à une Groovy 2.0 script?

J'ai 2 scripts, où je suis en train de tester en passant un argument, et il échoue. J'ai examiné la documentation de GroovyScriptEngine mais il ne semble pas gérer le cas où je veux passer un arg plutôt que la valeur d'une propriété de la paire (en liaison).

Voici l'erreur que je reçois:

C:\AeroFS\Work\Groovy_Scripts>groovy scriptengineexample.groovy
hello, world
Caught: groovy.lang.MissingPropertyException: No such property: args 
        for class: hello
groovy.lang.MissingPropertyException: No such property: args for 
        class: hello
        at hello.run(hello.groovy:4)
        at Test.main(scriptengineexample.groovy:14)

Voici mes scripts:

import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;
import groovy.util.ResourceException ;
import groovy.util.ScriptException ;
import java.io.IOException ;

public class Test {
  public static void main( String[] args ) throws IOException, 
             ResourceException, ScriptException {
    GroovyScriptEngine gse = new GroovyScriptEngine( [ '.' ] as String[] )
    Binding binding = new Binding();
    binding.setVariable("input", "world");
    gse.run("hello.groovy", binding);
    System.out.println( "Output: " + binding.getVariable("output") );
  }
}

Et celui-ci:

//hello.groovy
println "hello.groovy"
for (arg in this.args ) {
  println "Argument:" + arg;
}
  • Ne devrait pas être setVariable(args ["entrée","le monde"])?
InformationsquelleAutor djangofan | 2013-04-16