parent
a86279df5e
commit
4e9b9baeab
@ -258,10 +258,12 @@ class CommandRunner(QObject):
|
||||
# 0 1 2 3
|
||||
# second split: ['--foo', '-v', 'bar baz']
|
||||
# (maxsplit=2)
|
||||
split_args = argstr.split()
|
||||
split_args = split.simple_split(argstr, keep=True)
|
||||
for i, arg in enumerate(split_args):
|
||||
arg = arg.strip()
|
||||
if not arg.startswith('-'):
|
||||
self._args = argstr.split(maxsplit=i)
|
||||
self._args = split.simple_split(argstr, keep=True,
|
||||
maxsplit=i)
|
||||
break
|
||||
else:
|
||||
# If there are only flags, we got it right on the first try
|
||||
|
@ -129,3 +129,34 @@ class SplitTests(unittest.TestCase):
|
||||
with self.subTest(cmd=cmd):
|
||||
items = split.split(cmd, keep=True)
|
||||
self.assertEqual(items, out[1].split('|'))
|
||||
|
||||
|
||||
class SimpleSplitTests(unittest.TestCase):
|
||||
|
||||
"""Test simple_split."""
|
||||
|
||||
TESTS = {
|
||||
' foo bar': [' foo', ' bar'],
|
||||
'foobar': ['foobar'],
|
||||
' foo bar baz ': [' foo', ' bar', ' baz', ' '],
|
||||
'f\ti\ts\th': ['f', '\ti', '\ts', '\th'],
|
||||
'foo\nbar': ['foo', '\nbar'],
|
||||
}
|
||||
|
||||
def test_str_split(self):
|
||||
"""Test if the behaviour matches str.split."""
|
||||
for test in self.TESTS:
|
||||
with self.subTest(string=test):
|
||||
self.assertEqual(split.simple_split(test), test.split())
|
||||
|
||||
def test_str_split_maxsplit_1(self):
|
||||
"""Test if the behaviour matches str.split with maxsplit=1."""
|
||||
string = "foo bar baz"
|
||||
self.assertEqual(split.simple_split(string, maxsplit=1),
|
||||
string.split(maxsplit=1))
|
||||
|
||||
def test_split_keep(self):
|
||||
"""Test splitting with keep=True."""
|
||||
for test, expected in self.TESTS.items():
|
||||
with self.subTest(string=test):
|
||||
self.assertEqual(split.simple_split(test, keep=True), expected)
|
||||
|
@ -19,6 +19,8 @@
|
||||
|
||||
"""Our own fork of shlex.split with some added and removed features."""
|
||||
|
||||
import re
|
||||
|
||||
from qutebrowser.utils import log
|
||||
|
||||
|
||||
@ -144,3 +146,38 @@ def split(s, keep=False):
|
||||
out.append(spaces)
|
||||
|
||||
return out
|
||||
|
||||
|
||||
def simple_split(s, keep=False, maxsplit=0):
|
||||
"""Split a string on whitespace, optionally keeping the whitespace.
|
||||
|
||||
Args:
|
||||
s: The string to split.
|
||||
keep: Whether to keep whitespace.
|
||||
maxsplit: The maximum count of splits.
|
||||
|
||||
Return:
|
||||
A list of split strings.
|
||||
"""
|
||||
whitespace = '\n\t '
|
||||
if keep:
|
||||
pattern = '([' + whitespace + '])'
|
||||
else:
|
||||
pattern = '[' + whitespace + ']'
|
||||
parts = re.split(pattern, s, maxsplit)
|
||||
if keep:
|
||||
out = []
|
||||
ws = ''
|
||||
for part in parts:
|
||||
if not part:
|
||||
continue
|
||||
elif part in whitespace:
|
||||
ws += part
|
||||
else:
|
||||
out.append(ws + part)
|
||||
ws = ''
|
||||
if ws:
|
||||
out.append(ws)
|
||||
else:
|
||||
out = [p for p in parts if p]
|
||||
return out
|
||||
|
Loading…
Reference in New Issue
Block a user