ruby - 雷神 : How can I get my Thor task to display help when I have defined an argument?

标签 ruby thor

我正在将一个小项目转换为使用 Thor,但由于心不在焉,我想使用内置帮助记录可用的任务。 但是,如果我使用参数定义任务,任务级帮助将恢复为类的帮助 - 这意味着不会显示任务描述或预期参数的详细信息。

我希望能够有一个可以用参数而不是参数调用的方法,这样它就可以像这样使用

$ thor broke:foo hello
in a.thor broke:foo arg1=hello

我已将问题归结为以下 thorfile,除了损坏的帮助输出外,它可以正常工作。我已经删除了任何其他参数,因为它们对问题没有任何影响。 第一个任务 ok:foo 将正常显示帮助,第二个任务 broke:foo 帮助不大:

class Ok < Thor
    desc "foo", "ok test2"
    def foo
        puts "in a.thor ok:foo\n"
    end
end
class Broke < Thor
    argument :arg1, :type=>:string, :desc => "arg1"
    desc "foo", "broke test1"
    def foo
        puts "in a.thor broke:foo arg1=#{self.arg1}\n"
    end
end

请求帮助 ok:foo 任务方法给出:

$ thor help ok:foo
    Usage:
      thor ok:foo

    ok test

请求帮助 broke:foo 任务的帮助不大:

$ thor help broke:foo
    Tasks:
      thor broke:foo ARG1          # broke test1
      thor broke:help ARG1 [TASK]  # Describe available tasks or one specific task

如何定义参数并显示正确的任务帮助?

最佳答案

你的 thorfile 中有一个小错误。

您有参数而不是method_option

这里是正确的版本:

class Ok < Thor
    desc "foo", "ok test2"
    def foo
        puts "in a.thor ok:foo\n"
    end
end
class Broke < Thor
    method_option :arg1, :type=>:string, :desc => "arg1"
    desc "foo", "broke test1"
    def foo
        puts "in a.thor broke:foo arg1=#{self.arg1}\n"
    end
end

Wiki about method_option on github.com

关于ruby - 雷神 : How can I get my Thor task to display help when I have defined an argument?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11040420/

相关文章:

ruby-on-rails - 测试中的模拟和 stub

ruby - 为什么 Rails 生成器中出现 'bundle install' 失败?

ruby - Thor 是否提示过 Ruby 命令行应用程序中拼写错误的选项?

python - 对于命令行应用程序,什么是 python 替代 thor (ruby)?

ruby - 在堆栈/流对象中存储信息

ruby-on-rails - 如何在Ruby中正确操作动态超链接?

Ruby:打印任意方法的代码(并在上下文中执行)

ruby-on-rails - 无法使用代理选项安装 gem

ruby - 为什么 Thor 具有 no_tasks 方法?

ruby - 在 Ruby gem 中使用 Thor 作为生成器