Document completion of path in shells
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Closes #3135
authored by Kevin Deldycke · committed by Kevin Deldycke · parent cbb5b2d · Browse files · 2 files·+50 added−0 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+50 added−0 removed
- other:+50 added−0 removed
Provenance
- Commit
c4597ed- Tree
797d68c- Author
Kevin Deldycke <kevin@deldycke.com>- Committer
Kevin Deldycke <kevin@deldycke.com>- Parent
cbb5b2d
2 files changed+50 added−0 removed
modified:docs/faqs.md+48 added
102 unmodified linesExpand all103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154102 unmodified linesExpand all| Visual Studio Code | `EDITOR="code --wait"` || Sublime Text | `EDITOR="subl -w"` |
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.
(custom-completions-show-the-full-value)=### Custom completions show the full value
`Path` and `File` parameters complete differently from a parameter with a custom completer. Take a command with one of each:
```pythondef complete_dirs(ctx, param, incomplete): return [ CompletionItem(incomplete + name) for name in ("Documents", "Downloads", "Pictures") ]
@click.command()@click.option("--src", type=click.Path())@click.option("--dest", shell_complete=complete_dirs)def copy(src, dest): click.echo(f"Copy from {src} to {dest}")```
Completing `--src` shows only the last component of each candidate, and omits the leading path:
```console$ copy --src /home/user/<TAB><TAB>Documents Downloads Pictures```
Completing `--dest` shows each value in full, exactly as the completer returned it:
```console$ copy --dest /home/user/<TAB><TAB>/home/user/Documents /home/user/Downloads /home/user/Pictures```
This is not a bug in Click. The two parameters are completed by different sides.
`Path.shell_complete` and `File.shell_complete` never enumerate the directory: each returns a single item that is the incomplete value itself, tagged `file` (or `dir` for a `Path` restricted to directories). Then the shell takes over to propose completions, and each shell does so in its own way.
A custom completer sends no such marker. Its `CompletionItem` carries the value, a `type` marker, and an optional `help` string. The value a completer returns is both what the shell displays and what it inserts into the command line.
#### Answer
To change how suggestions are displayed, work on the shell side:
- Check whether your shell's completion system has an option that changes how candidates are displayed.- Write a `ShellComplete` subclass and register it with `add_completion_class`, as described in [Adding Support for a Shell](shell-completion.md#adding-support-for-a-shell).- Or return the shortened form directly from your completer, but make the parameter accept and interpret that shortened form into the full value.modified:docs/shell-completion.md+2 added
159 unmodified lines160 def cli(ev):161 click.echo(os.environ[ev])162 ```163 164+See {ref}`custom-completions-show-the-full-value` in the FAQ for why these values are displayed in full, while `Path` completions show only the last component of the path.165+166 ## Overriding Value Completion167 168 Value completions for a parameter can be customized without a custom type by providing a `shell_complete` function. The169 function is used instead of any completion provided by the type. It is passed 3 positional arguments:108 unmodified lines