Carefully re-add a small number of short flags

The mechanism for booleans attaches the short flag to whatever the opposite of
the default is.
This commit is contained in:
Aldo Cortesi 2017-03-07 20:57:54 +13:00
parent aed780bf48
commit 98ec3b77fe
6 changed files with 48 additions and 39 deletions

View File

@ -86,8 +86,8 @@ class Options(optmanager.OptManager):
"Continue serving after client playback or file read." "Continue serving after client playback or file read."
) )
self.add_option( self.add_option(
"no_server", False, bool, "server", True, bool,
"Don't start a proxy server." "Start a proxy server."
) )
self.add_option( self.add_option(
"server_replay_nopop", False, bool, "server_replay_nopop", False, bool,

View File

@ -362,18 +362,35 @@ class OptManager:
else: # pragma: no cover else: # pragma: no cover
raise NotImplementedError("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, short=None):
o = self._options[optname] o = self._options[optname]
f = optname.replace("_", "-")
def mkf(l, s):
l = l.replace("_", "-")
f = ["--%s" % l]
if s:
f.append("-" + s)
return f
flags = mkf(optname, short)
if o.typespec == bool: if o.typespec == bool:
g = parser.add_mutually_exclusive_group(required=False) g = parser.add_mutually_exclusive_group(required=False)
onf = mkf(optname, None)
offf = mkf("no-" + optname, None)
# The short option for a bool goes to whatever is NOT the default
if short:
if o.default:
offf = mkf("no-" + optname, short)
else:
onf = mkf(optname, short)
g.add_argument( g.add_argument(
"--no-%s" % f, *offf,
action="store_false", action="store_false",
dest=optname, dest=optname,
) )
g.add_argument( g.add_argument(
"--%s" % f, *onf,
action="store_true", action="store_true",
dest=optname, dest=optname,
help=o.help help=o.help
@ -381,7 +398,7 @@ class OptManager:
parser.set_defaults(**{optname: None}) parser.set_defaults(**{optname: None})
elif o.typespec in (int, typing.Optional[int]): elif o.typespec in (int, typing.Optional[int]):
parser.add_argument( parser.add_argument(
"--%s" % f, *flags,
action="store", action="store",
type=int, type=int,
dest=optname, dest=optname,
@ -390,7 +407,7 @@ class OptManager:
) )
elif o.typespec in (str, typing.Optional[str]): elif o.typespec in (str, typing.Optional[str]):
parser.add_argument( parser.add_argument(
"--%s" % f, *flags,
action="store", action="store",
type=str, type=str,
dest=optname, dest=optname,
@ -400,7 +417,7 @@ class OptManager:
) )
elif o.typespec == typing.Sequence[str]: elif o.typespec == typing.Sequence[str]:
parser.add_argument( parser.add_argument(
"--%s" % f, *flags,
action="append", action="append",
type=str, type=str,
dest=optname, dest=optname,

View File

@ -42,7 +42,6 @@ def common_options(parser, opts):
are emptied. are emptied.
""" """
) )
parser.add_argument( parser.add_argument(
"-q", "--quiet", "-q", "--quiet",
action="store_true", dest="quiet", action="store_true", dest="quiet",
@ -55,59 +54,50 @@ def common_options(parser, opts):
) )
# Basic options # Basic options
opts.make_parser(parser, "mode") opts.make_parser(parser, "mode", short="m")
opts.make_parser(parser, "anticache") opts.make_parser(parser, "anticache")
opts.make_parser(parser, "showhost") opts.make_parser(parser, "showhost")
opts.make_parser(parser, "rfile") opts.make_parser(parser, "rfile", metavar="PATH", short="r")
opts.make_parser(parser, "scripts", metavar="SCRIPT") opts.make_parser(parser, "scripts", metavar="SCRIPT", short="s")
opts.make_parser(parser, "stickycookie", metavar="FILTER") opts.make_parser(parser, "stickycookie", metavar="FILTER")
opts.make_parser(parser, "stickyauth", metavar="FILTER") opts.make_parser(parser, "stickyauth", metavar="FILTER")
opts.make_parser(parser, "streamfile") opts.make_parser(parser, "streamfile", metavar="PATH", short="w")
opts.make_parser(parser, "anticomp") opts.make_parser(parser, "anticomp")
# Proxy options # Proxy options
group = parser.add_argument_group("Proxy Options") group = parser.add_argument_group("Proxy Options")
opts.make_parser(group, "listen_host", metavar="HOST") opts.make_parser(group, "listen_host", metavar="HOST")
opts.make_parser(group, "listen_port", metavar="PORT", short="p")
opts.make_parser(group, "server", short="n")
opts.make_parser(group, "ignore_hosts", metavar="HOST") opts.make_parser(group, "ignore_hosts", metavar="HOST")
opts.make_parser(group, "tcp_hosts", metavar="HOST") opts.make_parser(group, "tcp_hosts", metavar="HOST")
opts.make_parser(group, "no_server")
opts.make_parser(group, "listen_port", metavar="PORT")
opts.make_parser(group, "upstream_auth", metavar="USER:PASS") opts.make_parser(group, "upstream_auth", metavar="USER:PASS")
opts.make_parser(group, "proxyauth", metavar="SPEC")
opts.make_parser(group, "rawtcp") opts.make_parser(group, "rawtcp")
# Proxy SSL options # Proxy SSL options
group = parser.add_argument_group("SSL") group = parser.add_argument_group("SSL")
opts.make_parser(group, "certs", metavar="SPEC") opts.make_parser(group, "certs", metavar="SPEC")
opts.make_parser(group, "ssl_insecure") opts.make_parser(group, "ssl_insecure", short="k")
# Client replay # Client replay
group = parser.add_argument_group("Client Replay") group = parser.add_argument_group("Client Replay")
opts.make_parser(group, "client_replay", metavar="PATH") opts.make_parser(group, "client_replay", metavar="PATH", short="C")
# Server replay # Server replay
group = parser.add_argument_group("Server Replay") group = parser.add_argument_group("Server Replay")
opts.make_parser(group, "server_replay", metavar="PATH") opts.make_parser(group, "server_replay", metavar="PATH", short="S")
opts.make_parser(group, "replay_kill_extra") opts.make_parser(group, "replay_kill_extra")
opts.make_parser(group, "server_replay_nopop") opts.make_parser(group, "server_replay_nopop")
# Replacements # Replacements
group = parser.add_argument_group("Replacements") group = parser.add_argument_group("Replacements")
opts.make_parser(group, "replacements", metavar="PATTERN") opts.make_parser(group, "replacements", metavar="PATTERN", short="R")
opts.make_parser(group, "replacement_files", metavar="PATTERN") opts.make_parser(group, "replacement_files", metavar="PATTERN")
# Set headers # Set headers
group = parser.add_argument_group("Set Headers") group = parser.add_argument_group("Set Headers")
opts.make_parser(group, "setheaders", metavar="PATTERN") opts.make_parser(group, "setheaders", metavar="PATTERN", short="H")
# Proxy authentication
group = parser.add_argument_group(
"Proxy Authentication",
"""
Specify which users are allowed to access the proxy and the method
used for authenticating them.
"""
).add_mutually_exclusive_group()
opts.make_parser(group, "proxyauth", metavar="SPEC")
def mitmproxy(opts): def mitmproxy(opts):

View File

@ -23,7 +23,7 @@ class DumpMaster(master.Master):
if with_dumper: if with_dumper:
self.addons.add(dumper.Dumper()) self.addons.add(dumper.Dumper())
if not self.options.no_server: if self.options.server:
self.add_log( self.add_log(
"Proxy server listening at http://{}:{}".format(server.address[0], server.address[1]), "Proxy server listening at http://{}:{}".format(server.address[0], server.address[1]),
"info" "info"

View File

@ -55,14 +55,14 @@ def process_options(parser, opts, args):
opts.merge(adict) opts.merge(adict)
pconf = config.ProxyConfig(opts) pconf = config.ProxyConfig(opts)
if opts.no_server: if opts.server:
return server.DummyServer(pconf)
else:
try: try:
return server.ProxyServer(pconf) return server.ProxyServer(pconf)
except exceptions.ServerException as v: except exceptions.ServerException as v:
print(str(v), file=sys.stderr) print(str(v), file=sys.stderr)
sys.exit(1) sys.exit(1)
else:
return server.DummyServer(pconf)
def run(MasterKlass, args): # pragma: no cover def run(MasterKlass, args): # pragma: no cover

View File

@ -294,6 +294,7 @@ class TTypes(optmanager.OptManager):
self.add_option("str", "str", str, "help") self.add_option("str", "str", str, "help")
self.add_option("optstr", "optstr", typing.Optional[str], "help", "help") self.add_option("optstr", "optstr", typing.Optional[str], "help", "help")
self.add_option("bool", False, bool, "help") self.add_option("bool", False, bool, "help")
self.add_option("bool_on", True, bool, "help")
self.add_option("int", 0, int, "help") self.add_option("int", 0, int, "help")
self.add_option("optint", 0, typing.Optional[int], "help") self.add_option("optint", 0, typing.Optional[int], "help")
self.add_option("seqstr", [], typing.Sequence[str], "help") self.add_option("seqstr", [], typing.Sequence[str], "help")
@ -303,10 +304,11 @@ class TTypes(optmanager.OptManager):
def test_make_parser(): def test_make_parser():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
opts = TTypes() opts = TTypes()
opts.make_parser(parser, "str") opts.make_parser(parser, "str", short="a")
opts.make_parser(parser, "bool") opts.make_parser(parser, "bool", short="b")
opts.make_parser(parser, "int") opts.make_parser(parser, "int", short="c")
opts.make_parser(parser, "seqstr") opts.make_parser(parser, "seqstr", short="d")
opts.make_parser(parser, "bool_on", short="e")
with pytest.raises(ValueError): with pytest.raises(ValueError):
opts.make_parser(parser, "unknown") opts.make_parser(parser, "unknown")