# OptParse - how do I get the argument values?

**URL:** <https://www.ruby-forum.com/t/optparse-how-do-i-get-the-argument-values/236651>\
**Category:** Ruby\
**Created:** [September 23, 2013, 11:26am UTC](https://www.ruby-forum.com/t/optparse-how-do-i-get-the-argument-values/236651 "2013-09-23T11:26:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [September 23, 2013, 11:26am UTC](https://www.ruby-forum.com/t/optparse-how-do-i-get-the-argument-values/236651/1 "2013-09-23T11:26:06Z")

</div>

I am trying to use OptParser to get the values of my options, but in the  
blocks I only seem to get boolean values.

options = {}  
opt\_parser = OptionParser.new do |opts|  
opts.on(’-h’, ‘–help’, ‘Help’) { usage() }  
opts.on(’-a’, ‘–app’, ‘iOS Application’) { |v| options[:app\_name] = v  
}  
opts.on(’-v’, ‘–ios\_version’, ‘iOS Version’) { |v|  
options[:ios\_version] = v }  
opts.on(’-p’, ‘–assets\_path’, ‘assets\_path’) { |v|  
options[:assets\_path] = v }  
opts.on(’-s’, ‘–sim\_path’, ‘sim\_path’) { |v| options[:sim\_path] = v }  
end.parse!

options.keys.each do |key|  
puts 'key is ’ + key.to\_s  
puts 'value is ’ + options[key].to\_s  
end

For the command line  
sync\_simulator.rb --app angry --assets\_path /Users/paul/worms  
–ios\_version 7.0

The output is  
key is ios\_version  
value is true  
key is assets\_path  
value is true  
key is app\_name  
value is true

I am hoping to get  
key is ios\_version  
value is 7.0  
key is assets\_path  
value is /Home/paul/worms  
key is app\_name  
value is angry

---

<div class="post-metadata">

**Author:** ![pozza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e8cbc/32.png) [@pozza](https://www.ruby-forum.com/u/pozza)\
**Post date:** [September 23, 2013, 12:11pm UTC](https://www.ruby-forum.com/t/optparse-how-do-i-get-the-argument-values/236651/2 "2013-09-23T12:11:50Z")

</div>

On 2013-09-23, at 5:26 AM, Paul W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> opts.on(‘-p’, ‘–assets\_path’, ‘assets\_path’) { |v|  
> sync\_simulator.rb --app angry --assets\_path /Users/paul/worms  
> I am hoping to get  
> key is ios\_version  
> value is 7.0  
> key is assets\_path  
> value is /Home/paul/worms  
> key is app\_name  
> value is angry

You need to indicate to OptParser that you expect an argument, otherwise  
it returns true to say “I saw that option on the command line”. This can  
be done by adding a token to the long flag (for more detail see

> **[Class: OptionParser (Ruby 2.0.0)](https://ruby-doc.org/stdlib-2.0.0/libdoc/optparse/rdoc/OptionParser.html)**
>
> Class : OptionParser - Ruby 2.0.0

and the example there).

#!/usr/bin/env ruby

require ‘optparse’

options = {}  
opt\_parser = OptionParser.new do |opts|  
opts.on(‘-h’, ‘–help’, ‘Help’) { usage() }  
opts.on(‘-a’, ‘–app NAME’, ‘iOS Application’) {  
|name| options[:app\_name] = name  
}  
opts.on(‘-v’, ‘–ios\_version VERSION’, ‘iOS Version’) { |version|  
options[:ios\_version] = version  
}  
opts.on(‘-p’, ‘–assets\_path PATH’, ‘assets\_path’) { |path|  
options[:assets\_path] = path  
}  
opts.on(‘-s’, ‘–sim\_path PATH’, ‘sim\_path’) { |path|  
options[:sim\_path] = path  
}  
end.parse!

options.each\_pair do |key, value|  
puts “key is #{key}”  
puts “value is #{value}”  
end

**END**

ratdog:tmp mike$ ./try.rb --app angry --assets\_path /Users/paul/worms  
–ios\_version 7.0  
key is app\_name  
value is angry  
key is assets\_path  
value is /Users/paul/worms  
key is ios\_version  
value is 7.0

Hope this helps,

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.
