|
20 | 20 | from contextlib import redirect_stdout, redirect_stderr |
21 | 21 |
|
22 | 22 |
|
23 | | -class CommandResult(namedtuple_with_defaults('CommandResult', ['stdout', 'stderr', 'data'])): |
24 | | - """Encapsulates the results from a command. |
| 23 | +class CommandResult(namedtuple_with_defaults('CommandResult', ['stdout', 'stderr', 'stop', 'data'])): |
| 24 | + """Encapsulates the results from a cmd2 app command |
25 | 25 |
|
26 | 26 | Named tuple attributes |
27 | 27 | ---------------------- |
28 | | - stdout: str - Output captured from stdout while this command is executing |
29 | | - stderr: str - Output captured from stderr while this command is executing. None if no error captured. |
30 | | - data - Data returned by the command. |
| 28 | + stdout: str - output captured from stdout while this command is executing |
| 29 | + stderr: str - output captured from stderr while this command is executing. None if no error captured. |
| 30 | + stop: bool - return value of the command's corresponding do_* function |
| 31 | + data - possible data populated by the command. |
31 | 32 |
|
32 | 33 | Any combination of these fields can be used when developing a scripting API for a given command. |
33 | | - By default stdout and stderr will be captured for you. If there is additional command specific data, |
| 34 | + By default stdout, stderr, and stop will be captured for you. If there is additional command specific data, |
34 | 35 | then write that to cmd2's _last_result member. That becomes the data member of this tuple. |
35 | 36 |
|
36 | 37 | In some cases, the data member may contain everything needed for a command and storing stdout |
@@ -67,6 +68,9 @@ def __init__(self, cmd2_app): |
67 | 68 | self._cmd2_app = cmd2_app |
68 | 69 | self.cmd_echo = False |
69 | 70 |
|
| 71 | + # Tells if any of the commands run via __call__ returned True for stop |
| 72 | + self.stop = False |
| 73 | + |
70 | 74 | def __dir__(self): |
71 | 75 | """Return a custom set of attribute names""" |
72 | 76 | attributes = [] |
@@ -95,16 +99,20 @@ def __call__(self, command: str, echo: Optional[bool] = None) -> CommandResult: |
95 | 99 |
|
96 | 100 | self._cmd2_app._last_result = None |
97 | 101 |
|
| 102 | + stop = False |
98 | 103 | try: |
99 | 104 | self._cmd2_app.stdout = copy_cmd_stdout |
100 | 105 | with redirect_stdout(copy_cmd_stdout): |
101 | 106 | with redirect_stderr(copy_stderr): |
102 | | - self._cmd2_app.onecmd_plus_hooks(command, pyscript_bridge_call=True) |
| 107 | + stop = self._cmd2_app.onecmd_plus_hooks(command, pyscript_bridge_call=True) |
103 | 108 | finally: |
104 | | - self._cmd2_app.stdout = copy_cmd_stdout.inner_stream |
| 109 | + with self._cmd2_app.sigint_protection: |
| 110 | + self._cmd2_app.stdout = copy_cmd_stdout.inner_stream |
| 111 | + self.stop = stop or self.stop |
105 | 112 |
|
106 | 113 | # Save the output. If stderr is empty, set it to None. |
107 | 114 | result = CommandResult(stdout=copy_cmd_stdout.getvalue(), |
108 | 115 | stderr=copy_stderr.getvalue() if copy_stderr.getvalue() else None, |
| 116 | + stop=stop, |
109 | 117 | data=self._cmd2_app._last_result) |
110 | 118 | return result |
0 commit comments