| 1 |
a47eb4c |
Dawsh |
2y ago |
#!/usr/bin/env python3 |
| 2 |
a47eb4c |
Dawsh |
2y ago |
|
| 3 |
eafb4a4 |
Mrdn |
2y ago |
import os |
| 4 |
a47eb4c |
Dawsh |
2y ago |
|
| 5 |
a47eb4c |
Dawsh |
2y ago |
import typer |
| 6 |
a47eb4c |
Dawsh |
2y ago |
from typer._completion_shared import Shells |
| 7 |
a47eb4c |
Dawsh |
2y ago |
|
| 8 |
a47eb4c |
Dawsh |
2y ago |
import cli.admin |
| 9 |
a47eb4c |
Dawsh |
2y ago |
import cli.subscription |
| 10 |
a47eb4c |
Dawsh |
2y ago |
import cli.user |
| 11 |
a47eb4c |
Dawsh |
2y ago |
|
| 12 |
a47eb4c |
Dawsh |
2y ago |
app = typer.Typer(no_args_is_help=True, add_completion=False) |
| 13 |
a47eb4c |
Dawsh |
2y ago |
app.add_typer(cli.admin.app, name="admin") |
| 14 |
a47eb4c |
Dawsh |
2y ago |
app.add_typer(cli.subscription.app, name="subscription") |
| 15 |
a47eb4c |
Dawsh |
2y ago |
app.add_typer(cli.user.app, name="user") |
| 16 |
a47eb4c |
Dawsh |
2y ago |
|
| 17 |
a47eb4c |
Dawsh |
2y ago |
|
| 18 |
a47eb4c |
Dawsh |
2y ago |
# Hidden completion app |
| 19 |
eafb4a4 |
Mrdn |
2y ago |
app_completion = typer.Typer( |
| 20 |
eafb4a4 |
Mrdn |
2y ago |
no_args_is_help=True, help="Generate and install completion scripts.", hidden=True |
| 21 |
eafb4a4 |
Mrdn |
2y ago |
) |
| 22 |
a47eb4c |
Dawsh |
2y ago |
app.add_typer(app_completion, name="completion") |
| 23 |
a47eb4c |
Dawsh |
2y ago |
|
| 24 |
a47eb4c |
Dawsh |
2y ago |
|
| 25 |
a47eb4c |
Dawsh |
2y ago |
def get_default_shell() -> Shells: |
| 26 |
eafb4a4 |
Mrdn |
2y ago |
""" |
| 27 |
eafb4a4 |
Mrdn |
2y ago |
Find the default shell |
| 28 |
eafb4a4 |
Mrdn |
2y ago |
""" |
| 29 |
eafb4a4 |
Mrdn |
2y ago |
shell = os.environ.get("SHELL") |
| 30 |
a47eb4c |
Dawsh |
2y ago |
if shell: |
| 31 |
eafb4a4 |
Mrdn |
2y ago |
shell = shell.split("/")[-1] |
| 32 |
a47eb4c |
Dawsh |
2y ago |
if shell in Shells.__members__: |
| 33 |
a47eb4c |
Dawsh |
2y ago |
return getattr(Shells, shell) |
| 34 |
a47eb4c |
Dawsh |
2y ago |
return Shells.bash |
| 35 |
a47eb4c |
Dawsh |
2y ago |
|
| 36 |
a47eb4c |
Dawsh |
2y ago |
|
| 37 |
eafb4a4 |
Mrdn |
2y ago |
@app_completion.command( |
| 38 |
eafb4a4 |
Mrdn |
2y ago |
help="Show completion for the specified shell, to copy or customize it." |
| 39 |
eafb4a4 |
Mrdn |
2y ago |
) |
| 40 |
eafb4a4 |
Mrdn |
2y ago |
def show( |
| 41 |
eafb4a4 |
Mrdn |
2y ago |
ctx: typer.Context, |
| 42 |
eafb4a4 |
Mrdn |
2y ago |
shell: Shells = typer.Option( |
| 43 |
eafb4a4 |
Mrdn |
2y ago |
None, help="The shell to install completion for.", case_sensitive=False |
| 44 |
eafb4a4 |
Mrdn |
2y ago |
), |
| 45 |
eafb4a4 |
Mrdn |
2y ago |
) -> None: |
| 46 |
a47eb4c |
Dawsh |
2y ago |
if shell is None: |
| 47 |
a47eb4c |
Dawsh |
2y ago |
shell = get_default_shell() |
| 48 |
a47eb4c |
Dawsh |
2y ago |
typer.completion.show_callback(ctx, None, shell) |
| 49 |
a47eb4c |
Dawsh |
2y ago |
|
| 50 |
a47eb4c |
Dawsh |
2y ago |
|
| 51 |
a47eb4c |
Dawsh |
2y ago |
@app_completion.command(help="Install completion for the specified shell.") |
| 52 |
eafb4a4 |
Mrdn |
2y ago |
def install( |
| 53 |
eafb4a4 |
Mrdn |
2y ago |
ctx: typer.Context, |
| 54 |
eafb4a4 |
Mrdn |
2y ago |
shell: Shells = typer.Option( |
| 55 |
eafb4a4 |
Mrdn |
2y ago |
None, help="The shell to install completion for.", case_sensitive=False |
| 56 |
eafb4a4 |
Mrdn |
2y ago |
), |
| 57 |
eafb4a4 |
Mrdn |
2y ago |
) -> None: |
| 58 |
a47eb4c |
Dawsh |
2y ago |
if shell is None: |
| 59 |
a47eb4c |
Dawsh |
2y ago |
shell = get_default_shell() |
| 60 |
a47eb4c |
Dawsh |
2y ago |
typer.completion.install_callback(ctx, None, shell) |
| 61 |
a47eb4c |
Dawsh |
2y ago |
|
| 62 |
a47eb4c |
Dawsh |
2y ago |
|
| 63 |
a47eb4c |
Dawsh |
2y ago |
if __name__ == "__main__": |
| 64 |
a47eb4c |
Dawsh |
2y ago |
typer.completion.completion_init() |
| 65 |
eafb4a4 |
Mrdn |
2y ago |
app(prog_name=os.environ.get("CLI_PROG_NAME")) |