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 wa
You could also wrap this up in a service class so it can be unit-tested and used across your rake tasks:
# frozen_string_literal: true
class RakeConfirmDialog
def initialize(question)
@question = "#{question} (y/n)"
@answer = "".inquiry
end
def confirm!
prompt until (proceed? || abort?)
respond
proceed?
end
private
def prompt
STDOUT.puts @question
@answer = STDIN.gets.strip.downcase.inquiry
end
def respond
STDOUT.puts proceed? ? "Proceeding." : "Aborting."
end
def proceed?
@answer.y?
end
def abort?
@answer.n?
end
end
Then use it like so in your task:
next unless RakeConfirmDialog.new(
"About to close the Hellmouth forever. Are you sure you want 'Buffy the Vampire Slayer' to have a happy ending?"
).confirm!