Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consuming groups in function arguments #53

Open
LecrisUT opened this issue Aug 10, 2023 · 2 comments
Open

Consuming groups in function arguments #53

LecrisUT opened this issue Aug 10, 2023 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@LecrisUT
Copy link
Contributor

LecrisUT commented Aug 10, 2023

This might be a bit tricky to implement, but would be nice to have:

@click.command
@optgroup.group('My group', 'my_group')
@optgroup.option('--foo')
@optgroup.option('--bar')
@click.option('--click-option')
def my_func(my_group,click_option):
  pass

I.e. access the whole group as a dict my_group. A hacky workaround would be:

def _my_group(func):
    """Select group options"""

    @optgroup.group('My group'')
    @optgroup.option('--foo')
    @optgroup.option('--bar')
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        my_group = {
            opt: kwargs.pop(opt)
            for opt in ('foo', 'bar')
        }
        return func(*args, my_group=my_group, **kwargs)

    return wrapper

@click.command
@_my_group
@click.option('--click-option')
def my_func(my_group,click_option):
  pass

Probably this can be adapted for proper implementation

@KalleDK
Copy link

KalleDK commented Sep 7, 2023

I am looking for this too, tried to see if I could do something with handle_parse_result and set the result on the context, but your hack seems better

@espdev
Copy link
Member

espdev commented Nov 2, 2023

Hello guys,
I don't mind. PRs are welcome :)

@espdev espdev added the help wanted Extra attention is needed label Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants