-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
Support Nested/Dependent Mutually Exclusive Arguments in argparse #147964
Copy link
Copy link
Open
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Projects
Status
Features