We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Confusing confdir argument
My tests are messy cause I used mitmproxy and mitmdump, both of them in 4.0.1 and 4.0.3 and now I mixed all cases.

At some point, I think I had an error saying that --confdir was deprecated and had to use "--set confdir=" (I can't reproduce this case though with mitmproxy or mitmdump...)

I spent some time to try to make "--set confdir=" work in a weird bash script and arguments with quotes and maybe it failed due to me.
But I realized --confdir was still working eventually for mitmdump in 4.0.3.

Question to sum up:
Is it attended to have both "--confdir" and "--set confdir=" working at the same time for mitmproxy & mitmdump?
If yes, help (-h) should specify it clearly with something like: "--confdir PATH, --set confdir=PATH"
If not, one of them should be deleted.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `mitmproxy/tools/cmdline.py`
Content:
```
1 import argparse
2 
3 from mitmproxy.addons import core
4 
5 
6 def common_options(parser, opts):
7     parser.add_argument(
8         '--version',
9         action='store_true',
10         help="show version number and exit",
11         dest='version',
12     )
13     parser.add_argument(
14         '--options',
15         action='store_true',
16         help="Show all options and their default values",
17     )
18     parser.add_argument(
19         '--commands',
20         action='store_true',
21         help="Show all commands and their signatures",
22     )
23     parser.add_argument(
24         "--confdir",
25         type=str, dest="confdir", default=core.CONF_DIR,
26         metavar="PATH",
27         help="Path to the mitmproxy config directory"
28     )
29     parser.add_argument(
30         "--set",
31         type=str, dest="setoptions", default=[],
32         action="append",
33         metavar="option[=value]",
34         help="""
35             Set an option. When the value is omitted, booleans are set to true,
36             strings and integers are set to None (if permitted), and sequences
37             are emptied. Boolean values can be true, false or toggle.
38         """
39     )
40     parser.add_argument(
41         "-q", "--quiet",
42         action="store_true", dest="quiet",
43         help="Quiet."
44     )
45     parser.add_argument(
46         "-v", "--verbose",
47         action="store_const", dest="verbose", const='debug',
48         help="Increase log verbosity."
49     )
50 
51     # Basic options
52     opts.make_parser(parser, "mode", short="m")
53     opts.make_parser(parser, "anticache")
54     opts.make_parser(parser, "showhost")
55     opts.make_parser(parser, "rfile", metavar="PATH", short="r")
56     opts.make_parser(parser, "scripts", metavar="SCRIPT", short="s")
57     opts.make_parser(parser, "stickycookie", metavar="FILTER")
58     opts.make_parser(parser, "stickyauth", metavar="FILTER")
59     opts.make_parser(parser, "save_stream_file", metavar="PATH", short="w")
60     opts.make_parser(parser, "anticomp")
61 
62     # Proxy options
63     group = parser.add_argument_group("Proxy Options")
64     opts.make_parser(group, "listen_host", metavar="HOST")
65     opts.make_parser(group, "listen_port", metavar="PORT", short="p")
66     opts.make_parser(group, "server", short="n")
67     opts.make_parser(group, "ignore_hosts", metavar="HOST")
68     opts.make_parser(group, "tcp_hosts", metavar="HOST")
69     opts.make_parser(group, "upstream_auth", metavar="USER:PASS")
70     opts.make_parser(group, "proxyauth", metavar="SPEC")
71     opts.make_parser(group, "rawtcp")
72     opts.make_parser(group, "http2")
73 
74     # Proxy SSL options
75     group = parser.add_argument_group("SSL")
76     opts.make_parser(group, "certs", metavar="SPEC")
77     opts.make_parser(group, "ssl_insecure", short="k")
78 
79     # Client replay
80     group = parser.add_argument_group("Client Replay")
81     opts.make_parser(group, "client_replay", metavar="PATH", short="C")
82 
83     # Server replay
84     group = parser.add_argument_group("Server Replay")
85     opts.make_parser(group, "server_replay", metavar="PATH", short="S")
86     opts.make_parser(group, "server_replay_kill_extra")
87     opts.make_parser(group, "server_replay_nopop")
88 
89     # Replacements
90     group = parser.add_argument_group("Replacements")
91     opts.make_parser(group, "replacements", metavar="PATTERN", short="R")
92 
93     # Set headers
94     group = parser.add_argument_group("Set Headers")
95     opts.make_parser(group, "setheaders", metavar="PATTERN", short="H")
96 
97 
98 def mitmproxy(opts):
99     parser = argparse.ArgumentParser(usage="%(prog)s [options]")
100     common_options(parser, opts)
101 
102     opts.make_parser(parser, "console_layout")
103     opts.make_parser(parser, "console_layout_headers")
104     group = parser.add_argument_group(
105         "Filters",
106         "See help in mitmproxy for filter expression syntax."
107     )
108     opts.make_parser(group, "intercept", metavar="FILTER")
109     opts.make_parser(group, "view_filter", metavar="FILTER")
110     return parser
111 
112 
113 def mitmdump(opts):
114     parser = argparse.ArgumentParser(usage="%(prog)s [options] [filter]")
115 
116     common_options(parser, opts)
117     opts.make_parser(parser, "flow_detail", metavar = "LEVEL")
118     parser.add_argument(
119         'filter_args',
120         nargs="...",
121         help="""
122             Filter expression, equivalent to setting both the view_filter
123             and save_stream_filter options.
124         """
125     )
126     return parser
127 
128 
129 def mitmweb(opts):
130     parser = argparse.ArgumentParser(usage="%(prog)s [options]")
131 
132     group = parser.add_argument_group("Mitmweb")
133     opts.make_parser(group, "web_open_browser")
134     opts.make_parser(group, "web_port", metavar="PORT")
135     opts.make_parser(group, "web_iface", metavar="INTERFACE")
136 
137     common_options(parser, opts)
138     group = parser.add_argument_group(
139         "Filters",
140         "See help in mitmproxy for filter expression syntax."
141     )
142     opts.make_parser(group, "intercept", metavar="FILTER")
143     return parser
144
```

Path: `mitmproxy/tools/_main.py`
Content:
```
1 """
2 This file contains python3.6+ syntax!
3 Feel free to import and use whatever new package you deem necessary.
4 """
5 
6 import os
7 import sys
8 import asyncio
9 import argparse  # noqa
10 import signal  # noqa
11 import typing  # noqa
12 
13 from mitmproxy.tools import cmdline  # noqa
14 from mitmproxy import exceptions, master  # noqa
15 from mitmproxy import options  # noqa
16 from mitmproxy import optmanager  # noqa
17 from mitmproxy import proxy  # noqa
18 from mitmproxy import log  # noqa
19 from mitmproxy.utils import debug, arg_check  # noqa
20 
21 OPTIONS_FILE_NAME = "config.yaml"
22 
23 
24 def assert_utf8_env():
25     spec = ""
26     for i in ["LANG", "LC_CTYPE", "LC_ALL"]:
27         spec += os.environ.get(i, "").lower()
28     if "utf" not in spec:
29         print(
30             "Error: mitmproxy requires a UTF console environment.",
31             file=sys.stderr
32         )
33         print(
34             "Set your LANG environment variable to something like en_US.UTF-8",
35             file=sys.stderr
36         )
37         sys.exit(1)
38 
39 
40 def process_options(parser, opts, args):
41     if args.version:
42         print(debug.dump_system_info())
43         sys.exit(0)
44     if args.quiet or args.options or args.commands:
45         # also reduce log verbosity if --options or --commands is passed,
46         # we don't want log messages from regular startup then.
47         args.termlog_verbosity = 'error'
48         args.flow_detail = 0
49     if args.verbose:
50         args.termlog_verbosity = 'debug'
51         args.flow_detail = 2
52 
53     adict = {}
54     for n in dir(args):
55         if n in opts:
56             adict[n] = getattr(args, n)
57     opts.merge(adict)
58 
59     return proxy.config.ProxyConfig(opts)
60 
61 
62 def run(
63         master_cls: typing.Type[master.Master],
64         make_parser: typing.Callable[[options.Options], argparse.ArgumentParser],
65         arguments: typing.Sequence[str],
66         extra: typing.Callable[[typing.Any], dict] = None
67 ) -> master.Master:  # pragma: no cover
68     """
69         extra: Extra argument processing callable which returns a dict of
70         options.
71     """
72     debug.register_info_dumpers()
73 
74     opts = options.Options()
75     master = master_cls(opts)
76 
77     parser = make_parser(opts)
78 
79     # To make migration from 2.x to 3.0 bearable.
80     if "-R" in sys.argv and sys.argv[sys.argv.index("-R") + 1].startswith("http"):
81         print("-R is used for specifying replacements.\n"
82               "To use mitmproxy in reverse mode please use --mode reverse:SPEC instead")
83 
84     try:
85         args = parser.parse_args(arguments)
86     except SystemExit:
87         arg_check.check()
88         sys.exit(1)
89     try:
90         opts.confdir = args.confdir
91         optmanager.load_paths(
92             opts,
93             os.path.join(opts.confdir, OPTIONS_FILE_NAME),
94         )
95         pconf = process_options(parser, opts, args)
96         server: typing.Any = None
97         if pconf.options.server:
98             try:
99                 server = proxy.server.ProxyServer(pconf)
100             except exceptions.ServerException as v:
101                 print(str(v), file=sys.stderr)
102                 sys.exit(1)
103         else:
104             server = proxy.server.DummyServer(pconf)
105 
106         master.server = server
107         if args.options:
108             print(optmanager.dump_defaults(opts))
109             sys.exit(0)
110         if args.commands:
111             master.commands.dump()
112             sys.exit(0)
113         opts.set(*args.setoptions, defer=True)
114         if extra:
115             opts.update(**extra(args))
116 
117         loop = asyncio.get_event_loop()
118         for signame in ('SIGINT', 'SIGTERM'):
119             try:
120                 loop.add_signal_handler(getattr(signal, signame), master.shutdown)
121             except NotImplementedError:
122                 # Not supported on Windows
123                 pass
124 
125         # Make sure that we catch KeyboardInterrupts on Windows.
126         # https://stackoverflow.com/a/36925722/934719
127         if os.name == "nt":
128             async def wakeup():
129                 while True:
130                     await asyncio.sleep(0.2)
131             asyncio.ensure_future(wakeup())
132 
133         master.run()
134     except exceptions.OptionsError as e:
135         print("%s: %s" % (sys.argv[0], e), file=sys.stderr)
136         sys.exit(1)
137     except (KeyboardInterrupt, RuntimeError):
138         pass
139     return master
140 
141 
142 def mitmproxy(args=None) -> typing.Optional[int]:  # pragma: no cover
143     if os.name == "nt":
144         print("Error: mitmproxy's console interface is not supported on Windows. "
145               "You can run mitmdump or mitmweb instead.", file=sys.stderr)
146         return 1
147     assert_utf8_env()
148     from mitmproxy.tools import console
149     run(console.master.ConsoleMaster, cmdline.mitmproxy, args)
150     return None
151 
152 
153 def mitmdump(args=None) -> typing.Optional[int]:  # pragma: no cover
154     from mitmproxy.tools import dump
155 
156     def extra(args):
157         if args.filter_args:
158             v = " ".join(args.filter_args)
159             return dict(
160                 save_stream_filter=v,
161                 readfile_filter=v,
162                 dumper_filter=v,
163             )
164         return {}
165 
166     m = run(dump.DumpMaster, cmdline.mitmdump, args, extra)
167     if m and m.errorcheck.has_errored:  # type: ignore
168         return 1
169     return None
170 
171 
172 def mitmweb(args=None) -> typing.Optional[int]:  # pragma: no cover
173     from mitmproxy.tools import web
174     run(web.master.WebMaster, cmdline.mitmweb, args)
175     return None
176
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/mitmproxy/tools/_main.py b/mitmproxy/tools/_main.py
--- a/mitmproxy/tools/_main.py
+++ b/mitmproxy/tools/_main.py
@@ -87,7 +87,7 @@
         arg_check.check()
         sys.exit(1)
     try:
-        opts.confdir = args.confdir
+        opts.set(*args.setoptions, defer=True)
         optmanager.load_paths(
             opts,
             os.path.join(opts.confdir, OPTIONS_FILE_NAME),
@@ -110,7 +110,6 @@
         if args.commands:
             master.commands.dump()
             sys.exit(0)
-        opts.set(*args.setoptions, defer=True)
         if extra:
             opts.update(**extra(args))
 
diff --git a/mitmproxy/tools/cmdline.py b/mitmproxy/tools/cmdline.py
--- a/mitmproxy/tools/cmdline.py
+++ b/mitmproxy/tools/cmdline.py
@@ -20,12 +20,6 @@
         action='store_true',
         help="Show all commands and their signatures",
     )
-    parser.add_argument(
-        "--confdir",
-        type=str, dest="confdir", default=core.CONF_DIR,
-        metavar="PATH",
-        help="Path to the mitmproxy config directory"
-    )
     parser.add_argument(
         "--set",
         type=str, dest="setoptions", default=[],

{"golden_diff": "diff --git a/mitmproxy/tools/_main.py b/mitmproxy/tools/_main.py\n--- a/mitmproxy/tools/_main.py\n+++ b/mitmproxy/tools/_main.py\n@@ -87,7 +87,7 @@\n         arg_check.check()\n         sys.exit(1)\n     try:\n-        opts.confdir = args.confdir\n+        opts.set(*args.setoptions, defer=True)\n         optmanager.load_paths(\n             opts,\n             os.path.join(opts.confdir, OPTIONS_FILE_NAME),\n@@ -110,7 +110,6 @@\n         if args.commands:\n             master.commands.dump()\n             sys.exit(0)\n-        opts.set(*args.setoptions, defer=True)\n         if extra:\n             opts.update(**extra(args))\n \ndiff --git a/mitmproxy/tools/cmdline.py b/mitmproxy/tools/cmdline.py\n--- a/mitmproxy/tools/cmdline.py\n+++ b/mitmproxy/tools/cmdline.py\n@@ -20,12 +20,6 @@\n         action='store_true',\n         help=\"Show all commands and their signatures\",\n     )\n-    parser.add_argument(\n-        \"--confdir\",\n-        type=str, dest=\"confdir\", default=core.CONF_DIR,\n-        metavar=\"PATH\",\n-        help=\"Path to the mitmproxy config directory\"\n-    )\n     parser.add_argument(\n         \"--set\",\n         type=str, dest=\"setoptions\", default=[],\n", "issue": "Confusing confdir argument\nMy tests are messy cause I used mitmproxy and mitmdump, both of them in 4.0.1 and 4.0.3 and now I mixed all cases.\r\n\r\nAt some point, I think I had an error saying that --confdir was deprecated and had to use \"--set confdir=\" (I can't reproduce this case though with mitmproxy or mitmdump...)\r\n\r\nI spent some time to try to make \"--set confdir=\" work in a weird bash script and arguments with quotes and maybe it failed due to me.\r\nBut I realized --confdir was still working eventually for mitmdump in 4.0.3.\r\n\r\nQuestion to sum up:\r\nIs it attended to have both \"--confdir\" and \"--set confdir=\" working at the same time for mitmproxy & mitmdump?\r\nIf yes, help (-h) should specify it clearly with something like: \"--confdir PATH, --set confdir=PATH\"\r\nIf not, one of them should be deleted.\n", "before_files": [{"content": "import argparse\n\nfrom mitmproxy.addons import core\n\n\ndef common_options(parser, opts):\n    parser.add_argument(\n        '--version',\n        action='store_true',\n        help=\"show version number and exit\",\n        dest='version',\n    )\n    parser.add_argument(\n        '--options',\n        action='store_true',\n        help=\"Show all options and their default values\",\n    )\n    parser.add_argument(\n        '--commands',\n        action='store_true',\n        help=\"Show all commands and their signatures\",\n    )\n    parser.add_argument(\n        \"--confdir\",\n        type=str, dest=\"confdir\", default=core.CONF_DIR,\n        metavar=\"PATH\",\n        help=\"Path to the mitmproxy config directory\"\n    )\n    parser.add_argument(\n        \"--set\",\n        type=str, dest=\"setoptions\", default=[],\n        action=\"append\",\n        metavar=\"option[=value]\",\n        help=\"\"\"\n            Set an option. When the value is omitted, booleans are set to true,\n            strings and integers are set to None (if permitted), and sequences\n            are emptied. Boolean values can be true, false or toggle.\n        \"\"\"\n    )\n    parser.add_argument(\n        \"-q\", \"--quiet\",\n        action=\"store_true\", dest=\"quiet\",\n        help=\"Quiet.\"\n    )\n    parser.add_argument(\n        \"-v\", \"--verbose\",\n        action=\"store_const\", dest=\"verbose\", const='debug',\n        help=\"Increase log verbosity.\"\n    )\n\n    # Basic options\n    opts.make_parser(parser, \"mode\", short=\"m\")\n    opts.make_parser(parser, \"anticache\")\n    opts.make_parser(parser, \"showhost\")\n    opts.make_parser(parser, \"rfile\", metavar=\"PATH\", short=\"r\")\n    opts.make_parser(parser, \"scripts\", metavar=\"SCRIPT\", short=\"s\")\n    opts.make_parser(parser, \"stickycookie\", metavar=\"FILTER\")\n    opts.make_parser(parser, \"stickyauth\", metavar=\"FILTER\")\n    opts.make_parser(parser, \"save_stream_file\", metavar=\"PATH\", short=\"w\")\n    opts.make_parser(parser, \"anticomp\")\n\n    # Proxy options\n    group = parser.add_argument_group(\"Proxy Options\")\n    opts.make_parser(group, \"listen_host\", metavar=\"HOST\")\n    opts.make_parser(group, \"listen_port\", metavar=\"PORT\", short=\"p\")\n    opts.make_parser(group, \"server\", short=\"n\")\n    opts.make_parser(group, \"ignore_hosts\", metavar=\"HOST\")\n    opts.make_parser(group, \"tcp_hosts\", metavar=\"HOST\")\n    opts.make_parser(group, \"upstream_auth\", metavar=\"USER:PASS\")\n    opts.make_parser(group, \"proxyauth\", metavar=\"SPEC\")\n    opts.make_parser(group, \"rawtcp\")\n    opts.make_parser(group, \"http2\")\n\n    # Proxy SSL options\n    group = parser.add_argument_group(\"SSL\")\n    opts.make_parser(group, \"certs\", metavar=\"SPEC\")\n    opts.make_parser(group, \"ssl_insecure\", short=\"k\")\n\n    # Client replay\n    group = parser.add_argument_group(\"Client Replay\")\n    opts.make_parser(group, \"client_replay\", metavar=\"PATH\", short=\"C\")\n\n    # Server replay\n    group = parser.add_argument_group(\"Server Replay\")\n    opts.make_parser(group, \"server_replay\", metavar=\"PATH\", short=\"S\")\n    opts.make_parser(group, \"server_replay_kill_extra\")\n    opts.make_parser(group, \"server_replay_nopop\")\n\n    # Replacements\n    group = parser.add_argument_group(\"Replacements\")\n    opts.make_parser(group, \"replacements\", metavar=\"PATTERN\", short=\"R\")\n\n    # Set headers\n    group = parser.add_argument_group(\"Set Headers\")\n    opts.make_parser(group, \"setheaders\", metavar=\"PATTERN\", short=\"H\")\n\n\ndef mitmproxy(opts):\n    parser = argparse.ArgumentParser(usage=\"%(prog)s [options]\")\n    common_options(parser, opts)\n\n    opts.make_parser(parser, \"console_layout\")\n    opts.make_parser(parser, \"console_layout_headers\")\n    group = parser.add_argument_group(\n        \"Filters\",\n        \"See help in mitmproxy for filter expression syntax.\"\n    )\n    opts.make_parser(group, \"intercept\", metavar=\"FILTER\")\n    opts.make_parser(group, \"view_filter\", metavar=\"FILTER\")\n    return parser\n\n\ndef mitmdump(opts):\n    parser = argparse.ArgumentParser(usage=\"%(prog)s [options] [filter]\")\n\n    common_options(parser, opts)\n    opts.make_parser(parser, \"flow_detail\", metavar = \"LEVEL\")\n    parser.add_argument(\n        'filter_args',\n        nargs=\"...\",\n        help=\"\"\"\n            Filter expression, equivalent to setting both the view_filter\n            and save_stream_filter options.\n        \"\"\"\n    )\n    return parser\n\n\ndef mitmweb(opts):\n    parser = argparse.ArgumentParser(usage=\"%(prog)s [options]\")\n\n    group = parser.add_argument_group(\"Mitmweb\")\n    opts.make_parser(group, \"web_open_browser\")\n    opts.make_parser(group, \"web_port\", metavar=\"PORT\")\n    opts.make_parser(group, \"web_iface\", metavar=\"INTERFACE\")\n\n    common_options(parser, opts)\n    group = parser.add_argument_group(\n        \"Filters\",\n        \"See help in mitmproxy for filter expression syntax.\"\n    )\n    opts.make_parser(group, \"intercept\", metavar=\"FILTER\")\n    return parser\n", "path": "mitmproxy/tools/cmdline.py"}, {"content": "\"\"\"\nThis file contains python3.6+ syntax!\nFeel free to import and use whatever new package you deem necessary.\n\"\"\"\n\nimport os\nimport sys\nimport asyncio\nimport argparse  # noqa\nimport signal  # noqa\nimport typing  # noqa\n\nfrom mitmproxy.tools import cmdline  # noqa\nfrom mitmproxy import exceptions, master  # noqa\nfrom mitmproxy import options  # noqa\nfrom mitmproxy import optmanager  # noqa\nfrom mitmproxy import proxy  # noqa\nfrom mitmproxy import log  # noqa\nfrom mitmproxy.utils import debug, arg_check  # noqa\n\nOPTIONS_FILE_NAME = \"config.yaml\"\n\n\ndef assert_utf8_env():\n    spec = \"\"\n    for i in [\"LANG\", \"LC_CTYPE\", \"LC_ALL\"]:\n        spec += os.environ.get(i, \"\").lower()\n    if \"utf\" not in spec:\n        print(\n            \"Error: mitmproxy requires a UTF console environment.\",\n            file=sys.stderr\n        )\n        print(\n            \"Set your LANG environment variable to something like en_US.UTF-8\",\n            file=sys.stderr\n        )\n        sys.exit(1)\n\n\ndef process_options(parser, opts, args):\n    if args.version:\n        print(debug.dump_system_info())\n        sys.exit(0)\n    if args.quiet or args.options or args.commands:\n        # also reduce log verbosity if --options or --commands is passed,\n        # we don't want log messages from regular startup then.\n        args.termlog_verbosity = 'error'\n        args.flow_detail = 0\n    if args.verbose:\n        args.termlog_verbosity = 'debug'\n        args.flow_detail = 2\n\n    adict = {}\n    for n in dir(args):\n        if n in opts:\n            adict[n] = getattr(args, n)\n    opts.merge(adict)\n\n    return proxy.config.ProxyConfig(opts)\n\n\ndef run(\n        master_cls: typing.Type[master.Master],\n        make_parser: typing.Callable[[options.Options], argparse.ArgumentParser],\n        arguments: typing.Sequence[str],\n        extra: typing.Callable[[typing.Any], dict] = None\n) -> master.Master:  # pragma: no cover\n    \"\"\"\n        extra: Extra argument processing callable which returns a dict of\n        options.\n    \"\"\"\n    debug.register_info_dumpers()\n\n    opts = options.Options()\n    master = master_cls(opts)\n\n    parser = make_parser(opts)\n\n    # To make migration from 2.x to 3.0 bearable.\n    if \"-R\" in sys.argv and sys.argv[sys.argv.index(\"-R\") + 1].startswith(\"http\"):\n        print(\"-R is used for specifying replacements.\\n\"\n              \"To use mitmproxy in reverse mode please use --mode reverse:SPEC instead\")\n\n    try:\n        args = parser.parse_args(arguments)\n    except SystemExit:\n        arg_check.check()\n        sys.exit(1)\n    try:\n        opts.confdir = args.confdir\n        optmanager.load_paths(\n            opts,\n            os.path.join(opts.confdir, OPTIONS_FILE_NAME),\n        )\n        pconf = process_options(parser, opts, args)\n        server: typing.Any = None\n        if pconf.options.server:\n            try:\n                server = proxy.server.ProxyServer(pconf)\n            except exceptions.ServerException as v:\n                print(str(v), file=sys.stderr)\n                sys.exit(1)\n        else:\n            server = proxy.server.DummyServer(pconf)\n\n        master.server = server\n        if args.options:\n            print(optmanager.dump_defaults(opts))\n            sys.exit(0)\n        if args.commands:\n            master.commands.dump()\n            sys.exit(0)\n        opts.set(*args.setoptions, defer=True)\n        if extra:\n            opts.update(**extra(args))\n\n        loop = asyncio.get_event_loop()\n        for signame in ('SIGINT', 'SIGTERM'):\n            try:\n                loop.add_signal_handler(getattr(signal, signame), master.shutdown)\n            except NotImplementedError:\n                # Not supported on Windows\n                pass\n\n        # Make sure that we catch KeyboardInterrupts on Windows.\n        # https://stackoverflow.com/a/36925722/934719\n        if os.name == \"nt\":\n            async def wakeup():\n                while True:\n                    await asyncio.sleep(0.2)\n            asyncio.ensure_future(wakeup())\n\n        master.run()\n    except exceptions.OptionsError as e:\n        print(\"%s: %s\" % (sys.argv[0], e), file=sys.stderr)\n        sys.exit(1)\n    except (KeyboardInterrupt, RuntimeError):\n        pass\n    return master\n\n\ndef mitmproxy(args=None) -> typing.Optional[int]:  # pragma: no cover\n    if os.name == \"nt\":\n        print(\"Error: mitmproxy's console interface is not supported on Windows. \"\n              \"You can run mitmdump or mitmweb instead.\", file=sys.stderr)\n        return 1\n    assert_utf8_env()\n    from mitmproxy.tools import console\n    run(console.master.ConsoleMaster, cmdline.mitmproxy, args)\n    return None\n\n\ndef mitmdump(args=None) -> typing.Optional[int]:  # pragma: no cover\n    from mitmproxy.tools import dump\n\n    def extra(args):\n        if args.filter_args:\n            v = \" \".join(args.filter_args)\n            return dict(\n                save_stream_filter=v,\n                readfile_filter=v,\n                dumper_filter=v,\n            )\n        return {}\n\n    m = run(dump.DumpMaster, cmdline.mitmdump, args, extra)\n    if m and m.errorcheck.has_errored:  # type: ignore\n        return 1\n    return None\n\n\ndef mitmweb(args=None) -> typing.Optional[int]:  # pragma: no cover\n    from mitmproxy.tools import web\n    run(web.master.WebMaster, cmdline.mitmweb, args)\n    return None\n", "path": "mitmproxy/tools/_main.py"}], "after_files": [{"content": "import argparse\n\nfrom mitmproxy.addons import core\n\n\ndef common_options(parser, opts):\n    parser.add_argument(\n        '--version',\n        action='store_true',\n        help=\"show version number and exit\",\n        dest='version',\n    )\n    parser.add_argument(\n        '--options',\n        action='store_true',\n        help=\"Show all options and their default values\",\n    )\n    parser.add_argument(\n        '--commands',\n        action='store_true',\n        help=\"Show all commands and their signatures\",\n    )\n    parser.add_argument(\n        \"--set\",\n        type=str, dest=\"setoptions\", default=[],\n        action=\"append\",\n        metavar=\"option[=value]\",\n        help=\"\"\"\n            Set an option. When the value is omitted, booleans are set to true,\n            strings and integers are set to None (if permitted), and sequences\n            are emptied. Boolean values can be true, false or toggle.\n        \"\"\"\n    )\n    parser.add_argument(\n        \"-q\", \"--quiet\",\n        action=\"store_true\", dest=\"quiet\",\n        help=\"Quiet.\"\n    )\n    parser.add_argument(\n        \"-v\", \"--verbose\",\n        action=\"store_const\", dest=\"verbose\", const='debug',\n        help=\"Increase log verbosity.\"\n    )\n\n    # Basic options\n    opts.make_parser(parser, \"mode\", short=\"m\")\n    opts.make_parser(parser, \"anticache\")\n    opts.make_parser(parser, \"showhost\")\n    opts.make_parser(parser, \"rfile\", metavar=\"PATH\", short=\"r\")\n    opts.make_parser(parser, \"scripts\", metavar=\"SCRIPT\", short=\"s\")\n    opts.make_parser(parser, \"stickycookie\", metavar=\"FILTER\")\n    opts.make_parser(parser, \"stickyauth\", metavar=\"FILTER\")\n    opts.make_parser(parser, \"save_stream_file\", metavar=\"PATH\", short=\"w\")\n    opts.make_parser(parser, \"anticomp\")\n\n    # Proxy options\n    group = parser.add_argument_group(\"Proxy Options\")\n    opts.make_parser(group, \"listen_host\", metavar=\"HOST\")\n    opts.make_parser(group, \"listen_port\", metavar=\"PORT\", short=\"p\")\n    opts.make_parser(group, \"server\", short=\"n\")\n    opts.make_parser(group, \"ignore_hosts\", metavar=\"HOST\")\n    opts.make_parser(group, \"tcp_hosts\", metavar=\"HOST\")\n    opts.make_parser(group, \"upstream_auth\", metavar=\"USER:PASS\")\n    opts.make_parser(group, \"proxyauth\", metavar=\"SPEC\")\n    opts.make_parser(group, \"rawtcp\")\n    opts.make_parser(group, \"http2\")\n\n    # Proxy SSL options\n    group = parser.add_argument_group(\"SSL\")\n    opts.make_parser(group, \"certs\", metavar=\"SPEC\")\n    opts.make_parser(group, \"ssl_insecure\", short=\"k\")\n\n    # Client replay\n    group = parser.add_argument_group(\"Client Replay\")\n    opts.make_parser(group, \"client_replay\", metavar=\"PATH\", short=\"C\")\n\n    # Server replay\n    group = parser.add_argument_group(\"Server Replay\")\n    opts.make_parser(group, \"server_replay\", metavar=\"PATH\", short=\"S\")\n    opts.make_parser(group, \"server_replay_kill_extra\")\n    opts.make_parser(group, \"server_replay_nopop\")\n\n    # Replacements\n    group = parser.add_argument_group(\"Replacements\")\n    opts.make_parser(group, \"replacements\", metavar=\"PATTERN\", short=\"R\")\n\n    # Set headers\n    group = parser.add_argument_group(\"Set Headers\")\n    opts.make_parser(group, \"setheaders\", metavar=\"PATTERN\", short=\"H\")\n\n\ndef mitmproxy(opts):\n    parser = argparse.ArgumentParser(usage=\"%(prog)s [options]\")\n    common_options(parser, opts)\n\n    opts.make_parser(parser, \"console_layout\")\n    opts.make_parser(parser, \"console_layout_headers\")\n    group = parser.add_argument_group(\n        \"Filters\",\n        \"See help in mitmproxy for filter expression syntax.\"\n    )\n    opts.make_parser(group, \"intercept\", metavar=\"FILTER\")\n    opts.make_parser(group, \"view_filter\", metavar=\"FILTER\")\n    return parser\n\n\ndef mitmdump(opts):\n    parser = argparse.ArgumentParser(usage=\"%(prog)s [options] [filter]\")\n\n    common_options(parser, opts)\n    opts.make_parser(parser, \"flow_detail\", metavar = \"LEVEL\")\n    parser.add_argument(\n        'filter_args',\n        nargs=\"...\",\n        help=\"\"\"\n            Filter expression, equivalent to setting both the view_filter\n            and save_stream_filter options.\n        \"\"\"\n    )\n    return parser\n\n\ndef mitmweb(opts):\n    parser = argparse.ArgumentParser(usage=\"%(prog)s [options]\")\n\n    group = parser.add_argument_group(\"Mitmweb\")\n    opts.make_parser(group, \"web_open_browser\")\n    opts.make_parser(group, \"web_port\", metavar=\"PORT\")\n    opts.make_parser(group, \"web_iface\", metavar=\"INTERFACE\")\n\n    common_options(parser, opts)\n    group = parser.add_argument_group(\n        \"Filters\",\n        \"See help in mitmproxy for filter expression syntax.\"\n    )\n    opts.make_parser(group, \"intercept\", metavar=\"FILTER\")\n    return parser\n", "path": "mitmproxy/tools/cmdline.py"}, {"content": "\"\"\"\nThis file contains python3.6+ syntax!\nFeel free to import and use whatever new package you deem necessary.\n\"\"\"\n\nimport os\nimport sys\nimport asyncio\nimport argparse  # noqa\nimport signal  # noqa\nimport typing  # noqa\n\nfrom mitmproxy.tools import cmdline  # noqa\nfrom mitmproxy import exceptions, master  # noqa\nfrom mitmproxy import options  # noqa\nfrom mitmproxy import optmanager  # noqa\nfrom mitmproxy import proxy  # noqa\nfrom mitmproxy import log  # noqa\nfrom mitmproxy.utils import debug, arg_check  # noqa\n\nOPTIONS_FILE_NAME = \"config.yaml\"\n\n\ndef assert_utf8_env():\n    spec = \"\"\n    for i in [\"LANG\", \"LC_CTYPE\", \"LC_ALL\"]:\n        spec += os.environ.get(i, \"\").lower()\n    if \"utf\" not in spec:\n        print(\n            \"Error: mitmproxy requires a UTF console environment.\",\n            file=sys.stderr\n        )\n        print(\n            \"Set your LANG environment variable to something like en_US.UTF-8\",\n            file=sys.stderr\n        )\n        sys.exit(1)\n\n\ndef process_options(parser, opts, args):\n    if args.version:\n        print(debug.dump_system_info())\n        sys.exit(0)\n    if args.quiet or args.options or args.commands:\n        # also reduce log verbosity if --options or --commands is passed,\n        # we don't want log messages from regular startup then.\n        args.termlog_verbosity = 'error'\n        args.flow_detail = 0\n    if args.verbose:\n        args.termlog_verbosity = 'debug'\n        args.flow_detail = 2\n\n    adict = {}\n    for n in dir(args):\n        if n in opts:\n            adict[n] = getattr(args, n)\n    opts.merge(adict)\n\n    return proxy.config.ProxyConfig(opts)\n\n\ndef run(\n        master_cls: typing.Type[master.Master],\n        make_parser: typing.Callable[[options.Options], argparse.ArgumentParser],\n        arguments: typing.Sequence[str],\n        extra: typing.Callable[[typing.Any], dict] = None\n) -> master.Master:  # pragma: no cover\n    \"\"\"\n        extra: Extra argument processing callable which returns a dict of\n        options.\n    \"\"\"\n    debug.register_info_dumpers()\n\n    opts = options.Options()\n    master = master_cls(opts)\n\n    parser = make_parser(opts)\n\n    # To make migration from 2.x to 3.0 bearable.\n    if \"-R\" in sys.argv and sys.argv[sys.argv.index(\"-R\") + 1].startswith(\"http\"):\n        print(\"-R is used for specifying replacements.\\n\"\n              \"To use mitmproxy in reverse mode please use --mode reverse:SPEC instead\")\n\n    try:\n        args = parser.parse_args(arguments)\n    except SystemExit:\n        arg_check.check()\n        sys.exit(1)\n    try:\n        opts.set(*args.setoptions, defer=True)\n        optmanager.load_paths(\n            opts,\n            os.path.join(opts.confdir, OPTIONS_FILE_NAME),\n        )\n        pconf = process_options(parser, opts, args)\n        server: typing.Any = None\n        if pconf.options.server:\n            try:\n                server = proxy.server.ProxyServer(pconf)\n            except exceptions.ServerException as v:\n                print(str(v), file=sys.stderr)\n                sys.exit(1)\n        else:\n            server = proxy.server.DummyServer(pconf)\n\n        master.server = server\n        if args.options:\n            print(optmanager.dump_defaults(opts))\n            sys.exit(0)\n        if args.commands:\n            master.commands.dump()\n            sys.exit(0)\n        if extra:\n            opts.update(**extra(args))\n\n        loop = asyncio.get_event_loop()\n        for signame in ('SIGINT', 'SIGTERM'):\n            try:\n                loop.add_signal_handler(getattr(signal, signame), master.shutdown)\n            except NotImplementedError:\n                # Not supported on Windows\n                pass\n\n        # Make sure that we catch KeyboardInterrupts on Windows.\n        # https://stackoverflow.com/a/36925722/934719\n        if os.name == \"nt\":\n            async def wakeup():\n                while True:\n                    await asyncio.sleep(0.2)\n            asyncio.ensure_future(wakeup())\n\n        master.run()\n    except exceptions.OptionsError as e:\n        print(\"%s: %s\" % (sys.argv[0], e), file=sys.stderr)\n        sys.exit(1)\n    except (KeyboardInterrupt, RuntimeError):\n        pass\n    return master\n\n\ndef mitmproxy(args=None) -> typing.Optional[int]:  # pragma: no cover\n    if os.name == \"nt\":\n        print(\"Error: mitmproxy's console interface is not supported on Windows. \"\n              \"You can run mitmdump or mitmweb instead.\", file=sys.stderr)\n        return 1\n    assert_utf8_env()\n    from mitmproxy.tools import console\n    run(console.master.ConsoleMaster, cmdline.mitmproxy, args)\n    return None\n\n\ndef mitmdump(args=None) -> typing.Optional[int]:  # pragma: no cover\n    from mitmproxy.tools import dump\n\n    def extra(args):\n        if args.filter_args:\n            v = \" \".join(args.filter_args)\n            return dict(\n                save_stream_filter=v,\n                readfile_filter=v,\n                dumper_filter=v,\n            )\n        return {}\n\n    m = run(dump.DumpMaster, cmdline.mitmdump, args, extra)\n    if m and m.errorcheck.has_errored:  # type: ignore\n        return 1\n    return None\n\n\ndef mitmweb(args=None) -> typing.Optional[int]:  # pragma: no cover\n    from mitmproxy.tools import web\n    run(web.master.WebMaster, cmdline.mitmweb, args)\n    return None\n", "path": "mitmproxy/tools/_main.py"}]}