[英]Ruby Thor based executable with namespaces
是否可以創建一個接受命名空間的基於 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 class 中的 greet 方法,以便顯示:
晚上好!
更改Formal
class 的命名空間
class Formal < Thor
namespace "formal"
...
end
您已經嵌套了類,因此命名空間嵌套。 如果你把它們分開你就可以talk:formal
的嘿嘿沒時間測試它。 應該管用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.