简体   繁体   English

如何将选项传递给Rake任务?

[英]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. 我想知道是否有可能从命令行将选项传递给任务,而无需Rakefile接受选项。

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 ? --name(或其他选项)如何传递给我的任务?

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 UPDATE

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

As far as I know you cannot add flags to Rake tasks. 据我所知,您不能将标记添加到Rake任务中。 I think the closest syntax available is using ENV variables. 我认为最接近的语法是使用ENV变量。

rake task NAME=test_first_one

Then in your task: 然后在您的任务中:

name = ENV['NAME']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM