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

Method onecmd

cmd2/cmd2.py:3362–3395  ·  view source on GitHub ↗

Execute the actual do_* method for a command. If the command provided doesn't exist, then it executes default() instead. :param statement: intended to be a Statement instance parsed command from the input stream, alternative acceptance of a str is present

(self, statement: Statement | str, *, add_to_history: bool = True)

Source from the content-addressed store, hash-verified

3360 return category
3361
3362 def onecmd(self, statement: Statement | str, *, add_to_history: bool = True) -> bool:
3363 """Execute the actual do_* method for a command.
3364
3365 If the command provided doesn't exist, then it executes default() instead.
3366
3367 :param statement: intended to be a Statement instance parsed command from the input stream, alternative
3368 acceptance of a str is present only for backward compatibility with cmd
3369 :param add_to_history: If True, then add this command to history. Defaults to True.
3370 :return: a flag indicating whether the interpretation of commands should stop
3371 """
3372 # For backwards compatibility with cmd, allow a str to be passed in
3373 if not isinstance(statement, Statement):
3374 statement = self._input_line_to_statement(statement)
3375
3376 command_func = self.get_command_func(statement.command)
3377 if command_func:
3378 # Check to see if this command should be stored in history
3379 if (
3380 statement.command not in self.exclude_from_history
3381 and statement.command not in self.disabled_commands
3382 and add_to_history
3383 ):
3384 self.history.append(statement)
3385
3386 try:
3387 self.current_command = statement
3388 stop = command_func(statement)
3389 finally:
3390 self.current_command = None
3391
3392 else:
3393 stop = self.default(statement)
3394
3395 return stop if stop is not None else False
3396
3397 def default(self, statement: Statement) -> bool | None:
3398 """Execute when the command given isn't a recognized command implemented by a do_* method.

Callers 6

onecmd_plus_hooksMethod · 0.95
test_custom_stdoutFunction · 0.95
test_onecmd_raw_str_quitFunction · 0.80

Calls 4

get_command_funcMethod · 0.95
defaultMethod · 0.95
appendMethod · 0.80

Tested by 5

test_custom_stdoutFunction · 0.76
test_onecmd_raw_str_quitFunction · 0.64