Skip to content

Argparse logs incorrect ambiguous option #125254

Closed

Description

@clavedeluna

Bug report

Bug description:

Given this example script

import argparse
class CsvListAction(argparse.Action):
    """
    argparse Action to convert "a,b,c" into ["a", "b", "c"]
    """

    def __call__(self, parser, namespace, values, option_string=None):
        # Conversion to dict removes duplicates while preserving order
        items = list(dict.fromkeys(values.split(",")).keys())
        setattr(namespace, self.dest, items)


parser = argparse.ArgumentParser(description="Testing")

parser.add_argument("directory", type=str, help="path to find files")
parser.add_argument(
    "--output",
    type=str,
    help="name of output file to produce",
)

name_args_group = parser.add_mutually_exclusive_group()
name_args_group.add_argument(
    "--name-exclude",
    action=CsvListAction,
    help="Comma-separated set of name ID(s) to exclude",
)
name_args_group.add_argument(
    "--name-include",
    action=CsvListAction,
    help="Comma-separated set of name ID(s) to include",
)


parser.add_argument(
    "--path-exclude",
    action=CsvListAction,
    default=[],
    help="Comma-separated set of UNIX glob patterns to exclude",
)
parser.add_argument(
    "--path-include",
    action=CsvListAction,
    default=[],
    help="Comma-separated set of UNIX glob patterns to include",
)
parser.parse_args()

Notice the cli args are name-exclude and name-include

Run the following command, which incorrectly puts the arg as --name
python argparse_mini.py /some/path --output here.txt --name something --path-exclude *request.py

If you run it with Python 3.12.2 or below, the resulting log statement is what we expect
argparse_mini.py: error: ambiguous option: --name could match --name-exclude, --name-include

However, tested with 3.12.7 and the log statement instead is
argparse_mini.py: error: ambiguous option: *request.py could match --name-exclude, --name-include

which is clearly incorrect, as *request.py was the argument to a different CLI option

CPython versions tested on:

3.11, 3.12

Operating systems tested on:

macOS

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 10, 2024
  2. ZeroIntensity commented on Oct 10, 2024

    @ZeroIntensity
    Member

    cc @savannahostrowski, you've been working on argparse recently, right?

  3. added
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on Oct 10, 2024
  4. added a commit that references this issue on Oct 10, 2024
  5. serhiy-storchaka commented on Oct 10, 2024

    @serhiy-storchaka
    Member

    Thank you for your report @clavedeluna. This was a regression introduced in #58573.

  6. added a commit that references this issue on Oct 12, 2024
  7. added 2 commits that reference this issue on Oct 12, 2024
  8. added 2 commits that reference this issue on Oct 12, 2024
  9. moved this from Bugs to Doc issues in Argparse issueson Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

3.12only security fixes3.13only security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions