Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1729,9 +1729,9 @@ def parse_in_order(argv = default_argv, setter = nil, exact: require_exact, **,
end
end
begin
opt, cb, *val = sw.parse(rest, argv) {|*exc| raise(*exc)}
val = callback!(cb, 1, *val) if cb
callback!(setter, 2, sw.switch_name, *val) if setter
opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
val = callback!(cb, 1, val) if cb
callback!(setter, 2, sw.switch_name, val) if setter
rescue ParseError
raise $!.set_option(arg, rest)
end
Expand Down Expand Up @@ -1761,16 +1761,16 @@ def parse_in_order(argv = default_argv, setter = nil, exact: require_exact, **,
raise $!.set_option(arg, true)
end
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
raise $!.set_option(arg, arg.length > 2)
else
raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"
end
begin
argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
val = callback!(cb, 1, *val) if cb
callback!(setter, 2, sw.switch_name, *val) if setter
val = callback!(cb, 1, val) if cb
callback!(setter, 2, sw.switch_name, val) if setter
rescue ParseError
raise $!.set_option(arg, arg.length > 2)
end
Expand Down Expand Up @@ -1798,6 +1798,8 @@ def parse_in_order(argv = default_argv, setter = nil, exact: require_exact, **,

# Calls callback with _val_.
def callback!(cb, max_arity, *args) # :nodoc:
args.compact!

if (size = args.size) < max_arity and cb.to_proc.lambda?
(arity = cb.arity) < 0 and arity = (1-arity)
arity = max_arity if arity > max_arity
Expand Down
2 changes: 2 additions & 0 deletions test/optparse/test_acceptable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,7 @@ def test_decimal_numeric
def test_array
assert_equal(%w"", no_error {@opt.parse!(%w"--array a,b,c")})
assert_equal(%w"a b c", @array)
assert_equal(%w"", no_error {@opt.parse!(%w"--array a")})
assert_equal(%w"a", @array)
end
end
13 changes: 13 additions & 0 deletions test/optparse/test_optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def test_into
@opt.def_option "-v", "--verbose" do @verbose = 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 "-a", "--array [VAL]", Array do |val| val end
result = {}
@opt.parse %w(--host localhost --port 8000 -v), into: result
assert_equal({host: "localhost", port: 8000, verbose: true}, result)
Expand All @@ -84,6 +85,18 @@ def test_into
result = {}
@opt.parse %w(--option OPTION -v), into: 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

def test_require_exact
Expand Down