Skip to content

Commit c53f16b

Browse files
authored
pythongh-94787: Add tip to put mutually-exclusive group into help group
It's not obvious that a group can be added to a group, and this is important when trying to organize your help output. (New main branch PR originally from PR python#94788)
1 parent 8d7089f commit c53f16b

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

Doc/library/argparse.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,25 @@ Mutual exclusion
20312031

20322032
Note that currently mutually exclusive argument groups do not support the
20332033
*title* and *description* arguments of
2034-
:meth:`~ArgumentParser.add_argument_group`.
2034+
:meth:`~ArgumentParser.add_argument_group`. However, you can still add a
2035+
mutually exclusive group to another group so they are organized within
2036+
the help messages as follows::
2037+
2038+
>>> parser = argparse.ArgumentParser(prog='PROG')
2039+
>>> group = parser.add_argument_group('Group title', 'Group desciption')
2040+
>>> exclusive_group = group.add_mutually_exclusive_group(required=True)
2041+
>>> exclusive_group.add_argument('--foo', help='foo help')
2042+
>>> exclusive_group.add_argument('--bar', help='bar help')
2043+
>>> parser.print_help()
2044+
usage: PROG [-h] (--foo | --bar)
2045+
optional arguments:
2046+
-h, --help show this help message and exit
2047+
2048+
Group title:
2049+
Group desciption
2050+
2051+
--foo FOO foo help
2052+
--bar BAR bar help
20352053

20362054
.. versionchanged:: 3.11
20372055
Calling :meth:`add_argument_group` or :meth:`add_mutually_exclusive_group`

0 commit comments

Comments
 (0)