Skip to content

Commit 329a2e2

Browse files
committed
Changed default behavior of runcmds_plus_hooks() to not stop when Ctrl-C is pressed and instead run the next command in its list.
1 parent 52aad8b commit 329a2e2

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
of this parameter.
2929
* Removed ability to run Python commands from the command line with `py`. Now `py` takes no arguments
3030
and just opens an interactive Python shell.
31+
* Changed default behavior of `runcmds_plus_hooks()` to not stop when Ctrl-C is pressed and instead
32+
run the next command in its list.
3133
* Enhancements
3234
* Added support for custom tab completion and up-arrow input history to `cmd2.Cmd2.read_input`.
3335
See [read_input.py](https://github.com/python-cmd2/cmd2/blob/master/examples/read_input.py)

cmd2/cmd2.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,16 +2405,17 @@ def runcmds_plus_hooks(
24052405
cmds: Union[List[HistoryItem], List[str]],
24062406
*,
24072407
add_to_history: bool = True,
2408-
stop_on_keyboard_interrupt: bool = True,
2408+
stop_on_keyboard_interrupt: bool = False,
24092409
) -> bool:
24102410
"""
24112411
Used when commands are being run in an automated fashion like text scripts or history replays.
24122412
The prompt and command line for each command will be printed if echo is True.
24132413
24142414
:param cmds: commands to run
24152415
:param add_to_history: If True, then add these commands to history. Defaults to True.
2416-
:param stop_on_keyboard_interrupt: stop command loop if Ctrl-C is pressed instead of just
2417-
moving to the next command. Defaults to True.
2416+
:param stop_on_keyboard_interrupt: if True, then stop running contents of cmds if Ctrl-C is pressed instead of moving
2417+
to the next command in the list. This is used when the commands are part of a
2418+
group, like a text script, which should stop upon Ctrl-C. Defaults to False.
24182419
:return: True if running of commands should stop
24192420
"""
24202421
for line in cmds:
@@ -4684,7 +4685,7 @@ def do_run_script(self, args: argparse.Namespace) -> Optional[bool]:
46844685
if args.transcript:
46854686
self._generate_transcript(script_commands, os.path.expanduser(args.transcript))
46864687
else:
4687-
return self.runcmds_plus_hooks(script_commands)
4688+
return self.runcmds_plus_hooks(script_commands, stop_on_keyboard_interrupt=True)
46884689

46894690
finally:
46904691
with self.sigint_protection:

tests/test_cmd2.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -422,19 +422,19 @@ def do_keyboard_interrupt(self, _):
422422

423423
setattr(base_app, 'do_keyboard_interrupt', types.MethodType(do_keyboard_interrupt, base_app))
424424

425-
# Default behavior is to stop command loop on Ctrl-C
425+
# Default behavior is to not stop runcmds_plus_hooks() on Ctrl-C
426426
base_app.history.clear()
427427
base_app.runcmds_plus_hooks(['help', 'keyboard_interrupt', 'shortcuts'])
428428
out, err = capsys.readouterr()
429-
assert err.startswith("Interrupting this command")
430-
assert len(base_app.history) == 2
429+
assert not err
430+
assert len(base_app.history) == 3
431431

432-
# Ctrl-C should not stop command loop in this case
432+
# Ctrl-C should stop runcmds_plus_hooks() in this case
433433
base_app.history.clear()
434-
base_app.runcmds_plus_hooks(['help', 'keyboard_interrupt', 'shortcuts'], stop_on_keyboard_interrupt=False)
434+
base_app.runcmds_plus_hooks(['help', 'keyboard_interrupt', 'shortcuts'], stop_on_keyboard_interrupt=True)
435435
out, err = capsys.readouterr()
436-
assert not err
437-
assert len(base_app.history) == 3
436+
assert err.startswith("Interrupting this command")
437+
assert len(base_app.history) == 2
438438

439439

440440
def test_relative_run_script(base_app, request):

0 commit comments

Comments
 (0)