【问题标题】:Using parameters in a groovy script在 groovy 脚本中使用参数
【发布时间】:2019-05-03 10:48:42
【问题描述】:

刚从 groovy 开始,如果有人可以帮助我,不胜感激。

我正在脚本中编写一个函数,该函数将接受一个参数。

def print_arg(def arg){
    println "$arg"
}

我将该文件保存为 test.groovy。

如何从命令行测试函数的抛出参数?

groovy test.groovy print_arg input
groovy test.groovy print_arg 'input'
groovy test.groovy print_arg(input)

以上都不行

我怀疑我在做一些根本上愚蠢的事情:-)

任何提示,不胜感激。

【问题讨论】:

    标签: groovy


    【解决方案1】:

    以下是从脚本调用方法的方式

    def print_arg(def arg){
        println "$arg"
    }
    
    print_arg ("hello") 
    

    脚本接收args 数组,因此以下会将参数传递给您的函数

    def print_arg(def arg){
            println "$arg"
        }
    
    print_arg (args[0]) 
    

    (您可以稍后选择更优雅的机制How to capture arguments passed to a Groovy script? - 我的答案来自上面的链接,因为这个问题更基本,所以无法复制)

    【讨论】:

    • 谢谢 Jayan,你是个绝对的绅士
    猜你喜欢
    • 2017-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多