如何传递命名参数为Rake任务? [英] How can I pass named arguments to a Rake task?

查看:141
本文介绍了如何传递命名参数为Rake任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来传递命名参数为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屋!

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