Skip to content

argparse.ArgumentParser does not raise ArgumentError on parse_args() with missing positional argument #141195

@nihadh-ML

Description

@nihadh-ML

Bug report

Bug description:

Following Code ends up as exception of type SystemExit when called as python test.py or python test.py -c. The catching is possible only over BaseException. According to documentation the code should raise an ArgumentError exception.

#test.py
parser = argparse.ArgumentParser(exit_on_error=False)
parser.add_argument('command',
                        choices=['call', 'text', 'email'],
                        help='What should be done')
parser.add_argument('--contact',
                        help='How to reach')
try:
    parser.parse_args()
except argparse.ArgumentError as err:
    print('Error happened')

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions