Skip to content

Ambiguity in behavior of ArgumentParser prefix_chars when same flag is specified with different prefixes #95836

@joshnatis

Description

@joshnatis

Environment

$ python --version
Python 3.8.10
$ uname -a
Linux 4.4.0-19041-Microsoft #1237-Microsoft Sat Sep 11 14:32:00 PST 2021 x86_64 x86_64 x86_64 GNU/Linux

Description

It seems that flags specified with different prefixes but the same 'name' (for lack of a better word) are treated as one flag.

Example

# example.py
import argparse

parser = argparse.ArgumentParser(prefix_chars='-+')
parser.add_argument('-e', metavar='<example>', action='append')
parser.add_argument('+e', metavar='<example>', action='append')
args = parser.parse_args()

print(args)
$ python example.py -e hello1 +e hello2
Namespace(e=['hello1', 'hello2'])

I was kind of hoping that somebody had thought of a smart way of treating them as two distinct flags : ). Regardless, I don't see anything about this on https://docs.python.org/3/library/argparse.html#prefix-chars, so I'm filing this as a bug in the documentation. It seems like a nitty detail but the page advertises itself as being thorough:

This page contains the API reference information. For a more gentle introduction to Python command-line parsing, have a look at the argparse tutorial.

Linked PRs

Metadata

Metadata

Labels

docsDocumentation in the Doc dir

Projects

Status

Doc issues

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions