Lex a string into a list of tokens. Shortcuts and aliases are expanded and comments are removed. :param line: the command line being lexed :return: A list of tokens :raises Cmd2ShlexError: if a shlex error occurs (e.g. No closing quotation)
(self, line: str)
| 397 | return valid, errmsg |
| 398 | |
| 399 | def tokenize(self, line: str) -> list[str]: |
| 400 | """Lex a string into a list of tokens. Shortcuts and aliases are expanded and comments are removed. |
| 401 | |
| 402 | :param line: the command line being lexed |
| 403 | :return: A list of tokens |
| 404 | :raises Cmd2ShlexError: if a shlex error occurs (e.g. No closing quotation) |
| 405 | """ |
| 406 | # expand shortcuts and aliases |
| 407 | line = self._expand(line) |
| 408 | |
| 409 | # check if this line is a comment |
| 410 | if line.lstrip().startswith(constants.COMMENT_CHAR): |
| 411 | return [] |
| 412 | |
| 413 | # split on whitespace |
| 414 | try: |
| 415 | tokens = shlex_split(line) |
| 416 | except ValueError as ex: |
| 417 | raise Cmd2ShlexError(ex) from None |
| 418 | |
| 419 | # custom lexing |
| 420 | return self.split_on_punctuation(tokens) |
| 421 | |
| 422 | def parse(self, line: str) -> Statement: |
| 423 | """Tokenize the input and parse it into a [cmd2.parsing.Statement][] object. |