gitscope
← marzneshin / v0.1.0 / marzban-cli.py
https://dev.kronoz.win/r/marzneshin.git
marzban-cli.py · Python
Blame Raw
 1#!/usr/bin/env python3
 2import os  # noqa
 3import readline  # noqa
 4import sys  # noqa
 5
 6sys.path.insert(0, os.getcwd())  # noqa
 7
 8import typer
 9from typer._completion_shared import Shells
10
11import cli.admin
12import cli.subscription
13import cli.user
14
15app = typer.Typer(no_args_is_help=True, add_completion=False)
16app.add_typer(cli.admin.app, name="admin")
17app.add_typer(cli.subscription.app, name="subscription")
18app.add_typer(cli.user.app, name="user")
19
20
21# Hidden completion app
22app_completion = typer.Typer(no_args_is_help=True, help="Generate and install completion scripts.", hidden=True)
23app.add_typer(app_completion, name="completion")
24
25
26def get_default_shell() -> Shells:
27    shell = os.environ.get('SHELL')
28    if shell:
29        shell = shell.split('/')[-1]
30        if shell in Shells.__members__:
31            return getattr(Shells, shell)
32    return Shells.bash
33
34
35@app_completion.command(help="Show completion for the specified shell, to copy or customize it.")
36def show(ctx: typer.Context, shell: Shells = typer.Option(None,
37                                                          help="The shell to install completion for.",
38                                                          case_sensitive=False)) -> None:
39    if shell is None:
40        shell = get_default_shell()
41    typer.completion.show_callback(ctx, None, shell)
42
43
44@app_completion.command(help="Install completion for the specified shell.")
45def install(ctx: typer.Context, shell: Shells = typer.Option(None,
46                                                             help="The shell to install completion for.",
47                                                             case_sensitive=False)) -> None:
48    if shell is None:
49        shell = get_default_shell()
50    typer.completion.install_callback(ctx, None, shell)
51
52
53if __name__ == "__main__":
54    typer.completion.completion_init()
55    app(prog_name=os.environ.get('CLI_PROG_NAME'))