def _split_args(self, argstr, keep): """Split the arguments from an arg string. Args: argstr: An argument string. keep: Whether to keep special chars and whitespace Return: A list containing the splitted strings. """ if not argstr: self._args = [] elif self._cmd.maxsplit is None: self._args = 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.lstrip('-') in self._cmd.flags_with_args: flag_arg_count += 1 else: self._args = [] maxsplit = i + self._cmd.maxsplit + flag_arg_count args = split.simple_split(argstr, keep=keep, maxsplit=maxsplit) for s in args: # remove quotes and replace \" by " if s == '""' or s == "''": s = '' else: s = re.sub(r"""(^|[^\\])["']""", r'\1', s) s = re.sub(r"""\\(["'])""", r'\1', s) self._args.append(s) break else: # If there are only flags, we got it right on the first try # already. self._args = split_args
def set_cmd_text_command(self, text, space=False, append=False): """Preset the statusbar to some text. You can use the `{url}` and `{url:pretty}` variables here which will get replaced by the encoded/decoded URL. // Wrapper for set_cmd_text to check the arguments and allow multiple strings which will get joined. Args: text: The commandline to set. space: If given, a space is added to the end. append: If given, the text is appended to the current text. """ args = split.simple_split(text) args = runners.replace_variables(self._win_id, args) text = ' '.join(args) if space: text += ' ' if append: if not self.text(): raise cmdexc.CommandError("No current text!") text = self.text() + text if not text or text[0] not in modeparsers.STARTCHARS: raise cmdexc.CommandError( "Invalid command text '{}'.".format(text)) self.set_cmd_text(text)
def _split_args(self, cmd: command.Command, argstr: str, keep: bool) -> List[str]: """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
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
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]
def test_simple_split(keep, maxsplit, s): split.simple_split(s, keep=keep, maxsplit=maxsplit)
def test_str_split(self): """Test if the behavior matches str.split.""" for test in self.TESTS: with self.subTest(string=test): self.assertEqual(split.simple_split(test), test.rstrip().split())
def test_str_split_maxsplit_0(self): """Test if the behaviour matches str.split with maxsplit=0.""" string = " foo bar baz " self.assertEqual(split.simple_split(string, maxsplit=0), string.rstrip().split(maxsplit=0))
def test_split_keep(self, test, expected): """Test splitting with keep=True.""" assert split.simple_split(test, keep=True) == expected
def test_str_split(self, test): """Test if the behavior matches str.split.""" assert split.simple_split(test) == test.rstrip().split()
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)
def test_str_split_maxsplit_0(self): """Test if the behavior matches str.split with maxsplit=0.""" string = " foo bar baz " self.assertEqual(split.simple_split(string, maxsplit=0), string.rstrip().split(maxsplit=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
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.rstrip().split())