Add FAQ entry about tab completion in prompts (#3826)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents e5cb12b, cb43e05 · Browse files · 2 files·+64 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+64 added−1 removed
- other:+64 added−1 removed
Provenance
- Commit
b024123- Tree
542b710- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
e5cb12b- Parent 2
cb43e05
2 files changed+64 added−1 removed
modified:docs/contrib.md+10 added-1 removed
30 unmodified lines31323334353637383940414243443645464748496 unmodified lines30 unmodified lines| [click-app](https://github.com/simonw/click-app) | Cookiecutter template for creating new CLIs. |  |  || [Cloup](https://github.com/janluke/cloup) | Adds option groups, constraints, command aliases, help themes, suggestions and more. |  |  || [Click Extra](https://github.com/kdeldycke/click-extra) | Cloup + colorful `--help`, `--config`, `--show-params`, `--verbosity` options, etc. |  |  |
## Interactive input libraries
Click reads a prompt with the built-in {func}`input` and embeds no line editor of its own. If you need fancy features like completion of arbitrary values, selection menus or fuzzy search you need to replace {func}`click.prompt` with a third-party library. The projects below are popular and actively maintained:
| Project | Description | Popularity | Activity ||---------------------------------------------------------------------------|----------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------|| [Textual](https://github.com/Textualize/textual) | Build terminal and web user interfaces with a Python API. |  |  || [prompt-toolkit](https://github.com/prompt-toolkit/python-prompt-toolkit) | Build interactive command lines with completion, history and key bindings. |  |  |
```{note}To make it into the list above, a project:To make it into any of the lists above, a project:
- must be actively maintained (at least one commit in the last year)- must have a reasonable number of stars (at least 20)
6 unmodified linesmodified:docs/faqs.md+54 added
103 unmodified lines104 | Sublime Text | `EDITOR="subl -w"` |105 106 For an editor with no such flag, a workaround is to point `EDITOR` at a small wrapper script that opens the editor and blocks until it exits.107 108+### Tab completion during interactive prompts109+110+A command asks for a file path while it runs:111+112+```python113+import click114+115+116+@click.command()117+def convert():118+ source = click.prompt("File to convert", type=click.Path(exists=True))119+ click.echo(f"Converting {source}")120+```121+122+Pressing {kbd}`Tab` while typing the answer inserts a tab character instead of completing the path:123+124+```console125+$ convert126+File to convert: Doc<TAB>127+Error: Path 'Doc\t' does not exist.128+```129+130+This is not a bug in Click. Tab completion of the command line belongs to the shell: the shell asks the program for candidates through Click's completion mode, before any command code runs. A prompt works the other way around. The program already runs and reads the line itself, so Click never sees the {kbd}`Tab` key.131+132+{func}`~click.prompt` reads that line with the built-in {func}`input`, and Click passes the whole prompt text to it as-is. This is where Python's {mod}`readline` module takes over, but nothing binds {kbd}`Tab` to completion by default.133+134+#### Answer135+136+Bind {kbd}`Tab` in `readline` and the prompt completes paths with no other change:137+138+```python139+import readline140+141+if "libedit" in (readline.__doc__ or ""):142+ readline.parse_and_bind("bind ^I rl_complete")143+else:144+ readline.parse_and_bind("tab: complete")145+```146+147+See the {mod}`readline` documentation for the binding syntax, for the two libraries it can be built on, and for writing a completer of your own. The module is Unix-only, so the import fails on Windows.148+149+Readline completes file names. It knows nothing about the parameter, so it cannot complete other values, such as the choices of a {class}`~click.Choice` parameter. Click embeds no line editor of its own: for completion of such values at a prompt, use one of the [interactive input libraries](contrib.md#interactive-input-libraries).150+151+When the value does not have to be asked while the command runs, another option is to take it out of the prompt and put it on the command line, as an argument or an option:152+153+```python154+@click.command()155+@click.argument("source", type=click.Path(exists=True))156+def convert(source):157+ click.echo(f"Converting {source}")158+```159+160+The shell completes paths there natively and Click's completion mode takes part in it (as described in [Shell Completion](shell-completion.md)).161+162 (custom-completions-show-the-full-value)=163 ### Custom completions show the full value164 165 `Path` and `File` parameters complete differently from a parameter with a custom completer. Take a command with one of each:43 unmodified lines