小编Sie*_*bbe的帖子

Ruby 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
Run Code Online (Sandbox Code Playgroud)

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

thor talk:formal:greet  # says a formal hello
thor talk:greet         # says hello
Run Code Online (Sandbox Code Playgroud)

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

./thorfile greet
Run Code Online (Sandbox Code Playgroud)

哪个显示:

你好!

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

晚上好!

ruby executable namespaces thor

5
推荐指数
1
解决办法
1631
查看次数

标签 统计

executable ×1

namespaces ×1

ruby ×1

thor ×1