diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-01-06 08:23:10 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-01-06 08:23:10 +0000 |
commit | 49684589cdbbbec3d43b73c1255f9c16171b055d (patch) | |
tree | b3fbc07ffeda4a61ca34f64dea679c4a533f0e79 | |
parent | 4d0e0a38239b7558f3f7934076b6e3dd54b1547f (diff) |
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-- | ChangeLog | 5 | ||||
-rw-r--r-- | NEWS | 3 | ||||
-rw-r--r-- | lib/optparse.rb | 27 | ||||
-rw-r--r-- | test/optparse/test_optparse.rb | 11 |
4 files changed, 33 insertions, 13 deletions
@@ -1,3 +1,8 @@ +Wed Jan 6 17:22:53 2016 Nobuyoshi Nakada <[email protected]> + + * 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 <[email protected]> * ChangeLog: fix wrong class name. @@ -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 |