Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rake custom arguments for all tasks?

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?

like image 886
Misha M Avatar asked Aug 05 '26 16:08

Misha M


2 Answers

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.

like image 148
mu is too short Avatar answered Aug 08 '26 11:08

mu is too short


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
like image 22
Rein Henrichs Avatar answered Aug 08 '26 09:08

Rein Henrichs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!