Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong parse long argument #81408

Closed
chr0139 mannequin opened this issue Jun 11, 2019 · 5 comments
Closed

Wrong parse long argument #81408

chr0139 mannequin opened this issue Jun 11, 2019 · 5 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@chr0139
Copy link
Mannequin

chr0139 mannequin commented Jun 11, 2019

BPO 37227
Nosy @ericvsmith, @tirkarthi

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2019-06-11.09:26:10.163>
created_at = <Date 2019-06-11.08:53:06.919>
labels = ['invalid', 'type-bug', 'library']
title = 'Wrong parse long argument'
updated_at = <Date 2019-06-13.05:11:18.962>
user = 'https://bugs.python.org/chr0139'

bugs.python.org fields:

activity = <Date 2019-06-13.05:11:18.962>
actor = 'paul.j3'
assignee = 'none'
closed = True
closed_date = <Date 2019-06-11.09:26:10.163>
closer = 'eric.smith'
components = ['Library (Lib)']
creation = <Date 2019-06-11.08:53:06.919>
creator = 'chr0139'
dependencies = []
files = []
hgrepos = []
issue_num = 37227
keywords = []
message_count = 5.0
messages = ['345203', '345204', '345206', '345207', '345208']
nosy_count = 4.0
nosy_names = ['eric.smith', 'paul.j3', 'xtreak', 'chr0139']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue37227'
versions = ['Python 2.7']

@chr0139
Copy link
Mannequin Author

chr0139 mannequin commented Jun 11, 2019

I have this script

import argparse
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-l', '--list', action='store_true', help="show help")
group.add_argument('-u', '--upgrade', action='store_true', help="show help")
parser.parse_args(['--li'])

and this is the unexpected result:

Namespace(list=True, upgrade=False)

@chr0139 chr0139 mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jun 11, 2019
@ericvsmith
Copy link
Member

And what is your expected result, and why?

@chr0139
Copy link
Mannequin Author

chr0139 mannequin commented Jun 11, 2019

I think --li is not the same argument as --list

So I expect that

parser.parse_args(['-l']) or parser.parse_args(['--list'])

should be only two options which should leads to the result

Namespace(list=True, upgrade=False)

@tirkarthi
Copy link
Member

argparse allows abbreviations by default so --li matches --list as a prefix match. This is documented at https://docs.python.org/2.7/library/argparse.html#argument-abbreviations-prefix-matching

From python 3.5 allow_abbrev=False can be passed to ArgumentParser to disable this. See also bpo-10981 and bpo-14910 that implemented this. https://docs.python.org/3/library/argparse.html#argument-abbreviations-prefix-matching.

@ericvsmith
Copy link
Member

Although note that with allow_abbrev=False, -l will conflict with --long, which I'm not sure is a great design.

I'm closing this as not a bug, since it works as documented and there's a workaround (albeit not in 2.7, which is closed to new features).

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants