class TwoOrThree(argparse._AppendAction):

    def __call__(self, parser, namespace, values, option_string=None):
        if not 2 <= len(values) <= 3:
            raise argparse.ArgumentError(self, 
                '%s takes 2 or 3 values, %d given' % (option_string, len(
                values)))
super(TwoOrThree, self).__call__(parser, namespace, values, option_string)
Using the same option multiple times in Python's Argparse