diff options
| author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2024-02-10 22:56:32 +0900 |
|---|---|---|
| committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2024-02-11 00:47:41 +0900 |
| commit | f960fbc10256ee227ad6887089388e0bda59aab5 (patch) | |
| tree | 0dc4070cea37de37b41866b9981cdbbd35e66dee | |
| parent | fdd92c2d61cc8eb86b187d8f116ed640c959a62c (diff) | |
[ruby/optparse] Search exactly when `require_exact`
To work with options defined as `--[no]-something`.
Fix https://bugs.ruby-lang.org/issues/20252
Fix https://github.com/ruby/optparse/pull/60
https://github.com/ruby/optparse/commit/78afdab307
| -rw-r--r-- | lib/optparse.rb | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb index fbcd7f9746..8382364977 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -1054,7 +1054,7 @@ XXX # Shows option summary. # Officious['help'] = proc do |parser| - Switch::NoArgument.new(nil, nil, ["-h"], ["--help"]) do |arg| + Switch::NoArgument.new do |arg| puts parser.help exit end @@ -1479,7 +1479,7 @@ XXX default_style = default_style.guess(arg = a) default_pattern, conv = search(:atype, o) unless default_pattern end - ldesc << "--#{q}" << "--no-#{q}" + ldesc << "--[no-]#{q}" (o = q.downcase).tr!('_', '-') long << o not_pattern, not_conv = search(:atype, FalseClass) unless not_style @@ -1654,14 +1654,19 @@ XXX opt, rest = $1, $2 opt.tr!('_', '-') begin - sw, = complete(:long, opt, true) - if require_exact && !sw.long.include?("--#{opt}") - throw :terminate, arg unless raise_unknown - raise InvalidOption, arg + if require_exact + sw, = search(:long, opt) + else + sw, = complete(:long, opt, true) end rescue ParseError throw :terminate, arg unless raise_unknown raise $!.set_option(arg, true) + else + unless sw + throw :terminate, arg unless raise_unknown + raise InvalidOption, arg + end end begin opt, cb, *val = sw.parse(rest, argv) {|*exc| raise(*exc)} |
