yes_no.py 772 B

123456789101112131415161718192021222324252627282930
  1. # Copyright 2015 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. from __future__ import print_function
  5. import sys
  6. def YesNo(prompt):
  7. """Prompts with a yes/no question, returns True if yes."""
  8. print(prompt, end=' ')
  9. sys.stdout.flush()
  10. # http://code.activestate.com/recipes/134892/
  11. if sys.platform == 'win32':
  12. import msvcrt
  13. ch = msvcrt.getch()
  14. else:
  15. import termios
  16. import tty
  17. fd = sys.stdin.fileno()
  18. old_settings = termios.tcgetattr(fd)
  19. ch = 'n'
  20. try:
  21. tty.setraw(sys.stdin.fileno())
  22. ch = sys.stdin.read(1)
  23. finally:
  24. termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  25. print(ch)
  26. return ch in ('Y', 'y')