This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Argparse exclusive group inside required exclusive group displays incorrectly
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Deprecate unsupported nesting of argparse groups
View: 22047
Assigned To: Nosy List: acooke, iritkatriel, paul.j3
Priority: normal Keywords:

Created on 2021-11-02 13:39 by acooke, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg405509 - (view) Author: andrew cooke (acooke) Date: 2021-11-02 13:39
The code below, when invoked with -h, prints:

(.env) [andrew@localhost py]$ python -m tests_sa.argparse_bug -h
usage: argparse_bug.py [-h] (-a A | [-b B | -c C)]

options:
  -h, --help  show this help message and exit
  -a A
  -b B
  -c C

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()
msg405521 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2021-11-02 17:05
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.
msg405531 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2021-11-02 18:42
https://bugs.python.org/issue29553
Argparser does not display closing parentheses in nested mutex groups

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.
msg408715 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-16 15:38
Nesting argument groups and mutually exclusive groups is now deprecated (see issue22047). Thank you for the bug report.
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 89853
2021-12-16 15:38:30iritkatrielsetstatus: open -> closed

superseder: Deprecate unsupported nesting of argparse groups

nosy: + iritkatriel
messages: + msg408715
resolution: duplicate
stage: resolved
2021-11-02 18:42:57paul.j3setmessages: + msg405531
2021-11-02 17:05:45paul.j3setnosy: + paul.j3
messages: + msg405521
2021-11-02 13:39:20acookecreate