Thor is a simple and efficient tool for building self-documenting command line utilities. I’m a shell guy, I use a terminal everyday and most of my job is done in my favorite text editor or in a terminal. To be effective I need to have a suitable text editor and the ability to automate most of the things I do in the terminal.
Here comes Thor. Thor will help you to script your common actions and command-lines. Rather than having five commands to do a recurent job why just not having one?!
Thor will remove the pain of having to handle command-line options parsing, documentation and tasks dependencies. You’ll be able to use Thor on a given project or even system-wide. It’s a system-wide Rake on steroïds which ease command-line options parsing, inline help generation, file-system manipulation, templating, …
This post will show you how you can use Thor to simplify your daily tasks. We’ll see all Thor capabilities and show some simple but real-world use cases.
Last but not least, Thor was written by Yehuda Katz.
Let’s go ! Install it via
gem install thor
To create tasks you need a Thorfile (Rakefile equivalent) which will include our tasks code.
A simple example would be:
Now you can try
thor list ou
thor -T to see available tasks:
test ---- thor test:hi # Say hi
Tasks can take arguments. This way you don’t need to use environment variables anymore as you’re used to with Rake:
Now help displays:
$ thor help test:hi Usage: thor test:hi NAME Say hi
Let’s try our new task:
$ thor test:hi Nico Hi Nico !
What we’ve defined is a parameter so it is mandatory. If you forget a mandatory parameter, Thor will complain about it:
"hi" was called incorrectly. Call as "thor test:hi NAME".
You’ll often need “optionnal parameters”. Options can be added this way:
Give it a try:
$ thor test:hi Nico -v
Hi Nico ! How are you today?
Options can be defined with a default, given type, …:
method_option :path, :type => :string, :default => "~/", :required => false, :aliases => "-p"
Available types are:
Add task dependencies
Thor allows to invoke a task from within another task so you can chain task calls and define dependencies:
Thor::Group class can achieve the same goal calling each method of the group, one by one, in definition order:
Interacting with user
Thor bundles some methods which ease interacting with user. Here are some:
To be able to use it you’ll need to include
Thor::Actions in your class.
You should really take a look at these methods documentation which will save you some time.
Sometimes you’ll need to have your tasks defined in a namespace to ensure there’s no collision with other tasks. No problem:
One the most interesting feature of Thor is that a task can be installed system-wide. It means that you don’t need to have a Thorfile in your current directory to be able to call a task :
thor install Thorfile
You can now use tasks defined in your Thorfile from anywhere!
Let’s say your working on a Rails app you often need to clone and setup maybe because there’s a lot of developers on the project or maybe you need multiple repos to test things …
You can easily extend this example and use it everywhere by installing it system-wide rather than adding the same Rakefile to each project.
Such a framework allows you to easily create automated tasks, generators and more. It can even be a basis for a command-line interface to one of your lib and saves you from writing boring command-line parser.
You should really give it a try if you find yourself doing the same thing again and again in your terminal. It’s super easy to setup and all needed tools are bundled. If you want to improve and speed up your workflow, Thor will help you for sure.