Skip to content

cmd2.py_bridge

cmd2.py_bridge

Bridges calls made inside of a Python environment to the Cmd2 host app while maintaining a reasonable degree of isolation between the two.

CommandResult

Bases: NamedTuple

Encapsulates the results from a cmd2 app command

:stdout: str - output captured from stdout while this command is executing :stderr: str - output captured from stderr while this command is executing :stop: bool - return value of onecmd_plus_hooks after it runs the given command line. :data: possible data populated by the command.

Any combination of these fields can be used when developing a scripting API for a given command. By default stdout, stderr, and stop will be captured for you. If there is additional command specific data, then write that to cmd2's last_result member. That becomes the data member of this tuple.

In some cases, the data member may contain everything needed for a command and storing stdout and stderr might just be a duplication of data that wastes memory. In that case, the StdSim can be told not to store output with its pause_storage member. While this member is True, any output sent to StdSim won't be saved in its buffer.

The code would look like this::

if isinstance(self.stdout, StdSim):
    self.stdout.pause_storage = True

if isinstance(sys.stderr, StdSim):
    sys.stderr.pause_storage = True

See cmd2.utils.StdSim for more information.

.. note::

Named tuples are immutable. The contents are there for access, not for modification.

stdout class-attribute instance-attribute

stdout = ''

stderr class-attribute instance-attribute

stderr = ''

stop class-attribute instance-attribute

stop = False

data class-attribute instance-attribute

data = None

PyBridge

PyBridge(cmd2_app, *, add_to_history=True)

Provides a Python API wrapper for application commands.

PARAMETER DESCRIPTION
cmd2_app

app being controlled by this PyBridge.

TYPE: Cmd

add_to_history

If True, then add all commands run by this PyBridge to history. Defaults to True.

TYPE: bool DEFAULT: True

Source code in cmd2/py_bridge.py
def __init__(self, cmd2_app: 'cmd2.Cmd', *, add_to_history: bool = True) -> None:
    self._cmd2_app = cmd2_app
    self._add_to_history = add_to_history
    self.cmd_echo = False

    # Tells if any of the commands run via __call__ returned True for stop
    self.stop = False

cmd_echo instance-attribute

cmd_echo = False

stop instance-attribute

stop = False