Skip to content

Commit 7e9b567

Browse files
committed
Added flag to prevent a command from being added to history
1 parent 73d1c34 commit 7e9b567

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

cmd2/cmd2.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,10 +1724,11 @@ def parseline(self, line: str) -> Tuple[str, str, str]:
17241724
statement = self.statement_parser.parse_command_only(line)
17251725
return statement.command, statement.args, statement.command_and_args
17261726

1727-
def onecmd_plus_hooks(self, line: str, py_bridge_call: bool = False) -> bool:
1727+
def onecmd_plus_hooks(self, line: str, *, add_to_history: bool = True, py_bridge_call: bool = False) -> bool:
17281728
"""Top-level function called by cmdloop() to handle parsing a line and running the command and all of its hooks.
17291729
17301730
:param line: line of text read from input
1731+
:param add_to_history: If True, then add this command to history. Defaults to True.
17311732
:param py_bridge_call: This should only ever be set to True by PyBridge to signify the beginning
17321733
of an app() call from Python. It is used to enable/disable the storage of the
17331734
command's stdout.
@@ -1795,7 +1796,7 @@ def onecmd_plus_hooks(self, line: str, py_bridge_call: bool = False) -> bool:
17951796
statement = self.precmd(statement)
17961797

17971798
# go run the command function
1798-
stop = self.onecmd(statement)
1799+
stop = self.onecmd(statement, add_to_history=add_to_history)
17991800

18001801
# postcommand hooks
18011802
data = plugin.PostcommandData(stop, statement)
@@ -1852,12 +1853,13 @@ def _run_cmdfinalization_hooks(self, stop: bool, statement: Optional[Statement])
18521853
except Exception as ex:
18531854
self.pexcept(ex)
18541855

1855-
def runcmds_plus_hooks(self, cmds: List[Union[HistoryItem, str]]) -> bool:
1856+
def runcmds_plus_hooks(self, cmds: List[Union[HistoryItem, str]], *, add_to_history: bool = True) -> bool:
18561857
"""
18571858
Used when commands are being run in an automated fashion like text scripts or history replays.
18581859
The prompt and command line for each command will be printed if echo is True.
18591860
18601861
:param cmds: commands to run
1862+
:param add_to_history: If True, then add these commands to history. Defaults to True.
18611863
:return: True if running of commands should stop
18621864
"""
18631865
for line in cmds:
@@ -1867,7 +1869,7 @@ def runcmds_plus_hooks(self, cmds: List[Union[HistoryItem, str]]) -> bool:
18671869
if self.echo:
18681870
self.poutput('{}{}'.format(self.prompt, line))
18691871

1870-
if self.onecmd_plus_hooks(line):
1872+
if self.onecmd_plus_hooks(line, add_to_history=add_to_history):
18711873
return True
18721874

18731875
return False
@@ -2167,13 +2169,15 @@ def _cmd_func_name(self, command: str) -> str:
21672169
target = COMMAND_FUNC_PREFIX + command
21682170
return target if callable(getattr(self, target, None)) else ''
21692171

2170-
def onecmd(self, statement: Union[Statement, str]) -> bool:
2172+
# noinspection PyMethodOverriding
2173+
def onecmd(self, statement: Union[Statement, str], *, add_to_history: bool = True) -> bool:
21712174
""" This executes the actual do_* method for a command.
21722175
21732176
If the command provided doesn't exist, then it executes default() instead.
21742177
21752178
:param statement: intended to be a Statement instance parsed command from the input stream, alternative
21762179
acceptance of a str is present only for backward compatibility with cmd
2180+
:param add_to_history: If True, then add this command to history. Defaults to True.
21772181
:return: a flag indicating whether the interpretation of commands should stop
21782182
"""
21792183
# For backwards compatibility with cmd, allow a str to be passed in
@@ -2183,8 +2187,9 @@ def onecmd(self, statement: Union[Statement, str]) -> bool:
21832187
func = self.cmd_func(statement.command)
21842188
if func:
21852189
# Check to see if this command should be stored in history
2186-
if statement.command not in self.exclude_from_history \
2187-
and statement.command not in self.disabled_commands:
2190+
if statement.command not in self.exclude_from_history and \
2191+
statement.command not in self.disabled_commands and add_to_history:
2192+
21882193
self.history.append(statement)
21892194

21902195
stop = func(statement)

0 commit comments

Comments
 (0)