if action.choices is not None and value not in action.choices:
    tup = value, ', '.join(map(repr, action.choices))
msg = _('invalid choice: %r (choose from %s)') % tup
raise ArgumentError(action, msg)
Python argparse choices from an infinite set