def parse(self, text: str, *, keep: bool = False) -> ParseResult: """Split the commandline text into command and arguments. Args: text: Text to parse. keep: Whether to keep special chars and whitespace. """ cmdstr, sep, argstr = text.partition(' ') if not cmdstr: raise cmdexc.NoSuchCommandError("No command given") if self._partial_match: cmdstr = self._completion_match(cmdstr) try: cmd = objects.commands[cmdstr] except KeyError: raise cmdexc.NoSuchCommandError(f'{cmdstr}: no such command') args = self._split_args(cmd, argstr, keep) if keep and args: cmdline = [cmdstr, sep + args[0]] + args[1:] elif keep: cmdline = [cmdstr, sep] else: cmdline = [cmdstr] + args[:] return ParseResult(cmd=cmd, args=args, cmdline=cmdline)
def parse(self, text, *, aliases=True, fallback=False, keep=False): """Split the commandline text into command and arguments. Args: text: Text to parse. aliases: Whether to handle aliases. fallback: Whether to do a fallback splitting when the command was unknown. keep: Whether to keep special chars and whitespace Return: A ParseResult tuple. """ cmdstr, sep, argstr = text.partition(' ') count, cmdstr = self._parse_count(cmdstr) if not cmdstr and not fallback: raise cmdexc.NoSuchCommandError("No command given") if aliases: new_cmd = self._get_alias(text) if new_cmd is not None: log.commands.debug("Re-parsing with '{}'.".format(new_cmd)) return self.parse(new_cmd, aliases=False, fallback=fallback, keep=keep) try: cmd = cmdutils.cmd_dict[cmdstr] except KeyError: if fallback: cmd = None args = None if keep: cmdstr, sep, argstr = text.partition(' ') cmdline = [cmdstr, sep] + argstr.split() else: cmdline = text.split() else: raise cmdexc.NoSuchCommandError( '{}: no such command'.format(cmdstr)) else: args = self._split_args(cmd, argstr, keep) if keep and args: cmdline = [cmdstr, sep + args[0]] + args[1:] elif keep: cmdline = [cmdstr, sep] else: cmdline = [cmdstr] + args[:] return ParseResult(cmd=cmd, args=args, cmdline=cmdline, count=count)
def parse(self, text, aliases=True, fallback=False, alias_no_args=True, keep=False): """Split the commandline text into command and arguments. Args: text: Text to parse. aliases: Whether to handle aliases. fallback: Whether to do a fallback splitting when the command was unknown. alias_no_args: Whether to apply an alias if there are no arguments. keep: Whether to keep special chars and whitespace Return: A split string commandline, e.g ['open', 'www.google.com'] """ cmdstr, sep, argstr = text.partition(' ') if not cmdstr and not fallback: raise cmdexc.NoSuchCommandError("No command given") if aliases: new_cmd = self._get_alias(text, alias_no_args) if new_cmd is not None: log.commands.debug("Re-parsing with '{}'.".format(new_cmd)) return self.parse(new_cmd, aliases=False, fallback=fallback, keep=keep) try: self._cmd = cmdutils.cmd_dict[cmdstr] except KeyError: if fallback and keep: cmdstr, sep, argstr = text.partition(' ') return [cmdstr, sep] + argstr.split() elif fallback: return text.split() else: raise cmdexc.NoSuchCommandError( '{}: no such command'.format(cmdstr)) self._split_args(argstr, keep) retargs = self._args[:] if keep and retargs: return [cmdstr, sep + retargs[0]] + retargs[1:] elif keep: return [cmdstr, sep] else: return [cmdstr] + retargs
def _parse_all_gen(self, text, *args, aliases=True, **kwargs): """Split a command on ;; and parse all parts. If the first command in the commandline is a non-split one, it only returns that. Args: text: Text to parse. aliases: Whether to handle aliases. *args/**kwargs: Passed to parse(). Yields: ParseResult tuples. """ text = text.strip().lstrip(':').strip() if not text: raise cmdexc.NoSuchCommandError("No command given") if aliases: text = self._get_alias(text, text) if ';;' in text: # Get the first command and check if it doesn't want to have ;; # split. first = text.split(';;')[0] result = self.parse(first, *args, **kwargs) if result.cmd.no_cmd_split: sub_texts = [text] else: sub_texts = [e.strip() for e in text.split(';;')] else: sub_texts = [text] for sub in sub_texts: yield self.parse(sub, *args, **kwargs)
def parse(self, text, *, fallback=False, keep=False): """Split the commandline text into command and arguments. Args: text: Text to parse. fallback: Whether to do a fallback splitting when the command was unknown. keep: Whether to keep special chars and whitespace Return: A ParseResult tuple. """ cmdstr, sep, argstr = text.partition(' ') count, cmdstr = self._parse_count(cmdstr) if not cmdstr and not fallback: raise cmdexc.NoSuchCommandError("No command given") if self._partial_match: cmdstr = self._completion_match(cmdstr) try: cmd = cmdutils.cmd_dict[cmdstr] except KeyError: if not fallback: raise cmdexc.NoSuchCommandError( '{}: no such command'.format(cmdstr)) cmdline = split.split(text, keep=keep) return ParseResult(cmd=None, args=None, cmdline=cmdline, count=count) args = self._split_args(cmd, argstr, keep) if keep and args: cmdline = [cmdstr, sep + args[0]] + args[1:] elif keep: cmdline = [cmdstr, sep] else: cmdline = [cmdstr] + args[:] return ParseResult(cmd=cmd, args=args, cmdline=cmdline, count=count)
def parse(self, text, aliases=True, fallback=False, alias_no_args=True): """Split the commandline text into command and arguments. Args: text: Text to parse. aliases: Whether to handle aliases. fallback: Whether to do a fallback splitting when the command was unknown. alias_no_args: Whether to apply an alias if there are no arguments. Return: A split string commandline, e.g ['open', 'www.google.com'] """ parts = text.strip().split(maxsplit=1) if not parts: raise cmdexc.NoSuchCommandError("No command given") elif len(parts) > 1: cmdstr, argstr = parts else: cmdstr = parts[0] argstr = None if aliases: new_cmd = self._get_alias(text, alias_no_args) if new_cmd is not None: log.commands.debug("Re-parsing with '{}'.".format(new_cmd)) return self.parse(new_cmd, aliases=False) try: self._cmd = cmdutils.cmd_dict[cmdstr] except KeyError: if fallback: parts = text.split(' ') if text.endswith(' '): parts.append('') return parts else: raise cmdexc.NoSuchCommandError( '{}: no such command'.format(cmdstr)) self._split_args(argstr) retargs = self._args[:] if text.endswith(' '): retargs.append('') return [cmdstr] + retargs