Skip to content

Commit de90590

Browse files
committed
add '--no-history' option to run_script
In many cases you may not want to clutter your history with hundreds of commands contained in some script file you are running. runcmds_plus_hooks() already has that capability, let's expose it via the '--no-history' option of the run_script command. Signed-off-by: Harald Welte <[email protected]>
1 parent be15939 commit de90590

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

cmd2/cmd2.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4894,7 +4894,8 @@ def _persist_history(self) -> None:
48944894
except OSError as ex:
48954895
self.perror(f"Cannot write persistent history file '{self.persistent_history_file}': {ex}")
48964896

4897-
def _generate_transcript(self, history: Union[List[HistoryItem], List[str]], transcript_file: str) -> None:
4897+
def _generate_transcript(self, history: Union[List[HistoryItem], List[str]], transcript_file: str,
4898+
add_to_history: bool = True) -> None:
48984899
"""Generate a transcript file from a given history of commands"""
48994900
self.last_result = False
49004901

@@ -4944,7 +4945,8 @@ def _generate_transcript(self, history: Union[List[HistoryItem], List[str]], tra
49444945

49454946
# then run the command and let the output go into our buffer
49464947
try:
4947-
stop = self.onecmd_plus_hooks(history_item, raise_keyboard_interrupt=True)
4948+
stop = self.onecmd_plus_hooks(history_item, raise_keyboard_interrupt=True,
4949+
add_to_history=add_to_history)
49484950
except KeyboardInterrupt as ex:
49494951
self.perror(ex)
49504952
stop = True
@@ -5045,6 +5047,11 @@ def _current_script_dir(self) -> Optional[str]:
50455047
help='record the output of the script as a transcript file',
50465048
completer=path_complete,
50475049
)
5050+
run_script_parser.add_argument(
5051+
'--no-history',
5052+
action='store_true',
5053+
help="Don't add commands issued by script to the history",
5054+
)
50485055
run_script_parser.add_argument('script_path', help="path to the script file", completer=path_complete)
50495056

50505057
@with_argparser(run_script_parser)
@@ -5083,15 +5090,18 @@ def do_run_script(self, args: argparse.Namespace) -> Optional[bool]:
50835090
return None
50845091

50855092
orig_script_dir_count = len(self._script_dir)
5093+
add_to_history = not args.no_history
50865094

50875095
try:
50885096
self._script_dir.append(os.path.dirname(expanded_path))
50895097

50905098
if args.transcript:
50915099
# self.last_resort will be set by _generate_transcript()
5092-
self._generate_transcript(script_commands, os.path.expanduser(args.transcript))
5100+
self._generate_transcript(script_commands, os.path.expanduser(args.transcript),
5101+
add_to_history=add_to_history)
50935102
else:
5094-
stop = self.runcmds_plus_hooks(script_commands, stop_on_keyboard_interrupt=True)
5103+
stop = self.runcmds_plus_hooks(script_commands, stop_on_keyboard_interrupt=True,
5104+
add_to_history=add_to_history)
50955105
self.last_result = True
50965106
return stop
50975107

0 commit comments

Comments
 (0)