MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / tokenize

Method tokenize

cmd2/parsing.py:399–420  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.

Callers 5

parseMethod · 0.95
test_tokenize_defaultFunction · 0.80
test_tokenizeFunction · 0.80

Calls 4

_expandMethod · 0.95
split_on_punctuationMethod · 0.95
shlex_splitFunction · 0.85
Cmd2ShlexErrorClass · 0.85

Tested by 4

test_tokenize_defaultFunction · 0.64
test_tokenizeFunction · 0.64