Java 如何捕获传递给 Groovy 脚本的参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6367384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to capture arguments passed to a Groovy script?
提问by djangofan
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.
我刚刚开始使用 Groovy。我在任何地方都找不到关于如何处理 Groovy 脚本参数的任何示例,因此我自己破解了这个方法。必须有更好的方法来做到这一点?如果是这样,我正在寻找这种更好的方法,因为我可能忽略了显而易见的事情。
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")
采纳答案by xlson
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.
如果您想要更高级的解析而不仅仅是获取参数,您可以使用 Groovy CliBuilder 来帮助您。它可以帮助您处理命令行标志、可选参数和打印使用说明。
Checkout CliBuilder's Javadoc or MrHakis postabout it.
查看CliBuilder的 Javadoc 或 MrHakis帖子。
回答by djangofan
Sorry about asking the question. I just figured it out:
很抱歉问这个问题。我刚刚想通了:
println args[0]
println args[1]
println args[2]
回答by Aniket Thakur
It is very much similar to Java and you can use the same java syntax. For eg.
它与 Java 非常相似,您可以使用相同的 Java 语法。例如。
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 -
另请注意,如果您不提供 main 方法或提供如上例所示的方法,那么您可以获得参数 asargs[i]
但您可以更改数组的名称(再次与 java 相同)。所以你可以有类似的东西 -
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 CliBuilderfor smart parsing. But again in that too it internally used def options = cli.parse(args)
.
重点是它不是硬编码的东西。最后,正如其他答案中所建议的,您始终可以使用CliBuilder进行智能解析。但同样也是在它内部使用的def options = cli.parse(args)
。
回答by paulkav1
The simplest is just to use this.args
as an array e.g.:
最简单的就是this.args
用作数组,例如:
test.groovy
测试.groovy
println this.args[0]
Call:
称呼:
C:>groovy test this
Output:
输出:
this
回答by Zak
try this:
尝试这个:
args.each{println it}