Last Updated: February 25, 2016
· 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{user}/#{repo}.git"
      puts "Repository: #{repo} cloned successfully"
      puts "There was a problem with your request, please try again later."

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

rake git:clone[someuser,somerepo]

Hope you find it useful.