From 1ffbd29ed503c87ad911a3a3c620ac3b8cb3cf85 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Wed, 17 Jul 2019 17:56:23 -0600 Subject: [PATCH] Added choice() --- scripts/wk/std.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/scripts/wk/std.py b/scripts/wk/std.py index 64a1c492..79aaffdb 100644 --- a/scripts/wk/std.py +++ b/scripts/wk/std.py @@ -68,6 +68,28 @@ def beep(repeat=1): repeat -= 1 +def choice(choices, prompt='答えろ!'): + """Choose an option from a provided list, returns str. + + Choices provided will be converted to uppercase and returned as such. + Similar to the commands choice (Windows) and select (Linux).""" + LOG.debug('choices: %s, prompt: %s', choices, prompt) + answer = None + choices = [str(c).upper()[:1] for c in choices] + prompt = '{} [{}]: '.format(prompt, '/'.join(choices)) + regex = '^({})$'.format('|'.join(choices)) + + # Loop until acceptable answer is given + while answer is None: + tmp = input_text(prompt=prompt) + if re.search(regex, tmp, re.IGNORECASE): + answer = tmp.upper() + + # Done + LOG.info('%s %s', prompt, answer) + return answer + + def clear_screen(): """Simple wrapper for clear/cls.""" if os.name == 'nt':