Search code examples
taskthor

Thor task list from within the class?


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


Solution

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

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