aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-01-06 08:23:10 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-01-06 08:23:10 +0000
commitcfb6723fd6624be21a8c83f482b925dacf27e3e2 (patch)
treeb3fbc07ffeda4a61ca34f64dea679c4a533f0e79
parent361fa838a3f5a8ba2fd3be9037ce7fa92e40e74d (diff)
downloadruby-cfb6723fd6624be21a8c83f482b925dacf27e3e2.tar.gz
optparse.rb: into kwdarg
* lib/optparse.rb (OptionParser#order!): add `into` optional keyword argument to store the results. [Feature #11191] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53444 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--NEWS3
-rw-r--r--lib/optparse.rb27
-rw-r--r--test/optparse/test_optparse.rb11
4 files changed, 33 insertions, 13 deletions
diff --git a/ChangeLog b/ChangeLog
index b9a660654b..5942d0c02a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Wed Jan 6 17:22:53 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * lib/optparse.rb (OptionParser#order!): add `into` optional
+ keyword argument to store the results. [Feature #11191]
+
Tue Jan 5 21:44:37 2016 SHIBATA Hiroshi <hsbt@ruby-lang.org>
* ChangeLog: fix wrong class name.
diff --git a/NEWS b/NEWS
index 46e4771531..7384eb7931 100644
--- a/NEWS
+++ b/NEWS
@@ -21,6 +21,9 @@ with all sufficient information, see the ChangeLog file or Redmine
* CSV
* Add a liberal_parsing option. [Feature #11839]
+* optparse
+ * Add an into option. [Feature #11191]
+
=== Stdlib compatibility issues (excluding feature bug fixes)
=== Built-in global variables compatibility issues
diff --git a/lib/optparse.rb b/lib/optparse.rb
index b33d4cb395..280aa4934d 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -1508,17 +1508,18 @@ XXX
#
# Returns the rest of +argv+ left unparsed.
#
- def order(*argv, &block)
+ def order(*argv, into: nil, &nonopt)
argv = argv[0].dup if argv.size == 1 and Array === argv[0]
- order!(argv, &block)
+ order!(argv, into: into, &nonopt)
end
#
# Same as #order, but removes switches destructively.
# Non-option arguments remain in +argv+.
#
- def order!(argv = default_argv, &nonopt)
- parse_in_order(argv, &nonopt)
+ def order!(argv = default_argv, into: nil, &nonopt)
+ setter = ->(name, val) {into[name.to_sym] = val} if into
+ parse_in_order(argv, setter, &nonopt)
end
def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
@@ -1599,18 +1600,18 @@ XXX
# Parses command line arguments +argv+ in permutation mode and returns
# list of non-option arguments.
#
- def permute(*argv)
+ def permute(*argv, into: nil)
argv = argv[0].dup if argv.size == 1 and Array === argv[0]
- permute!(argv)
+ permute!(argv, into: into)
end
#
# Same as #permute, but removes switches destructively.
# Non-option arguments remain in +argv+.
#
- def permute!(argv = default_argv)
+ def permute!(argv = default_argv, into: nil)
nonopts = []
- order!(argv, &nonopts.method(:<<))
+ order!(argv, into: into, &nonopts.method(:<<))
argv[0, 0] = nonopts
argv
end
@@ -1619,20 +1620,20 @@ XXX
# Parses command line arguments +argv+ in order when environment variable
# POSIXLY_CORRECT is set, and in permutation mode otherwise.
#
- def parse(*argv)
+ def parse(*argv, into: nil)
argv = argv[0].dup if argv.size == 1 and Array === argv[0]
- parse!(argv)
+ parse!(argv, into: into)
end
#
# Same as #parse, but removes switches destructively.
# Non-option arguments remain in +argv+.
#
- def parse!(argv = default_argv)
+ def parse!(argv = default_argv, into: nil)
if ENV.include?('POSIXLY_CORRECT')
- order!(argv)
+ order!(argv, into: into)
else
- permute!(argv)
+ permute!(argv, into: into)
end
end
diff --git a/test/optparse/test_optparse.rb b/test/optparse/test_optparse.rb
index f17f8ee895..a2540db241 100644
--- a/test/optparse/test_optparse.rb
+++ b/test/optparse/test_optparse.rb
@@ -64,4 +64,15 @@ class TestOptionParser < Test::Unit::TestCase
assert_equal(%w"", no_error {@opt.parse!(%w"--regexp=/foo/n")})
assert_equal(/foo/n, @reopt)
end
+
+ def test_into
+ @opt.def_option "-h", "--host=HOST", "hostname"
+ @opt.def_option "-p", "--port=PORT", "port", Integer
+ @opt.def_option "-v", "--verbose" do @verbose = true end
+ @opt.def_option "-q", "--quiet" do @quiet = true end
+ result = {}
+ @opt.parse %w(--host localhost --port 8000 -v), into: result
+ assert_equal({host: "localhost", port: 8000, verbose: true}, result)
+ assert_equal(true, @verbose)
+ end
end