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 exclusive group inside required exclusive group displays incorrectly #89853
Comments
The code below, when invoked with -h, prints: (.env) [andrew@localhost py]$ python -m tests_sa.argparse_bug -h options: where the final two characters in the usage line are swapped. It should be usage: argparse_bug.py [-h] (-a A | [-b B | -c C]) or maybe even usage: argparse_bug.py [-h] (-a A | (-b B | -c C)) from argparse import ArgumentParser
def main():
parser = ArgumentParser()
outer = parser.add_mutually_exclusive_group(required=True)
outer.add_argument('-a')
inner = outer.add_mutually_exclusive_group()
inner.add_argument('-b')
inner.add_argument('-c')
parser.parse_args()
if __name__ == '__main__':
main() |
There was a bug/issue that addressed problems with nested mutually_exclusive_groups. It should be easy to find. The problem is that the usage formatter is brittle, building a string and then striping out "unnecessary" characters. I assume the fix handled the default case (not required), but apparently it missed this variation. I wasn't too involved with that, since in my opinion nesting these groups is useless, and should be avoided. You might as well use one union group. Test the parsing for yourself. |
https://bugs.python.org/issue29553 supposedly fixed the parentheses for nested groups. You can read its discussion and patches to see why it does not handle your case. I don't see any examples have required groups. [] is used for un-required, () for required. This patch did not change how the usage was generated. It just refined how excess [()] were removed. Proper handling of groups requires a major rewrite of the usage formatter. |
Nesting argument groups and mutually exclusive groups is now deprecated (see bpo-22047). Thank you for the bug report. |
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: