为所有任务 Rake 自定义参数?

为所有任务 Rake 自定义参数?

问题描述:

我想传递一个参数来独立于我运行的任务.

I want to pass in an argument to rake independent of the task I run.

例如:

rake my_arg=foo
rake my_arg=foo :install
rake my_arg=foo :upgrade
rake my_arg=foo :bar

有没有办法做到这一点?

Is there a way to do this?

您可以像这样发送参数:

You can send arguments in like this:

rake some_task arg1=value arg2=value

然后从您的 rake 任务中的 ENV 中提取命名参数:

Then pull the named parameters out of ENV inside your rake task:

arg1 = ENV['arg1']
arg2 = ENV['arg2']

您还可以提供更传统的命令行开关,如下所示:

You can also supply more traditional command line switches like this:

rake some_task -- --arg1=value --arg2=value

然后使用 OptionParser(或一些其他选项解析器)来解压 ARGV.如果您想使用开关,请不要忘记额外的 --,它会告诉 rake 停止将命令行解析为开关.

And then use OptionParser (or some other option parser) to unpack ARGV. Don't forget the extra -- if you want to use switches, that will tell rake to stop parsing the command line as switches.