【问题标题】:Have Thor complain about misspelled options in a Ruby command line app?Thor 抱怨过 Ruby 命令行应用程序中的选项拼写错误吗?
【发布时间】:2011-05-25 12:58:28
【问题描述】:

是否可以让 Thor 抱怨命令行选项拼写错误/无法识别?

例子:

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

Thor 真的很好,但如果它只是忽略不知道如何处理的输入,对我来说并没有太大帮助。 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.

在上述两种情况下,我该怎么做才能让 Thor 抱怨?

Maid gem 的代码在 GitHub 上http://github.com/benjaminoakes/maid

【问题讨论】:

    标签: ruby command-line-arguments thor


    【解决方案1】:

    我从 Yehuda Katz 那里得到了a tweet。 (再次感谢!)这是解决方案:

    class YourApp < Thor
      check_unknown_options!
      # ...
    end
    

    我测试并将其添加到我的项目中。这是新的行为:

    $ maid --slient
    Unknown switches '--slient'
    
    $ maid rules.rb
    Could not find task "rules.rb".
    

    the full code on GitHub

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      相关资源
      最近更新 更多