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
Comments
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) |
And what is your expected result, and why? |
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) |
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. |
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). |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: