ruby-changes:65691
From: Nobuyoshi <ko1@a...>
Date: Mon, 29 Mar 2021 19:41:59 +0900 (JST)
Subject: [ruby-changes:65691] e8317d90b0 (master): [ruby/optparse] Fixed error message of unparsed non-option
https://git.ruby-lang.org/ruby.git/commit/?id=e8317d90b0 From e8317d90b0b102ba270b456cc6cb7c6865c6b448 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Mon, 29 Mar 2021 16:42:49 +0900 Subject: [ruby/optparse] Fixed error message of unparsed non-option Close https://github.com/ruby/optparse/issues/3 https://github.com/ruby/optparse/commit/94c5cf4032 --- lib/optparse.rb | 5 +++++ test/optparse/test_optparse.rb | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/optparse.rb b/lib/optparse.rb index 8b09915..ad20ce7 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -1646,7 +1646,12 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1646 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 diff --git a/test/optparse/test_optparse.rb b/test/optparse/test_optparse.rb index fec14fc..5f5ea18 100644 --- a/test/optparse/test_optparse.rb +++ b/test/optparse/test_optparse.rb @@ -97,4 +97,12 @@ class TestOptionParser < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/optparse/test_optparse.rb#L97 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 -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/