import difflib


def name_matches(scripts, arg):
    """Check for a match of the script in available scripts

    if all, return the entire script list
    if the exact script is available, return that script
    if no exact script name detected, match the argument with keywords
    title and name of all scripts and return the closest matches
    """
    arg = arg.strip().lower()
    matches = []

    if not arg:
        raise ValueError("No dataset name specified")

    if arg == "all":
        return scripts

    for script in scripts:
        if arg == script.name.lower():
            return [script]

    for script in scripts:
        script_match_ratio = difflib.SequenceMatcher(None, script.name, arg).ratio()
        if script_match_ratio > 0.53:
            matches.append((script.name, script_match_ratio))

    matches.sort(key=lambda x: -x[1])

    print('\nThe dataset "{}" '
          "isn't currently available in the pydataweaver.".format(arg))
    if matches:
        print("Did you mean:" " \n\t{}".format("\n\t".join([i[0] for i in matches])))