2012-05-02 82 views
8

my_gem你好名1名2 NAME3给我如何在Thor中指定多个参数或参数?

my_gem你好至少需要1参数:my_gem你好名

我是不是应该分析它们与参数之间用分隔符分开?

e.g

my_gem你好1,名称2,名称3,nameN

在文件时,它看起来像

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

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

或者是反正有做到这一点?

回答

12

是的,还有另一种方法。

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 
+0

这也被称为图示操作:http://stackoverflow.com/questions/4170037/what-does-the-star-mean -in-ruby和http://www.skorks.com/2009/08/method-arguments-in-ruby/ –