[英]How to use thor in rails?
Thor是用於構建強大的命令行界面的工具包。
它總是用於單個命令行。 如果我想在rails項目中使用它,例如:
LIB /任務/ my_cli.rb
require "thor"
class MyCLI < Thor
desc "hello NAME", "say hello to NAME"
def hello(name)
puts "Hello #{name}"
end
end
放置MyCLI.start(ARGV)
?
如果我將它放在該文件( lib/tasks/my_cli.rb
)下,當我運行我的rspec測試時,它會顯示命令消息:
Commands:
rspec help [COMMAND] # Describe available commands or one specific command
rspec hello NAME # say hello to NAME
我不想在我的bundle exec rspec
看到它,所以我將MyCLI.start(ARGV)
到bin/rails
。 它看起來很好。 但在我這樣做之后:
$ ./bin/rails s -b 0.0.0.0
$ [CTRL+C]
我看到這個消息:
=> Booting Thin
=> Rails 4.2.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop
^CStopping ...
Exiting
Could not find command "_b".
這是什么意思:
Could not find command "_b".
所以,我不知道如何在rails項目中使用thor的最佳實踐。
你必須使用method_option: https : //github.com/erikhuda/thor/wiki/Method-Options
並且不要傳遞參數,就好像它是一個普通的方法
require "thor"
class MyCLI < Thor
desc "hello NAME", "say hello to NAME"
method_option :name, aliases: '-b', type: :string, desc: 'It`s the named passed'
def hello
puts "Hello #{options[:name]}"
end
end
然后用thor
命令使用它: thor mycli:hello -b Robert
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.