Add type and choices to options dump commets.

This commit is contained in:
Aldo Cortesi 2017-03-07 19:29:08 +13:00
parent 320d8848ab
commit b0ba765598
2 changed files with 34 additions and 21 deletions

View File

@ -56,9 +56,7 @@ class Options(optmanager.OptManager):
"onboarding_host", APP_HOST, str, "onboarding_host", APP_HOST, str,
""" """
Domain to serve the onboarding app from. For transparent mode, use Domain to serve the onboarding app from. For transparent mode, use
an IP when a DNS entry for the app domain is not present. Default: an IP when a DNS entry for the app domain is not present. """
%s
""" % APP_HOST
) )
self.add_option( self.add_option(
"onboarding_port", APP_PORT, int, "onboarding_port", APP_PORT, int,
@ -235,7 +233,7 @@ class Options(optmanager.OptManager):
) )
self.add_option( self.add_option(
"cadir", CA_DIR, str, "cadir", CA_DIR, str,
"Location of the default mitmproxy CA files. (%s)" % CA_DIR "Location of the default mitmproxy CA files."
) )
self.add_option( self.add_option(
"certs", [], Sequence[str], "certs", [], Sequence[str],
@ -251,11 +249,11 @@ class Options(optmanager.OptManager):
) )
self.add_option( self.add_option(
"ciphers_client", DEFAULT_CLIENT_CIPHERS, str, "ciphers_client", DEFAULT_CLIENT_CIPHERS, str,
"Set supported ciphers for client connections. (OpenSSL Syntax)" "Set supported ciphers for client connections using OpenSSL syntax."
) )
self.add_option( self.add_option(
"ciphers_server", None, Optional[str], "ciphers_server", None, Optional[str],
"Set supported ciphers for server connections. (OpenSSL Syntax)" "Set supported ciphers for server connections using OpenSSL syntax."
) )
self.add_option( self.add_option(
"client_certs", None, Optional[str], "client_certs", None, Optional[str],
@ -273,7 +271,7 @@ class Options(optmanager.OptManager):
) )
self.add_option( self.add_option(
"listen_host", "", str, "listen_host", "", str,
"Address to bind proxy to (defaults to all interfaces)" "Address to bind proxy to."
) )
self.add_option( self.add_option(
"listen_port", LISTEN_PORT, int, "listen_port", LISTEN_PORT, int,
@ -281,7 +279,7 @@ class Options(optmanager.OptManager):
) )
self.add_option( self.add_option(
"upstream_bind_address", "", str, "upstream_bind_address", "", str,
"Address to bind upstream requests to (defaults to none)" "Address to bind upstream requests to."
) )
self.add_option( self.add_option(
"mode", "regular", str, "mode", "regular", str,
@ -338,7 +336,7 @@ class Options(optmanager.OptManager):
"upstream_auth", None, Optional[str], "upstream_auth", None, Optional[str],
""" """
Add HTTP Basic authentcation to upstream proxy and reverse proxy Add HTTP Basic authentcation to upstream proxy and reverse proxy
requests. Format: username:password requests. Format: username:password.
""" """
) )
self.add_option( self.add_option(
@ -396,7 +394,7 @@ class Options(optmanager.OptManager):
"Focus follows new flows." "Focus follows new flows."
) )
self.add_option( self.add_option(
"console_palette", "dark", Optional[str], "console_palette", "dark", str,
"Color palette.", "Color palette.",
choices=sorted(console_palettes), choices=sorted(console_palettes),
) )
@ -426,11 +424,11 @@ class Options(optmanager.OptManager):
# Web options # Web options
self.add_option( self.add_option(
"web_open_browser", True, bool, "web_open_browser", True, bool,
"Start a browser" "Start a browser."
) )
self.add_option( self.add_option(
"web_debug", False, bool, "web_debug", False, bool,
"Mitmweb debugging" "Mitmweb debugging."
) )
self.add_option( self.add_option(
"web_port", 8081, int, "web_port", 8081, int,
@ -444,11 +442,11 @@ class Options(optmanager.OptManager):
# Dump options # Dump options
self.add_option( self.add_option(
"filtstr", None, Optional[str], "filtstr", None, Optional[str],
"The filter string for mitmdump" "The filter string for mitmdump."
) )
self.add_option( self.add_option(
"flow_detail", 1, int, "flow_detail", 1, int,
"Flow detail display level" "Flow detail display level."
) )
self.update(**kwargs) self.update(**kwargs)

View File

@ -359,8 +359,8 @@ class OptManager:
optname, optname,
getattr(self, optname) + [optval] getattr(self, optname) + [optval]
) )
else: else: # pragma: no cover
raise ValueError("Unsupported option type: %s", o.typespec) raise NotImplementedError("Unsupported option type: %s", o.typespec)
def make_parser(self, parser, optname, metavar=None): def make_parser(self, parser, optname, metavar=None):
o = self._options[optname] o = self._options[optname]
@ -421,10 +421,25 @@ def dump(opts):
for k in sorted(opts.keys()): for k in sorted(opts.keys()):
o = opts._options[k] o = opts._options[k]
s[k] = o.default s[k] = o.default
s.yaml_set_comment_before_after_key( txt = o.help.strip()
k,
before = "\n" + "\n".join(textwrap.wrap( if o.choices:
textwrap.dedent(o.help.strip()) txt += " Valid values are %s." % ", ".join(repr(c) for c in o.choices)
)), else:
if o.typespec in (str, int, bool):
t = o.typespec.__name__
elif o.typespec == typing.Optional[str]:
t = "optional str"
elif o.typespec == typing.Sequence[str]:
t = "sequence of str"
else: # pragma: no cover
raise NotImplementedError
txt += " Type %s." % t
txt = "\n".join(
textwrap.wrap(
textwrap.dedent(txt)
) )
)
s.yaml_set_comment_before_after_key(k, before = "\n" + txt)
return ruamel.yaml.round_trip_dump(s) return ruamel.yaml.round_trip_dump(s)