Click: Mutually exclusive required options

Created on 8 Oct 2018  路  4Comments  路  Source: pallets/click

Hi.

I have a situation where there's 3 ways a subcommand can go and each has a distinct option assigned to it. One of 3 has to be picked, but never in a combination with others.

Is there a way to achieve native click required=True- type of functionality(prompts, errors etc) in such case? So far I'm left with forest of ugly ifs.

Most helpful comment

This feature has been requested 4 years ago. It has been requested multiple times. It is listed as missing functionality on multiple blogs. Is the basis of rejections a combination of (1) having to modify interface and (2) you can implement it yourself? Or am I misunderstanding something here?

All 4 comments

Hi,

You could try making a subclass of option as suggested here:
https://stackoverflow.com/questions/37310718/mutually-exclusive-option-groups-in-python-click

Also see these issues for related discussion:
https://github.com/pallets/click/issues/257
https://github.com/pallets/click/issues/509

I am going to close this since something similar has been proposed and rejected in the past. However if you have a strong case for including something like this in click feel free to continue discussing it here. (Or if I've misunderstood what you're asking for.)

This feature has been requested 4 years ago. It has been requested multiple times. It is listed as missing functionality on multiple blogs. Is the basis of rejections a combination of (1) having to modify interface and (2) you can implement it yourself? Or am I misunderstanding something here?

Based on the original rejection, it looks like Armin didn't think there was a clean way to implement this. It is also fairly easy to subclass option and get the requested behavior, which I think is a compelling reason to not add it to Click unless there is a really clean API for it.

Alternatively maybe we could add a section of some more subclass examples to the docs?

Was this page helpful?
0 / 5 - 0 ratings