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

local switch -v taken as global version switch but "version unknown" #274

Open
yashi opened this issue Feb 15, 2018 · 2 comments
Open

local switch -v taken as global version switch but "version unknown" #274

yashi opened this issue Feb 15, 2018 · 2 comments
Labels

Comments

@yashi
Copy link

yashi commented Feb 15, 2018

Hi,

when I run the following code, I get

$ bundle exec ruby ./ecm.rb bar -v
ecm: version unknown
error: ecm: version unknown

It doesn't even print the defined version number.
I was expecting to see error: Unknown option -v. Is this a bug?

It prints the version number if you put -v before the sub-command, as I expected:

$ bundle exec ruby ./ecm.rb -v bar
ecm.rb version 0.0.1

If a sub-command knows -v, you get it as a local option, as I expected:

$ bundle exec ruby ./ecm.rb foo -v
globals: {"version"=>false, :version=>false, "help"=>false, :help=>false}
options: {"v"=>true, :v=>true}
#!/usr/bin/env ruby

require 'gli'
include GLI::App

program_desc 'A Command to test'
version '0.0.1'

subcommand_option_handling :normal
arguments :strict

desc 'foo command'
command :foo do |c|
  c.desc 'Verbose'
  c.switch :v

  c.action do |global_options, options, args|
    puts "globals: #{global_options}"
    puts "options: #{options}"
  end
end


desc 'bar command'
command :bar do |c|
  c.action do |global_options, options, args|
    puts "globals: #{global_options}"
    puts "options: #{options}"
  end
end

exit run(ARGV)
@davetron5000
Copy link
Owner

Hmm, this does seem like a bug.

@davetron5000
Copy link
Owner

This may not be easily fixable. The problem is that GLI is using the standard library OptionParser under the hood. It hard-codes --version and interprets -v as --version. What's happening is that when you run bundle exec ruby ./ecm.rb bar -v, since bar does not accept -v, that is getting passed to the OptionParser as --version, which uses OptionParser's internal version that GLI doesn't use.

That's all fine, except that OptionParser is calling exit instead of raising an exception.

I'm leaving this open as documentation, but I'm not sure how to fix it.

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

No branches or pull requests

2 participants