views:

3370

answers:

4

So I am able to pass in arguments as follows

desc "Testing args"
task: :hello, :user, :message do |t, args|
  args.with_defaults(:message => "Thanks for logging on")
  puts "Hello #{args[:user]}. #{:message}"
end

I am also able to load the current environment for a Rails application

desc "Testing environment"
task: :hello => :environment do 
  puts "Hello #{User.first.name}."
end

What I would like to do is be able to have variables and environment

desc "Testing environment and variables"
task: :hello => :environment, :message do |t, args|
  args.with_defaults(:message => "Thanks for logging on")
  puts "Hello #{User.first.name}. #{:message}"
end

But that is not a valid task call. Does anyone know how I can achieve this?

+1  A: 

Take a look at this article by Jay Fields: http://blog.jayfields.com/2006/10/rake-tasks-with-parameters.html

semanticart
I had actually come across that article before. His solution is to use environment variables. i.e. setting ENV['message'] on the command line by running the command - rake hello message="My Message". That is a good solution but I am interested in passing variables in directly so that I do not have to worry about stomping on other ENV variables. In the example above ENV['username'] happens to be set in my shell so if no username is passed in then it will take my own username by default, a side effect I do not want.
Will
Another solution is to prompt the user for the username and read it with STDIN.gets.chomp. But I may not want an interactive task.
Will
The way Rake handles arguments has changed since 2006. Take a look also at the most recent release notes, for example: http://rake.rubyforge.org/files/doc/release_notes/rake-0_8_3_rdoc.html
Telemachus
+5  A: 

When you pass in arguments to rake tasks, you can require the environment using the :needs option. For example:


desc "Testing environment and variables"
task :hello, :message, :needs => :environment do |t, args|
  args.with_defaults(:message => "Thanks for logging on")
  puts "Hello #{User.first.name}. #{:message}"
end
hgimenez
Note that the most current docs describe the `:needs` method this way: "That format is still supported for compatability, but it is not recommended for use." (I mention this only for future browsers, since it suggests that eventually `:needs` won't be supported any longer. The section is called "Deprecated Task Paramaters Format"...)
Telemachus
+3  A: 

Just for completeness, here the example from the docs mentioned above:

   task :name, [:first_name, :last_name] => [:pre_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}"
   end

Notes:

  • You may omit the #with_defaults call, obviously.
  • You have to use an Array for your arguments, even if there is only one.
  • The prerequisites do not need to be an Array.
  • args is an instance of Rake::TaskArguments.
  • t is an instance of Rake::Task.
mikezter