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

Extra options become parameters #725

Open
zrewald opened this issue Apr 21, 2020 · 1 comment
Open

Extra options become parameters #725

zrewald opened this issue Apr 21, 2020 · 1 comment

Comments

@zrewald
Copy link

zrewald commented Apr 21, 2020

🌈 (Not sure if that's needed for issues, but whatever)

Hey, All.

I was giving Thor a test run, and noticed that options that are not specified for a method are included in the parameters passed to it.

Version deets:

  • Ruby: 2.6.0
  • Thor: 1.0.1

Here's a basic file that will cause the issue (com_test.rb):

require 'thor'

class CLI < Thor
  def self.exit_on_failure?
    true
  end
  
  desc "hello NAME", "say hello to NAME"
  option :to, :type => :string
  def hello(*params)
    puts params.to_s
    puts options
  end
end

CLI.start(ARGV)

Results:

$ ruby com_test.rb hello one two three
["one", "two", "three"]
{}
$ ruby com_test.rb hello --to you
[]
{"to"=>"you"}

Here's where things get weird.

$ ruby com_test.rb hello --to you --from me
["--from", "me"]
{"to"=>"you"}

What is the reasoning behind this behavior, as opposed to silently ignoring the extra option or throwing an exception?

I took a glance through the docs for anything regarding a situation like this, but came up empty-handed. Apologies if this is clearly spelled out somewhere in there.

Thanks!

@patrick-motard
Copy link

I believe this only works when you have def hello(*params). It won't work when you have def hello. With *params you're effectively saying "Send any additional input to the method for this command. I want to use them for something.". Given that, it's a bit strange to wonder why the framework is doing what you're telling it to do.

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

No branches or pull requests

2 participants