Suffix

Rake and multiple arguments

Beware of spaces when passing Rake arguments.

Rake has a new syntax for passing arguments. In the old version it looked something like this:

task :hello, :first_name, :last_name, :needs => :environment do |t, args|
  puts "Hello #{first_name} #{last_name}"
end

Running this task in a newer version of Rake shows a deprecation warning:

WARNING: 'task :t, arg, :needs => [deps]' is deprecated. Please use 'task :t, [args] => [deps]' instead.

Luckily it’s easy to switch to the newer syntax:

task :hello, [:first_name, :last_name] => :environment do |t, args|
  puts "Hello #{first_name} #{last_name}"
end

A word of caution

I took me some time to figure out why my rake task wouldn’t accept multiple parameters. All I got was the following error:

Don't know how to build task 'greeter:hello[John,'

This, of course, has nothing to do with the new syntax as I thought but the space between my arguments on the command line.

rake greeter:hello[John, Doe] # wrong, notice the space
rake greeter:hello[John,Doe] # right

Standard UNIX stuff, I know, but still… sneaky.