Skip to content

Commit 3a4893e

Browse files
committed
onecmd_plus_hooks() now sets self.exit_code when a SystemExit handled
1 parent fabe4cd commit 3a4893e

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
may have a prepended prefix.
3535
* Settables now allow changes to be applied to any arbitrary object attribute. It no longer needs to match an
3636
attribute added to the cmd2 instance itself.
37+
* Raising ``SystemExit`` or calling ``sys.exit()`` in a command or hook function will set ``self.exit_code``
38+
to the exit code used in those calls. It will also result in the command loop stopping.
39+
3740
## 1.5.0 (January 31, 2021)
3841
* Bug Fixes
3942
* Fixed bug where setting `always_show_hint=True` did not show a hint when completing `Settables`

cmd2/cmd2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,8 @@ def onecmd_plus_hooks(
23622362
except KeyboardInterrupt as ex:
23632363
if raise_keyboard_interrupt and not stop:
23642364
raise ex
2365-
except SystemExit:
2365+
except SystemExit as ex:
2366+
self.exit_code = ex.code
23662367
stop = True
23672368
except PassThroughException as ex:
23682369
raise ex.wrapped_ex
@@ -2374,7 +2375,8 @@ def onecmd_plus_hooks(
23742375
except KeyboardInterrupt as ex:
23752376
if raise_keyboard_interrupt and not stop:
23762377
raise ex
2377-
except SystemExit:
2378+
except SystemExit as ex:
2379+
self.exit_code = ex.code
23782380
stop = True
23792381
except PassThroughException as ex:
23802382
raise ex.wrapped_ex

docs/features/commands.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ the following interaction::
175175
2
176176

177177

178+
Raising ``SystemExit(code)`` or calling ``sys.exit(code)`` in a command
179+
or hook function also sets ``self.exit_code`` and stops the program.
180+
178181
Exception Handling
179182
------------------
180183

tests/test_cmd2.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,13 +537,16 @@ def test_system_exit_in_command(base_app, capsys):
537537
"""Test raising SystemExit in a command"""
538538
import types
539539

540+
exit_code = 5
541+
540542
def do_system_exit(self, _):
541-
raise SystemExit
543+
raise SystemExit(exit_code)
542544

543545
setattr(base_app, 'do_system_exit', types.MethodType(do_system_exit, base_app))
544546

545547
stop = base_app.onecmd_plus_hooks('system_exit')
546548
assert stop
549+
assert base_app.exit_code == exit_code
547550

548551

549552
def test_passthrough_exception_in_command(base_app):

tests/test_plugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def cmdfinalization_hook_system_exit(
229229
) -> cmd2.plugin.CommandFinalizationData:
230230
"""A command finalization hook which raises a SystemExit"""
231231
self.called_cmdfinalization += 1
232-
raise SystemExit
232+
raise SystemExit(5)
233233

234234
def cmdfinalization_hook_keyboard_interrupt(
235235
self, data: cmd2.plugin.CommandFinalizationData
@@ -930,6 +930,7 @@ def test_cmdfinalization_hook_system_exit():
930930
stop = app.onecmd_plus_hooks('say hello')
931931
assert stop
932932
assert app.called_cmdfinalization == 1
933+
assert app.exit_code == 5
933934

934935

935936
def test_cmdfinalization_hook_keyboard_interrupt():

0 commit comments

Comments
 (0)