[英]Bind variables from Command line to test groovy Script
I have a groovy script used in conjunction with GroovyScriptEngine: 我有一个与GroovyScriptEngine结合使用的groovy脚本:
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. 从java可以正常运行。 The printHello.groovy starts keeping as already defined all the bound variables.
printHello.groovy开始将所有绑定变量保持为已定义的状态。 The script "/home/user/tmp/printHello.groovy" is something like this:
脚本“ /home/user/tmp/printHello.groovy”是这样的:
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 [???] $ groovy printHello.groovy [???]
That could be very useful for testing. 这对于测试可能非常有用。
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. 在脚本中,所有参数均可通过
args
变量进行访问。 More info . 更多信息 。
Is that what You were looking for? 那是您要找的东西吗?
UPDATE 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. 正如我在上面从命令行调用脚本时所写的那样,您可以传递保存在
args
列表中的args
。 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
. 问题在于,
GroovyScriptEngine
不会使用其main方法调用外部脚本-没有args
列表,因此它会因MissingPropertyException
而失败。 The idea is to set fake args
. 这个想法是设置假的
args
。
java : 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 : printHello.groovy :
if(args) {
setBinding(new Binding(Eval.me(args[0])))
}
println("${testVar} !!!")
In printHello.groovy args
is checked. 在printHello.groovy中检查
args
。 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
. 如果评估结果为true,则表示从命令行使用参数调用了脚本,并设置了新的
Binding
从传递的参数的第一个元素开始评估(纯groovy脚本扩展了groovy.lang.Script
。如果args
评估为false,则表示该脚本用GroovyScriptEngine
运行。
Command line invocation: 命令行调用:
groovy printHello.groovy [testVar:\'hi\']
Exception handling might be added with other improvements as well. 异常处理可能还会添加其他改进。 Hope that helps.
希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.