wroberts/pytimeparse

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?

aisch commented

@dnephin that's probably a more correct way to do it (type rather than action). would be nice to add that to this lib.

@dnephin @aisch sounds like a good idea. Can either of you make a pull request?