GH-55797: Add ArgumentParser.add_mutually_inclusive_group#150981
GH-55797: Add ArgumentParser.add_mutually_inclusive_group#150981savannahostrowski wants to merge 6 commits into
ArgumentParser.add_mutually_inclusive_group#150981Conversation
Documentation build overview
75 files changed ·
|
ArgumentParser.add_mutually_inclusive_groupArgumentParser.add_mutually_inclusive_group
clytaemnestra
left a comment
There was a problem hiding this comment.
If I understand correctly, this is meant to mirror add_mutually_exclusive_group(). One thing I noticed: the exclusive group rejects a required=True member at setup time (ValueError: mutually exclusive arguments must be optional), but the inclusive group accepts it. I'm wondering how add_mutually_inclusive_group() should behave in that case?
For example, with a non-required group where one member is required=True:
>>> g = p.add_mutually_inclusive_group()
>>> g.add_argument("--foo")
_StoreAction(option_strings=['--foo'], dest='foo', nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None, deprecated=False)
>>> g.add_argument("--bar", required=True)
_StoreAction(option_strings=['--bar'], dest='bar', nargs=None, const=None, default=None, type=None, choices=None, required=True, help=None, metavar=None, deprecated=False)
>>> p.parse_args([])
usage: [-h] [--foo FOO & --bar BAR]
: error: the following arguments are required: --barSince the group is all-or-nothing, I'd have expected nothing (empty args) to be accepted, but --bar being required makes that impossible. Should this combination be guarded against (like the exclusive group does) or is it intended? Apologies if I'm missing something.
|
@clytaemnestra Good catch - you're right, we need to add a guard to raise if someone tries to add required=True to an argument, since it doesn't really make sense at the member level. |
Adds
ArgumentParser.add_mutually_inclusive_group(), a flat, symmetric"all-or-nothing" group: the arguments in the group must either all be
provided together, or none of them.
It mirrors
add_mutually_exclusive_group:required=Truemakes the groupmandatory, usage renders with & (e.g.
[--lport LPORT & --rport RPORT]),and
parents=/subparsers are handled.I have scoped deliberately to the flat, symmetric case and not nested boolean
composition which was deprecated 3.11 / removed 3.14, see #127133.
I'm still collecting some feedback on the issue so leaving this as a draft for now.