click-contrib/click-option-group

Request to support AllOrNoneOptionGroup class

roopahc opened this issue · 4 comments

Hello, we have a use case of providing a group of parameters as optional arguments, but they should be specified together. Either all options specified in the group should be provided or None of them should be specified. This feature should work for the same command.
For example.

import click
from click_option_group import optgroup, AllorNoneOptionGroup

@click.command()
@optgroup.group('Server URL', help='URL of some server connection')
@optgroup.option('-u', '--url', default='http://localhost:80', help='Url')
# pass both are none
@optgroup.group('Credentials', cls=AllOrNoneOptionGroup, help='The sources of the input data')
@optgroup.option('user', type=click.File(), help='Username')
@optgroup.option('password', type=click.File(), help='Password')
@click.option('--debug/--no-debug', default=False, help='Debug flag')
def cli(**params):
    print(params)

if __name__ == '__main__':
    cli()

It would be great if you could support this feature.

Hi @roopahc,

I think this behavior can be easily implemented. Moreover, we already have RequiredAllOptionGroup class. I can implement AllOptionGroup class for consistency with MutuallyExclusiveOptionGroup/RequiredMutuallyExclusiveOptionGroup.

@espdev thanks a lot. I can send the PR too.

@roopahc It would be great! If you will send PR could you add tests for new functionality also?

@espdev Please find this PR: #13