wroberts / pytimeparse

A small Python module to parse various kinds of time expressions.
MIT License
287 stars 39 forks source link

add argparse utility for parsing to timedelta #11

Open aisch opened 8 years ago

aisch commented 8 years ago

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 8 years ago

I've done something similar using type instead of action:

aisch commented 8 years ago

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

wroberts commented 8 years ago

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