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