我有一个类似的脚本
OptionParser.new do |opts|
opt.on("-h","--help","help") do
puts opts
end
end.parse!
但是每当我调用 rails runner my_script.rb --help
时,它都会向我显示有关 rails runner 的帮助,而不是我的脚本。有什么方法可以防止 Rails Runner 吞掉这个选项吗?
最佳答案
恐怕你不能用 runner 做到这一点 - runner 首先搜索它自己的选项,如果是 --help
或 -h
它会打印帮助并退出在检查您的脚本是否存在之前:
# Railites: lib/rails/commands/runner
opts.on("-h", "--help",
"Show this help message.") { $stdout.puts opts; exit }
然而,您可以通过根本不使用运行器并编写纯 ruby 脚本来解决这个问题:
#!/usr/bin/env ruby
require 'optparse'
environment = 'development'
OptionParser.new do |opts|
opt.on("-e", "--environment") do |v|
environment = v
end
opt.on("-h","--help","help") do
puts opts
end
end.parse!
# RAILS_ENV is used by environment.rb file to load correct configuration
ENV['RAILS_ENV'] = environment
# Load your rails application
require_relative "../config/environment.rb"
puts User.count # Your code here
关于ruby-on-rails - rails runners 的信息文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34343631/