Пример #1
0
    def _split_args(self, cmd, argstr, keep):
        """Split the arguments from an arg string.

        Args:
            cmd: The command we're currently handling.
            argstr: An argument string.
            keep: Whether to keep special chars and whitespace

        Return:
            A list containing the split strings.
        """
        if not argstr:
            return []
        elif cmd.maxsplit is None:
            return split.split(argstr, keep=keep)
        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 = split.simple_split(argstr, keep=keep)
            flag_arg_count = 0
            for i, arg in enumerate(split_args):
                arg = arg.strip()
                if arg.startswith('-'):
                    if arg in cmd.flags_with_args:
                        flag_arg_count += 1
                else:
                    maxsplit = i + cmd.maxsplit + flag_arg_count
                    return split.simple_split(argstr,
                                              keep=keep,
                                              maxsplit=maxsplit)

            # If there are only flags, we got it right on the first try
            # already.
            return split_args
Пример #2
0
 def test_maxsplit_0_keep(self):
     """Test special case with maxsplit=0 and keep=True."""
     s = "foo  bar"
     assert split.simple_split(s, keep=True, maxsplit=0) == [s]
Пример #3
0
 def test_split_keep(self, test, expected):
     """Test splitting with keep=True."""
     assert split.simple_split(test, keep=True) == expected
Пример #4
0
 def test_str_split_maxsplit(self, s, maxsplit):
     """Test if the behavior matches str.split with given maxsplit."""
     actual = split.simple_split(s, maxsplit=maxsplit)
     expected = s.rstrip().split(maxsplit=maxsplit)
     assert actual == expected
Пример #5
0
 def test_str_split(self, test):
     """Test if the behavior matches str.split."""
     assert split.simple_split(test) == test.rstrip().split()
Пример #6
0
def test_simple_split(keep, maxsplit, s):
    split.simple_split(s, keep=keep, maxsplit=maxsplit)