【发布时间】:2015-09-10 01:56:46
【问题描述】:
Thor 是一个用于构建强大的命令行界面的工具包。
它总是用于单个命令行。如果我想在 Rails 项目中使用它,例如:
lib/tasks/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 的最佳实践。
【问题讨论】:
-
我认为它可能会将您发送到 Rails 服务器的
-b传递给 thor。我很好奇这里的用例是什么。为什么要在 Rails 项目中使用 thor?肯定有一种方法可以做您正在寻找的事情,如果有一些关于这里动机的背景知识会很好。 -
@fdisk 谢谢。我必须使用一些选项手动运行批处理。所以我使用了
thor。还有其他方法可以做某事吗? -
您也可以使用
rake来启动lib/tasks中定义的任务,这是我在大多数Rails 商店中看到的。 -
如果使用
rake,可以选择吗?
标签: ruby-on-rails ruby command-line rspec thor