From f7a407cabda6eb787fb95fc6e3c1b2215b1aec19 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Sat, 14 Oct 2023 21:52:01 +0300 Subject: [PATCH] [ruby/optparse] Fix `require_exact` to work with options defined as `--[no]-something` https://github.com/ruby/optparse/commit/4e346ad337 --- lib/optparse.rb | 6 +++--- test/optparse/test_optparse.rb | 12 ++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/optparse.rb b/lib/optparse.rb index ecf6adc45f..ccfea6dcf0 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -1048,7 +1048,7 @@ XXX # Shows option summary. # Officious['help'] = proc do |parser| - Switch::NoArgument.new do |arg| + Switch::NoArgument.new(nil, nil, ["-h"], ["--help"]) do |arg| puts parser.help exit end @@ -1473,7 +1473,7 @@ XXX default_style = default_style.guess(arg = a) default_pattern, conv = search(:atype, o) unless default_pattern end - ldesc << "--[no-]#{q}" + ldesc << "--#{q}" << "--no-#{q}" (o = q.downcase).tr!('_', '-') long << o not_pattern, not_conv = search(:atype, FalseClass) unless not_style @@ -1649,7 +1649,7 @@ XXX opt.tr!('_', '-') begin sw, = complete(:long, opt, true) - if require_exact && !sw.long.include?(arg) + if require_exact && !sw.long.include?("--#{opt}") throw :terminate, arg unless raise_unknown raise InvalidOption, arg end diff --git a/test/optparse/test_optparse.rb b/test/optparse/test_optparse.rb index bfa705ad03..be9bcb8425 100644 --- a/test/optparse/test_optparse.rb +++ b/test/optparse/test_optparse.rb @@ -88,9 +88,9 @@ class TestOptionParser < Test::Unit::TestCase end @opt.require_exact = true - %w(--zrs -F -Ffoo).each do |arg| + [%w(--zrs foo), %w(--zrs=foo), %w(-F foo), %w(-Ffoo)].each do |args| result = {} - @opt.parse([arg, 'foo'], into: result) + @opt.parse(args, into: result) assert_equal({zrs: 'foo'}, result) end @@ -99,6 +99,14 @@ class TestOptionParser < Test::Unit::TestCase assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-zrs foo))} assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-zr foo))} assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-z foo))} + + @opt.def_option('-f', '--[no-]foo', 'foo') {|arg| @foo = arg} + @opt.parse(%w[-f]) + assert_equal(true, @foo) + @opt.parse(%w[--foo]) + assert_equal(true, @foo) + @opt.parse(%w[--no-foo]) + assert_equal(false, @foo) end def test_raise_unknown