Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ def compsys(to, name = File.basename($0)) # :nodoc:
# Shows option summary.
#
Officious['help'] = proc do |parser|
Switch::NoArgument.new do |arg|
Switch::NoArgument.new(nil, nil, ["-h"], ["--help"]) do |arg|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related to the require_exact bug fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it we will get a no method error on nil (here https://github.com/ruby/optparse/pull/60/files#diff-efe4cf792254b968b88d74bf0c0072b5caba64bacfaa0bb43e2a019e7a339518R1645) when just calling $ cli --help.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't reproduce that failure even without this part of the change.

puts parser.help
exit
end
Expand Down Expand Up @@ -1466,7 +1466,7 @@ def make_switch(opts, block = nil)
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
Expand Down Expand Up @@ -1642,7 +1642,7 @@ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
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
Expand Down
12 changes: 10 additions & 2 deletions test/optparse/test_optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ def test_require_exact
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

Expand All @@ -99,6 +99,14 @@ def test_require_exact
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
Expand Down