From 2f0dbf5bad19d96880e2ef795660db1b8f04cdc7 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Mon, 20 May 2019 23:47:50 -0400 Subject: Refactor exit_code implementation cmd2.Cmd.cmdloop() now returns self.exit_code which should be an integer Also: - Refactored examples to call sys.exit(app.cmdloop()) in their __main__ - Running transcript tests now sets the exit_code accordingly based on success/failure - Updated CHANGELOG - Updated README - Updated Sphinx docs - Added unit test for case when transcript test fails --- examples/exit_code.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'examples/exit_code.py') diff --git a/examples/exit_code.py b/examples/exit_code.py index 8ae2d310..f4b19091 100755 --- a/examples/exit_code.py +++ b/examples/exit_code.py @@ -3,7 +3,6 @@ """A simple example demonstrating the following how to emit a non-zero exit code in your cmd2 application. """ import cmd2 -import sys from typing import List @@ -29,15 +28,12 @@ Usage: exit [exit_code] self.perror("{} isn't a valid integer exit code".format(arg_list[0])) self.exit_code = -1 - self._should_quit = True - return self._STOP_AND_EXIT - - def postloop(self) -> None: - """Hook method executed once when the cmdloop() method is about to return.""" - code = self.exit_code if self.exit_code is not None else 0 - self.poutput('{!r} exiting with code: {}'.format(sys.argv[0], code)) + return True if __name__ == '__main__': + import sys app = ReplWithExitCode() - app.cmdloop() + sys_exit_code = app.cmdloop() + app.poutput('{!r} exiting with code: {}'.format(sys.argv[0], sys_exit_code)) + sys.exit(sys_exit_code) -- cgit v1.2.1