I have a rake task that needs to insert a value into multiple databases.
I'd like to pass this value into the rake task from the command line, or from another rake task.
How can I do this?
I have a rake task that needs to insert a value into multiple databases.
I'd like to pass this value into the rake task from the command line, or from another rake task.
How can I do this?
options and dependencies need to be inside arrays:
Then
RAILS NOTE:
I use a regular ruby argument in the rake file:
then I stub out the rake tasks at the bottom of the file (since rake will look for a task based on that argument name).
command line:
this feels cleaner to me than the var=foo ENV var and the task args[blah, blah2] solutions.
the stub is a little jenky, but not too bad if you just have a few environments that are a one-time setup
In addition to answer by kch (I didn't find how to leave a comment to that, sorry):
You don't have to specify variables as
ENV
variables before therake
command. You can just set them as usual command line parameters like that:and access those from your rake file as ENV variables like such:
If you want to pass named arguments (e.g. with standard
OptionParser
) you could use something like this:note the
--
, that's necessary for bypassing standard Rake arguments. Should work with Rake 0.9.x, <= 10.3.x.Newer Rake has changed its parsing of
--
, and now you have to make sure it's not passed to theOptionParser#parse
method, for example withparser.parse!(ARGV[2..-1])
exit
at the end will make sure that the extra arguments won't be interpreted as Rake task.Also the shortcut for arguments should work:
When rake scripts look like this, maybe it's time to look for another tool that would allow this just out of box.
I couldn't figure out how to pass args and also the :environment until I worked this out:
And then I call like this: