Add better inspector arg parsing (#2642)

This commit is contained in:
Adam Hopkins 2022-12-26 12:27:40 +02:00 committed by GitHub
parent c573019e7f
commit 28f5b3c301
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 7 deletions

View File

@ -148,8 +148,17 @@ Or, a path to a directory to run as a simple HTTP server:
if unknown: if unknown:
for arg in unknown: for arg in unknown:
if arg.startswith("--"): if arg.startswith("--"):
key, value = arg.split("=") try:
setattr(self.args, key.lstrip("-"), value) 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__} kwargs = {**self.args.__dict__}
host = kwargs.pop("host") host = kwargs.pop("host")

View File

@ -47,11 +47,16 @@ def make_inspector_parser(parser: ArgumentParser) -> None:
action=SanicSubParsersAction, action=SanicSubParsersAction,
dest="action", dest="action",
description=( description=(
"Run one of the below subcommands. If you have created a custom " "Run one or none of the below subcommands. Using inspect without "
"Inspector instance, then you can run custom commands. See ___ " "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." "for more details."
), ),
title="Required\n========\n Subcommands", title=" Subcommands",
parser_class=InspectorSubParser, parser_class=InspectorSubParser,
) )
reloader = subparsers.add_parser( reloader = subparsers.add_parser(

View File

@ -126,7 +126,9 @@ class Config(dict, metaclass=DescriptorMeta):
def __init__( def __init__(
self, 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, env_prefix: Optional[str] = SANIC_PREFIX,
keep_alive: Optional[bool] = None, keep_alive: Optional[bool] = None,
*, *,

View File

@ -71,7 +71,8 @@ class Inspector:
kwargs = {} kwargs = {}
if request.body: if request.body:
kwargs = request.json kwargs = request.json
output = method(**kwargs) args = kwargs.pop("args", ())
output = method(*args, **kwargs)
if isawaitable(output): if isawaitable(output):
output = await output output = await output

View File

@ -326,6 +326,8 @@ def test_inspector_inspect(urlopen, caplog, capsys):
(["shutdown"], {}), (["shutdown"], {}),
(["scale", "9"], {"replicas": 9}), (["scale", "9"], {"replicas": 9}),
(["foo", "--bar=something"], {"bar": "something"}), (["foo", "--bar=something"], {"bar": "something"}),
(["foo", "--bar"], {"bar": True}),
(["foo", "--no-bar"], {"bar": False}),
(["foo", "positional"], {"args": ["positional"]}), (["foo", "positional"], {"args": ["positional"]}),
( (
["foo", "positional", "--bar=something"], ["foo", "positional", "--bar=something"],