-
-
Notifications
You must be signed in to change notification settings - Fork 30k
-
-
Notifications
You must be signed in to change notification settings - Fork 30k
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
argparse group nesting lost on inheritance #61011
Comments
If you wrap a mutually exclusive group inside an argument group in an argparse.ArgumentParser, and then use parents= to inherit from that parser, the non-exclusive argument group is forgotten in the help output, and the arguments move to the default "optional arguments" section. For example: # construct the parser with a mutually exclusive group
>>> import argparse
>>> parent = argparse.ArgumentParser(add_help=False)
>>> group = parent.add_argument_group('the group')
>>> group.add_argument('--foo') and None
>>> mutex = group.add_mutually_exclusive_group()
>>> mutex.add_argument('-a', action='store_true') and None
>>> mutex.add_argument('-b', action='store_true') and None
>>> parent.print_help()
usage: [--foo FOO] [-a | -b]
# now try to inherit from it; "the group" is forgotten for
# mutex arguments, but remains for others
>>> argparse.ArgumentParser(add_help=False, parents=[parent]).print_help()
usage: [--foo FOO] [-a | -b]
I see the same behavior on 2.7.3 and 3.3.0. The problem is that One thing about the patch: it assumes that the |
@dougal sorry about the delay in getting back to you. |
To put this issue in perspective:
proposes adding these attributes to MEG. There I proposed doing so via this nested group mechanism. So that patch requires this one to work correctly with parents.
|
The subcommands grouping mechanism proposed in http://bugs.python.org/issue9341 This _add_container_actions method is brittle. It has to know too much about the structure of a parser and its groups. Any change to that structure could break this [parents] mechanism. In a refactoring, each 'add_xxx' method would have a 'copy_xxx' companion method. |
Reproduced on 3.11: >>> argparse.ArgumentParser(add_help=False, parents=[parent]).print_help()
usage: [--foo FOO] [-a | -b] options: the group: |
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: