问题
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