Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have Thor complain about misspelled options in a Ruby command line app?

Is it possible to have Thor complain about misspelled/unrecognized command line options?

Example:

maid --slient  # Oops!  Misspelled.  It should give a warning or usage info.
maid --silent  # Do the behavior I programmed for the "silent" option.

Thor is really nice, but it isn't too helpful for me if it just ignores input it doesn't know how to handle. Maid also has an option to specify a file of Maid rules like so:

maid --rules=rules.rb  # Good
maid -r rules.rb       # Short version
maid rules.rb          # Oops!  That's not valid.  It should give a warning or usage info.

What can I do to make Thor complain in the two cases above?

The code for the Maid gem is on GitHub at http://github.com/benjaminoakes/maid

like image 241
Benjamin Oakes Avatar asked May 25 '11 12:05

Benjamin Oakes


1 Answers

I got a tweet from Yehuda Katz. (Thanks again!) Here's the solution:

class YourApp < Thor
  check_unknown_options!
  # ...
end

I tested and added it into my project. Here's the new behavior:

$ maid --slient
Unknown switches '--slient'

$ maid rules.rb
Could not find task "rules.rb".

See the full code on GitHub.

like image 71
Benjamin Oakes Avatar answered Sep 28 '22 16:09

Benjamin Oakes