Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better inspector arg parsing #2642

Merged
merged 1 commit into from Dec 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions sanic/cli/app.py
Expand Up @@ -148,8 +148,17 @@ def _inspector(self):
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")
Expand Down
11 changes: 8 additions & 3 deletions sanic/cli/inspector.py
Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion sanic/config.py
Expand Up @@ -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,
*,
Expand Down
3 changes: 2 additions & 1 deletion sanic/worker/inspector.py
Expand Up @@ -71,7 +71,8 @@ async def _action(self, request: Request, action: str):
kwargs = {}
if request.body:
kwargs = request.json
output = method(**kwargs)
args = kwargs.pop("args", ())
output = method(*args, **kwargs)
if isawaitable(output):
output = await output

Expand Down
2 changes: 2 additions & 0 deletions tests/test_cli.py
Expand Up @@ -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"],
Expand Down