【问题标题】:How to capture arguments passed to a Groovy script?如何捕获传递给 Groovy 脚本的参数?
【发布时间】:2011-09-16 01:54:26
【问题描述】:

我刚刚开始使用 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")

【问题讨论】:

    标签: java groovy command-line-arguments


    【解决方案1】:

    如果您使用--compile-static--type-checked 选项运行脚本,args 变量将不可见,因此编译会引发错误:

    [静态类型检查] - 变量 [args] 未声明。

    我喜欢在脚本顶部执行以下操作:

    import groovy.transform.Field
    
    @Field String[] args = binding.getVariable('args') as String[]
    

    这会将args 公开为一个全局变量,可以在脚本中的任何位置使用。


    参考资料:

    https://stackoverflow.com/a/53783178/2089675

    https://groovy-lang.org/structure.html#_variables

    【讨论】:

      【解决方案2】:

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

      查看 CliBuilder 的 Javadoc 或 MrHakis post 了解它。

      【讨论】:

        【解决方案3】:

        试试这个:

        args.each{println it}
        

        【讨论】:

          【解决方案4】:

          最简单的就是将this.args用作数组,例如:

          test.groovy

          println this.args[0]
          

          呼叫:

          C:>groovy test this
          

          输出:

          this
          

          【讨论】:

            【解决方案5】:

            它与 Java 非常相似,您可以使用相同的 java 语法。例如。

            class TestExecutor {
            
                public static void main(def args) {
                    println("Printing arguments");
                    for(String arguments : args) {
                        println (arguments);
                    }
                }
            
            } 
            

            运行它,您应该会看到打印的参数

            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
            

            还请注意,如果您不提供 main 方法或提供类似上述示例的方法,那么您可以将参数设为 args[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);
                }
            }
            

            重点是它不是硬编码的。最后,正如其他答案中所建议的,您始终可以使用 CliBuilder 进行智能解析。但同样,它在内部也使用了def options = cli.parse(args)

            【讨论】:

            • "//在上面的 for 循环中使用 args 会抛出错误" - 什么 args ?
            • "如果你不提供 main 方法或者提供一个类似上面例子的方法,那么......但是你可以更改数组的名称" - 不明白。
            • @BlessedGeek 这意味着如果您不提供 main 方法或在参数中提供一个数组名称为 args 的方法,您可以访问通过 args[i] 传递的参数。但是,如果您将参数中的数组名称从 main(def args) 更改为 main(def argsNew),这是允许的,您将无法再访问以 args[i] 传递的参数,您将不得不使用 argsNew[i],如上面的代码中所述。
            【解决方案6】:

            抱歉问这个问题。我刚刚想通了:

            println args[0]
            println args[1]
            println args[2]
            

            【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-18
            • 2011-08-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多