Prompt the user to choose one of the choices. Choices are identified in the prompt_text by square brackets around a single letter option.
(prompt_text)
| 197 | |
| 198 | |
| 199 | def prompt(prompt_text): |
| 200 | """Prompt the user to choose one of the choices. |
| 201 | |
| 202 | Choices are identified in the prompt_text by square brackets around a |
| 203 | single letter option. |
| 204 | """ |
| 205 | choices = set(m.group(1) for m in re.finditer(r"\[(.)\]", prompt_text)) |
| 206 | while True: |
| 207 | sys.stderr.flush() |
| 208 | sys.stdout.write(prompt_text) |
| 209 | sys.stdout.flush() |
| 210 | response = sys.stdin.readline().strip().lower() |
| 211 | if not response: |
| 212 | continue |
| 213 | response = response[0] |
| 214 | if response in choices: |
| 215 | return response |
| 216 | |
| 217 | |
| 218 | # We need different encoding/decoding strategies for text data being passed |
no test coverage detected