为所有任务 Rake 自定义参数? [英] Rake custom arguments for all tasks?

查看:29
本文介绍了为所有任务 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.

这篇关于为所有任务 Rake 自定义参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆