如何捕获传递给 Groovy 脚本的参数? [英] How to capture arguments passed to a Groovy script?

查看:17
本文介绍了如何捕获传递给 Groovy 脚本的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 Groovy.我在任何地方都找不到关于如何处理 Groovy 脚本参数的任何示例,所以我自己破解了这个方法.必须有更好的方法来做到这一点?如果是这样,我正在寻找这种更好的方法,因为我可能忽略了显而易见的事情.

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

推荐答案

如果您想要更高级的解析而不仅仅是获取参数,您可以使用 Groovy CliBuilder 来帮助您.它可以帮助您处理命令行标志、可选参数和打印使用说明.

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.

检查 CliBuilder 的 Javadoc 或MrHakis 发布关于它.

Checkout CliBuilder's Javadoc or MrHakis post about it.

这篇关于如何捕获传递给 Groovy 脚本的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆