diff --git a/lib/optparse.rb b/lib/optparse.rb index 67beeb9ee3..b0284850cf 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -1768,10 +1768,11 @@ XXX if pat.empty? search(typ, opt) {|sw| return [sw, opt]} # exact match or... end - raise AmbiguousOption, catch(:ambiguous) { + ambiguous = catch(:ambiguous) { visit(:complete, typ, opt, icase, *pat) {|o, *sw| return sw} - raise InvalidOption.new(opt, additional: self.:additional_message.curry[typ]) } + exc = ambiguous ? AmbiguousOption : InvalidOption + raise exc.new(opt, additional: self.:additional_message.curry[typ]) end private :complete diff --git a/test/optparse/test_did_you_mean.rb b/test/optparse/test_did_you_mean.rb index 82a086acae..19bb4b9b89 100644 --- a/test/optparse/test_did_you_mean.rb +++ b/test/optparse/test_did_you_mean.rb @@ -39,4 +39,10 @@ class TestOptionParser::DidYouMean < TestOptionParser @opt.permute!(%w"--baa") end end + + def test_ambiguos + assert_raise_with_message(OptionParser::AmbiguousOption, /ambiguous option: --ba\nDid you mean\?\s+baz\s+bar\Z/) do + @opt.permute!(%w"--ba") + end + end end