[ruby/optparse] Fix parsing array arguments with :into
option
https://github.com/ruby/optparse/commit/19700e96d8
This commit is contained in:
parent
cbc40bb130
commit
a35d324862
@ -1729,9 +1729,9 @@ XXX
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
begin
|
begin
|
||||||
opt, cb, *val = sw.parse(rest, argv) {|*exc| raise(*exc)}
|
opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
|
||||||
val = callback!(cb, 1, *val) if cb
|
val = callback!(cb, 1, val) if cb
|
||||||
callback!(setter, 2, sw.switch_name, *val) if setter
|
callback!(setter, 2, sw.switch_name, val) if setter
|
||||||
rescue ParseError
|
rescue ParseError
|
||||||
raise $!.set_option(arg, rest)
|
raise $!.set_option(arg, rest)
|
||||||
end
|
end
|
||||||
@ -1761,7 +1761,7 @@ XXX
|
|||||||
raise $!.set_option(arg, true)
|
raise $!.set_option(arg, true)
|
||||||
end
|
end
|
||||||
begin
|
begin
|
||||||
opt, cb, *val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
|
opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
|
||||||
rescue ParseError
|
rescue ParseError
|
||||||
raise $!.set_option(arg, arg.length > 2)
|
raise $!.set_option(arg, arg.length > 2)
|
||||||
else
|
else
|
||||||
@ -1769,8 +1769,8 @@ XXX
|
|||||||
end
|
end
|
||||||
begin
|
begin
|
||||||
argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
|
argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
|
||||||
val = callback!(cb, 1, *val) if cb
|
val = callback!(cb, 1, val) if cb
|
||||||
callback!(setter, 2, sw.switch_name, *val) if setter
|
callback!(setter, 2, sw.switch_name, val) if setter
|
||||||
rescue ParseError
|
rescue ParseError
|
||||||
raise $!.set_option(arg, arg.length > 2)
|
raise $!.set_option(arg, arg.length > 2)
|
||||||
end
|
end
|
||||||
@ -1798,6 +1798,8 @@ XXX
|
|||||||
|
|
||||||
# Calls callback with _val_.
|
# Calls callback with _val_.
|
||||||
def callback!(cb, max_arity, *args) # :nodoc:
|
def callback!(cb, max_arity, *args) # :nodoc:
|
||||||
|
args.compact!
|
||||||
|
|
||||||
if (size = args.size) < max_arity and cb.to_proc.lambda?
|
if (size = args.size) < max_arity and cb.to_proc.lambda?
|
||||||
(arity = cb.arity) < 0 and arity = (1-arity)
|
(arity = cb.arity) < 0 and arity = (1-arity)
|
||||||
arity = max_arity if arity > max_arity
|
arity = max_arity if arity > max_arity
|
||||||
|
@ -199,5 +199,7 @@ class TestOptionParserAcceptable < TestOptionParser
|
|||||||
def test_array
|
def test_array
|
||||||
assert_equal(%w"", no_error {@opt.parse!(%w"--array a,b,c")})
|
assert_equal(%w"", no_error {@opt.parse!(%w"--array a,b,c")})
|
||||||
assert_equal(%w"a b c", @array)
|
assert_equal(%w"a b c", @array)
|
||||||
|
assert_equal(%w"", no_error {@opt.parse!(%w"--array a")})
|
||||||
|
assert_equal(%w"a", @array)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -74,6 +74,7 @@ class TestOptionParser < Test::Unit::TestCase
|
|||||||
@opt.def_option "-v", "--verbose" do @verbose = true end
|
@opt.def_option "-v", "--verbose" do @verbose = true end
|
||||||
@opt.def_option "-q", "--quiet" do @quiet = true end
|
@opt.def_option "-q", "--quiet" do @quiet = true end
|
||||||
@opt.def_option "-o", "--option [OPT]" do |opt| @option = opt end
|
@opt.def_option "-o", "--option [OPT]" do |opt| @option = opt end
|
||||||
|
@opt.def_option "-a", "--array [VAL]", Array do |val| val end
|
||||||
result = {}
|
result = {}
|
||||||
@opt.parse %w(--host localhost --port 8000 -v), into: result
|
@opt.parse %w(--host localhost --port 8000 -v), into: result
|
||||||
assert_equal({host: "localhost", port: 8000, verbose: true}, result)
|
assert_equal({host: "localhost", port: 8000, verbose: true}, result)
|
||||||
@ -84,6 +85,18 @@ class TestOptionParser < Test::Unit::TestCase
|
|||||||
result = {}
|
result = {}
|
||||||
@opt.parse %w(--option OPTION -v), into: result
|
@opt.parse %w(--option OPTION -v), into: result
|
||||||
assert_equal({verbose: true, option: "OPTION"}, result)
|
assert_equal({verbose: true, option: "OPTION"}, result)
|
||||||
|
result = {}
|
||||||
|
@opt.parse %w(-a b,c,d), into: result
|
||||||
|
assert_equal({array: %w(b c d)}, result)
|
||||||
|
result = {}
|
||||||
|
@opt.parse %w(--array b,c,d), into: result
|
||||||
|
assert_equal({array: %w(b c d)}, result)
|
||||||
|
result = {}
|
||||||
|
@opt.parse %w(-a b), into: result
|
||||||
|
assert_equal({array: %w(b)}, result)
|
||||||
|
result = {}
|
||||||
|
@opt.parse %w(--array b), into: result
|
||||||
|
assert_equal({array: %w(b)}, result)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_require_exact
|
def test_require_exact
|
||||||
|
Loading…
x
Reference in New Issue
Block a user