Published by Simon Schoeters

Rake and multiple 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}"

Running this task in a newer version of Rake shows a depreciation 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}"

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, off 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.