Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix require_exact to work with options defined as --[no]-something #60

Merged
merged 1 commit into from
Feb 9, 2024

Conversation

fatkodima
Copy link
Contributor

While working on rubocop/rubocop#12282 I identified that optparse does not work correctly when require_exact is set and an option with --[no-] prefix is defined. It raises invalid option when this option is provided via command line.

Example

OptionParser.new do |opts|
  opts.require_exact = true

  opts.on("-v", "--[no-]verbose", "Run verbosely") do
    # ...
  end
end
$ cli --verbose
invalid option: --verbose
For usage information, use --help

This config was introduced in #2.
cc @jeremyevans (as implementor)

@@ -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.

@fatkodima
Copy link
Contributor Author

fatkodima commented Dec 18, 2023

Reproduction test script:

# cli.rb
require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem "optparse", github: "ruby/optparse"
end

require "optparse"

options = OptionParser.new do |opts|
  opts.require_exact = true

  opts.on("-v", "--[no-]verbose", "Run verbosely") do
    # ...
  end
end

options.parse!
▶ ruby cli.rb --help
Fetching https://github.com/ruby/optparse.git
Resolving dependencies...
/Users/fatkodima/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/bundler/gems/optparse-667ab35f593f/lib/optparse.rb:1645:in `block in parse_in_order': undefined method `include?' for nil:NilClass (NoMethodError)

            if require_exact && !sw.long.include?(arg)
                                        ^^^^^^^^^
	from /Users/fatkodima/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/bundler/gems/optparse-667ab35f593f/lib/optparse.rb:1636:in `catch'
	from /Users/fatkodima/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/bundler/gems/optparse-667ab35f593f/lib/optparse.rb:1636:in `parse_in_order'
	from /Users/fatkodima/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/bundler/gems/optparse-667ab35f593f/lib/optparse.rb:1630:in `order!'
	from /Users/fatkodima/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/bundler/gems/optparse-667ab35f593f/lib/optparse.rb:1739:in `permute!'
	from /Users/fatkodima/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/bundler/gems/optparse-667ab35f593f/lib/optparse.rb:1764:in `parse!'
	from cli.rb:22:in `<main>'

@nobu nobu merged commit 4e346ad into ruby:master Feb 9, 2024
30 checks passed
@fatkodima fatkodima deleted the fix-require_extract-and-no-combo branch February 9, 2024 10:46
nobu added a commit to nobu/ruby that referenced this pull request Feb 10, 2024
nobu added a commit to nobu/ruby that referenced this pull request Feb 10, 2024
matzbot pushed a commit to ruby/ruby that referenced this pull request Feb 10, 2024
nobu added a commit that referenced this pull request Feb 10, 2024
To work with options defined as `--[no]-something`.

Fix https://bugs.ruby-lang.org/issues/20252
Fix #60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants