# Rake task with arguments

**URL:** <https://www.ruby-forum.com/t/rake-task-with-arguments/186765>\
**Category:** Ruby\
**Created:** [February 25, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/rake-task-with-arguments/186765 "2008-02-25T21:42:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [February 25, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/rake-task-with-arguments/186765/1 "2008-02-25T21:42:41Z")

</div>

I found an example ([http://www.betweentherails.com/rake/](http://www.betweentherails.com/rake/)) of passing  
arguments to a rake task in the new (0.8.n) version of rake. From this  
example, I created the following test:

namespace :foo do  
desc ‘lol’  
task :bar, :num do |t, args|  
puts “num = #{args.num}”  
end  
end

I took a look at the task list:

$ rake --tasks  
(in /path/to/my/dir)  
rake foo:bar[num] # lol

All looks well … until I try to run it:

$ rake foo:bar[123]  
rake: No match

Hmm … let’s try without the argument:

$ rake foo:bar  
(in /path/to/my/dir)  
num =

o.O

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [February 25, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/rake-task-with-arguments/186765/2 "2008-02-25T21:45:38Z")

</div>

On Feb 25, 2:39 pm, Reacher [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hmm .. let’s try without the argument:
> 
> $ rake foo:bar  
> (in /path/to/my/dir)  
> num =
> 
> o.O

I figured it out

$ rake foo:bar[123]  
(in /path/to/my/dir)  
num = 123

BTW, csh is evil

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [February 25, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/rake-task-with-arguments/186765/3 "2008-02-25T21:50:58Z")

</div>

On Tue, Feb 26, 2008 at 05:39:57AM +0900, Reacher wrote:

> All looks well … until I try to run it:
> 
> $ rake foo:bar[123]  
> rake: No match

Try this instead:

$ rake ‘foo:bar[123]’

The shell is interpreting the 's as globbing metacharacters. You have  
to  
quote them so the shell passes them to ruby as-is.

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [February 25, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/rake-task-with-arguments/186765/4 "2008-02-25T22:08:37Z")

</div>

Jos B. wrote:

> On Tue, Feb 26, 2008 at 05:39:57AM +0900, Reacher wrote:
> 
> > All looks well … until I try to run it:
> > 
> > $ rake foo:bar[123]  
> > rake: No match
> 
> Try this instead:
> 
> $ rake ‘foo:bar[123]’

Yuck.

FWIW, you can also embed arguments in the task name, which makes the  
command line cleaner. This is yucky in its own special way.

$ cat rakefile  
foo\_task\_pat = /^foo(\w+)$/

make\_foo\_dep\_name =  
proc do |taskname|  
“foo/#{taskname[foo\_task\_pat, 1]}”  
end

rule foo\_task\_pat =\> make\_foo\_dep\_name do |t|  
puts “handling rule for #{t.name.inspect}”  
end

directory “foo”

file “foo/bar” =\> “foo” do  
system “touch foo/bar”  
end  
$ rm -rf foo  
$ rake foobar  
(in /home/vjoel/ruby/misc/rake/args)  
handling rule for “foobar”  
$ ls foo  
bar

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [February 25, 2008, 10:24pm UTC](https://www.ruby-forum.com/t/rake-task-with-arguments/186765/5 "2008-02-25T22:24:01Z")

</div>

On Tue, Feb 26, 2008 at 06:07:38AM +0900, Joel VanderWerf wrote:

> Jos B. wrote:
> 
> > $ rake ‘foo:bar[123]’
> 
> Yuck.

Hey, using 's in rake wasn’t my idea…

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [February 25, 2008, 10:45pm UTC](https://www.ruby-forum.com/t/rake-task-with-arguments/186765/6 "2008-02-25T22:45:53Z")

</div>

On Feb 25, 2008, at 3:44 PM, Reacher wrote:

> I figured it out
> 
> $ rake foo:bar[123]  
> (in /path/to/my/dir)  
> num = 123
> 
> BTW, csh is evil

Of course csh is evil! That’s nothing new.

> **[Csh Programming Considered Harmful](http://ooblick.com/text/CshProgrammingConsideredHarmful.html)**

This works just fine with bash:

rab://tmp $ cat Rakefile  
namespace :foo do  
desc ‘lol’  
task :bar, :num do |t, args|  
puts “num = #{args.num}”  
end  
end  
rab://tmp $ rake foo:bar[123]  
(in /private/tmp)  
num = 123

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [February 25, 2008, 11:05pm UTC](https://www.ruby-forum.com/t/rake-task-with-arguments/186765/7 "2008-02-25T23:05:55Z")

</div>

On Feb 25, 3:44 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Of course csh is evil! That’s nothing new.[Csh Programming Considered Harmful](http://ooblick.com/text/CshProgrammingConsideredHarmful.html)  
> rab://tmp $ rake foo:bar[123]  
> (in /private/tmp)  
> num = 123
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

When I got my first real job programming, I had no experience with  
\*NIX .. at all. The shell we worked in was tcsh. Currently, everyone  
in our office uses ksh, but I’ve been slow to conform, since I’m used  
to tcsh and it’s few but handy niceties. I think the results of this  
thread may be the poke needed to move to bash.

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [December 9, 2009, 5:08pm UTC](https://www.ruby-forum.com/t/rake-task-with-arguments/186765/8 "2009-12-09T17:08:00Z")

</div>

Reacher wrote:

> Currently, everyone  
> in our office uses ksh, but I’ve been slow to conform, since I’m used  
> to tcsh and it’s few but handy niceties. I think the results of this  
> thread may be the poke needed to move to bash.

Skip bash and go to straight to zsh. You won’t regret it.
