Skip to content

ArgumentParser incorrectly parses dashed option args containing spaces as positional args #143270

@qris

Description

@qris

Bug report

Bug description:

Python 3.13.11 (main, Dec  6 2025, 08:52:52) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from argparse import ArgumentParser
>>> parser = ArgumentParser(exit_on_error=False)

>>> parser.add_argument('tags', nargs='*')
_StoreAction(option_strings=[], dest='tags', nargs='*', const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None, deprecated=False)
>>> parser.parse_args(['--timeout'])
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    parser.parse_args(['--timeout'])
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/argparse.py", line 1904, in parse_args
    raise ArgumentError(None, msg)
argparse.ArgumentError: unrecognized arguments: --timeout

>>> parser.parse_args(['--timeout', '1'])
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    parser.parse_args(['--timeout', '1'])
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/argparse.py", line 1904, in parse_args
    raise ArgumentError(None, msg)
argparse.ArgumentError: unrecognized arguments: --timeout

>>> parser.parse_args(['--timeout 1'])
Namespace(tags=['--timeout 1'])

Note that in the last case, there was no complaint about --timeout or --timeout 1 being an unrecognized argument. It was simply silently treated as a positional argument, which seems wrong to me as it starts with two dashes.

Admittedly this is an edge case, but it's easy to hit by mistake when calling such a process with subprocess and a list of arguments, and not necessarily easy to debug.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Status

    Doc issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions