Bug report
Bug description:
As reported in Discourse, even though the documentation on the exit_on_error argument for argparse.ArgumentParser says:
exit_on_error- Determines whether or notArgumentParserexits with error info when an error occurs. (default:True)
The parse_args method would still exit with error info when there are unrecognized arguments:
import argparse parser = argparse.ArgumentParser(exit_on_error=False) try: parser.parse_args('invalid arguments'.split()) except argparse.ArgumentError: print('ArgumentError caught.')
which outputs:
usage: test.py [-h]
test.py: error: unrecognized arguments: invalid arguments
CPython versions tested on:
3.12, CPython main branch
Operating systems tested on:
Linux, Windows
Linked PRs
- gh-121018: ensure ArgumentParser.parse_args with exit_on_error=False raises instead of exiting when given unrecognized arguments #121019
- [3.12] gh-121018: Ensure ArgumentParser.parse_args with exit_on_error=False raises instead of exiting when given unrecognized arguments (GH-121019) #121031
- [3.13] gh-121018: Ensure ArgumentParser.parse_args with exit_on_error=False raises instead of exiting when given unrecognized arguments (GH-121019) #121032
- gh-121018: Fix more cases of exiting in argparse when exit_on_error=False #121056
- [3.13] gh-121018: Fix more cases of exiting in argparse when exit_on_error=False (GH-121056) #121128
- [3.12] gh-121018: Fix more cases of exiting in argparse when exit_on_error=False (GH-121056) #121129
- gh-121018: Fix typo in NEWS entry #121510
- [3.12] gh-121018: Fix typo in NEWS entry (GH-121510) #121516