from typing import Callable
from typing import Any
import click


def _get(func: Callable, pred: Callable, no_error: bool = False) -> Any:
    """
    Retrieve the first instance from `func` that matches `pred`.
    """
    value = next((elm for elm in func() if pred(elm)), None)
    if value or no_error:
        return value
    name = func.__name__.replace("list_", "").replace("_", "-").rstrip("s")
    raise click.ClickException("invalid {}".format(name))