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