console.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """
  2. Console/terminal user interface functionality.
  3. """
  4. from fabric.api import prompt
  5. def confirm(question, default=True):
  6. """
  7. Ask user a yes/no question and return their response as True or False.
  8. ``question`` should be a simple, grammatically complete question such as
  9. "Do you wish to continue?", and will have a string similar to " [Y/n] "
  10. appended automatically. This function will *not* append a question mark for
  11. you.
  12. By default, when the user presses Enter without typing anything, "yes" is
  13. assumed. This can be changed by specifying ``default=False``.
  14. """
  15. # Set up suffix
  16. if default:
  17. suffix = "Y/n"
  18. else:
  19. suffix = "y/N"
  20. # Loop till we get something we like
  21. while True:
  22. response = prompt("%s [%s] " % (question, suffix)).lower()
  23. # Default
  24. if not response:
  25. return default
  26. # Yes
  27. if response in ['y', 'yes']:
  28. return True
  29. # No
  30. if response in ['n', 'no']:
  31. return False
  32. # Didn't get empty, yes or no, so complain and loop
  33. print("I didn't understand you. Please specify '(y)es' or '(n)o'.")