Last Updated: February 25, 2016
·
1.633K
· javierg

Use of arguments in rake tasks

Usually if you want to add arguments to a rake task the best way to do it is through environment variables which are accesible to rake through the global ENV.

But sometimes it makes sense to pass some of those arguments through the command line. For doing that you just need to call your rake task as:

rake your:fancy:my_task ARGUMENT=1 OTHER_ARGUMENT=2

Then on your task code, you can access them as if they where (as it turns out they actually are) environment variables:

namespace :your do
  namespace :fancy do
    task :my_task => :environment do
         first_argument = ENV["ARGUMENT"]
         second_argument = ENV["OTHER_ARGUMENT"]
    end
  end
end