diff options
author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> | 2009-07-21 11:49:04 +0200 |
---|---|---|
committer | Nicolas Chauvat <nicolas.chauvat@logilab.fr> | 2009-07-21 11:49:04 +0200 |
commit | 725f6d5185fcad0c1a7a2e676341f2ee9674230c (patch) | |
tree | cd3d9dcab7137e2a66e0df08133be51181046470 | |
parent | e25c1bc672639403c63b599945f2fb563d4deb16 (diff) | |
download | logilab-common-725f6d5185fcad0c1a7a2e676341f2ee9674230c.tar.gz |
[shellutils] add confirm() that wraps raw_input()
-rw-r--r-- | shellutils.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/shellutils.py b/shellutils.py index a8f216d..67e2888 100644 --- a/shellutils.py +++ b/shellutils.py @@ -277,3 +277,19 @@ class ProgressBar(object): """Refresh the progression bar display.""" self._stream.write(self._fstr % ('.' * min(self._progress, self._size)) ) self._stream.flush() + +def confirm(question, default_is_yes=True): + """ask for confirmation and return true on positive answer""" + if default_is_yes: + input_str = '%s [Y/n]: ' + else: + input_str = '%s [y/N]: ' + answer = raw_input(input_str % (question)).strip().lower() + if default_is_yes: + if answer in ('n', 'no'): + return False + return True + if answer in ('y', 'yes'): + return True + return False + |