简体   繁体   English

Thor 是否抱怨 Ruby 命令行应用程序中的选项拼写错误?

[英]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?是否可以让 Thor 抱怨拼写错误/无法识别的命令行选项?

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. Thor 真的很好,但如果它只是忽略不知道如何处理的输入,它对我来说并没有太大帮助。 Maid also has an option to specify a file of Maid rules like so: Maid 还可以选择指定一个 Maid 规则文件,如下所示:

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?在上述两种情况下,我该怎么做才能让 Thor 抱怨?

The code for the Maid gem is on GitHub at http://github.com/benjaminoakes/maid Maid gem 的代码在 GitHub 上,地址为http://github.com/benjaminoakes/maid

I got a tweet from Yehuda Katz.我收到了 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 .请参阅GitHub 上的完整代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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