comparison libervia/cli/base.py @ 4224:8499b3ad5edb

cli (base): fix exit code transmission when `SystemExit` is catched.
author Goffi <goffi@goffi.org>
date Tue, 05 Mar 2024 17:31:56 +0100
parents 730f542e4ad0
children d01b8d002619
comparison
equal deleted inserted replaced
4223:919bdf7768d8 4224:8499b3ad5edb
732 732
733 def _run(self, args=None, namespace=None): 733 def _run(self, args=None, namespace=None):
734 self.loop = LiberviaCLILoop() 734 self.loop = LiberviaCLILoop()
735 try: 735 try:
736 self.loop.run(self, args, namespace) 736 self.loop.run(self, args, namespace)
737 except SystemExit: 737 except SystemExit as e:
738 # The loop is stopped, but we execute it one more time to call `a_quit` which 738 # The loop is stopped, but we execute it one more time to call `a_quit` which
739 # will call any cleaning method including async ones. 739 # will call any cleaning method including async ones.
740 asyncio.get_event_loop().run_until_complete(self.a_quit()) 740 asyncio.get_event_loop().run_until_complete(self.a_quit(e.code))
741 741
742 @classmethod 742 @classmethod
743 def run(cls): 743 def run(cls):
744 cls()._run() 744 cls()._run()
745 745
802 802
803 self.loop.quit(exit_code) 803 self.loop.quit(exit_code)
804 if raise_exc: 804 if raise_exc:
805 raise QuitException 805 raise QuitException
806 806
807 async def a_quit(self, exit_code=0, raise_exc=True): 807 async def a_quit(self, exit_code: int=0, raise_exc=True):
808 """Execute async quit callback before actually quitting 808 """Execute async quit callback before actually quitting
809 809
810 This method should be prefered to ``quit``, as it executes async quit callbacks 810 This method should be prefered to ``quit``, as it executes async quit callbacks
811 which may be necessary for proper cleaning of session. 811 which may be necessary for proper cleaning of session.
812 """ 812 """