ruby - 如何将 --help, -h 标志添加到 Thor 命令?

我在 Ruby 可执行文件中创建了一个 Thor 类,它在使用 ./foo help bar 时正确显示了帮助。

为了让它更直观(为了我的用户的理智),我还想支持 ./foo bar --help./foo bar -h。当我这样做时,我得到:

ERROR: "foo bar" was called with arguments ["--help"]
Usage: "foo bar"

我可以手动执行 method_option :help, ... 并在 bar 方法中处理它,但我希望有更简单的方法来做到这一点(重定向该命令到 ./foo help bar).

有谁知道一个简单易行的方法吗?

最佳答案

假设 Foo 是您继承自 Thor 的类,您可以在 Foo.start 之前的某处调用以下代码:

help_commands = Thor::HELP_MAPPINGS + ["help"]
# => ["-h", "-?", "--help", "-D"]

if help_commands.any? { |cmd| ARGV.include? cmd }
  help_commands.each do |cmd|
    if match = ARGV.delete(cmd)
      ARGV.unshift match
    end
  end
end

与其进入 Thor 并修补一些方法以具有不同的 ARGV 解析行为,不如通过将任何帮助命令移动到列表的前面来作弊。

https://stackoverflow.com/questions/49042591/

相关文章:

python - 如何在 Python 中将加权边列表转换为邻接矩阵?

sql - 在PostgreSQL中获取2个字符之间的子字符串

google-cloud-platform - 在 Google Cloud Storage 上恢复

javafx - 如何在 JavaFX 中设置默认关闭操作?

python - "invalid character ' u ' looking for begi

javascript - 如何将 jointjs 与 reactjs 一起使用?

android-studio - "APK was defined multiple times",

sql - 带有窗口函数的 CASE 语句

python - 需要字段的 django rest 框架错误

ios - 更改模型后的核心数据迁移