Rails 4 optparse invalid_option
Rails 4 optparse invalid_option
我有以下代码。使用 -c 1 有效,但使用 -e 1 会导致无输出,而使用 --from_date 会导致 'invalid_option' 错误。我做错了什么?
require 'optparse'
namespace :schedule do |args|
desc "Create schedule entries for recurring events"
task :create_recurring => :environment do
options = {}
optparse = OptionParser.new(args) do |opts|
opts.on("--from_date {from_date}", "Date to start creating Schedules") do |from_date|
options[:from_date] = from_date
end
opts.on("-e", "--event_id {event_id}", "Event to create Schedules for") do |event_id|
options[:event_id] = event_id
end
opts.on("-c","--calendar_id {calendar_id}", "Calendar to create Schedules for") do |calendar_id|
options[:calendar_id] = calendar_id
end
end
begin
optparse.parse!
rescue OptionsParser::InvalidOption => e
puts "invalid option : e.inspect"
end
puts "options #{options.inspect}"
end
end
使用你的 Rakefile,我已经设法让它工作(我有 Rake 0.9.6;显然 newer Rake versions may have issues?):
rake schedule:create_recurring -- -c1 -e42 --from_date=foo
这输出:
options {:calendar_id=>"1", :event_id=>"42", :from_date=>"foo"}
我有以下代码。使用 -c 1 有效,但使用 -e 1 会导致无输出,而使用 --from_date 会导致 'invalid_option' 错误。我做错了什么?
require 'optparse'
namespace :schedule do |args|
desc "Create schedule entries for recurring events"
task :create_recurring => :environment do
options = {}
optparse = OptionParser.new(args) do |opts|
opts.on("--from_date {from_date}", "Date to start creating Schedules") do |from_date|
options[:from_date] = from_date
end
opts.on("-e", "--event_id {event_id}", "Event to create Schedules for") do |event_id|
options[:event_id] = event_id
end
opts.on("-c","--calendar_id {calendar_id}", "Calendar to create Schedules for") do |calendar_id|
options[:calendar_id] = calendar_id
end
end
begin
optparse.parse!
rescue OptionsParser::InvalidOption => e
puts "invalid option : e.inspect"
end
puts "options #{options.inspect}"
end
end
使用你的 Rakefile,我已经设法让它工作(我有 Rake 0.9.6;显然 newer Rake versions may have issues?):
rake schedule:create_recurring -- -c1 -e42 --from_date=foo
这输出:
options {:calendar_id=>"1", :event_id=>"42", :from_date=>"foo"}