---
title: How To Use Arguments In a Rake Task
teaser:
tags: web,ruby,unix
author: Chad Pytel
published_on: 2012-02-23
---

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

```sh
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|
        Tweet.send(args[:username])
      end
    end

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

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

So you'll need to run it like this:

```sh
rake tweets:send\[cpytel\]
```

Or this:

```sh
rake 'tweets:send[cpytel]'
```

However, this is controlled by [the `NOMATCH` zsh option][nomatch]:

[nomatch]: http://zsh.sourceforge.net/Doc/Release/Options.html#index-NOMATCH

> 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:

```sh
unsetopt nomatch
rake tweets:send[cpytel]
```

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

[nomatch-dotfiles]: https://github.com/thoughtbot/dotfiles/pull/194
[dotfiles]: https://github.com/thoughtbot/dotfiles
