gitscope
← marzneshin / v0.3.0-alpha9 / marzneshin-cli.py
https://dev.kronoz.win/r/marzneshin.git
marzneshin-cli.py · Python
Blame Raw
 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, help="Generate and install completion scripts.", hidden=True
21)
22app.add_typer(app_completion, name="completion")
23
24
25def get_default_shell() -> Shells:
26    """
27    Find the default shell
28    """
29    shell = os.environ.get("SHELL")
30    if shell:
31        shell = shell.split("/")[-1]
32        if shell in Shells.__members__:
33            return getattr(Shells, shell)
34    return Shells.bash
35
36
37@app_completion.command(
38    help="Show completion for the specified shell, to copy or customize it."
39)
40def show(
41    ctx: typer.Context,
42    shell: Shells = typer.Option(
43        None, help="The shell to install completion for.", case_sensitive=False
44    ),
45) -> None:
46    if shell is None:
47        shell = get_default_shell()
48    typer.completion.show_callback(ctx, None, shell)
49
50
51@app_completion.command(help="Install completion for the specified shell.")
52def install(
53    ctx: typer.Context,
54    shell: Shells = typer.Option(
55        None, help="The shell to install completion for.", case_sensitive=False
56    ),
57) -> None:
58    if shell is None:
59        shell = get_default_shell()
60    typer.completion.install_callback(ctx, None, shell)
61
62
63if __name__ == "__main__":
64    typer.completion.completion_init()
65    app(prog_name=os.environ.get("CLI_PROG_NAME"))