I'm working on a CLI tool powered by the Thor gem. Say I have a Cli
class that inherits from Thor
, which defines a CLI tool that has a few commands associated with it:
class Cli < Thor
def version
# Prints out the version
end
def something
# Does something else
end
def thing
# Does a different thing
end
end
How can I define a method that runs before all commands, or after all commands?
For example, say I create a couple private methods – let's call them setup
and teardown
. How can I make setup
run before version
, something
, and thing
? And how can I make teardown
run after version
, something
, and thing
?
I am hoping there is some sort of way to declare a hook method (similar to Rails' before_action
) so I can avoid manually calling setup
first in every command, and teardown
last in every command.
Looks like there's no built-in way of doing such callbacks in the Thor gem. (I'd think this would be a popular feature, but maybe I'm looking at things the wrong way.)
An above comment points to a third-party solution, thor-hollaback, but I have not tried it.