How To Use Arguments In a Rake Task

Chad Pytel

I came across this today. You can write Rake tasks that accept arguments, called like this:

rake tweets:send[cpytel]

You define the rask task like this:

namespace :tweets do
  desc 'Send some tweets to a user'
  task :send, [:username] => [:environment] do |t, args|

Unfortunately, by default zsh can’t parse the call to the rake task correctly, so you’ll see the error:

zsh: no matches found: tweets:send[cpytel]

So you’ll need to run it like this:

rake tweets:send\[cpytel\]

Or this:

rake 'tweets:send[cpytel]'

However, this is controlled by the NOMATCH zsh option:

If a pattern for filename generation has no matches, print an error, instead of leaving it unchanged in the argument list. This also applies to file expansion of an initial ~ or =.

Used like so:

unsetopt nomatch
rake tweets:send[cpytel]

You can unset this in your .zshrc (we’ve set it in our dotfiles, so if you are using ours then you’re safe) without much loss in functionality for the typical case.