[Python] How to make an option available only when another option has a certain value?

Discussão em 'Python' iniciado por Stack, Abril 19, 2025.

  1. Stack

    Stack Membro Participativo

    I want to enhance an existing command-line application done with Python click to allow for certain options depending on what the --format option is set to.

    In my special case, I would like to enable --delimiter option when the --format option equals csv and an --indent option when the --format option equals json. It should all fit into one command, so I do not want to introduce subcommands if not absolutely necessary.

    I looked into the group mechanism of click and also the additional extension package click-option-group but I think this is only about grouping the commands, but does not fulfill the goal above.

    This question is similar: python-click: dependent options on another option, but the difference is that it has predefined values for the "sub option", whereas in my case --indent is a user-specified value, e.g. 2, 4, 1, et cetera... Furthermore, as all the answers that go into my direction are rather old, I would look for a more up-to-date answer, as to what is possible as of today. What also would be great if it were possible to use functionality of a Python library with preference on click instead of having to add additional code outside of the library.

    Continue reading...

Compartilhe esta Página