Skip to content

argparse: space in an incorrect argument adds it to nargs="*", instead of raising an error #114429

Closed as not planned
@abcydybcy

Description

@abcydybcy

Bug report

Bug description:

Code to recreate:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("args", nargs="*")
print(parser.parse_args())

Current behaviour:
If the wrong argument contains a space, it is added to the catch-all arguments:

> python bug.py --wrong-arg="foo bar"
Namespace(args=['--wrong-arg=foo bar'])

But if it doesn't contain a space, expected behaviour happens:

> python bug.py --wrong-arg="foobar" 
usage: bug.py [-h] [args ...]
bug.py: error: unrecognized arguments: --wrong-arg=foobar

Expected behaviour:
Calling this program as bug.py --wrong-argument="foo bar" prints an error message:

usage: bug.py [-h] [--name NAME] [args ...]
bug.py: error: unrecognized arguments: --wrong-arg=foo bar.

CPython versions tested on:

3.11

Operating systems tested on:

No response

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