Ruby Rails 4 optpass无效的_选项
我有以下代码。使用-c1可以工作,但是使用-e1会导致没有输出,而使用--from_date会导致“invalid_option”错误。我做错了什么Ruby Rails 4 optpass无效的_选项,ruby,ruby-on-rails-4,rake,Ruby,Ruby On Rails 4,Rake,我有以下代码。使用-c1可以工作,但是使用-e1会导致没有输出,而使用--from_date会导致“invalid_option”错误。我做错了什么 require 'optparse' namespace :schedule do |args| desc "Create schedule entries for recurring events" task :create_recurring => :environment do options = {} opt
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;显然): 这将产生:
options {:calendar_id=>"1", :event_id=>"42", :from_date=>"foo"}
我将optpasse行更改为:
optpasse.parse!(ARGV[2..-1])
与链接的StackOverflow一样,因为我使用的是Rake 10.4.2,但仍然没有成功:使用-c,但不使用-e或--from_date。我将Rake更新为10.4.2。使用optpasse.parse!(ARGV[2..-1])
,它对我很有效,但我使用的rake
命令与我编写的命令完全相同。(在-c1
或-e42
中没有空格,使用=
而不是--from_date=foo
中的空格。如果使用空格,rake
将哭泣。)
options {:calendar_id=>"1", :event_id=>"42", :from_date=>"foo"}