Skip to content

Support Nested/Dependent Mutually Exclusive Arguments in argparse #147964

@Tailung42

Description

@Tailung42

Feature or enhancement

Proposal:

Currently, Python’s argparse module supports mutually exclusive groups at a single level, meaning you can enforce constraints like:

a or b or c

However, there is no built-in way to express nested or dependent exclusivity, such as:

a or b or (c and d)

In this case, if argument c is provided, d must also be provided, but a and b remain mutually exclusive with the combination (c and d).

Result:

This would allow developers to write complex argument logic withou writing code manually for such validation: I actually needed for a cli app. This would be something like:

# Valid
mytool.py --a
mytool.py --b
mytool.py --c --d

# Invalid
mytool.py --c          # missing --d
mytool.py --c --d --a  # mutually exclusive with a

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Status

    Features

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions