Improve argparse behavior for its role in making the API work

This commit is contained in:
James R. Barlow
2019-05-22 15:55:48 -07:00
parent 8bcb85720c
commit db69b4d11a
2 changed files with 20 additions and 5 deletions
+2 -4
View File
@@ -128,10 +128,8 @@ def create_options(*, input_file, output_file, **kwargs):
cmdline.append(str(input_file))
cmdline.append(str(output_file))
try:
options = parser.parse_args(cmdline)
except argparse.ArgumentError as e:
raise ValueError(str(e))
parser.api_mode = True
options = parser.parse_args(cmdline)
return options
+18 -1
View File
@@ -36,7 +36,24 @@ def numeric(basetype, min_=None, max_=None):
return _numeric
parser = argparse.ArgumentParser(
class ArgumentParser(argparse.ArgumentParser):
"""Override parser's default behavior of calling sys.exit()
https://stackoverflow.com/questions/5943249/python-argparse-and-controlling-overriding-the-exit-status-code
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.api_mode = False
def error(self, message):
if not self.api_mode:
super().error(message)
return
raise ValueError(message)
parser = ArgumentParser(
prog=PROGRAM_NAME,
fromfile_prefix_chars='@',
formatter_class=argparse.RawDescriptionHelpFormatter,