Reffering that answer I was trying to use OptionParser
to parse rake
arguments. I simplified example from there and I had to add two ARGV.shift
to make it work.
require 'optparse'
namespace :user do |args|
# Fix I hate to have here
puts "ARGV: #{ARGV}"
ARGV.shift
ARGV.shift
puts "ARGV: #{ARGV}"
desc 'Creates user account with given credentials: rake user:create'
# environment is required to have access to Rails models
task :create => :environment do
options = {}
OptionParser.new(args) do |opts|
opts.banner = "Usage: rake user:create [options]"
opts.on("-u", "--user {username}","Username") { |user| options[:user] = user }
end.parse!
puts "user: #{options[:user]}"
exit 0
end
end
This is the output:
$ rake user:create -- -u foo
ARGV: ["user:create", "--", "-u", "foo"]
ARGV: ["-u", "foo"]
user: foo
I assume ARGV.shift
is not the way it should be done. I would like to know why it doesn't work without it and how to fix it in a proper way.
exit(0)
andrake
not throwing"Don't know how to build task 'foo'"
when you try withrake user:create -- -u foo
? what if you do not wantrake
to stop there?OptionParser#parse
doesn't allow to use-u=foo
... – Buchenwald