Skip to content

Commit 246e46c

Browse files
committed
docs(parser): Add doctest to _extract_mutex_groups
Add working doctests demonstrating: - Extracting mutex groups from ArgumentParser - Verifying actions map to same MutuallyExclusiveGroup instance - Empty mapping for parsers without mutex groups Uses identity checks (is) instead of set() since dataclasses aren't hashable by default.
1 parent 8428326 commit 246e46c

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

docs/_ext/sphinx_argparse_neo/parser.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,35 @@ def _extract_mutex_groups(
366366
-------
367367
dict[int, MutuallyExclusiveGroup]
368368
Mapping from action id to the MutuallyExclusiveGroup it belongs to.
369+
370+
Examples
371+
--------
372+
Extract mutually exclusive groups from a parser with one group:
373+
374+
>>> parser = argparse.ArgumentParser()
375+
>>> group = parser.add_mutually_exclusive_group()
376+
>>> _ = group.add_argument("--foo", help="Use foo")
377+
>>> _ = group.add_argument("--bar", help="Use bar")
378+
>>> mutex_map = _extract_mutex_groups(parser)
379+
>>> len(mutex_map)
380+
2
381+
382+
Each action in the group maps to the same MutuallyExclusiveGroup:
383+
384+
>>> values = list(mutex_map.values())
385+
>>> values[0] is values[1]
386+
True
387+
>>> len(values[0].arguments)
388+
2
389+
>>> [arg.names[0] for arg in values[0].arguments]
390+
['--foo', '--bar']
391+
392+
A parser without mutex groups returns an empty mapping:
393+
394+
>>> parser2 = argparse.ArgumentParser()
395+
>>> _ = parser2.add_argument("--verbose")
396+
>>> _extract_mutex_groups(parser2)
397+
{}
369398
"""
370399
mutex_map: dict[int, MutuallyExclusiveGroup] = {}
371400

0 commit comments

Comments
 (0)