Skip to content

Commit 6b38d41

Browse files
committed
Transcript generation no longer terminates _cmdloop() when a command returns True for stop
1 parent 57c64c4 commit 6b38d41

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

cmd2/cmd2.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3428,7 +3428,7 @@ def do_history(self, args: argparse.Namespace) -> Optional[bool]:
34283428
except Exception as e:
34293429
self.perror('Saving {!r} - {}'.format(args.output_file, e), traceback_war=False)
34303430
elif args.transcript:
3431-
return self._generate_transcript(history, args.transcript)
3431+
self._generate_transcript(history, args.transcript)
34323432
else:
34333433
# Display the history items retrieved
34343434
for hi in history:
@@ -3507,10 +3507,9 @@ def _persist_history(self):
35073507
msg = "can not write persistent history file '{}': {}"
35083508
self.perror(msg.format(self.persistent_history_file, ex), traceback_war=False)
35093509

3510-
def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcript_file: str) -> Optional[bool]:
3510+
def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcript_file: str) -> None:
35113511
"""
35123512
Generate a transcript file from a given history of commands
3513-
:return: True if running of commands should stop
35143513
"""
35153514
# Validate the transcript file path to make sure directory exists and write access is available
35163515
transcript_path = os.path.abspath(os.path.expanduser(transcript_file))
@@ -3521,7 +3520,6 @@ def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcrip
35213520
return
35223521

35233522
commands_run = 0
3524-
stop = False
35253523
try:
35263524
with self.sigint_protection:
35273525
# Disable echo while we manually redirect stdout to a StringIO buffer
@@ -3573,6 +3571,11 @@ def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcrip
35733571
self.echo = saved_echo
35743572
self.stdout = saved_stdout
35753573

3574+
# Check if all commands ran
3575+
if commands_run < len(history):
3576+
warning = "Command {} triggered a stop and ended transcript generation early".format(commands_run)
3577+
self.perror(warning, err_color=constants.WARNING_COLOR, traceback_war=False)
3578+
35763579
# finally, we can write the transcript out to the file
35773580
try:
35783581
with open(transcript_file, 'w') as fout:
@@ -3588,8 +3591,6 @@ def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcrip
35883591
msg = '{} {} saved to transcript file {!r}'
35893592
self.pfeedback(msg.format(commands_run, plural, transcript_file))
35903593

3591-
return stop
3592-
35933594
edit_description = ("Edit a file in a text editor\n"
35943595
"\n"
35953596
"The editor used is determined by a settable parameter. To set it:\n"
@@ -3677,7 +3678,7 @@ def do_load(self, args: argparse.Namespace) -> Optional[bool]:
36773678
self._script_dir.append(os.path.dirname(expanded_path))
36783679

36793680
if args.transcript:
3680-
return self._generate_transcript(script_commands, os.path.expanduser(args.transcript))
3681+
self._generate_transcript(script_commands, os.path.expanduser(args.transcript))
36813682
else:
36823683
return self.runcmds_plus_hooks(script_commands)
36833684

tests/test_transcript.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,11 @@ def test_generate_transcript_stop(capsys):
231231
assert not stop
232232
assert err.startswith("2 commands")
233233

234-
# Since quit returns True for stop, only the first 2 commands will run and stop should be True
234+
# Since quit returns True for stop, only the first 2 commands will run
235235
commands = ['help', 'quit', 'alias']
236-
stop = app._generate_transcript(commands, transcript_fname)
236+
app._generate_transcript(commands, transcript_fname)
237237
_, err = capsys.readouterr()
238-
assert stop
239-
assert err.startswith("2 commands")
238+
assert "triggered a stop" in err
240239

241240

242241
@pytest.mark.parametrize('expected, transformed', [

0 commit comments

Comments
 (0)