[ruby/optparse] Fixed error message of unparsed non-option

Close https://github.com/ruby/optparse/issues/3

https://github.com/ruby/optparse/commit/94c5cf4032
This commit is contained in:
Nobuyoshi Nakada 2021-03-29 16:42:49 +09:00
parent 2bbae0e91a
commit e8317d90b0
No known key found for this signature in database
GPG Key ID: 7CD2805BFA3770C6
2 changed files with 13 additions and 0 deletions

View File

@ -1646,7 +1646,12 @@ XXX
end
begin
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 = cb.call(val) if cb
setter.call(sw.switch_name, val) if setter

View File

@ -97,4 +97,12 @@ class TestOptionParser < Test::Unit::TestCase
assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-zr foo))}
assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-z foo))}
end
def test_nonopt_pattern
@opt.def_option(/^[^-]/) do |arg|
assert(false, "Never gets called")
end
e = assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-t))}
assert_equal(["-t"], e.args)
end
end