def _split_args(self, argstr): """Split the arguments from an arg string.""" if argstr is None: self._args = [] elif self._cmd.split: self._args = utils.safe_shlex_split(argstr) else: # If split=False, we still want to split the flags, but not # everything after that. # We first split the arg string and check the index of the first # non-flag args, then we re-split again properly. # example: # # input: "--foo -v bar baz" # first split: ['--foo', '-v', 'bar', 'baz'] # 0 1 2 3 # second split: ['--foo', '-v', 'bar baz'] # (maxsplit=2) split_args = argstr.split() for i, arg in enumerate(split_args): if not arg.startswith('-'): self._args = argstr.split(maxsplit=i) break else: # If there are only flags, we got it right on the first try # already. self._args = split_args
def test_escaped_single(self): """Test safe_shlex_split with a single escaped string.""" items = utils.safe_shlex_split(r"one 'two'\'' three' four") self.assertEqual(items, ['one', "two' three", 'four'])
def test_escaped(self): """Test safe_shlex_split with a normal escaped string.""" items = utils.safe_shlex_split(r'one "two\" three" four') self.assertEqual(items, ['one', 'two" three', 'four'])
def test_single_quoted(self): """Test safe_shlex_split with a single quoted string.""" items = utils.safe_shlex_split("one 'two three' four") self.assertEqual(items, ['one', 'two three', 'four'])
def test_quoted(self): """Test safe_shlex_split with a normally quoted string.""" items = utils.safe_shlex_split('one "two three" four') self.assertEqual(items, ['one', 'two three', 'four'])
def test_normal(self): """Test safe_shlex_split with a simple string.""" items = utils.safe_shlex_split('one two') self.assertEqual(items, ['one', 'two'])
def test_unfinished_escape(self): """Test safe_shlex_split with an unfinished escape.""" items = utils.safe_shlex_split('one\\') self.assertEqual(items, ['one\\'])
def test_unbalanced_single_quotes(self): """Test safe_shlex_split with unbalanded single quotes.""" items = utils.safe_shlex_split(r"one 'two three") self.assertEqual(items, ['one', "two three"])
def test_unbalanced_quotes(self): """Test safe_shlex_split with unbalanded quotes.""" items = utils.safe_shlex_split(r'one "two three') self.assertEqual(items, ['one', 'two three'])
def test_both(self): """Test safe_shlex_split with an unfinished escape and quotes..""" items = utils.safe_shlex_split('one "two\\') self.assertEqual(items, ['one', 'two\\'])