Merge pull request #2896 from cortesi/cmdesc

Fix an issue with quoting and commands
This commit is contained in:
Aldo Cortesi 2018-02-23 13:32:44 +13:00 committed by GitHub
commit 06807414c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 3 deletions

View File

@ -68,6 +68,21 @@ class CommandBuffer:
else:
self._cursor = x
def maybequote(self, value):
if " " in value and not value.startswith("\""):
return "\"%s\"" % value
return value
def parse_quoted(self, txt):
parts, remhelp = self.master.commands.parse_partial(txt)
for i, p in enumerate(parts):
parts[i] = mitmproxy.command.ParseResult(
value = self.maybequote(p.value),
type = p.type,
valid = p.valid
)
return parts, remhelp
def render(self):
"""
This function is somewhat tricky - in order to make the cursor
@ -75,7 +90,7 @@ class CommandBuffer:
character-for-character offset match in the rendered output, up
to the cursor. Beyond that, we can add stuff.
"""
parts, remhelp = self.master.commands.parse_partial(self.text)
parts, remhelp = self.parse_quoted(self.text)
ret = []
for p in parts:
if p.valid:
@ -95,8 +110,9 @@ class CommandBuffer:
return ret
def flatten(self, txt):
parts, _ = self.master.commands.parse_partial(txt)
return " ".join([x.value for x in parts])
parts, _ = self.parse_quoted(txt)
ret = [x.value for x in parts]
return " ".join(ret)
def left(self) -> None:
self.cursor = self.cursor - 1

View File

@ -211,6 +211,22 @@ class TestCommand:
],
[]
],
[
"flow \"one two",
[
command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
command.ParseResult(value = "\"one two", type = flow.Flow, valid = False),
],
["str"]
],
[
"flow \"one two\"",
[
command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
command.ParseResult(value = "one two", type = flow.Flow, valid = False),
],
["str"]
],
]
with taddons.context() as tctx:
tctx.master.addons.add(TAddon())