def date_arg(value): """ Validate the --dates argument Function will ensure that the --dates argument matches a given regex. Possible values of the date are: - MMDD (eg. 0515) - MMDD-MMDD (eg. 0115-0315) - yesterday - today Parameters ---------- - value - *string* - automatically passed by the ArgumentParser object Returns ------- Returns the value back to the ArgumentParser object Exceptions ---------- - argparse.ArgumentTypeError - if the passed argument doesn't match the regex """ # Regex check if not re.match('^(\d{4}|\d{4}-\d{4}|yesterday|today)$', value): raise argparse.ArgumentTypeError("must be in the form MMDD, " "MMDD-MMDD, yesterday, " "or today".format(value)) # Make sure end date is >= start date if re.match("\d{4}-\d{4}", value): start, end = value.split('-') if not end >= start: raise argparse.ArgumentTypeError("The start date is less than the " "end date") return value