"""CLI Utils."""

import shlex


def shlex_quote_if_needed(arg: str) -> str:
    """Quote for shell, if needed."""
    res = shlex.quote(arg)
    if res == f"'{res}'":
        # simple quoting
        return arg
    return res


def shell_args_to_line(argv: list[str]) -> str:
    """Convert a list of arguments to a single string for the shell.

    This quotes only the arguments that need to be quoted.
    """
    return " ".join([shlex_quote_if_needed(a) for a in argv])
