Skip to content
Myr

Add FAQ entry about tab completion in prompts

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Closes #173

authored by Kevin Deldycke · committed by Kevin Deldycke · parent 00f257b · 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

2 files+64 added−1 removed

modified:docs/contrib.md+10 added-1 removed
modified:docs/faqs.md+54 added
103 unmodified lines
104| Sublime Text | `EDITOR="subl -w"` |105106For 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.107108### Tab completion during interactive prompts109110A command asks for a file path while it runs:111112```python113import click114115116@click.command()117def convert():118 source = click.prompt("File to convert", type=click.Path(exists=True))119 click.echo(f"Converting {source}")120```121122Pressing {kbd}`Tab` while typing the answer inserts a tab character instead of completing the path:123124```console125$ convert126File to convert: Doc<TAB>127Error: Path 'Doc\t' does not exist.128```129130This 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.131132{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.133134#### Answer135136Bind {kbd}`Tab` in `readline` and the prompt completes paths with no other change:137138```python139import readline140141if "libedit" in (readline.__doc__ or ""):142 readline.parse_and_bind("bind ^I rl_complete")143else:144 readline.parse_and_bind("tab: complete")145```146147See 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.148149Readline 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).150151When 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:152153```python154@click.command()155@click.argument("source", type=click.Path(exists=True))156def convert(source):157 click.echo(f"Converting {source}")158```159160The shell completes paths there natively and Click's completion mode takes part in it (as described in [Shell Completion](shell-completion.md)).161162(custom-completions-show-the-full-value)=163### Custom completions show the full value164165`Path` and `File` parameters complete differently from a parameter with a custom completer. Take a command with one of each:
43 unmodified lines