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 help error: arguments created by add_mutually_exclusive_group() are shown outside their parent group created by add_argument_group() #70070
Comments
So, a parent parser is created. It has a "global arguments" group (by add_argument_group()) and this group has a mutually exclusive group. Then a child parser is created used previous parser as parent. The next code shows the problem: import argparse
# create parent parser
parent_parser = argparse.ArgumentParser(add_help = False)
# create group for its arguments
global_group = parent_parser.add_argument_group("global arguments")
global_group.add_argument("--global-arg1")
global_group.add_argument("--global-arg2")
mutex_group = global_group.add_mutually_exclusive_group()
mutex_group.add_argument("--mutex-arg1")
mutex_group.add_argument("--mutex-arg2")
# create child parser
child_parser = argparse.ArgumentParser(parents = [parent_parser])
child_parser.add_argument("--child-arg1")
child_parser.add_argument("--child-arg2")
print("="*100)
parent_parser.print_help()
print("="*100)
child_parser.print_help() The output: ==================================================================================================== global arguments: optional arguments: global arguments: The error is that the --mutex-arg-s can be seen in "optional arguments" part instead of "global arguments" part in the second (child help) case. |
In _ActionsContainer._add_container_actions there is this note:
In other bug/issues we've noted that the way to give an exclusive group a title and description is to nest it in an argument_group. So it looks like no one has worked through the details - in this parent situation - of adding a nested mutually exclusive group. In my preliminary tests, it looks like the 'group_map' is being messed up by the block of code that adds the mutually_exclusive_groups. I don't know how easy it is to fix that. The immediate user solution is not use parents and nested mutually exclusive groups together. |
I've attached a file that monkey patches the _ActionsContainer._add_container_actions method. It corrects the I may construct of formal patch later. |
paul.j3, thx the the patch, it is perfect!!! |
Unfortunately the problem consists in case of normal sub-group too :(. If you complete my previous example code with the next code, then the problem can be seen: sub_group = global_group.add_argument_group()
sub_group.add_argument("--sub-arg1")
sub_group.add_argument("--sub-arg2") Part of unexpected output: optional arguments: Is there a patch for it too? :) |
Argument groups are not designed to be nested. If you print_help the parent parser, you'll see that the sub_args are missing entirely, not just displaced. They appear in the usage, but not the help lines. sub_group has no record that it was added to global_group (it doesn't have the ._container attribute). All containers, both parsers and groups, have a ._action_groups list (inherited from their common super), but I don't think any of the group code uses that list. And the part of the help_formatter than handles groups is not recursive. It handles just one level of groups. I suspect the sub_group actions appear in the 'optional arguments' group for similar reasons as with the mutually_exclusive_group, but the details probably differ. There have been a number of questions on SO about using argument_groups to add actions to a mutually_exclusive_group. They either want a convenient way of adding a group of actions, or they want some sort of 'any' logic applied to the subgroup. We've had to say - no you can't nest groups like that. I have explored in another bug/issue the idea of nesting groups and applying all sorts of logic (not just xor), but that's a big issue. |
Earlier issue on the same topic - passing a mutually exclusive group via parents http://bugs.python.org/issue16807 Can they be consolidated? |
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: