(prompt: str, default: Optional[Any] = None, *,
optional: bool = True,
choices: Optional[abc.Sequence[Any]] = None)
| 26 | |
| 27 | |
| 28 | def default_text_input(prompt: str, default: Optional[Any] = None, *, |
| 29 | optional: bool = True, |
| 30 | choices: Optional[abc.Sequence[Any]] = None) -> Union[str, None]: |
| 31 | # CLI util; defer click import until actually needed (see #473) |
| 32 | import click |
| 33 | |
| 34 | _skip = 'skip' |
| 35 | kwargs = dict(text=prompt) |
| 36 | if default: |
| 37 | kwargs.update(default=default) |
| 38 | else: |
| 39 | # make click print [skip] next to prompt |
| 40 | if optional: |
| 41 | kwargs.update(default=_skip) |
| 42 | if choices: |
| 43 | _type = click.Choice(choices) |
| 44 | kwargs.update(type=_type) |
| 45 | # a special case to skip user input instead of forcing input |
| 46 | if optional: |
| 47 | def allow_skip(value): |
| 48 | if value == _skip: |
| 49 | return value |
| 50 | return click.types.convert_type(_type)(value) |
| 51 | |
| 52 | kwargs.update(value_proc=allow_skip) |
| 53 | |
| 54 | value = click.prompt(**kwargs) |
| 55 | if optional and value == _skip: |
| 56 | value = None |
| 57 | return value |
| 58 | |
| 59 | |
| 60 | def strtrunc(s: Any, maxlen: int = 60) -> str: |
no outgoing calls