开发者

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/mai开发者_如何学Cd


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜