Skip to content

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

Closed
AlanEvangelista mannequin opened this issue Mar 9, 2017 · 9 comments
Closed

argparse arguments in main parser hide an argument in subparser #73963

AlanEvangelista mannequin opened this issue Mar 9, 2017 · 9 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@AlanEvangelista
Copy link
Mannequin

AlanEvangelista mannequin commented Mar 9, 2017

BPO 29777
Nosy @ericvsmith, @ZackerySpytz

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 = None
created_at = <Date 2017-03-09.20:35:40.619>
labels = ['type-feature', 'library']
title = 'argparse arguments in main parser hide an argument in subparser'
updated_at = <Date 2020-11-27.23:14:49.989>
user = 'https://bugs.python.org/AlanEvangelista'

bugs.python.org fields:

activity = <Date 2020-11-27.23:14:49.989>
actor = 'ZackerySpytz'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2017-03-09.20:35:40.619>
creator = 'Alan Evangelista'
dependencies = []
files = []
hgrepos = []
issue_num = 29777
keywords = []
message_count = 7.0
messages = ['289327', '289328', '289358', '289430', '289732', '289734', '381970']
nosy_count = 4.0
nosy_names = ['eric.smith', 'paul.j3', 'Alan Evangelista', 'ZackerySpytz']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue29777'
versions = ['Python 2.7']

@AlanEvangelista
Copy link
Mannequin Author

AlanEvangelista mannequin commented Mar 9, 2017

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?

@AlanEvangelista AlanEvangelista mannequin added the stdlib Python modules in the Lib dir label Mar 9, 2017
@ericvsmith
Copy link
Member

No, we won't be adding new features to 2.7. Sorry.

@ericvsmith ericvsmith added the type-feature A feature request or enhancement label Mar 9, 2017
@AlanEvangelista
Copy link
Mannequin Author

AlanEvangelista mannequin commented Mar 10, 2017

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.

@paulj3
Copy link
Mannequin

paulj3 mannequin commented Mar 11, 2017

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.

@AlanEvangelista
Copy link
Mannequin Author

AlanEvangelista mannequin commented Mar 16, 2017

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:

  • keep this issue opened until the bug with short options is fixed and we are more confident in fixing this in Py2
  • close the issue with "won't fix" resolution and add the known issue to argparse documentation in Python 2.6 doc

@AlanEvangelista
Copy link
Mannequin Author

AlanEvangelista mannequin commented Mar 16, 2017

s/Python 2.6/Python 2/ in last comment

@ZackerySpytz
Copy link
Mannequin

ZackerySpytz mannequin commented Nov 27, 2020

Python 2.7 is no longer supported, so I think this issue should be closed.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@erlend-aasland erlend-aasland moved this to Features in Argparse issues May 19, 2022
@Conchylicultor
Copy link
Contributor

This is still broken in 3.10, but is a duplicate of #58573, so could be closed

@savannahostrowski
Copy link
Member

Closing as this is a duplicate of #58573

@github-project-automation github-project-automation bot moved this from Features to Doc issues in Argparse issues Sep 24, 2024
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-feature A feature request or enhancement
Projects
Status: Doc issues
Development

No branches or pull requests

3 participants