gpt4 book ai didi

Grails 脚本和使用 Groovy CLIBuilder 传递参数?

转载 作者:行者123 更新时间:2023-12-02 15:34:17 25 4
gpt4 key购买 nike

我创建了一个非常简单的脚本,并希望将参数传递给脚本。

像:

grails> helloworld -n Howdy
grails> helloworld -name Howdy

使用脚本:
target(main: 'Hello World') {
def cli = new CliBuilder()
cli.with
{
h(longOpt: 'help', 'Help - Usage Information')
n(longOpt: 'name', 'Name to say hello to', args: 1, required: true)
}
def opt = cli.parse(args)
if (!opt) return
if (opt.h) cli.usage()
println "Hello ${opt.n}"
}

我似乎在我所做的每一次尝试中都失败了。该脚本一直提示 -n 选项不存在。

当我调试脚本时,args 参数的值看起来像是重新排列的值。

使用以下命令调用脚本时:
grails> helloworld -n Howdy 

脚本中 args 的值为: 你好-n

我在这里错过了什么做错了?有什么建议么?

最佳答案

您的问题是您正在通过 grails 运行代码贝壳。我已将您的代码转换为 CLI.groovy像这样:

class CLI{
public static void main(String [] args){
def cli = new CliBuilder()
cli.with
{
h(longOpt: 'help', 'Help - Usage Information')
n(longOpt: 'name', 'Name to say hello to', args: 1, required: true)
}
def opt = cli.parse(args)
if (!opt) return
if (opt.h) cli.usage()
println "Hello ${opt.n}"
}
}

之后我使用 groovy像这样从 linux shell 运行它的命令:
 archer@capitan $ groovy CLI -n Daddy

它输出:
 archer@capitan $ groovy CLI -n Daddy
Hello Daddy

所以它就像一个魅力。

关于Grails 脚本和使用 Groovy CLIBuilder 传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14292839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com