It would be great to have a built in helper to prompt the user for yes/no confirmation, and only continue on yes. I am going to build something for my own use that does this, and I will post my code here if you think it would be suitable to be included.
from distutils.util import strtobool
from invoke.exceptions import ParseError
def confirm(ctx, prompt='Continue?\n', failure_prompt='User cancelled task'):
'''
Prompt the user to continue. Repeat on unknown response. Raise
Failure Exception on negative response
'''
response = raw_input(prompt)
try:
response_bool = strtobool(response)
except ValueError:
# If there is a preferred way to print to the command line, I would love to know
print 'Unkown Response. Confirm with y, yes, t, true, on or 1; cancel with n, no, f, false, off or 0.'
confirm(ctx, prompt, failure_prompt)
if not response_bool:
# Unsure if this is the right Exception to raise here.
raise ParseError(failure_prompt)
How does that look? Is this out of the scope for this project?
Slightly confused, you set this up as if it was a task (i.e. to be invoked like invoke confirm othertask) but I don't see how that makes a bunch of sense? What's the use case?
Very similar is this confirm() function from Fabric's contrib.console module, but that one is intended for use within your task bodies. That use case makes more sense to me than a 'tasky' implementation and I'd probably be OK adding something similar to either invoke core or https://github.com/pyinvoke/invocations (again granted that it's not actually a task, just a function.)
Intrigued by strtobool though, wasn't aware of that guy.
You are right, I didn't need to include the ctx when calling this task. I was just in the habit of passing it into functions, but it really is not necessary for confirm. Without the ctx it would look like:
def confirm(prompt='Continue?\n', failure_prompt='User cancelled task'):
'''
Prompt the user to continue. Repeat on unknown response. Raise
ParseError on negative response
'''
response = raw_input(prompt)
try:
response_bool = strtobool(response)
except ValueError:
print 'Unkown Response. Confirm with y, yes, t, true, on or 1; cancel with n, no, f, false, off or 0.'
confirm(prompt, failure_prompt)
if not response_bool:
raise ParseError(failure_prompt)
I found the strtobool thing on http://stackoverflow.com/a/18472142/907060.
If you want, I can create a pull request on pyinvoke/invocations
Most helpful comment
If you want, I can create a pull request on pyinvoke/invocations