r"""Presents the user with a prompt (typically in the form of a question) and a number of options. Arguments: prompt (str): The prompt to show opts (list): The options to show to the user default: The default option to choose Returns: The users choice in the for
(prompt, opts, default = None)
| 129 | print('Please answer yes or no') |
| 130 | |
| 131 | def options(prompt, opts, default = None): |
| 132 | r"""Presents the user with a prompt (typically in the |
| 133 | form of a question) and a number of options. |
| 134 | |
| 135 | Arguments: |
| 136 | prompt (str): The prompt to show |
| 137 | opts (list): The options to show to the user |
| 138 | default: The default option to choose |
| 139 | |
| 140 | Returns: |
| 141 | The users choice in the form of an integer. |
| 142 | |
| 143 | Examples: |
| 144 | |
| 145 | >>> options("Select a color", ("red", "green", "blue"), "green") |
| 146 | Traceback (most recent call last): |
| 147 | ... |
| 148 | ValueError: options(): default must be a number or None |
| 149 | |
| 150 | Tests: |
| 151 | |
| 152 | >>> p = testpwnproc("print(options('select a color', ('red', 'green', 'blue')))") |
| 153 | >>> p.sendline(b"\33[C\33[A\33[A\33[B\33[1;5A\33[1;5B 0310") |
| 154 | >>> _ = p.recvall() |
| 155 | >>> saved_stdin = sys.stdin |
| 156 | >>> try: |
| 157 | ... sys.stdin = io.TextIOWrapper(io.BytesIO(b"\n4\n\n3\n")) |
| 158 | ... with context.local(log_level="INFO"): |
| 159 | ... options("select a color A", ("red", "green", "blue"), 0) |
| 160 | ... options("select a color B", ("red", "green", "blue")) |
| 161 | ... finally: |
| 162 | ... sys.stdin = saved_stdin |
| 163 | [?] select a color A |
| 164 | 1) red |
| 165 | 2) green |
| 166 | 3) blue |
| 167 | Choice [1] 0 |
| 168 | [?] select a color B |
| 169 | 1) red |
| 170 | 2) green |
| 171 | 3) blue |
| 172 | Choice [?] select a color B |
| 173 | 1) red |
| 174 | 2) green |
| 175 | 3) blue |
| 176 | Choice [?] select a color B |
| 177 | 1) red |
| 178 | 2) green |
| 179 | 3) blue |
| 180 | Choice 2 |
| 181 | """ |
| 182 | |
| 183 | if default is not None and not isinstance(default, six.integer_types): |
| 184 | raise ValueError('options(): default must be a number or None') |
| 185 | |
| 186 | if term.term_mode: |
| 187 | numfmt = '%' + str(len(str(len(opts)))) + 'd) ' |
| 188 | print(' [?] ' + prompt) |
no test coverage detected