Ruby Thor基于名称空间的可执行文件

时间:2011-07-03 08:39:50

标签: ruby namespaces executable thor

是否有可能创建一个接受命名空间的基于Thor的Ruby可执行文件?例如,允许命令行中的以下内容:./thorfile greet:formal

鉴于我有以下的thorfile:

#!/usr/bin/env ruby

require 'rubygems'
require 'thor'

class TalkTasks < Thor
  namespace       "talk"

  desc      "greet", "says hello"
  def greet
    puts "Hello!"
  end

  class Formal < Thor
    namespace "talk:formal"

    desc    "greet", "says a formal hello"
    def greet
      puts "Good evening!"
    end
  end

end

TalkTasks.start

此thorfile提供以下任务(thor -T):

thor talk:formal:greet  # says a formal hello
thor talk:greet         # says hello

我也可以直接使用thorfile作为可执行文件:

./thorfile greet

显示:

  

您好!

我如何获得./thorfile formal:greet(或类似的东西)来执行Formal类中的greet方法,以便显示:

  

晚上好!

1 个答案:

答案 0 :(得分:0)

更改Formal

的命名空间
class Formal < Thor
    namespace "formal"
    ...
end

您已经嵌套了类,因此命名空间嵌套了。如果你把它们分开,你可以做talk:formal没时间测试它。应该工作。