Skip to content

Invocations

chikamichi edited this page Aug 2, 2011 · 9 revisions

Thor comes with a invocation-dependency system as well, which allows a task to be invoked only once. For example:

class Counter < Thor
  desc "one", "Prints 1 2 3"
  def one
    puts 1
    invoke :two
    invoke :three
  end

  desc "two", "Prints 2 3"
  def two
    puts 2
    invoke :three
  end

  desc "three", "Prints 3"
  def three
    puts 3
  end
end

When invoking the task one:

thor counter:one

The output is "1 2 3", which means that the three task was invoked only once. You can even invoke tasks from another class, so be sure to check the documentation documentation for Thor class.

Notice invocations do not share the same object. I.e, Thor will instantiate Counter once to invoke the task one, then, it instantiates another to invoke the task two and another for task three. This happens to allow options and arguments to be parsed again. For example, if two and three have different options and both of them were given to the command line, calling invoke makes them be parsed each time and used accordingly by each task.

Clone this wiki locally