prompt_util.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import sys
  2. import os
  3. def clear_screen():
  4. os.system('cls' if os.name=='nt' else 'clear')
  5. def get_input(prompt, accept_blank=True):
  6. try:
  7. while True:
  8. response = raw_input(prompt)
  9. if response == "" and accept_blank==False:
  10. print("A response is required here.\n")
  11. else:
  12. return response
  13. except KeyboardInterrupt:
  14. print("\n\nExiting without saving changes.")
  15. sys.exit(1)
  16. def get_yes_no(prompt, yn_ok=True, default=None):
  17. """Ask the user a Yes or No question.
  18. yn_ok set to True will allow 'y' or 'n' response too.
  19. A default may be specified when the user just presses enter."""
  20. if not prompt.endswith(" "):
  21. prompt += " "
  22. while True:
  23. response = get_input(prompt).lower()
  24. if response == "yes":
  25. return True
  26. elif response == "y" and yn_ok:
  27. return True
  28. elif response == "no":
  29. return False
  30. elif response == "n" and yn_ok:
  31. return False
  32. elif response == "" and default != None:
  33. return default
  34. else:
  35. print("A Yes or No response is required.\n")