Improve argparse behavior for its role in making the API work
This commit is contained in:
+2
-4
@@ -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
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user