diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2017-03-07 14:27:50 +1300 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2017-03-07 14:27:50 +1300 |
commit | ac3b0d69cc29b5469f6b4cc55af528f6266d42cf (patch) | |
tree | 780efa34b2ec94bea4814febca7a4a8578d9a2ac /mitmproxy/optmanager.py | |
parent | 79f5883c2fbe475269a02920aaaad053e797abec (diff) | |
download | mitmproxy-ac3b0d69cc29b5469f6b4cc55af528f6266d42cf.tar.gz mitmproxy-ac3b0d69cc29b5469f6b4cc55af528f6266d42cf.tar.bz2 mitmproxy-ac3b0d69cc29b5469f6b4cc55af528f6266d42cf.zip |
Add the --set option to set options directly
The --set option is a universal flag for setting options. Some examples:
Turn on a boolean:
mitmdump --set onboarding=false
Add a value to a sequence:
mitumdupm --set setheaders=/foo/bar/voing
Zero a sequence:
mitumdupm --set setheaders
Diffstat (limited to 'mitmproxy/optmanager.py')
-rw-r--r-- | mitmproxy/optmanager.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/mitmproxy/optmanager.py b/mitmproxy/optmanager.py index beb9084c..eb56ef2d 100644 --- a/mitmproxy/optmanager.py +++ b/mitmproxy/optmanager.py @@ -323,6 +323,45 @@ class OptManager: options=options ) + def set(self, spec): + parts = spec.split("=", maxsplit=1) + if len(parts) == 1: + optname, optval = parts[0], None + else: + optname, optval = parts[0], parts[1] + o = self._options[optname] + + if o.typespec in (str, typing.Optional[str]): + setattr(self, optname, optval) + elif o.typespec in (int, typing.Optional[int]): + if optval: + try: + optval = int(optval) + except ValueError: + raise exceptions.OptionsError("Not an integer: %s" % optval) + setattr(self, optname, optval) + elif o.typespec == bool: + if not optval or optval == "true": + v = True + elif optval == "false": + v = False + else: + raise exceptions.OptionsError( + "Boolean must be \"true\", \"false\", or have the value " "omitted (a synonym for \"true\")." + ) + setattr(self, optname, v) + elif o.typespec == typing.Sequence[str]: + if not optval: + setattr(self, optname, []) + else: + setattr( + self, + optname, + getattr(self, optname) + [optval] + ) + else: + raise ValueError("Unsupported option type: %s", o.typespec) + def make_parser(self, parser, optname, metavar=None): o = self._options[optname] f = optname.replace("_", "-") |