Prompts for confirmation (yes/no question). If the user aborts the input by sending a interrupt signal this function will catch it and raise an `Abort` exception.
(
text: str,
default: bool | None = False,
abort: bool = False,
prompt_suffix: str = ": ",
show_default: bool = True,
err: bool = False,
)
| 149 | |
| 150 | |
| 151 | def confirm( |
| 152 | text: str, |
| 153 | default: bool | None = False, |
| 154 | abort: bool = False, |
| 155 | prompt_suffix: str = ": ", |
| 156 | show_default: bool = True, |
| 157 | err: bool = False, |
| 158 | ) -> bool: |
| 159 | """Prompts for confirmation (yes/no question). |
| 160 | |
| 161 | If the user aborts the input by sending a interrupt signal this |
| 162 | function will catch it and raise an `Abort` exception. |
| 163 | """ |
| 164 | prompt = _build_prompt( |
| 165 | text, |
| 166 | prompt_suffix, |
| 167 | show_default, |
| 168 | "y/n" if default is None else ("Y/n" if default else "y/N"), |
| 169 | ) |
| 170 | |
| 171 | while True: |
| 172 | try: |
| 173 | # Write the prompt separately so that we get nice |
| 174 | # coloring through colorama on Windows |
| 175 | echo(prompt[:-1], nl=False, err=err) |
| 176 | # Echo the last character to stdout to work around an issue where |
| 177 | # readline causes backspace to clear the whole line. |
| 178 | value = visible_prompt_func(prompt[-1:]).lower().strip() |
| 179 | except (KeyboardInterrupt, EOFError): # pragma: no cover |
| 180 | raise Abort() from None |
| 181 | if value in ("y", "yes"): |
| 182 | rv = True |
| 183 | elif value in ("n", "no"): |
| 184 | rv = False |
| 185 | elif default is not None and value == "": |
| 186 | rv = default |
| 187 | else: # pragma: no cover |
| 188 | echo("Error: invalid input", err=err) |
| 189 | continue |
| 190 | break |
| 191 | if abort and not rv: |
| 192 | raise Abort() |
| 193 | return rv |
| 194 | |
| 195 | |
| 196 | @overload |
nothing calls this directly
no test coverage detected
searching dependent graphs…