diff options
author | KP <kp@shotgunsoftware.com> | 2013-06-11 18:50:21 -0500 |
---|---|---|
committer | KP <kp@shotgunsoftware.com> | 2013-06-13 10:13:34 -0500 |
commit | 52bb0054fb4fe2fa1359561887e4f3611cac8c82 (patch) | |
tree | 413548a99fa254a9c2fa2545e599f1f7f1026943 | |
parent | 03fd29976870d1d321ecaded1de70b0417b1a9fb (diff) | |
download | askbot-52bb0054fb4fe2fa1359561887e4f3611cac8c82.tar.gz askbot-52bb0054fb4fe2fa1359561887e4f3611cac8c82.tar.bz2 askbot-52bb0054fb4fe2fa1359561887e4f3611cac8c82.zip |
add default option to yes/no prompt
-rw-r--r-- | askbot/utils/console.py | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/askbot/utils/console.py b/askbot/utils/console.py index 7190e1a1..c1ffa14e 100644 --- a/askbot/utils/console.py +++ b/askbot/utils/console.py @@ -126,9 +126,27 @@ def simple_dialog(prompt_phrase, required=False): time.sleep(1) -def get_yes_or_no(prompt_phrase): +def get_yes_or_no(prompt_phrase, default=None): + """Prompts user for a yes or no response with an optional default + value which will be inferred if the user just hits enter + + :param prompt_phrase: (str) Question to prompt the user with + + :param default: (str) Either 'yes' or 'no'. If a valid option is + provided, the user can simply press enter to accept the default. + If an invalid option is passed in, a `ValueError` is raised. + + :returns: (str) 'yes' or 'no' + """ while True: - response = raw_input(prompt_phrase + ' (yes/no)\n> ').strip() + prompt_phrase += ' (yes/no)' + if default: + prompt_phrase += '\n[%s] >' % default + else: + prompt_phrase += '\n >' % default + response = raw_input(prompt_phrase).strip() + if not response and default: + return default if response in ('yes', 'no'): return response |