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

Method _input_line_to_statement

cmd2/cmd2.py:3112–3150  ·  view source on GitHub ↗

Parse the user's input line and convert it to a Statement, ensuring that all macros are also resolved. :param line: the line being parsed :return: parsed command line as a Statement :raises Cmd2ShlexError: if a shlex error occurs (e.g. No closing quotation) :raises E

(self, line: str)

Source from the content-addressed store, hash-verified

3110 raise EmptyStatement from None
3111
3112 def _input_line_to_statement(self, line: str) -> Statement:
3113 """Parse the user's input line and convert it to a Statement, ensuring that all macros are also resolved.
3114
3115 :param line: the line being parsed
3116 :return: parsed command line as a Statement
3117 :raises Cmd2ShlexError: if a shlex error occurs (e.g. No closing quotation)
3118 :raises EmptyStatement: when the resulting Statement is blank
3119 """
3120 used_macros = []
3121 orig_line = None
3122
3123 # Continue until all macros are resolved
3124 while True:
3125 # Get a complete statement (handling multiline input)
3126 statement = self._complete_statement(line)
3127
3128 # If this is the first loop iteration, save the original line
3129 if orig_line is None:
3130 orig_line = statement.raw
3131
3132 # Check if this command matches a macro and wasn't already processed to avoid an infinite loop
3133 if statement.command in self.macros and statement.command not in used_macros:
3134 used_macros.append(statement.command)
3135 try:
3136 line = self._resolve_macro(statement)
3137 except MacroError as ex:
3138 self.perror(ex)
3139 raise EmptyStatement from None
3140 else:
3141 # No macro found or already processed. The statement is complete.
3142 break
3143
3144 # Restore original 'raw' text if a macro was expanded
3145 if orig_line != statement.raw:
3146 statement_dict = statement.to_dict()
3147 statement_dict["raw"] = orig_line
3148 statement = Statement.from_dict(statement_dict)
3149
3150 return statement
3151
3152 def _resolve_macro(self, statement: Statement) -> str:
3153 """Resolve a macro and return the resulting string.

Callers 3

onecmd_plus_hooksMethod · 0.95
onecmdMethod · 0.95

Calls 6

_complete_statementMethod · 0.95
_resolve_macroMethod · 0.95
perrorMethod · 0.95
appendMethod · 0.80
to_dictMethod · 0.45
from_dictMethod · 0.45

Tested by 1