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

Options with arguments may not be properly removed before executing command #47

Closed
JosephPecoraro opened this issue May 10, 2017 · 1 comment

Comments

@JosephPecoraro
Copy link
Contributor

@JosephPecoraro JosephPecoraro commented May 10, 2017

Options with arguments may not be properly removed before executing command.

Test program:

require 'commander/import'

program :name, 'Name'
program :version, '0.1'
program :description, 'Issue with Global Option parsing.'

default_command :cmd

global_option('--target=STRING', String, 'Specify a target.') do |s|
  puts "Parsed Global Option: #{s}"
end

command :cmd do |c|
  c.action do
    puts 'Success'
  end
end

Example:

$ ruby test.rb --target 1
Parsed Global Option: 1
Success

$ ruby test.rb --target=1
Parsed Global Option: 1
invalid option: --target=1

Here it is clear that the Global Option handler for --target ran and received the expected argument 1. However this global option was not consumed before invoking the sub-command, and produced invalid option. Doing the same thing without an = sign works for both global option parsing and is removed before the sub-command.


Relevant code to look at is remove_global_option which I don't think correctly handles arguments that contains an =. In this case arg is --target=1 and the code tries to remove switches named --target with direct comparisons, which of course fails.

Specifically this part looks fishy:

  args.delete_if do |arg|
    if switches.any? { |s| s[0, arg.length] == arg }
      ...
    end
  end
@JosephPecoraro

This comment has been minimized.

Copy link
Contributor Author

@JosephPecoraro JosephPecoraro commented Mar 12, 2020

Awesome!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

1 participant
You can’t perform that action at this time.