зеркало из https://github.com/github/ruby.git
[ruby/optparse] Add symbolize_names to getopts
https://github.com/ruby/optparse/commit/3e63d878f8
This commit is contained in:
Родитель
4c767c1354
Коммит
cea6951ecf
|
@ -1775,7 +1775,16 @@ XXX
|
|||
# # params["bar"] = "x" # --bar x
|
||||
# # params["zot"] = "z" # --zot Z
|
||||
#
|
||||
def getopts(*args)
|
||||
# Option +symbolize_names+ (boolean) specifies whether returned Hash keys should be Symbols; defaults to +false+ (use Strings).
|
||||
#
|
||||
# params = ARGV.getopts("ab:", "foo", "bar:", "zot:Z;zot option", symbolize_names: true)
|
||||
# # params[:a] = true # -a
|
||||
# # params[:b] = "1" # -b1
|
||||
# # params[:foo] = "1" # --foo
|
||||
# # params[:bar] = "x" # --bar x
|
||||
# # params[:zot] = "z" # --zot Z
|
||||
#
|
||||
def getopts(*args, symbolize_names: false)
|
||||
argv = Array === args.first ? args.shift : default_argv
|
||||
single_options, *long_options = *args
|
||||
|
||||
|
@ -1804,14 +1813,14 @@ XXX
|
|||
end
|
||||
|
||||
parse_in_order(argv, result.method(:[]=))
|
||||
result
|
||||
symbolize_names ? result.transform_keys(&:to_sym) : result
|
||||
end
|
||||
|
||||
#
|
||||
# See #getopts.
|
||||
#
|
||||
def self.getopts(*args)
|
||||
new.getopts(*args)
|
||||
def self.getopts(*args, symbolize_names: false)
|
||||
new.getopts(*args, symbolize_names: symbolize_names)
|
||||
end
|
||||
|
||||
#
|
||||
|
@ -2289,8 +2298,8 @@ XXX
|
|||
# rescue OptionParser::ParseError
|
||||
# end
|
||||
#
|
||||
def getopts(*args)
|
||||
options.getopts(self, *args)
|
||||
def getopts(*args, symbolize_names: false)
|
||||
options.getopts(self, *args, symbolize_names: symbolize_names)
|
||||
end
|
||||
|
||||
#
|
||||
|
|
|
@ -11,23 +11,39 @@ class TestOptionParserGetopts < Test::Unit::TestCase
|
|||
o = @opt.getopts(%w[-a], "ab")
|
||||
assert_equal(true, o['a'])
|
||||
assert_equal(false, o['b'])
|
||||
|
||||
o = @opt.getopts(%w[-a], "ab", symbolize_names: true)
|
||||
assert_equal(true, o[:a])
|
||||
assert_equal(false, o[:b])
|
||||
end
|
||||
|
||||
def test_short_arg
|
||||
o = @opt.getopts(%w[-a1], "a:b:")
|
||||
assert_equal("1", o['a'])
|
||||
assert_equal(nil, o['b'])
|
||||
|
||||
o = @opt.getopts(%w[-a1], "a:b:", symbolize_names: true)
|
||||
assert_equal("1", o[:a])
|
||||
assert_equal(nil, o[:b])
|
||||
end
|
||||
|
||||
def test_long_noarg
|
||||
o = @opt.getopts(%w[--foo], "", "foo", "bar")
|
||||
assert_equal(true, o['foo'])
|
||||
assert_equal(false, o['bar'])
|
||||
|
||||
o = @opt.getopts(%w[--foo], "", "foo", "bar", symbolize_names: true)
|
||||
assert_equal(true, o[:foo])
|
||||
assert_equal(false, o[:bar])
|
||||
end
|
||||
|
||||
def test_long_arg
|
||||
o = @opt.getopts(%w[--bar ZOT], "", "foo:FOO", "bar:BAR")
|
||||
assert_equal("FOO", o['foo'])
|
||||
assert_equal("ZOT", o['bar'])
|
||||
|
||||
o = @opt.getopts(%w[--bar ZOT], "", "foo:FOO", "bar:BAR", symbolize_names: true)
|
||||
assert_equal("FOO", o[:foo])
|
||||
assert_equal("ZOT", o[:bar])
|
||||
end
|
||||
end
|
||||
|
|
Загрузка…
Ссылка в новой задаче