Handle command-line argument errors properly

At some point we stopped handling exceptions from get_common_options properly.
This commit is contained in:
Aldo Cortesi 2016-07-18 17:09:55 +12:00
parent d2268ddb1e
commit bd733e1232
2 changed files with 37 additions and 29 deletions

View File

@ -6,6 +6,7 @@ import re
import configargparse
from mitmproxy import exceptions
from mitmproxy import filt
from mitmproxy.proxy import config
from netlib import human
@ -149,17 +150,17 @@ def get_common_options(args):
try:
p = parse_replace_hook(i)
except ParseException as e:
raise configargparse.ArgumentTypeError(e)
raise exceptions.OptionsError(e)
reps.append(p)
for i in args.replace_file:
try:
patt, rex, path = parse_replace_hook(i)
except ParseException as e:
raise configargparse.ArgumentTypeError(e)
raise exceptions.OptionsError(e)
try:
v = open(path, "rb").read()
except IOError as e:
raise configargparse.ArgumentTypeError(
raise exceptions.OptionsError(
"Could not read replace file: %s" % path
)
reps.append((patt, rex, v))
@ -169,17 +170,17 @@ def get_common_options(args):
try:
p = parse_setheader(i)
except ParseException as e:
raise configargparse.ArgumentTypeError(e)
raise exceptions.OptionsError(e)
setheaders.append(p)
if args.outfile and args.outfile[0] == args.rfile:
if args.outfile[1] == "wb":
raise configargparse.ArgumentTypeError(
raise exceptions.OptionsError(
"Cannot use '{}' for both reading and writing flows. "
"Are you looking for --afile?".format(args.rfile)
)
else:
raise configargparse.ArgumentTypeError(
raise exceptions.OptionsError(
"Cannot use '{}' for both reading and appending flows. "
"That would trigger an infinite loop."
)
@ -194,7 +195,12 @@ def get_common_options(args):
body_size_limit = args.body_size_limit
if body_size_limit:
try:
body_size_limit = human.parse_size(body_size_limit)
except ValueError, e:
raise exceptions.OptionsError(
"Invalid body size limit specification: %s" % body_size_limit
)
return dict(
app=args.app,

View File

@ -64,7 +64,10 @@ def mitmproxy(args=None): # pragma: no cover
parser = cmdline.mitmproxy()
args = parser.parse_args(args)
console_options = console.master.Options(**cmdline.get_common_options(args))
try:
console_options = console.master.Options(
**cmdline.get_common_options(args)
)
console_options.palette = args.palette
console_options.palette_transparent = args.palette_transparent
console_options.eventlog = args.eventlog
@ -73,7 +76,6 @@ def mitmproxy(args=None): # pragma: no cover
console_options.limit = args.limit
console_options.no_mouse = args.no_mouse
try:
proxy_config = process_options(parser, console_options, args)
server = get_server(console_options.no_server, proxy_config)
m = console.master.ConsoleMaster(server, console_options)
@ -96,12 +98,12 @@ def mitmdump(args=None): # pragma: no cover
if args.quiet:
args.flow_detail = 0
try:
dump_options = dump.Options(**cmdline.get_common_options(args))
dump_options.flow_detail = args.flow_detail
dump_options.keepserving = args.keepserving
dump_options.filtstr = " ".join(args.args) if args.args else None
try:
proxy_config = process_options(parser, dump_options, args)
server = get_server(dump_options.no_server, proxy_config)
master = dump.DumpMaster(server, dump_options)
@ -130,6 +132,7 @@ def mitmweb(args=None): # pragma: no cover
args = parser.parse_args(args)
try:
web_options = web.master.Options(**cmdline.get_common_options(args))
web_options.intercept = args.intercept
web_options.wdebug = args.wdebug
@ -139,7 +142,6 @@ def mitmweb(args=None): # pragma: no cover
web_options.whtpasswd = args.whtpasswd
web_options.process_web_options(parser)
try:
proxy_config = process_options(parser, web_options, args)
server = get_server(web_options.no_server, proxy_config)
m = web.master.WebMaster(server, web_options)