diff --git a/httpie/cli.py b/httpie/cli.py index 22797a53..29a55f6d 100644 --- a/httpie/cli.py +++ b/httpie/cli.py @@ -36,14 +36,24 @@ class KeyValueType(object): """A type used with `argparse`.""" def __init__(self, *separators): self.separators = separators + self.escapes = ['\\\\' + sep for sep in separators] def __call__(self, string): found = {} + found_escapes = [] + for esc in self.escapes: + found_escapes += [m.span() for m in re.finditer(esc, string)] for sep in self.separators: - regex = '[^\\\\]' + sep - match = re.search(regex, string) - if match: - found[match.start() + 1] = sep + matches = re.finditer(sep, string) + for match in matches: + start, end = match.span() + inside_escape = False + for estart, eend in found_escapes: + if start >= estart and end <= eend: + inside_escape = True + break + if not inside_escape: + found[start] = sep if not found: #noinspection PyExceptionInherit diff --git a/tests/tests.py b/tests/tests.py index 33026720..ea77d6ae 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -76,6 +76,13 @@ class TestItemParsing(BaseTest): }) self.assertIn('bar@baz', files) + def test_escape_longsep(self): + headers, data, files = cli.parse_items([ + self.kv('bob\\:==foo'), + ]) + self.assertDictEqual(data, { + 'bob:=': 'foo', + }) def test_valid_items(self): headers, data, files = cli.parse_items([