Prompts a user for input. This is a convenience function that can be used to prompt a user for input later. If the user aborts the input by sending an interrupt signal, this function will catch it and raise an `Abort` exception.
(
text: str,
default: Any | None = None,
hide_input: bool = False,
confirmation_prompt: bool | str = False,
type: ParamType | Any | None = None,
value_proc: Callable[[str], Any] | None = None,
prompt_suffix: str = ": ",
show_default: bool = True,
err: bool = False,
show_choices: bool = True,
)
| 72 | |
| 73 | |
| 74 | def prompt( |
| 75 | text: str, |
| 76 | default: Any | None = None, |
| 77 | hide_input: bool = False, |
| 78 | confirmation_prompt: bool | str = False, |
| 79 | type: ParamType | Any | None = None, |
| 80 | value_proc: Callable[[str], Any] | None = None, |
| 81 | prompt_suffix: str = ": ", |
| 82 | show_default: bool = True, |
| 83 | err: bool = False, |
| 84 | show_choices: bool = True, |
| 85 | ) -> Any: |
| 86 | """Prompts a user for input. This is a convenience function that can |
| 87 | be used to prompt a user for input later. |
| 88 | |
| 89 | If the user aborts the input by sending an interrupt signal, this |
| 90 | function will catch it and raise an `Abort` exception. |
| 91 | """ |
| 92 | |
| 93 | def prompt_func(text: str) -> str: |
| 94 | f = hidden_prompt_func if hide_input else visible_prompt_func |
| 95 | try: |
| 96 | # Write the prompt separately so that we get nice |
| 97 | # coloring through colorama on Windows |
| 98 | echo(text[:-1], nl=False, err=err) |
| 99 | # Echo the last character to stdout to work around an issue where |
| 100 | # readline causes backspace to clear the whole line. |
| 101 | return f(text[-1:]) |
| 102 | except (KeyboardInterrupt, EOFError): # pragma: no cover |
| 103 | # getpass doesn't print a newline if the user aborts input with ^C. |
| 104 | # Allegedly this behavior is inherited from getpass(3). |
| 105 | # A doc bug has been filed at https://bugs.python.org/issue24711 |
| 106 | if hide_input: |
| 107 | echo(None, err=err) |
| 108 | raise Abort() from None |
| 109 | |
| 110 | if value_proc is None: |
| 111 | value_proc = convert_type(type, default) |
| 112 | |
| 113 | prompt = _build_prompt( |
| 114 | text, prompt_suffix, show_default, default, show_choices, type |
| 115 | ) |
| 116 | |
| 117 | if confirmation_prompt: |
| 118 | if confirmation_prompt is True: |
| 119 | confirmation_prompt = "Repeat for confirmation" |
| 120 | |
| 121 | confirmation_prompt = _build_prompt(confirmation_prompt, prompt_suffix) |
| 122 | |
| 123 | while True: |
| 124 | while True: |
| 125 | value = prompt_func(prompt) |
| 126 | if value: |
| 127 | break |
| 128 | elif default is not None: |
| 129 | value = default |
| 130 | break |
| 131 | try: |
nothing calls this directly
no test coverage detected
searching dependent graphs…