Skip to content

Commit dcbffdb

Browse files
committed
Addressed review comments
1 parent 2f24a8a commit dcbffdb

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

cmd2/cmd2.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3341,6 +3341,9 @@ def do_history(self, args: argparse.Namespace) -> None:
33413341
except Exception as e:
33423342
self.perror('Saving {!r} - {}'.format(args.output_file, e), traceback_war=False)
33433343
elif args.transcript:
3344+
if self.redirecting:
3345+
self.perror("Redirection not supported while using history -t", traceback_war=False)
3346+
return
33443347
self._generate_transcript(history, args.transcript)
33453348
else:
33463349
# Display the history items retrieved
@@ -3350,12 +3353,9 @@ def do_history(self, args: argparse.Namespace) -> None:
33503353
def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcript_file: str) -> None:
33513354
"""Generate a transcript file from a given history of commands."""
33523355
import io
3353-
3354-
# Disable echo and redirection while we manually redirect stdout to a StringIO buffer
3355-
saved_allow_redirection = self.allow_redirection
3356+
# Disable echo while we manually redirect stdout to a StringIO buffer
33563357
saved_echo = self.echo
33573358
saved_stdout = self.stdout
3358-
self.allow_redirection = False
33593359
self.echo = False
33603360

33613361
# The problem with supporting regular expressions in transcripts
@@ -3394,7 +3394,6 @@ def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcrip
33943394
transcript += output.replace('/', r'\/')
33953395

33963396
# Restore altered attributes to their original state
3397-
self.allow_redirection = saved_allow_redirection
33983397
self.echo = saved_echo
33993398
self.stdout = saved_stdout
34003399

@@ -3506,7 +3505,18 @@ def do_load(self, args: argparse.Namespace) -> None:
35063505
return
35073506

35083507
if args.record_transcript:
3509-
self._generate_transcript(script_commands, args.record_transcript)
3508+
if self.redirecting:
3509+
self.perror("Redirection not supported while using load -r", traceback_war=False)
3510+
return
3511+
transcript_path = os.path.abspath(os.path.expanduser(args.record_transcript))
3512+
transcript_dir = os.path.dirname(transcript_path)
3513+
if not os.path.isdir(transcript_dir):
3514+
self.perror("{!r} is not a directory".format(transcript_dir), traceback_war=False)
3515+
return
3516+
if not os.access(transcript_dir, os.W_OK):
3517+
self.perror("You do not have write access to directory '{!r}".format(transcript_dir), traceback_war=False)
3518+
return
3519+
self._generate_transcript(script_commands, os.path.expanduser(args.record_transcript))
35103520
return
35113521

35123522
self.cmdqueue = script_commands + ['eos'] + self.cmdqueue
@@ -3825,7 +3835,7 @@ def cmdloop(self, intro: Optional[str] = None) -> None:
38253835

38263836
# If transcript-based regression testing was requested, then do that instead of the main loop
38273837
if self._transcript_files is not None:
3828-
self.run_transcript_tests(self._transcript_files)
3838+
self.run_transcript_tests([os.path.expanduser(tf) for tf in self._transcript_files])
38293839
else:
38303840
# If an intro was supplied in the method call, allow it to override the default
38313841
if intro is not None:

docs/freefeatures.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ Simply include one command per line, typed exactly as you would inside a ``cmd2`
2222
The ``load`` command loads commands from a script file into a queue and then the normal cmd2 REPL
2323
resumes control and executes the commands in the queue in FIFO order. A side effect of this
2424
is that if you redirect/pipe the output of a load command, it will redirect the output of the ``load``
25-
command itself, but will NOT redirect the output of the command loaded from the script file.
25+
command itself, but will NOT redirect the output of the command loaded from the script file. Of course,
26+
you can add redirection to the commands being run in the script file, e.g.::
27+
28+
# This is your script file
29+
command arg1 arg2 > file.txt
2630

2731
.. automethod:: cmd2.cmd2.Cmd.do_load
2832

0 commit comments

Comments
 (0)