Rake custom arguments for all tasks?

橙三吉。 提交于 2019-12-10 19:15:42

问题


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

For example:

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?


回答1:


You can send arguments in like this:

rake some_task arg1=value arg2=value

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

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.




回答2:


These arguments are put into ENV[] by rake, simulating environment variables. As such, you can just use actual environment variables instead. For instance:

export my_arg=foo
rake install upgrade bar

Since you can pass a list of rake commands, you could also do this even though it isn't a direct answer to your original question:

rake my_arg=foo install upgrade bar


来源:https://stackoverflow.com/questions/5559319/rake-custom-arguments-for-all-tasks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!