Skip to content

Commit 94c5cf4

Browse files
committed
Fixed error message of unparsed non-option
Close #3
1 parent 2fe984a commit 94c5cf4

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/optparse.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,12 @@ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
16461646
end
16471647
begin
16481648
opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
1649+
rescue ParseError
1650+
raise $!.set_option(arg, arg.length > 2)
1651+
else
16491652
raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"
1653+
end
1654+
begin
16501655
argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
16511656
val = cb.call(val) if cb
16521657
setter.call(sw.switch_name, val) if setter

test/optparse/test_optparse.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,12 @@ def test_require_exact
9797
assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-zr foo))}
9898
assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-z foo))}
9999
end
100+
101+
def test_nonopt_pattern
102+
@opt.def_option(/^[^-]/) do |arg|
103+
assert(false, "Never gets called")
104+
end
105+
e = assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-t))}
106+
assert_equal(["-t"], e.args)
107+
end
100108
end

0 commit comments

Comments
 (0)