Handle Named Arguments In A Rake Task
There are a number of ways to pass arguments into a Rake task. The standard approach only allows for positional arguments. When I need named and optional arguments, my preferred approach is to use environment variables.
Here is a skimmed down version of a user lookup task. Notice the task definition itself doesn't include any arguments. Instead, a couple optional values are extracted from the environment (ENV
) at the beginngin of the task.
This task can be invoked in the following ways:
or even with both arguments included, in which case the task has been written to give precedence to USER_ID
:
Last updated