summaryrefslogtreecommitdiffstats
path: root/pykolab
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2013-04-17 14:58:54 +0200
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2013-04-17 14:58:54 +0200
commit650c8222d76e2abe60f36c8c285531d1fc81c454 (patch)
tree3120a62524b48c867581624f4304c1c1ab4e67ce /pykolab
parent8a2bbf2e0fb37cfccbd614a60ec3bcbb0e96be3a (diff)
downloadpykolab-650c8222d76e2abe60f36c8c285531d1fc81c454.tar.gz
Enhance ask_menu to include a default, and a '?' option to list options
Diffstat (limited to 'pykolab')
-rw-r--r--pykolab/utils.py39
1 files changed, 34 insertions, 5 deletions
diff --git a/pykolab/utils.py b/pykolab/utils.py
index c10bbb2..02b20d4 100644
--- a/pykolab/utils.py
+++ b/pykolab/utils.py
@@ -126,11 +126,21 @@ def ask_confirmation(question, default="y", all_inclusive_no=True):
else:
return True
-def ask_menu(question, options={}):
- print question
+def ask_menu(question, options={}, default=''):
+ if not default == '':
+ print question + " [" + default + "]:"
+ else:
+ print question
+
answer_correct = False
max_key_length = 0
+ if isinstance(options, list):
+ _options = options
+ options = {}
+ for key in _options:
+ options[key] = key
+
keys = options.keys()
keys.sort()
@@ -142,10 +152,29 @@ def ask_menu(question, options={}):
str_format = "%%%ds" % max_key_length
- for key in keys:
- print " - " + eval("str_format % key") + ": " + options[key]
+ if default == '' or not default in options.keys():
+ for key in keys:
+ if options[key] == key:
+ print " - " + key
+ else:
+ print " - " + eval("str_format % key") + ": " + options[key]
+
+ answer = raw_input(_("Choice") + ": ")
+
+ else:
+ answer = raw_input(_("Choice (type '?' for options)") + ": ")
+
+ if answer == '?':
+ for key in keys:
+ if options[key] == key:
+ print " - " + key
+ else:
+ print " - " + eval("str_format % key") + ": " + options[key]
+
+ continue
- answer = raw_input(_("Choice") + ": ")
+ if answer == '' and default in options.keys():
+ answer = default
if answer in [str(x) for x in options.keys()]:
answer_correct = True