gpt4 book ai didi

ruby - 你如何在 Thor 中指定多个参数或参数?

转载 作者:数据小太阳 更新时间:2023-10-29 06:43:52 26 4
gpt4 key购买 nike

my_gem 你好 name1 name2 name3 给我一个

my_gem hello requires at least 1 argument: my_gem hello name

我应该只解析它们并用定界符分隔参数吗?

例如

my_gem hello name1,name2,name3,nameN

在文件中它看起来像

class MyCLI < Thor
desc "hello NAMES", "say hello to names"

def hello(names)
say "hello #{names.split(',')}"
end
end

或者有没有办法做到这一点?

最佳答案

是的,还有另一种方法。

require 'thor'
class TestApp < Thor
desc "hello NAMES", "long desc"
def hello(*names)
say "hello #{names.join('; ')}"
end
end

它可以这样调用:

$ thor test_app:hello first second third
hello first; second; third

关于ruby - 你如何在 Thor 中指定多个参数或参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10408296/

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