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

Method parse_command_only

cmd2/parsing.py:557–614  ·  view source on GitHub ↗

Identify the command and arguments from raw input. Partially parse input into a [cmd2.PartialStatement][] object. The command is identified, and shortcuts and aliases are expanded. Multiline commands are identified, but terminators and output redirection are not par

(self, rawinput: str)

Source from the content-addressed store, hash-verified

555 )
556
557 def parse_command_only(self, rawinput: str) -> PartialStatement:
558 """Identify the command and arguments from raw input.
559
560 Partially parse input into a [cmd2.PartialStatement][] object.
561
562 The command is identified, and shortcuts and aliases are expanded.
563 Multiline commands are identified, but terminators and output
564 redirection are not parsed.
565
566 This method is optimized for completion code and gracefully handles
567 unclosed quotes without raising exceptions.
568
569 [cmd2.parsing.PartialStatement.args][] will include all output redirection
570 clauses and command terminators.
571
572 Note:
573 Unlike [cmd2.parsing.StatementParser.parse][], this method
574 preserves internal whitespace within the args. It ensures
575 args has no leading whitespace, and it strips trailing
576 whitespace only if all quotes are closed.
577
578 :param rawinput: the command line as entered by the user
579 :return: a [cmd2.PartialStatement][] object representing the split input
580
581 """
582 # Expand shortcuts and aliases
583 line = self._expand(rawinput)
584
585 command = ""
586 args = ""
587 match = self._command_pattern.search(line)
588
589 if match:
590 # Extract the resolved command
591 command = match.group(1)
592
593 # If the command is empty, the input was either empty or started with
594 # something like a redirector ('>') or terminator (';').
595 if command:
596 # args is everything after the command match
597 args = line[match.end(1) :].lstrip()
598
599 try:
600 # Check for closed quotes
601 shlex_split(args)
602 except ValueError:
603 # Unclosed quote: preserve trailing whitespace for completion context.
604 pass
605 else:
606 # Quotes are closed: strip trailing whitespace
607 args = args.rstrip()
608
609 return PartialStatement(
610 command=command,
611 args=args,
612 raw=rawinput,
613 multiline_command=command in self.multiline_commands,
614 )

Calls 3

_expandMethod · 0.95
shlex_splitFunction · 0.85
PartialStatementClass · 0.85