From 28f5b3c3019cdfed09400ad57faca3985a0c6c54 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Mon, 26 Dec 2022 12:27:40 +0200 Subject: [PATCH] Add better inspector arg parsing (#2642) --- sanic/cli/app.py | 13 +++++++++++-- sanic/cli/inspector.py | 11 ++++++++--- sanic/config.py | 4 +++- sanic/worker/inspector.py | 3 ++- tests/test_cli.py | 2 ++ 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/sanic/cli/app.py b/sanic/cli/app.py index 444de0f6..3884a15d 100644 --- a/sanic/cli/app.py +++ b/sanic/cli/app.py @@ -148,8 +148,17 @@ Or, a path to a directory to run as a simple HTTP server: if unknown: for arg in unknown: if arg.startswith("--"): - key, value = arg.split("=") - setattr(self.args, key.lstrip("-"), value) + try: + key, value = arg.split("=") + key = key.lstrip("-") + except ValueError: + value = False if arg.startswith("--no-") else True + key = ( + arg.replace("--no-", "") + .lstrip("-") + .replace("-", "_") + ) + setattr(self.args, key, value) kwargs = {**self.args.__dict__} host = kwargs.pop("host") diff --git a/sanic/cli/inspector.py b/sanic/cli/inspector.py index 8bb8e90b..31aaee09 100644 --- a/sanic/cli/inspector.py +++ b/sanic/cli/inspector.py @@ -47,11 +47,16 @@ def make_inspector_parser(parser: ArgumentParser) -> None: action=SanicSubParsersAction, dest="action", description=( - "Run one of the below subcommands. If you have created a custom " - "Inspector instance, then you can run custom commands. See ___ " + "Run one or none of the below subcommands. Using inspect without " + "a subcommand will fetch general information about the state " + "of the application instance.\n\n" + "Or, you can optionally follow inspect with a subcommand. " + "If you have created a custom " + "Inspector instance, then you can run custom commands. See " + "https://sanic.dev/en/guide/deployment/inspector.html" "for more details." ), - title="Required\n========\n Subcommands", + title=" Subcommands", parser_class=InspectorSubParser, ) reloader = subparsers.add_parser( diff --git a/sanic/config.py b/sanic/config.py index 0d5eabf8..2b279482 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -126,7 +126,9 @@ class Config(dict, metaclass=DescriptorMeta): def __init__( self, - defaults: Dict[str, Union[str, bool, int, float, None]] = None, + defaults: Optional[ + Dict[str, Union[str, bool, int, float, None]] + ] = None, env_prefix: Optional[str] = SANIC_PREFIX, keep_alive: Optional[bool] = None, *, diff --git a/sanic/worker/inspector.py b/sanic/worker/inspector.py index 487ef3c5..c30aa6c4 100644 --- a/sanic/worker/inspector.py +++ b/sanic/worker/inspector.py @@ -71,7 +71,8 @@ class Inspector: kwargs = {} if request.body: kwargs = request.json - output = method(**kwargs) + args = kwargs.pop("args", ()) + output = method(*args, **kwargs) if isawaitable(output): output = await output diff --git a/tests/test_cli.py b/tests/test_cli.py index eff61a18..cb3842e7 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -326,6 +326,8 @@ def test_inspector_inspect(urlopen, caplog, capsys): (["shutdown"], {}), (["scale", "9"], {"replicas": 9}), (["foo", "--bar=something"], {"bar": "something"}), + (["foo", "--bar"], {"bar": True}), + (["foo", "--no-bar"], {"bar": False}), (["foo", "positional"], {"args": ["positional"]}), ( ["foo", "positional", "--bar=something"],