Skip to content

Commit 28f5b3c

Browse files
authored
Add better inspector arg parsing (#2642)
1 parent c573019 commit 28f5b3c

5 files changed

Lines changed: 26 additions & 7 deletions

File tree

sanic/cli/app.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,17 @@ def _inspector(self):
148148
if unknown:
149149
for arg in unknown:
150150
if arg.startswith("--"):
151-
key, value = arg.split("=")
152-
setattr(self.args, key.lstrip("-"), value)
151+
try:
152+
key, value = arg.split("=")
153+
key = key.lstrip("-")
154+
except ValueError:
155+
value = False if arg.startswith("--no-") else True
156+
key = (
157+
arg.replace("--no-", "")
158+
.lstrip("-")
159+
.replace("-", "_")
160+
)
161+
setattr(self.args, key, value)
153162

154163
kwargs = {**self.args.__dict__}
155164
host = kwargs.pop("host")

sanic/cli/inspector.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ def make_inspector_parser(parser: ArgumentParser) -> None:
4747
action=SanicSubParsersAction,
4848
dest="action",
4949
description=(
50-
"Run one of the below subcommands. If you have created a custom "
51-
"Inspector instance, then you can run custom commands. See ___ "
50+
"Run one or none of the below subcommands. Using inspect without "
51+
"a subcommand will fetch general information about the state "
52+
"of the application instance.\n\n"
53+
"Or, you can optionally follow inspect with a subcommand. "
54+
"If you have created a custom "
55+
"Inspector instance, then you can run custom commands. See "
56+
"https://sanic.dev/en/guide/deployment/inspector.html"
5257
"for more details."
5358
),
54-
title="Required\n========\n Subcommands",
59+
title=" Subcommands",
5560
parser_class=InspectorSubParser,
5661
)
5762
reloader = subparsers.add_parser(

sanic/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ class Config(dict, metaclass=DescriptorMeta):
126126

127127
def __init__(
128128
self,
129-
defaults: Dict[str, Union[str, bool, int, float, None]] = None,
129+
defaults: Optional[
130+
Dict[str, Union[str, bool, int, float, None]]
131+
] = None,
130132
env_prefix: Optional[str] = SANIC_PREFIX,
131133
keep_alive: Optional[bool] = None,
132134
*,

sanic/worker/inspector.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ async def _action(self, request: Request, action: str):
7171
kwargs = {}
7272
if request.body:
7373
kwargs = request.json
74-
output = method(**kwargs)
74+
args = kwargs.pop("args", ())
75+
output = method(*args, **kwargs)
7576
if isawaitable(output):
7677
output = await output
7778

tests/test_cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ def test_inspector_inspect(urlopen, caplog, capsys):
326326
(["shutdown"], {}),
327327
(["scale", "9"], {"replicas": 9}),
328328
(["foo", "--bar=something"], {"bar": "something"}),
329+
(["foo", "--bar"], {"bar": True}),
330+
(["foo", "--no-bar"], {"bar": False}),
329331
(["foo", "positional"], {"args": ["positional"]}),
330332
(
331333
["foo", "positional", "--bar=something"],

0 commit comments

Comments
 (0)