Permalink
Browse files

Added basic yes/no prompt module

  • Loading branch information...
1 parent fc873de commit 48d9c16f0ff5e92bf10b82739230f9fef3f92d53 @gbrindisi gbrindisi committed Apr 6, 2012
Showing with 51 additions and 1 deletion.
  1. +2 −1 AUTHORS
  2. +49 −0 clint/textui/prompt.py
View
3 AUTHORS
@@ -19,4 +19,5 @@ Patches and Suggestions
- takluyver
- kracekumar
- Alejandro Gómez <alejandrogomez>
-- Jason Piper <jpiper>
+- Jason Piper <jpiper>
+- Gianluca Brindisi <gbrindisi>
View
49 clint/textui/prompt.py
@@ -0,0 +1,49 @@
+# -*- coding: utf8 -*-
+
+"""
+clint.textui.prompt
+~~~~~~~~~~~~~~~~~~~
+
+Module for simple interactive prompts handling
+
+"""
+
+from __future__ import absolute_import
+
+from re import match, I
+
+def yn(prompt, default='y', batch=False):
+ # A sanity check against default value
+ # If not y/n then y is assumed
+ if default not in ['y', 'n']:
+ default = 'y'
+
+ # Let's build the prompt
+ choicebox = '[Y/n]' if default == 'y' else '[y/N]'
+ prompt = prompt + ' ' + choicebox + ' '
+
+ # If input is not a yes/no variant or empty
+ # keep asking
+ while True:
+ # If batch option is True then auto reply
+ # with default input
+ if not batch:
+ input = raw_input(prompt).strip()
+ else:
+ print prompt
+ input = ''
+
+ # If input is empty default choice is assumed
+ # so we return True
+ if input == '':
+ return True
+
+ # Given 'yes' as input if default choice is y
+ # then return True, False otherwise
+ if match('y(?:es)?', input, I):
+ return True if default == 'y' else False
+
+ # Given 'no' as input if default choice is n
+ # then return True, False otherwise
+ elif match('n(?:o)?', input, I):
+ return True if default == 'n' else False

0 comments on commit 48d9c16

Please sign in to comment.