Search code examples
rubythor

How can I retrieve the current command name in a Thor method?


I have several Thor tasks depending on each other. I'd like to have access to the name of the one currently running so that I can use it in the logger messages.

I can use the self object to find the task name but that's a Thor::Sandbox::MyClass object and that piece of information is deeply positioned. Does anybody know a getter? Something like:

class MyCLI < Thor
  def hello
    puts "hello, I am the task #{self.taskname}"
  end
end

Solution

  • May be this would work:

    def fun
     puts __method__
    end
    
    fun
    #=> :fun
    

    Also look in the caller method. It returns the current execution stack as a string.