[#69084] [Ruby trunk - Feature #11124] [Open] [PATCH] lib/*: use monotonic clock for timeouts — normalperson@...
Issue #11124 has been reported by Eric Wong.
5 messages
2015/05/06
[#69138] [Ruby trunk - Feature #11136] [PATCH] webrick: avoid fcntl module — nobu@...
Issue #11136 has been updated by Nobuyoshi Nakada.
3 messages
2015/05/12
[#69160] [Ruby trunk - Feature #11146] [PATCH] variable.c: initialize generic_iv_tbl at start — nobu@...
Issue #11146 has been updated by Nobuyoshi Nakada.
4 messages
2015/05/13
[#69175] Re: [Ruby trunk - Feature #11146] [PATCH] variable.c: initialize generic_iv_tbl at start
— Eric Wong <normalperson@...>
2015/05/13
nobu@ruby-lang.org wrote:
[ruby-core:69383] [Ruby trunk - Feature #11191] [Open] Add #to_h method to OptionParser
From:
ljjarvis@...
Date:
2015-05-27 20:44:53 UTC
List:
ruby-core #69383
Issue #11191 has been reported by Lee Jarvis.
----------------------------------------
Feature #11191: Add #to_h method to OptionParser
https://bugs.ruby-lang.org/issues/11191
* Author: Lee Jarvis
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
Simply collecting configuration values is a very popular use for OptionParser. Code like this is quite common:
~~~
config = {}
opts = OptionParser.new do |o|
o.on "-h", "--host=HOST", "hostname" do |h|
config[:host] = h
end
o.on "-p", "--port=PORT", "port", Integer do |p|
config[:port] = p
end
o.on "-v", "--verbose" do
config[:verbose] = true
end
o.on "-q", "--quiet" do
config[:quiet] = true
end
end
opts.parse!
# do something with config values
~~~
This boilerplate is one of the reasons I built Slop: https://github.com/leejarvis/slop
I'd like to add a `to_h` method to OptionParser which returns a Hash containing the switch name and switch argument values. This would reduce the above example to:
~~~
opts = OptionParser.new do |o|
o.on "-h", "--host=HOST", "hostname"
o.on "-p", "--port=PORT", "port", Integer
o.on "-v", "--verbose"
o.on "-q", "--quiet"
end
~~~
With this example, the output would look something like:
~~~
opts.parse %w(--host localhost --port 8000 --verbose)
puts opts.to_h #=>
{:host=>"localhost", :port=>8000, :verbose=>true, :quiet=>nil}
~~~
I've attached a patch that implements this functionality in quite a basic way. I'm very keen to hear what others think.
---Files--------------------------------
0001-Add-to_h-method-to-OptionParser.patch (2.97 KB)
--
https://bugs.ruby-lang.org/