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