Skip to content

Instantly share code, notes, and snippets.

@red-hood
Last active July 29, 2016 17:57
Show Gist options
  • Save red-hood/f1b025682b51f21c2fc17c6ef1366c95 to your computer and use it in GitHub Desktop.
Save red-hood/f1b025682b51f21c2fc17c6ef1366c95 to your computer and use it in GitHub Desktop.
from click.types import ParamType
from click.exceptions import BadParameter
class FallBackType(ParamType):
def __init__(self, fallback, def_type):
self.fallback = fallback
self.def_type = def_type
def convert(self, value, param, ctx):
try:
return self.def_type(value, param, ctx)
except BadParameter:
if callable(fallback):
return fallback(value, param, ctx)
return self.fallback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment