-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
add remove_argument_group to argparse #53596
Comments
[From http://code.google.com/p/argparse/issues/detail?id=71] There is a method ArgumentParser.add_argument_group() to create and add an argument group to the parser. I would like the ability to remove an argument group via a method like remove_argument_group(group). The use case for me is I create an argument group and then conditionally add a bunch of arguments to it. If zero arguments are added I would like to then remove the group so that an empty group does not show up in the help output. |
I wonder if this patch is needed.
p=argparse.ArgumentParser(prog='PROG')
p.print_help()
The empty 'positional arguments' group is not displayed. Removing a group that has arguments is more complicated (and error prone) since it requires removing those arguments as well. There is a '_remove_action' method. But as best I can tell it is only used by '_handle_conflict_resolve', a rarely used alternative conflict handler. |
If the empty argument group has a 'description' it is displayed. For example with positionals group that I mentioned earlier: parser = argparse.ArgumentParser()
parser._action_groups[0].description = 'test'
parser.print_help() produces
So the user can fix this empty group display issue by setting this 'description' value to None. |
IMHO, if we supply the ability to add an argument group, we need add the ability to remove the argument group too. |
hai shi Do you have a specific need for this, or do you want it just for the same of completeness? |
ISTM this use is likely rare enough that it doesn't warrant an API expansion. The argparse API is already somewhat large, so we should only expand it when there is a compelling benefit or the absence of a reasonable workaround (i.e. lazily adding a group only when the first argument is added). |
paul, raymond. Thanks for give me a quick answer:). |
This is over a decade old and the discussion is mostly that it's not worth doing, so I suggest we close. |
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: