Skip to content

Commit efd3eb0

Browse files
committed
Add 'history_includes_scripts' settable
At least in most of my use cases, the individual commands of a script executed by the command line should not end up in the history. If I go back in history, I want to find the commands I typed (like the run_script), and not the commands contained in the script I executed, or even the scripts that this script might execute itself. This patch introduces a new settable 'history_includes_scripts'. It is kept as 'true' by default to preserve the legacy behavior of existing cmd2 versions. However, it can be set to 'false' to make run_script not add to the history by default. Individual run_script executions can still use the --history option to make them override the default and add to the history. Signed-off-by: Harald Welte <[email protected]>
1 parent de90590 commit efd3eb0

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

cmd2/cmd2.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ def __init__(
323323
self.feedback_to_output = False # Do not include nonessentials in >, | output by default (things like timing)
324324
self.quiet = False # Do not suppress nonessential output
325325
self.timing = False # Prints elapsed time for each command
326+
self.history_includes_scripts = True # Should the history contain commands issued by scripts?
326327

327328
# The maximum number of CompletionItems to display during tab completion. If the number of completion
328329
# suggestions exceeds this number, they will be displayed in the typical columnized format and will
@@ -1103,6 +1104,7 @@ def allow_style_type(value: str) -> ansi.AllowStyle:
11031104
)
11041105
self.add_settable(Settable('quiet', bool, "Don't print nonessential feedback", self))
11051106
self.add_settable(Settable('timing', bool, "Report execution times", self))
1107+
self.add_settable(Settable('history_includes_scripts', bool, "History should contain commands issued by scripts", self))
11061108

11071109
# ----- Methods related to presenting output to the user -----
11081110

@@ -5048,8 +5050,8 @@ def _current_script_dir(self) -> Optional[str]:
50485050
completer=path_complete,
50495051
)
50505052
run_script_parser.add_argument(
5051-
'--no-history',
5052-
action='store_true',
5053+
'--history',
5054+
action=argparse.BooleanOptionalAction,
50535055
help="Don't add commands issued by script to the history",
50545056
)
50555057
run_script_parser.add_argument('script_path', help="path to the script file", completer=path_complete)
@@ -5090,7 +5092,10 @@ def do_run_script(self, args: argparse.Namespace) -> Optional[bool]:
50905092
return None
50915093

50925094
orig_script_dir_count = len(self._script_dir)
5093-
add_to_history = not args.no_history
5095+
if args.history == None:
5096+
add_to_history = self.history_includes_scripts
5097+
else:
5098+
add_to_history = args.history
50945099

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

0 commit comments

Comments
 (0)