def check_input(prompt, assertion=None, default=None):
    """Get input from cmdline, ensuring that it passes the given assertion.

    assertion: a function that if given a value will return None if the check
    should pass, otherwise returning a helpful error message as a string."""
    if default is not None:
        prompt += " [default=%s]: " % str(default)

    while True:
        value = input(prompt).strip()
        if value == "" and default is not None:
            value = default

        if assertion is not None:
            check = assertion(value)
            if check is not None:
                error_msg = '\tInvalid input'
                if not isinstance(check, bool):
                    error_msg += ': ' + str(check)
                print(error_msg)
                continue
        return value