# Optparse problem grabbing a value

**URL:** <https://www.ruby-forum.com/t/optparse-problem-grabbing-a-value/221700>\
**Category:** Ruby\
**Created:** [July 26, 2012, 6:59pm UTC](https://www.ruby-forum.com/t/optparse-problem-grabbing-a-value/221700 "2012-07-26T18:59:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [July 26, 2012, 6:59pm UTC](https://www.ruby-forum.com/t/optparse-problem-grabbing-a-value/221700/1 "2012-07-26T18:59:38Z")

</div>

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:

What am I doing wrong?

best

Jams

---

<div class="post-metadata">

**Author:** ![James\_H.on](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@James\_H.on](https://www.ruby-forum.com/u/James_H.on)\
**Post date:** [July 26, 2012, 7:16pm UTC](https://www.ruby-forum.com/t/optparse-problem-grabbing-a-value/221700/2 "2012-07-26T19:16:00Z")

</div>

On Thu, Jul 26, 2012 at 6:58 PM, James H.on [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![James\_H.on](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@James\_H.on](https://www.ruby-forum.com/u/James_H.on)\
**Post date:** [July 26, 2012, 9:00pm UTC](https://www.ruby-forum.com/t/optparse-problem-grabbing-a-value/221700/3 "2012-07-26T21:00:01Z")

</div>

Thanks! That got it!

On Thu, Jul 26, 2012 at 11:15 AM, Jess Gabriel y Galn \<

---

<div class="post-metadata">

**Author:** ![James\_H.on](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@James\_H.on](https://www.ruby-forum.com/u/James_H.on)\
**Post date:** [July 28, 2012, 10:14am UTC](https://www.ruby-forum.com/t/optparse-problem-grabbing-a-value/221700/4 "2012-07-28T10:14:57Z")

</div>

On Thu, Jul 26, 2012 at 8:59 PM, James H.on [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Thu, Jul 26, 2012 at 11:15 AM, Jess Gabriel y Galn  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > On Thu, Jul 26, 2012 at 6:58 PM, James H.on [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > option\_parser = OptionParser.new do |opts|  
> > 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: 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
