6

是否可以创建一个接受命名空间的基于 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 方法,以便显示:

晚上好!

4

1 回答 1

0

更改Formal类的命名空间

class Formal < Thor
    namespace "formal"
    ...
end

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

于 2012-09-06T12:06:40.630 回答