import unittest import argparse def email(string): """ check that email is correct""" import re # this ia a copy of the regex from django regex = re.compile( r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string r')@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$', re.IGNORECASE) # domain if not re.fullmatch(regex, string): raise argparse.ArgumentTypeError(f'{string} is not a correct email') return string class tests_email_argument(unittest.TestCase): def setUp(self): self.parser = argparse.ArgumentParser(description=' test of email parsing') self.parser.add_argument('email', help='your user mail', type=email,) def test1_email_incorrect(self): with self.assertRaises(argparse.ArgumentError): args = self.parser.parse_args(('odile.toto',))