班级内的托尔任务清单?

发布于 2025-01-06 00:59:13 字数 744 浏览 0 评论 0原文

我已经查遍了(谷歌搜索),但我一生都无法弄清楚如何做到这一点。是否可以获取当前类中定义的雷神任务列表?我正在尝试编写一个方法来确定传递给 thor 的参数是否有效,为此,我需要所有已定义任务的列表。我可以在某个常量中创建一个列表,但如果可能的话,我宁愿使用内置工具。

前任:

    #!/usr/bin/env ruby
    require 'thor'

    class Foo < Thor
      desc 'task_1', 'The first task'
      def task_1
        puts 1
      end #task_1

      desc 'task_2', 'The second task'
      def task_2
        puts 2
      end #task_2

      desc 'check_args', 'Checks that the arguments are valid.', :hide => true
        # get a list of the tasks defined in this class and check against ARGV
        if !valid
          invoke :help
          exit
        end #if
      end #check_args
    end #Foo

如果我的问题不够清楚或者我只是把这一切都搞错了,请告诉我:)

谢谢

I have looked ALL over (google searched) and I cannot for the life of me figure out how to do this. Is it possible to get a list of the thor tasks defined in the current class? I'm trying to write a method that determines whether or not the argument(s) passed to thor are valid and to do this, I need a list of all the defined tasks. I could just create a list in some constant but I'd rather use built in tools if possible.

Ex:

    #!/usr/bin/env ruby
    require 'thor'

    class Foo < Thor
      desc 'task_1', 'The first task'
      def task_1
        puts 1
      end #task_1

      desc 'task_2', 'The second task'
      def task_2
        puts 2
      end #task_2

      desc 'check_args', 'Checks that the arguments are valid.', :hide => true
        # get a list of the tasks defined in this class and check against ARGV
        if !valid
          invoke :help
          exit
        end #if
      end #check_args
    end #Foo

If my question isn't clear enough or I am just going about this ALL wrong, please let me know :)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深居我梦 2025-01-13 00:59:13

thor -T 也会列出您的所有任务。

thor -T would list all your tasks as well.

逆流 2025-01-13 00:59:13

当然,在发布这个问题后,我就明白了。

Foo.tasks 将返回一个哈希值 {task_name => [任务信息数组]}

And of course, right after posting this question, I figure it out.

Foo.tasks would return a hash {task_name => [array of task info]}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文