Skip to content

Commit

Permalink
add '--no-history' option to run_script
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
laf0rge committed Jan 31, 2024
1 parent be15939 commit de90590
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4894,7 +4894,8 @@ def _persist_history(self) -> None:
except OSError as ex:
self.perror(f"Cannot write persistent history file '{self.persistent_history_file}': {ex}")

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

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

# then run the command and let the output go into our buffer
try:
stop = self.onecmd_plus_hooks(history_item, raise_keyboard_interrupt=True)
stop = self.onecmd_plus_hooks(history_item, raise_keyboard_interrupt=True,
add_to_history=add_to_history)
except KeyboardInterrupt as ex:
self.perror(ex)
stop = True
Expand Down Expand Up @@ -5045,6 +5047,11 @@ def _current_script_dir(self) -> Optional[str]:
help='record the output of the script as a transcript file',
completer=path_complete,
)
run_script_parser.add_argument(
'--no-history',
action='store_true',
help="Don't add commands issued by script to the history",
)
run_script_parser.add_argument('script_path', help="path to the script file", completer=path_complete)

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

orig_script_dir_count = len(self._script_dir)
add_to_history = not args.no_history

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

if args.transcript:
# self.last_resort will be set by _generate_transcript()
self._generate_transcript(script_commands, os.path.expanduser(args.transcript))
self._generate_transcript(script_commands, os.path.expanduser(args.transcript),
add_to_history=add_to_history)
else:
stop = self.runcmds_plus_hooks(script_commands, stop_on_keyboard_interrupt=True)
stop = self.runcmds_plus_hooks(script_commands, stop_on_keyboard_interrupt=True,
add_to_history=add_to_history)
self.last_result = True
return stop

Expand Down

0 comments on commit de90590

Please sign in to comment.