Task Parameters in Rake 0.8.0

Looks like I’m not the only one that’s tired of munging ENV when I pass parameters to rake tasks. Along with a handful of other fixes, Rake 0.8.0 added the ability to pass parameters to your tasks without mucking with environment variables.

What used to be:

# rake invoke COMMAND=foo
task :invoke do
  puts "Invoking #{ENV['COMMAND']}
end

Can become:

# rake invoke[foo]
task :invoke, :command do |task, args|
  puts "Invoking #{args[:command]}"
end

If your task has prerequisites, they come after the parameter names in the declaration:

task :invoke, :command => :prerequisite do |task, args|
  # ...
end

Looks like the args are comma-separated and purely positional, no named or default arguments. It’s possible that there’s a better way to access argument values than this, too: I haven’t run across any documentation yet.


About this entry