From 45d18ac8cba462eb4f4f73e3e63ea539b44c6f83 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 5 Mar 2017 20:45:55 +1300 Subject: [PATCH] Start unifying options and the command-line: ints Moves all integer options apart from a few tricky ones like verbosity over to auto generation. Also add a metavar argument to parser generation to support this. --- mitmproxy/options.py | 25 ++++++++++++++++++++----- mitmproxy/optmanager.py | 11 ++++++++++- mitmproxy/tools/cmdline.py | 34 +++++++--------------------------- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/mitmproxy/options.py b/mitmproxy/options.py index 1d879ac81..c91e2f88a 100644 --- a/mitmproxy/options.py +++ b/mitmproxy/options.py @@ -28,7 +28,10 @@ class Options(optmanager.OptManager): "Toggle the mitmproxy onboarding app." ) self.add_option("onboarding_host", APP_HOST, str) - self.add_option("onboarding_port", APP_PORT, int) + self.add_option( + "onboarding_port", APP_PORT, int, + help="Port to serve the onboarding app from." + ) self.add_option( "anticache", False, bool, """ @@ -75,7 +78,10 @@ class Options(optmanager.OptManager): self.add_option("stickycookie", None, Optional[str]) self.add_option("stickyauth", None, Optional[str]) self.add_option("stream_large_bodies", None, Optional[int]) - self.add_option("verbosity", 2, int) + self.add_option( + "verbosity", 2, int, + "Log verbosity." + ) self.add_option("default_contentview", "auto", str) self.add_option("streamfile", None, Optional[str]) self.add_option("streamfile_append", False, bool) @@ -111,7 +117,10 @@ class Options(optmanager.OptManager): self.add_option("clientcerts", None, Optional[str]) self.add_option("ignore_hosts", [], Sequence[str]) self.add_option("listen_host", "", str) - self.add_option("listen_port", LISTEN_PORT, int) + self.add_option( + "listen_port", LISTEN_PORT, int, + "Proxy service port." + ) self.add_option("upstream_bind_address", "", str) self.add_option("mode", "regular", str) self.add_option( @@ -199,11 +208,17 @@ class Options(optmanager.OptManager): "web_debug", False, bool, "Mitmweb debugging" ) - self.add_option("web_port", 8081, int) + self.add_option( + "web_port", 8081, int, + "Mitmweb port." + ) self.add_option("web_iface", "127.0.0.1", str) # Dump options self.add_option("filtstr", None, Optional[str]) - self.add_option("flow_detail", 1, int) + self.add_option( + "flow_detail", 1, int, + "Flow detail display level" + ) self.update(**kwargs) diff --git a/mitmproxy/optmanager.py b/mitmproxy/optmanager.py index 240e36425..c16e29997 100644 --- a/mitmproxy/optmanager.py +++ b/mitmproxy/optmanager.py @@ -312,7 +312,7 @@ class OptManager: options=options ) - def make_parser(self, parser, option): + def make_parser(self, parser, option, metavar=None): o = self._options[option] f = option.replace("_", "-") if o.typespec == bool: @@ -330,5 +330,14 @@ class OptManager: help=o.help ) parser.set_defaults(**{option: o.default}) + elif o.typespec == int: + parser.add_argument( + "--%s" % f, + action="store", + type=int, + dest=option, + help=o.help, + metavar=metavar + ) else: raise ValueError("Unsupported option type: %s", o.typespec) diff --git a/mitmproxy/tools/cmdline.py b/mitmproxy/tools/cmdline.py index 41f4cedb0..a2c7e2b82 100644 --- a/mitmproxy/tools/cmdline.py +++ b/mitmproxy/tools/cmdline.py @@ -139,8 +139,8 @@ def get_common_options(args): ciphers_server = args.ciphers_server, clientcerts = args.clientcerts, ignore_hosts = args.ignore_hosts, - listen_host = args.addr, - listen_port = args.port, + listen_host = args.listen_addr, + listen_port = args.listen_port, upstream_bind_address = args.upstream_bind_address, mode = mode, upstream_cert = args.upstream_cert, @@ -285,7 +285,7 @@ def proxy_options(parser, opts): group = parser.add_argument_group("Proxy Options") group.add_argument( "-b", "--bind-address", - action="store", type=str, dest="addr", + action="store", type=str, dest="listen_addr", help="Address to bind proxy to (defaults to all interfaces)" ) group.add_argument( @@ -312,11 +312,7 @@ def proxy_options(parser, opts): """ ) opts.make_parser(group, "no_server") - group.add_argument( - "-p", "--port", - action="store", type=int, dest="port", - help="Proxy service port." - ) + opts.make_parser(group, "listen_port", metavar="PORT") http2 = group.add_mutually_exclusive_group() opts.make_parser(http2, "http2") @@ -418,14 +414,7 @@ def onboarding_app(parser, opts): %s """ % options.APP_HOST ) - group.add_argument( - "--onboarding-port", - action="store", - dest="onboarding_port", - type=int, - metavar="80", - help="Port to serve the onboarding app from." - ) + opts.make_parser(group, "onboarding_port", metavar="PORT") def client_replay(parser, opts): @@ -611,11 +600,7 @@ def mitmdump(opts): common_options(parser, opts) opts.make_parser(parser, "keepserving") - parser.add_argument( - "-d", "--detail", - action="count", dest="flow_detail", - help="Increase flow detail display level. Can be passed multiple times." - ) + opts.make_parser(parser, "flow_detail", metavar = "LEVEL") parser.add_argument( 'filter', nargs="...", @@ -632,12 +617,7 @@ def mitmweb(opts): group = parser.add_argument_group("Mitmweb") opts.make_parser(group, "web_open_browser") - group.add_argument( - "--web-port", - action="store", type=int, dest="web_port", - metavar="PORT", - help="Mitmweb port." - ) + opts.make_parser(parser, "web_port", metavar="PORT") group.add_argument( "--web-iface", action="store", dest="web_iface",