Created
August 19, 2014 18:02
-
-
Save ulope/4b6fc547408f782c0dcd to your computer and use it in GitHub Desktop.
DateTime ParamType for the click CLI Parser library (uses arrow)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arrow | |
from arrow.parser import ParserError | |
import click | |
class DateParamType(click.ParamType): | |
name = 'date' | |
def __init__(self, date_format="YYYY-MM-DD"): | |
super(DateParamType, self).__init__() | |
self.date_format = date_format | |
def convert(self, value, param, ctx): | |
try: | |
return arrow.get(value, self.date_format).date() | |
except ParserError: | |
self.fail("Could not parse '%s' as a date." % (value, )) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment