Skip to content

Commit 78afdab

Browse files
committed
Search exactly when require_exact
To work with options defined as `--[no]-something`. Fix https://bugs.ruby-lang.org/issues/20252 Fix #60
1 parent 213cb03 commit 78afdab

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lib/optparse.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ def compsys(to, name = File.basename($0)) # :nodoc:
10541054
# Shows option summary.
10551055
#
10561056
Officious['help'] = proc do |parser|
1057-
Switch::NoArgument.new(nil, nil, ["-h"], ["--help"]) do |arg|
1057+
Switch::NoArgument.new do |arg|
10581058
puts parser.help
10591059
exit
10601060
end
@@ -1479,7 +1479,7 @@ def make_switch(opts, block = nil)
14791479
default_style = default_style.guess(arg = a)
14801480
default_pattern, conv = search(:atype, o) unless default_pattern
14811481
end
1482-
ldesc << "--#{q}" << "--no-#{q}"
1482+
ldesc << "--[no-]#{q}"
14831483
(o = q.downcase).tr!('_', '-')
14841484
long << o
14851485
not_pattern, not_conv = search(:atype, FalseClass) unless not_style
@@ -1654,14 +1654,19 @@ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
16541654
opt, rest = $1, $2
16551655
opt.tr!('_', '-')
16561656
begin
1657-
sw, = complete(:long, opt, true)
1658-
if require_exact && !sw.long.include?("--#{opt}")
1659-
throw :terminate, arg unless raise_unknown
1660-
raise InvalidOption, arg
1657+
if require_exact
1658+
sw, = search(:long, opt)
1659+
else
1660+
sw, = complete(:long, opt, true)
16611661
end
16621662
rescue ParseError
16631663
throw :terminate, arg unless raise_unknown
16641664
raise $!.set_option(arg, true)
1665+
else
1666+
unless sw
1667+
throw :terminate, arg unless raise_unknown
1668+
raise InvalidOption, arg
1669+
end
16651670
end
16661671
begin
16671672
opt, cb, *val = sw.parse(rest, argv) {|*exc| raise(*exc)}

0 commit comments

Comments
 (0)