Skip to content

Commit 810ae51

Browse files
authored
gh-94787: [doc] Add to argparse doc an example of a mutually-exclusive group nested in an argument group (GH-94807)
1 parent 670007a commit 810ae51

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

Doc/library/argparse.rst

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,26 @@ 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, a mutually exclusive
2035+
group can be added to an argument group that has a title and description.
2036+
For example::
2037+
2038+
>>> parser = argparse.ArgumentParser(prog='PROG')
2039+
>>> group = parser.add_argument_group('Group title', 'Group description')
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 FOO | --bar BAR)
2045+
2046+
options:
2047+
-h, --help show this help message and exit
2048+
2049+
Group title:
2050+
Group description
2051+
2052+
--foo FOO foo help
2053+
--bar BAR bar help
20352054

20362055
.. versionchanged:: 3.11
20372056
Calling :meth:`add_argument_group` or :meth:`add_mutually_exclusive_group`

0 commit comments

Comments
 (0)