如何传递命名参数为Rake任务? [英] How can I pass named arguments to a Rake task?
问题描述
有没有一种方法来传递命名参数为Rake任务,而无需使用环境变量?
Is there a way to pass named arguments to a Rake task without using environment variables?
据我所知,Rake任务可以用两种格式接收参数:
I am aware that Rake tasks can accept arguments in two formats:
环境变量
$ rake my_task foo=bar
这将创建一个环境变量名为富
和值栏,可以在Rake任务进行访问
my_task
按 ENV ['富']
。
Rake任务参数
$ rake my_task['foo','bar']
这传递值富
和栏
前两个任务参数(如果它们被定义)。如果 my_task
被定义为:
This passes the values foo
and bar
to the first two task arguments (if they are defined). If my_task
were defined as:
task :my_task, :argument_1, :argument_2
然后 ARGUMENT_1
将具有值富
和 ARGUMENT_2
将具有值栏
。
推荐答案
您可以说这样的事情:
rake some_task -- --arg=value
然后,你的任务里, ARGV
将
[ 'some_task', '--arg=value' ]
所以你可以使用<一个href=\"http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html\">OptionParser (或其他选项的解析器)来解压缩 ARGV
就像任何旧CLI脚本;有趣的看 -
要保持耙从试图解析 - 精氨酸=像
作为流氓的开关。
so you could use OptionParser (or some other option parser) to unpack ARGV
just like in any old CLI script; the funny looking --
is necessary to keep rake from trying to parse --arg=like
as a rake switch.
您很可能与标准的环境变量的做法更好,这不是因为所有的丑 -
的东西,它是传递参数通常的方式耙任务。
You're probably better off with the standard environment variable approach, it isn't as ugly as all the --
stuff and it is the usual way of passing arguments to rake tasks.
这篇关于如何传递命名参数为Rake任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!