commands: Handle ArgumentParser exit.

This commit is contained in:
Florian Bruhin 2014-09-04 17:58:28 +02:00
parent 57d51ad9bb
commit 03a0a1c599
2 changed files with 14 additions and 2 deletions

View File

@ -31,6 +31,15 @@ class ArgumentParserError(Exception):
"""Exception raised when the ArgumentParser signals an error."""
class ArgumentParserExit(Exception):
"""Exception raised when the argument parser exitted."""
def __init__(self, status, msg):
self.status = status
super().__init__(msg)
class ArgumentParser(argparse.ArgumentParser):
"""Subclass ArgumentParser to be more suitable for runtime parsing."""
@ -39,8 +48,7 @@ class ArgumentParser(argparse.ArgumentParser):
super().__init__(add_help=False)
def exit(self, status=0, msg=None):
raise AssertionError('exit called, this should never happen. '
'Status: {}, message: {}'.format(status, msg))
raise ArgumentParserExit(status, msg)
def error(self, msg):
raise ArgumentParserError(msg[0].upper() + msg[1:])

View File

@ -119,6 +119,10 @@ class Command:
except argparser.ArgumentParserError as e:
message.error('{}: {}'.format(self.name, e))
return
except argparser.ArgumentParserExit as e:
log.commands.debug("argparser exited with status {}: {}".format(
e.status, e))
return
for name, arg in vars(namespace).items():
if isinstance(arg, list):