command: add command.Arg type

This type represents an argument to a command passed to another command. This
improves help text, and will be used in the partial parser to expand subcommand
types.
This commit is contained in:
Aldo Cortesi 2017-12-15 11:42:26 +13:00 committed by Aldo Cortesi
parent 1c097813c1
commit 2cfe45428a
3 changed files with 19 additions and 10 deletions

View File

@ -17,7 +17,8 @@ from mitmproxy import flow
def lexer(s):
# mypy mis-identifies shlex.shlex as abstract
lex = shlex.shlex(s, punctuation_chars=True) # type: ignore
lex = shlex.shlex(s) # type: ignore
lex.wordchars += "."
lex.whitespace_split = True
lex.commenters = ''
return lex
@ -36,6 +37,10 @@ class Cmd(str):
pass
class Arg(str):
pass
def typename(t: type, ret: bool) -> str:
"""
Translates a type to an explanatory string. If ret is True, we're
@ -157,7 +162,7 @@ class CommandManager:
Parse a possibly partial command. Return a sequence of (part, type) tuples.
"""
buf = io.StringIO(cmdstr)
parts: typing.List[str] = []
parts = [] # type: typing.List[str]
lex = lexer(buf)
while 1:
remainder = cmdstr[buf.tell():]
@ -174,8 +179,8 @@ class CommandManager:
elif cmdstr.endswith(" "):
parts.append("")
parse: typing.List[ParseResult] = []
params: typing.List[type] = []
parse = [] # type: typing.List[ParseResult]
params = [] # type: typing.List[type]
for i in range(len(parts)):
if i == 0:
params[:] = [Cmd]

View File

@ -34,9 +34,13 @@ class ListCompleter(Completer):
return ret
class CompletionState(typing.NamedTuple):
completer: Completer
parse: typing.Sequence[mitmproxy.command.ParseResult]
CompletionState = typing.NamedTuple(
"CompletionState",
[
("completer", Completer),
("parse", typing.Sequence[mitmproxy.command.ParseResult])
]
)
class CommandBuffer():

View File

@ -228,7 +228,7 @@ class ConsoleAddon:
prompt: str,
choices: typing.Sequence[str],
cmd: command.Cmd,
*args: str,
*args: command.Arg
) -> None:
"""
Prompt the user to choose from a specified list of strings, then
@ -250,7 +250,7 @@ class ConsoleAddon:
@command.command("console.choose.cmd")
def console_choose_cmd(
self, prompt: str, choicecmd: command.Cmd, *cmd: str
self, prompt: str, choicecmd: command.Cmd, *cmd: command.Arg
) -> None:
"""
Prompt the user to choose from a list of strings returned by a
@ -501,7 +501,7 @@ class ConsoleAddon:
contexts: typing.Sequence[str],
key: str,
cmd: command.Cmd,
*args: str,
*args: command.Arg
) -> None:
"""
Bind a shortcut key.