3

I have a script like

OptionParser.new do |opts|
  opt.on("-h","--help","help") do
    puts opts
  end
end.parse!

But whenever I call rails runner my_script.rb --help it shows me help for the rails runner and not my script. Is there a way that I can prevent rails runner from swallowing up this option?

Xodarap
  • 11,581
  • 11
  • 56
  • 94

1 Answers1

2

I am afraid you cannot do this with runner - runner first searches for its own options and in case of --help or -h it prints the help and exits before even checking whether your script exists or not:

# Railites: lib/rails/commands/runner
opts.on("-h", "--help",
    "Show this help message.") { $stdout.puts opts; exit }

You can however get around this by simply not using runner at all and writing pure ruby script:

#!/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
BroiSatse
  • 44,031
  • 8
  • 61
  • 86