add argparse utility for parsing to timedelta
aisch opened this issue · 3 comments
aisch commented
i dont know if you think its good to put in the lib but i have something like:
class TimeDeltaAction(argparse.Action):
def parse(self, value):
parsed = pytimeparse.parse(value)
if parsed is None:
raise argparse.ArgumentError(
self, '"{0}" invalid time-delta expression'.format(value),
)
return timedelta(seconds=parsed)
# argparse.Action
def __call__(self, parser, namespace, values, option_string=None):
if isinstance(values, basestring):
setattr(namespace, self.dest, self.parse(values))
else:
setattr(namespace, self.dest, map(self.parse, values))
copied around. would you consider a patch that adds it to pytimeparse
?
dnephin commented
I've done something similar using type
instead of action
: