Skip to content

Invocations

Brian Fontenot edited this page Jul 7, 2013 · 9 revisions

Thor comes with an invocation-dependency system as well, that 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 for the 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 allows 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 enables them to be parsed each time and used accordingly by each task.

Clone this wiki locally