简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM