Hamza

Khan-Cheema


Rake and command line arguments with Rails

I needed to make a Rake task in Rails to take a command line argument alongside the Rails environment.

It is not that obvious, so I thought I would post a simple example.


Rake Task

desc "Task description"
task :task_name, [:arg_name] => :environment do |t , args|
  puts "#{args.arg_name}"
end

Here’s how it is called:

rake task_name[arg_name]

It will also read in the RAILS_ENV environment.

I know it is not the best syntax, but that is how it is currently implemented in Rake.

Picture taken from tanakawho

Comments

blog comments powered by Disqus