【问题标题】:How you do specify multiple arguments or parameters in Thor?如何在 Thor 中指定多个参数或参数?
【发布时间】:2012-05-11 14:31:57
【问题描述】:

my_gem 你好 name1 name2 name3 给我一个

my_gem hello 至少需要 1 个参数:my_gem hello name

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

例如

my_gem 你好 name1,name2,name3,nameN

在文件中看起来像

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

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

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

【问题讨论】:

    标签: ruby command-line-interface command-line-arguments thor


    【解决方案1】:

    是的,还有另一种方法。

    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
    
    猜你喜欢
    • 2014-01-27
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2014-02-08
    相关资源
    最近更新 更多