import click
import pathlib


def handle_filename(_, param, value):
    """
    Process the filename argument

    Raise a UsageError when the file is missing unless the param is not required
    """
    if value is None:
        if not param.required:
            return None
        raise click.UsageError("** FILENAME is required")

    value = pathlib.Path(value)

    if value.exists():
        return value

    raise click.UsageError(f"** {value} does not exist :(")