A tiny wrapper to make script invocations nicer

This commit is contained in:
Aldo Cortesi 2016-06-14 12:34:22 +12:00
parent ccf4723505
commit e7b61995ad

View File

@ -9,12 +9,21 @@ from __future__ import absolute_import, print_function, division
import os
import shlex
import sys
import contextlib
import six
from mitmproxy import exceptions
@contextlib.contextmanager
def setargs(args):
oldargs = sys.argv
sys.argv = args
yield
sys.argv = oldargs
class Script(object):
"""
@ -113,7 +122,8 @@ class Script(object):
f = self.ns.get(name)
if f:
try:
return f(self.ctx, *args, **kwargs)
with setargs(self.args):
return f(self.ctx, *args, **kwargs)
except Exception:
six.reraise(
exceptions.ScriptException,