Skip to content

argparse.ArgumentParser.parses_args does not honor exit_on_error=False when given unrecognized arguments#121018

@blhsing

Description

@blhsing

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:

importargparseparser=argparse.ArgumentParser(exit_on_error=False) try: parser.parse_args('invalid arguments'.split()) exceptargparse.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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Doc issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions