Last Updated: February 25, 2016
·
8.099K
· ianrgz

Passing parameters to Rake tasks

Ever wonder how you can pass parameters to a rake task from the command line, well here is a quick tip on doing so.

Let's say we have a Rake task that clones a repository from github(a bit redundant but it serves well for the purpose of this tip):

namespace :git do
  desc "let's you clone a git repo by passing username and repo"
  task :clone, :user, :repo do |t, args|
    user = args[:user]
    repo = args[:repo]
    if system "git clone git@github.com:#{user}/#{repo}.git"
      puts "Repository: #{repo} cloned successfully"
    else
      puts "There was a problem with your request, please try again later."
    end
  end
end

Now from the command line you can call this task by running:

rake git:clone[someuser,somerepo]

Hope you find it useful.