Rake custom arguments for all tasks?
I want to pass in an argument to rake independe开发者_如何学JAVAnt 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?
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.
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
精彩评论