I'm using Python version 3.10.1. I'm trying to use the argparse library to handle arguments, including handling invalid arguments gracefully. However, I'm encountering a mind-boggling traceback. Here's the problem code:
import argparse
def check_positive(value):
try:
value = int(value)
if value <= 0:
raise argparse.ArgumentError(f"{value} is not a positive integer.")
except ValueError:
raise argparse.ArgumentError("Score threshold must be a positive integer.")
return value
if __name__ == "__main__":
parser = argparse.ArgumentParser(exit_on_error=False)
parser.add_argument("file", help="a help message")
parser.add_argument("score_threshold", type=check_positive, help="another help message")
args = parser.parse_args()
As a test, I specify the argument -102 as the score threshold. This should enter the if statement, and then raise the argparse.ArgumentError. However, in doing so, I encounter another exception:
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\argparse.py", line 2479, in _get_value
result = type_func(arg_string)
File "<snip>", line 151, in check_positive
raise argparse.ArgumentError(f"{value} is not a positive integer.")
TypeError: ArgumentError.__init__() missing 1 required positional argument: 'message'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<snip>", line 166, in <module>
args = parser.parse_args()
File "C:\Program Files\Python310\lib\argparse.py", line 1821, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "C:\Program Files\Python310\lib\argparse.py", line 1859, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "C:\Program Files\Python310\lib\argparse.py", line 2066, in _parse_known_args
stop_index = consume_positionals(start_index)
File "C:\Program Files\Python310\lib\argparse.py", line 2022, in consume_positionals
take_action(action, args)
File "C:\Program Files\Python310\lib\argparse.py", line 1915, in take_action
argument_values = self._get_values(action, argument_strings)
File "C:\Program Files\Python310\lib\argparse.py", line 2446, in _get_values
value = self._get_value(action, arg_string)
File "C:\Program Files\Python310\lib\argparse.py", line 2492, in _get_value
raise ArgumentError(action, msg % args)
argparse.ArgumentError: argument score_threshold: invalid check_positive value: '-102'
I have absolutely no idea why Python is complaining. I'm utterly baffled. Aren't I handling the error as expected? It should simply raise the argparse.ArgumentError and print -102 is not a positive integer. Also, I have no idea what the hell TypeError: ArgumentError.__init__() missing 1 required positional argument: 'message' means. I should only need to provide an error message, right? Is there some change to argparse.ArgumentError that I'm not aware of? Searching the Internet has yielded zero fruit, so I'm really scratching my head on this.