Skip to content

Click Utilities

elva.click_utils

Module holding wrappers and decorators for lazy loading command-line interface.

Functions:

lazy_cli(f)

Wrapper around group.

In addtion, the returned Group object gets a lazy_load(import_name, **kwargs) method being a wrapper around its group(cls=LazyGroup, import_name=import_name, invoke_without_command=True, **kwargs) method.

invoke_without_command is set to True so that it can be set in the lazy loaded command.

Parameters:

  • f (Callable) –

    function used as a CLI command.

Returns:

  • Group

    click.group() object with presets and shortcut method lazy_load

Source code in src/elva/click_utils.py
def lazy_cli(f: Callable) -> Group:
    """
    Wrapper around [`group`][click.group].

    In addtion, the returned [`Group`][click.Group] object gets a `lazy_load(import_name, **kwargs)` method being a wrapper around its `group(cls=LazyGroup, import_name=import_name, invoke_without_command=True, **kwargs)` method.

    `invoke_without_command` is set to `True` so that it can be set in the lazy loaded command.

    Arguments:
        f: function used as a CLI command.

    Returns:
        `click.group()` object with presets and shortcut method `lazy_load`
    """

    f = click.group(
        invoke_without_command=True,
        context_settings=dict(ignore_unknown_options=True, allow_extra_args=True),
    )(f)

    def lazy_load(import_name: str, **kwargs):
        return f.group(
            cls=LazyGroup,
            import_name=import_name,
            invoke_without_command=True,
            **kwargs,
        )

    f.lazy_load = lazy_load
    return f

elva_app_cli(**kwargs)

Wrapper around group with invoke_without_command=True.

Source code in src/elva/click_utils.py
def elva_app_cli(**kwargs):
    """
    Wrapper around [`group`][click.group] with `invoke_without_command=True`.
    """

    def decorator(f: Callable):
        if (
            "invoke_without_command" in kwargs.keys()
            and not kwargs["invoke_without_command"]
        ):
            print_help(click.group(**kwargs)(f))
            exit()
        kwargs["invoke_without_command"] = True
        return click.group(**kwargs)(f)

    return decorator

lazy_group_without_invoke(**kwargs)

Wrapper around group with invoke_without_command=False.

Source code in src/elva/click_utils.py
def lazy_group_without_invoke(**kwargs) -> Callable[[Callable], Group]:
    """
    Wrapper around [`group`][click.group] with `invoke_without_command=False`.
    """

    def decorator(f: Callable):
        if (
            "invoke_without_command" in kwargs.keys()
            and kwargs["invoke_without_command"]
        ):
            return click.group(**kwargs)(f)
        kwargs["invoke_without_command"] = False
        print_help(click.group(**kwargs)(f))
        exit()

    return decorator

lazy_group(**kwargs)

Decorator for a Group object from elva_app_cli.

Source code in src/elva/click_utils.py
def lazy_group(**kwargs) -> Callable[[Callable], Group]:
    """
    Decorator for a [`Group`][click.Group] object from [`elva_app_cli`][elva.click_utils.elva_app_cli].
    """

    def decorator(f: Callable):
        f = elva_app_cli(**kwargs)(f)
        return f

    return decorator

print_help(f)

Print help of function f with echo, considering f's context

Source code in src/elva/click_utils.py
def print_help(f: Command):
    """
    Print help of function `f` with [`echo`][click.echo], considering `f`'s context
    """

    try:
        with click.Context(f) as ctx:
            click.echo(f.get_help(ctx))
    except Exception:
        pass