Forum: Ruby OptParse - how do I get the argument values?

D53f5657b8b1959ab3b79dd78bf1aa38?d=identicon&s=25 Paul Wilkinson (ohthepain)
on 2013-09-23 11:26
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
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2013-09-23 12:11
(Received via mailing list)
On 2013-09-23, at 5:26 AM, Paul Wilkinson <lists@ruby-forum.com> 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
http://ruby-doc.org/stdlib-2.0.0/libdoc/optparse/r...
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 Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.