mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
This commit is contained in:
parent
4d0e0a3823
commit
49684589cd
4 changed files with 33 additions and 13 deletions
|
@ -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.
|
||||
|
|
3
NEWS
3
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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue