click-contrib/click-option-group

any way to permit a 'default' option value to fullfill a require option group?

netllama opened this issue · 2 comments

I'm creating an option group which will have two options. The two options should either not be used at all, or should both be required. I thought that AllOptionGroup would meet this requirement.

However, I also specified a default value for one of the two options. Ideally, I'd like the default value to be used if the option is not explicitly specified. However, that doesn't work for the AllOptionGroup, as it always requires the option to be specified at run time. This defeats the purpose of specifying a default value if it can't be used.

Here's what I am attempting:

from click_option_group import AllOptionGroup

desired_fw_opts = AllOptionGroup('Set Desired Firmware options', help='Options required to set desired FW version',)

@main.command()
@desired_fw_opts.option(
    '--set-desired-fw',
    '-s',
    'setfw',
    help='Set desired firmware version on all caches matching specified value.',
)
@desired_fw_opts.option(
    '--robex-s',
    '-rs',
    'robexs',
    default='opStatus = Live OR opStatus = Filling',
    show_default=True,
    help='robex to match caches when applying the specified desired firmware value.',
)
def upgrade_openbmc(setfw, robexs):
    print(f'setfw = {setfw}\trobexs = {robexs}')

Is what I'm trying to do possible with click-option-group ?

Hello @netllama

If I understand correctly, you want specify only one option (-s in your example) in AllOptionGroup:

$ cli -s Value
f'setfw = Value    robexs = opStatus = Live OR opStatus = Filling

AllOptionGroup works differently. You need to specify all options in CLI or none. If you do not specify options from the group, default values will be used. If we can specify not all options from "AllOptionGroup" group, it makes the group itself meaningless. This is a logical contradiction.

I think you can implement an option group class with the logic you need.

You can also try to use OptionGroup class. Maybe that's what you need. OptionGroup has no any constraints, this is just logical grouping options.

ok, thanks for the help