blhsing · GitHub

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 not ArgumentParser exits 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

Read the original on github.com ↗