-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
argparse arguments in main parser hide an argument in subparser #73963
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
Comments
If you have a argument named --<prefix> in a subparser and two arguments named --<prefix><any_suffix)> in the main parser and call the Python executable with python <script.py> --<prefix> argparse fails with: error: ambiguous option: --<prefix> could match --<prefix><suffix1>, --<prefix><suffix2> This probably happens due to how the argument abbreviation parsing is implemented. Is it possible to support disabling argument abbreviation in Python 2.7, as it is done in Python 3? |
No, we won't be adding new features to 2.7. Sorry. |
Adding the feature was just a workaround suggestion, but this is a bug. Arguments in the main parser should not "hide" an argument in a subparser in argument abbreviation. |
allow_abbrev as added with http://bugs.python.org/issue14910 I contributed to the patch, but my memory isn't fresh. The fact that this works across the subparser boundary is, in a sense, accidental. We didn't think about how abbreviations are handled across this boundary. The loop that matches flag strings with Action options ignores the subparser command. It's just another positional argument. So items that will later be parsed by the subparser are still being matched with the main parser's optionals. If they don't trigger this abbreviation they will just be put in the unidentified category. The patch is big enough that I hesitate to add it to Py2. There's doesn't seem to be enough manpower to properly test this obscure corner of code. Technically it was a feature addition, not a bug fix in 3.5. 'allow_abbrev=False' messes with the handling of short options: http://bugs.python.org/issue26967 In http://bugs.python.org/issue14910#msg204678 I suggest a subclassing patch that might work with Py2. All the logic for handing subparsers is in the _SubParsersAction class. 'parse_args' really doesn't know anything about the concept. As a result, similarly named Actions in the main and subparsers is inherently a confusing issue. http://bugs.python.org/issue9351 for example, changes how defaults are handled when there are matching Actions at both levels. The simplest fix is to use different flags in the main parser and subparsers. |
PA> In http://bugs.python.org/issue14910#msg204678 I suggest a subclassing patch that might work with Py2. This solves my particular case. I do not use any argument with action='count', so the regression introduced by the new option does not affect me. Thanks! PA> The patch is big enough that I hesitate to add it to Py2. I see 2 solutions:
|
s/Python 2.6/Python 2/ in last comment |
Python 2.7 is no longer supported, so I think this issue should be closed. |
This is still broken in 3.10, but is a duplicate of #58573, so could be closed |
Closing as this is a duplicate of #58573 |
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: