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.
- class cmd2.py_bridge.CommandResult(stdout: str = '', stderr: str = '', stop: bool = False, data: Any = None)
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
StdSim
for more information.Note
Named tuples are immutable. The contents are there for access, not for modification.
- stdout: str
Alias for field number 0
- stderr: str
Alias for field number 1
- stop: bool
Alias for field number 2
- data: Any
Alias for field number 3