0

I have the following code. Using -c 1 works, but using -e 1 results in no output and using --from_date results in an 'invalid_option' error. What am I doing wrong?

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
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
Denise Mauldin
  • 5,397
  • 5
  • 32
  • 51

1 Answers1

0

Using your Rakefile, I've managed to get this to work (I have Rake 0.9.6; apparently newer Rake versions may have issues?):

rake schedule:create_recurring -- -c1 -e42 --from_date=foo

This outputs:

options {:calendar_id=>"1", :event_id=>"42", :from_date=>"foo"}
Community
  • 1
  • 1
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • I changed the optparse line to: `optparse.parse!(ARGV[2..-1])` as in the linked StackOverflow since I'm using Rake 10.4.2, but still no go. :\ Works with -c, but not with -e or --from_date. – Denise Mauldin Sep 21 '15 at 02:24
  • I updated my Rake to 10.4.2. With the `optparse.parse!(ARGV[2..-1])`, it worked for me, but I used the `rake` command _exactly_ as I wrote it. (No space within `-c1` or `-e42`, and using `=` instead of space in `--from_date=foo`. If you use spaces, `rake` will cry.) – C. K. Young Sep 21 '15 at 11:39