Elie Teyssedou
Elie Teyssedou

Reputation: 779

How to pass an option to a Rake task?

I would to know if it was possible to pass an option from the commande line to the task without having the Rakefile taking option for itself.

Here is my task:

task :task do
 args = ARGV.drop(1)
 system("ruby test/batch_of_test.rb #{args.join(' ')}")
 args.each { |arg| task arg.to_sym do ; end }
end

Here is the command I want to be functional:

rake task --name test_first_one

The result I want is the same as this:

ruby test/batch_of_test.rb --name test_first_one

For this code, I get this error:

invalid option: --name

How can --name (or another option) can be passed to my task ?

Upvotes: 1

Views: 2155

Answers (2)

Charlie Egan
Charlie Egan

Reputation: 5163

As far as I know you cannot add flags to Rake tasks. I think the closest syntax available is using ENV variables.

rake task NAME=test_first_one

Then in your task:

name = ENV['NAME']

Upvotes: 2

bosskovic
bosskovic

Reputation: 2054

like this:

task :task, [args] => :environment do |t, args|
 system("ruby test/batch_of_test.rb #{args.join(' ')}")
 args.each { |arg| task arg.to_sym do ; end }
end

and you call it like this.

rake task[args]

UPDATE

task :task, [:options] => :environment do |t, args|
 arg = args[:options].pop
 options = args[:options] 
end

Upvotes: 1

Related Questions