如何在rails中使用雷神?
How to use thor in rails?
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 的最佳实践。
您必须使用 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
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 的最佳实践。
您必须使用 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