From: Robert Klemme Date: 2012-07-28T17:14:20+09:00 Subject: Re: optparse problem grabbing a value On Thu, Jul 26, 2012 at 8:59 PM, James Harrison wrote: > On Thu, Jul 26, 2012 at 11:15 AM, Jesús Gabriel y Galán > wrote: >> >> On Thu, Jul 26, 2012 at 6:58 PM, James Harrison >> This works: >> >> #!/usr/bin/env ruby >> require "ostruct" >> require "optparse" >> >> options = OpenStruct.new >> >> options.date = false >> >> option_parser = OptionParser.new do |opts| >> opts.on("-d", "--date DATE", String, "Optional: Specify date in >> format 00-00-00") do |date| >> options.date = date >> end >> end >> >> option_parser.parse! >> >> puts "<#{options.date}>" >> >> I had to add the DATE parameter in the long format specification and >> also call parse!: You can also have OptionParser convert it directly: $ irb19 -r optparse -r optparse/date irb(main):001:0> OptionParser.new{|o|o.on('-d D', Date){|v| p v,v.class}}.parse %w{-d 2010-02-01} # Date => [] irb(main):002:0> OptionParser.new{|o|o.on('-d D', DateTime){|v| p v,v.class}}.parse %w{-d 2010-02-01} # DateTime => [] Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/