Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Argparse logs incorrect ambiguous option #125254

Closed
@clavedeluna

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

Metadata

Metadata

Labels

3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

Projects

Status

Doc issues

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions