Witold Rugowski on web20 wave with Ruby on Rails

Rake and arguments for tasks

Posted on October 11, 2008 - Filed Under Ruby, RubyOnRails, tips

If you're new here, you may want to subscribe to my RSS feed. You can also get updates by email Thanks for visiting!

Rake is beautiful tool, helping automate many tasks using Ruby. When You want to pass some arguments You can add VAR=value on command line and then in task use ENV['VAR'] to access passed value. But it is not what You alway want. Sometimes You want more compact and clean way to pass arguments.

Starting with Rake 0.8.0 You can create tasks with arguments:

task :name, [:first_name, :last_name] do |t, args|
     args.with_defaults(:first_name => "John", :last_name => "Dough")
     puts "First name is #{args.first_name}"
     puts "Last  name is #{args.last_name}"

Now You can pass some arguments:

C:\>rake name
(in C:/)
First name is John
Last  name is Dough

C:\>rake name[Jim,Weirich]
(in C:/)
First name is Jim
Last  name is Weirich

Cool. I guess I don’t need to explain how arguments and setting default values does work?

However I have noticed some error. First documentation skip needed key in example when task have some dependency on other task (:needs) and argument names should be provided not as an array (for task without dependencies both array and list form are working). Here is example of task with two arguments and one dependency, which does work (I’m using Rake 0.8.3):

task :name, :first_name, :last_name, :needs => [:other] do |t, args|
     args.with_defaults(:first_name => "John", :last_name => "Dough")
     puts "First name is #{args.first_name}"
     puts "Last  name is #{args.last_name}"

task :other  do

What You can do with args?

Here it is modified task from Chad Fowlers Rails Recipes to dump data from database to fixtures. Handy to populate fixtures with some data entered with WWW interface:

desc 'Create YAML test fixtures from data in an existing database.
Defaults to development database. Set RAILS_ENV to override. Use args
table and limit to dump one table and limit number of records'
task :extract_fixtures, :table, :limit, :needs => :environment do |t, args|
  args.with_defaults(:table => nil, :limit => nil)
  limit = args.limit.nil? ? "" : "LIMIT #{args.limit}"
  sql = "SELECT * FROM %s #{limit}"
  skip_tables = ["schema_info" ]
  if args.table.nil?
     tables = ActiveRecord::Base.connection.tables - skip_tables
    tables = [ "#{args.table}"]

  tables.each do |table_name|
    i = "000""#{RAILS_ROOT}/test/fixtures/#{table_name}.yml" , 'w' ) do |file|
      data = ActiveRecord::Base.connection.select_all(sql % table_name)
      file.write data.inject({}) { |hash, record|
        hash["#{table_name}_#{i.succ!}" ] = record

You can dump all fixtures or only single table and limit it to first X entries. I find it useful recipe, I hope it can be used by someone else, too.

Popularity: 26% [?]

Hits for this post: 34941

Similar Posts


9 Responses to “Rake and arguments for tasks”

  1. Technik, Gothic und Anderes on December 5th, 2008 12:10

    links from Technoratibersichtlichen YAML-Format gespeichert werden. Möchte man bestehende Datenbankinhalte als Testdatensätze verwenden, so können Sie direkt in YAML exportiert werden. Ich halte mich an den Beitrag vonNetManiac

  2. mathieu Elie » home on October 30th, 2008 19:06

    links from Technoratietender le shell pour intégrer une completion user defined a des commandes quelconques. A custom Rake task to reset and seed your database Montre également comment appeler d’autres rake tasks.Rake and arguments for tasks : NetManiacCreating Custom Rake Tasks in a Rails Application rails custom rake task. stonean: Custom Rake tasks rake cutom task, facile mais a savoir Easy Automated Snapshot-Style Backups with Rsync

  3. Aimred - Arguments For Rake on October 16th, 2008 20:51

    Kramer auto Pingback[...] it’s possible to pass arguments to rake tasks on the commandline. I first read about it in this article (via and applied it to the article I wrote previously on code generation. All it [...]

  4. //DEVGURU » Blog Archive » Devgurowy przegląd sieci on October 20th, 2008 13:42

    [...] Netmaniac podpowiada jak dodać parametry do tasków Rake’a. [...]

  5. Skaldrom Y. Sarg on December 5th, 2008 10:23

    Thank you very much for this article! As a newbie, it gave me lots of insights.

    Maybe the following info saves some time for other n00bs like me:
    “rake extract_fixtures ‘mymodel’” gives:
    “Don’t know how to build task ‘mymodel’”.

    I think it should be called:
    “rake extract_fixtures['mymodel']“

  6. How do I pass command line arguments to a rake task? - Stack Overflow on May 18th, 2009 16:47

    Kramer auto Pingback[...] found the answer from these two websites: Net Maniac and [...]

  7. rake com argumentos « Tino Gomes on June 15th, 2009 17:15
  8. Rob H on August 25th, 2009 1:16

    Thanks. The :needs element was the key to understanding the new syntax. Other references show it differently and that does not work with the new args syntax.

  9. Jamesw on September 6th, 2009 1:18

    Just wanted to say thanks for this.
    I found the post extremely helpfull whilst creating some rake tasks that needed arguments passing to them.

Leave a Reply