1

I have a Rake task wherein I am taking two arguments from command line and i need to pass those arguments to the method inside the task. How do I do that?

namespace :auto_sales_commission do
   desc "Process for Daily Summary Commission"
   task :daily_sales_commission,[:arg1, :arg2] => :environment do |t,args|
        #some code
   end

   def get_date(arg1,arg2)
        #some code
   end
end
Niyanta
  • 473
  • 1
  • 9
  • 15
  • I am not really sure why is this a duplicate, since I am not sure how that question can solve my problem. Please elaborate if u can. – Niyanta Jun 03 '15 at 16:41
  • In the first example of the accepted answer, you can see that `args` can be accessed as a hash, so `args[:arg1]` would yield the first argument, and so on. – PinnyM Jun 03 '15 at 16:50

1 Answers1

1

You have access to the parameter like a hash-parameter (1):

task :daily_sales_commission, [:arg1, :arg2] do |t,args|
  #args can be treated like a hash.
  get_date(args[:arg1], args[:arg2]) #<- call your method
end

def get_date(arg1,arg2)
    #some code
end

These parameters are not common command line parameters, you must use them directly as parameters of the rake parameter. The example takes 1 and 2 as parameter values:

rake daily_sales_commission[1,2]

(1) It is not a Hash, it is a Rake::TaskArguments-object. But you can also use the [] to access the parameters.

knut
  • 27,320
  • 6
  • 84
  • 112