How to pass arguments to rake tasks?

less than 1 minute read

We use rake tasks for automating various repetitive tasks. Sometimes, we may need to pass arguments to these tasks to make them more flexible and dynamic.

Here’s how you can pass arguments to Rake task:

Syntax

namespace :my_namespace do
  desc "My task description"
  task :my_task, [:arg1, :arg2] => :environment do |_, args|
    args.with_defaults(arg1: "default1", arg2: "default2")

    puts "Argument 1: #{args.arg1}"
    puts "Argument 2: #{args.arg2}"
  end
end

Example

namespace :data do
  desc "Syns data from external source"
  task :sync, [:limit] => :environment do |_, args|
    args.with_defaults(limit: 100)

    DataSyncService.sync(limit: args.limit.to_i)
  end
end

In this example, the data:sync task accepts a :limit argument. It sets a default value of limit to 100 using with_defaults.

The task then calls a DataSyncService with limit as an argument.

To run the task, use the following command:

rake data:sync[50]

NOTE: If you are a ZSH user you may get the following error:
zsh: no matches found: data:sync[50]
In that case, you can escape the square brackets like this:

rake data:sync\[50\]

References