I want to run a Rake task that asks the user for input.
I know that I can supply input on the command line, but I want to ask the user if they are sure they want to proceed with a particular action in case they mistyped one of the values supplied to the Rake task.
Here's an example without using another task.
You could also wrap this up in a service class so it can be unit-tested and used across your rake tasks:
Then use it like so in your task:
A handy feature for user input is to put it in a
do..while
loop, to only continue when a user has supplied valid input. Ruby doesn't explicitly have this construct, but you can achieve the same thing withbegin
anduntil
. That would add to the accepted answer as follows:Something like this might work
Task reenabling and invoking from How to run Rake tasks from within Rake tasks?