rails / thor

Thor is a toolkit for building powerful command-line interfaces.
http://whatisthor.com/
MIT License
5.12k stars 553 forks source link

Extra options become parameters #725

Open zrewald opened 4 years ago

zrewald commented 4 years ago

🌈 (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:

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 commented 2 years ago

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.