|
1 | | -"""Shell completion commands. |
| 1 | +"""Shell completion: install + raw script generators. |
2 | 2 |
|
3 | | -Generates completion scripts for bash, zsh, and fish shells. |
4 | | -Uses Click's built-in completion generation via the ``_ROBOFLOW_COMPLETE`` |
5 | | -environment variable. |
| 3 | +Delegates installation to ``typer.completion.install`` (which itself |
| 4 | +wraps Click's ``shell_completion`` and auto-detects the shell via |
| 5 | +shellingham). Hidden commands are filtered by Click automatically. |
6 | 6 | """ |
7 | 7 |
|
8 | 8 | from __future__ import annotations |
9 | 9 |
|
10 | | -import sys |
| 10 | +import shutil |
| 11 | +from typing import Annotated, Optional |
11 | 12 |
|
12 | 13 | import click |
13 | 14 | import typer |
| 15 | +from typer._completion_classes import completion_init |
| 16 | +from typer._completion_shared import get_completion_script |
| 17 | +from typer.completion import install as typer_install |
14 | 18 |
|
15 | | -from roboflow.cli._compat import SortedGroup |
| 19 | +from roboflow.cli._compat import SortedGroup, ctx_to_args |
| 20 | +from roboflow.cli._output import output, output_error |
16 | 21 |
|
17 | | -completion_app = typer.Typer(cls=SortedGroup, help="Generate shell completions", no_args_is_help=True) |
| 22 | +completion_app = typer.Typer( |
| 23 | + cls=SortedGroup, |
| 24 | + help="Generate and install shell completions", |
| 25 | + no_args_is_help=True, |
| 26 | +) |
18 | 27 |
|
| 28 | +completion_init() |
19 | 29 |
|
20 | | -def _generate_completion(shell: str) -> None: |
21 | | - """Generate completion script for the given shell using Click's completion system.""" |
22 | | - from click.shell_completion import get_completion_class |
23 | 30 |
|
24 | | - comp_cls = get_completion_class(shell) |
25 | | - if comp_cls is None: |
26 | | - print(f"Shell '{shell}' is not supported for completion.", file=sys.stderr) |
27 | | - raise typer.Exit(code=1) |
28 | | - |
29 | | - from roboflow.cli import app |
30 | | - |
31 | | - # Access the underlying Click command |
32 | | - click_app = typer.main.get_command(app) |
33 | | - ctx = click.Context(click_app, info_name="roboflow") |
34 | | - comp = comp_cls(click_app, ctx, "roboflow", "_ROBOFLOW_COMPLETE") # type: ignore[arg-type] |
35 | | - print(comp.source()) # noqa: T201 |
| 31 | +def _generate_completion(shell: str) -> str: |
| 32 | + return get_completion_script(prog_name="roboflow", complete_var="_ROBOFLOW_COMPLETE", shell=shell) |
36 | 33 |
|
37 | 34 |
|
38 | 35 | @completion_app.command("bash") |
39 | 36 | def bash() -> None: |
40 | | - """Generate bash completion script. |
41 | | -
|
42 | | - Usage: eval "$(roboflow completion bash)" |
43 | | - Or save to a file: roboflow completion bash > ~/.roboflow-complete.bash |
44 | | - """ |
45 | | - _generate_completion("bash") |
| 37 | + """Print bash completion script. Usage: eval "$(roboflow completion bash)".""" |
| 38 | + print(_generate_completion("bash")) # noqa: T201 |
46 | 39 |
|
47 | 40 |
|
48 | 41 | @completion_app.command("zsh") |
49 | 42 | def zsh() -> None: |
50 | | - """Generate zsh completion script. |
51 | | -
|
52 | | - Usage: eval "$(roboflow completion zsh)" |
53 | | - Or save to a file: roboflow completion zsh > ~/.roboflow-complete.zsh |
54 | | - """ |
55 | | - _generate_completion("zsh") |
| 43 | + """Print zsh completion script. Usage: eval "$(roboflow completion zsh)".""" |
| 44 | + print(_generate_completion("zsh")) # noqa: T201 |
56 | 45 |
|
57 | 46 |
|
58 | 47 | @completion_app.command("fish") |
59 | 48 | def fish() -> None: |
60 | | - """Generate fish completion script. |
61 | | -
|
62 | | - Usage: roboflow completion fish | source |
63 | | - Or save to a file: roboflow completion fish > ~/.config/fish/completions/roboflow.fish |
64 | | - """ |
65 | | - _generate_completion("fish") |
| 49 | + """Print fish completion script. Usage: roboflow completion fish | source.""" |
| 50 | + print(_generate_completion("fish")) # noqa: T201 |
| 51 | + |
| 52 | + |
| 53 | +@completion_app.command("install") |
| 54 | +def install( |
| 55 | + ctx: typer.Context, |
| 56 | + shell: Annotated[ |
| 57 | + Optional[str], |
| 58 | + typer.Option("--shell", help="bash, zsh, or fish. Auto-detected when omitted."), |
| 59 | + ] = None, |
| 60 | +) -> None: |
| 61 | + """Install shell completion. Writes the script and updates your shell rc. Idempotent.""" |
| 62 | + args = ctx_to_args(ctx, shell=shell) |
| 63 | + |
| 64 | + if shutil.which("roboflow") is None: |
| 65 | + output_error( |
| 66 | + args, |
| 67 | + "The 'roboflow' command is not on your PATH.", |
| 68 | + hint="Ensure your install bin directory (e.g. ~/.local/bin) is on PATH.", |
| 69 | + exit_code=1, |
| 70 | + ) |
| 71 | + return |
| 72 | + |
| 73 | + try: |
| 74 | + installed_shell, path = typer_install(shell=shell, prog_name="roboflow", complete_var="_ROBOFLOW_COMPLETE") |
| 75 | + except click.exceptions.Exit: |
| 76 | + output_error( |
| 77 | + args, |
| 78 | + "Could not detect or install completion.", |
| 79 | + hint="Pass --shell with one of: bash, zsh, fish.", |
| 80 | + exit_code=3, |
| 81 | + ) |
| 82 | + return |
| 83 | + |
| 84 | + output( |
| 85 | + args, |
| 86 | + {"shell": installed_shell, "path": str(path)}, |
| 87 | + text=f"Installed {installed_shell} completion to {path}.\nOpen a new shell to enable it.", |
| 88 | + ) |
0 commit comments