summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKP <kp@shotgunsoftware.com>2013-06-11 18:49:53 -0500
committerKP <kp@shotgunsoftware.com>2013-06-13 10:13:34 -0500
commit03fd29976870d1d321ecaded1de70b0417b1a9fb (patch)
tree9fc85ce1dd371890a1abf1f478927654d916f2bd
parent8da392e21411548cef68afa625cbbe877df80c4b (diff)
downloadaskbot-03fd29976870d1d321ecaded1de70b0417b1a9fb.tar.gz
askbot-03fd29976870d1d321ecaded1de70b0417b1a9fb.tar.bz2
askbot-03fd29976870d1d321ecaded1de70b0417b1a9fb.zip
add numeric-based choice and multiple choice menus
-rw-r--r--askbot/utils/console.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/askbot/utils/console.py b/askbot/utils/console.py
index 23cff6f9..7190e1a1 100644
--- a/askbot/utils/console.py
+++ b/askbot/utils/console.py
@@ -34,6 +34,80 @@ def choice_dialog(prompt_phrase, choices = None, invalid_phrase = None):
print invalid_phrase % {'opt_string': opt_string}
time.sleep(1)
+def numeric_choice_dialog(prompt_phrase, choices):
+ """Prints a list of choices with numeric options and requires the
+ user to select a single choice from the list.
+
+ :param prompt_phrase: (str) Prompt to give the user asking them to
+ choose from the list.
+
+ :param choices: (list) List of string choices for the user to choose
+ from. The numeric value they will use to select from the list is the
+ list index of the choice.
+
+ :returns: (int) index number of the choice selected by the user
+ """
+ assert(hasattr(choices, '__iter__'))
+ assert(not isinstance(choices, basestring))
+ choice_menu = "\n".join(["%d - %s" % (i,x) for i, x in enumerate(choices)])
+ while True:
+ response = raw_input('\n%s\n%s> ' % (choice_menu, prompt_phrase))
+ try:
+ index = int(response)
+ except ValueError:
+ index = False
+ if index is False or index < 0 or index >= len(choices):
+ print "\n*** Please enter a number between 0 and %d ***" % (len(choices)-1)
+ else:
+ return index
+
+def numeric_multiple_choice_dialog(prompt_phrase, choices, all_option=False):
+ """Prints a list of choices with numeric options and requires the
+ user to select zero or more choices from the list.
+
+ :param prompt_phrase: (str) Prompt to give the user asking them to
+ choose from the list.
+
+ :param choices: (list) List of string choices for the user to choose
+ from. The numeric value they will use to select from the list is the
+ list index of the choice.
+
+ :param all_option: (bool) Optional. If True, the first choice will be a
+ fake option to choose all options. This is a convenience to avoid requiring
+ the user provide a lot of input when there are a lot of options
+
+ :returns: (list) list of index numbers of the choices selected by
+ the user
+ """
+ assert(hasattr(choices, '__iter__'))
+ assert(not isinstance(choices, basestring))
+ if all_option:
+ choices.insert(0, 'ALL')
+ choice_menu = "\n".join(["%d - %s" % (i,x) for i, x in enumerate(choices)])
+ choice_indexes = []
+ index = False
+ while True:
+ response = raw_input('\n%s\n%s> ' % (choice_menu, prompt_phrase))
+ selections = response.split()
+ print "selections: %s" % selections
+ for c in selections:
+ try:
+ index = int(c)
+ except ValueError:
+ index = False
+ if index < 0 or index >= len(choices):
+ index = False
+ print "\n*** Please enter only numbers between 0 and " +\
+ "%d separated by spaces ***" % (len(choices)-1)
+ break
+ else:
+ choice_indexes.append(index)
+ if index:
+ if all_option and 0 in choice_indexes and len(choice_indexes) > 1:
+ print "\n*** You cannot include other choices with the ALL " +\
+ "option ***"
+ else:
+ return choice_indexes
def simple_dialog(prompt_phrase, required=False):
"""asks user to enter a string, if `required` is True,