make the cli more click
This commit is contained in:
parent
c890c49c7b
commit
8fcf9b4785
2 changed files with 24 additions and 1 deletions
19
smal/src/pymxutils/mxutils/click.py
Normal file
19
smal/src/pymxutils/mxutils/click.py
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
# Copyright (C) 2026 saces@c-base.org
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
from functools import partial, wraps
|
||||||
|
|
||||||
|
import click
|
||||||
|
|
||||||
|
|
||||||
|
def click_catch_exception(func=None, *, handle):
|
||||||
|
if not func:
|
||||||
|
return partial(click_catch_exception, handle=handle)
|
||||||
|
|
||||||
|
@wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
try:
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
except handle as e:
|
||||||
|
raise click.ClickException(e)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
@ -2,11 +2,15 @@
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
import click
|
import click
|
||||||
from pygomx import CliV0
|
from pygomx import CliV0
|
||||||
|
import pygomx
|
||||||
|
|
||||||
|
from .click import click_catch_exception
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
@click.option("-u", "--url", "hs_url", metavar="url", help="homeserver url")
|
@click.option("-u", "--url", "hs_url", metavar="url", help="homeserver url")
|
||||||
@click.option("-t", "--token", "token", metavar="token", help="access token")
|
@click.option("-t", "--token", "token", metavar="token", help="access token")
|
||||||
|
@click_catch_exception(handle=(pygomx.errors.APIError))
|
||||||
def whoami(hs_url, token):
|
def whoami(hs_url, token):
|
||||||
"""this token belongs to?"""
|
"""this token belongs to?"""
|
||||||
|
|
||||||
|
|
@ -15,4 +19,4 @@ def whoami(hs_url, token):
|
||||||
else:
|
else:
|
||||||
cli = CliV0(hs_url, token)
|
cli = CliV0(hs_url, token)
|
||||||
|
|
||||||
print(cli.Whoami())
|
click.echo(cli.Whoami())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue