from typing import List
import re
import click


def check_descriptors(ctx, param, value: List[str]) -> List[str]:
    """
    Check format of each MeSH descriptor passed as command line argument. Raise
    exception if any has incorrect format.
    :param ctx: required for click callback feature
    :param param: required for click callback feature
    :param value: tuple of MeSH descriptors as written on command line
    :return: value if all descriptors are correctly formatted
    """
    for des in value:
        if not re.fullmatch(r'D(\d{6}|\d{9})', des):
            raise click.BadParameter(
                'Descriptor %s incorrect, should be D followed by 6 or 9 digits'
                % des, param_hint='MeSH DESCRIPTORS')
    return value