from typing import Callable
import click
import functools


def pass_context_to_kwargs(func: Callable) -> Callable:
    """Decorator for `click` CLIs that puts all the kwargs in the `click` context in
    the decorated function's invocation kwargs. This can be a more elegant way to
    receive the context in some cases.

    Arguments:
        func:
            The `click` command function to be decorated.

    Returns:
        The decorated function, which passes all kwargs in the `click` context to the
        invocation kwargs.
    """

    @click.pass_context  # needed to receive the context
    def wrapper(context: click.Context, *args, **kwargs) -> None:
        """Extend kwargs with the context dict parameters.

        Arguments:
            context:
                The decorated command's context object.
        """
        kwargs = {**context.obj, **kwargs} if context.obj else kwargs
        return context.invoke(func, *args, **kwargs)

    return functools.update_wrapper(wrapper, func)