Optparse problem grabbing a value

Hi folks,

I'm trying to switch from getoptlong to optparse. I'm having a problem
grabbing a value from the command line. Here's the code:

···

-----------
cat example.rb

#!/usr/bin/env ruby
require "ostruct"
require "optparse"

options = OpenStruct.new

options.date = false

option_parser = OptionParser.new do |opts|
  opts.on("-d", "--date", String, "Optional: Specify date in format
00-00-00") do |date|
    options.date = date
  end
end

puts "<#{options.date}>"

-------------

If I run:

ruby example.rb -d 12-07-12

I expected the output:

<12-07-12>

instead I saw:

<false>

What am I doing wrong?

best

Jams

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!:

./test.rb -d "12-10-12"
<12-10-12>

Jesus.

···

On Thu, Jul 26, 2012 at 6:58 PM, James Harrison <jam@jamandbees.net> wrote:

#!/usr/bin/env ruby
require "ostruct"
require "optparse"

options = OpenStruct.new

options.date = false

option_parser = OptionParser.new do |opts|
  opts.on("-d", "--date", String, "Optional: Specify date in format
00-00-00") do |date|
    options.date = date
  end
end

puts "<#{options.date}>"

Thanks! That got it!

···

On Thu, Jul 26, 2012 at 11:15 AM, Jesús Gabriel y Galán < jgabrielygalan@gmail.com> wrote:

On Thu, Jul 26, 2012 at 6:58 PM, James Harrison <jam@jamandbees.net> > wrote:
> #!/usr/bin/env ruby
> require "ostruct"
> require "optparse"
>
> options = OpenStruct.new
>
> options.date = false
>
> option_parser = OptionParser.new do |opts|
> opts.on("-d", "--date", String, "Optional: Specify date in format
> 00-00-00") do |date|
> options.date = date
> end
> end
>
> puts "<#{options.date}>"

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!:

./test.rb -d "12-10-12"
<12-10-12>

Jesus.

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: 2010-02-01 ((2455229j,0s,0n),+0s,2299161j)>
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: 2010-02-01T00:00:00+00:00 ((2455229j,0s,0n),+0s,2299161j)>
DateTime
=>

Kind regards

robert

···

On Thu, Jul 26, 2012 at 8:59 PM, James Harrison <jam@jamandbees.net> wrote:

On Thu, Jul 26, 2012 at 11:15 AM, Jesús Gabriel y Galán > <jgabrielygalan@gmail.com> wrote:

On Thu, Jul 26, 2012 at 6:58 PM, James Harrison <jam@jamandbees.net>

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!:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/