import os
from typing import Any
+from overrides import overrides
+
+
# This module is commonly used by others in here and should avoid
# taking any unnecessary dependencies back on them.
help=help
)
+ @overrides
def __call__(self, parser, namespace, values, option_strings=None):
if (
option_strings.startswith('--no-') or
raise argparse.ArgumentTypeError(msg)
+def valid_duration(txt: str) -> datetime.timedelta:
+ """If the string is a valid time duration, return a
+ datetime.timedelta representing the period of time. Otherwise
+ maybe raise an ArgumentTypeError or potentially just treat the
+ time window as zero in length.
+
+ >>> valid_duration('3m')
+ datetime.timedelta(seconds=180)
+
+ >>> valid_duration('your mom')
+ datetime.timedelta(0)
+
+ """
+ from datetime_utils import parse_duration
+ try:
+ secs = parse_duration(txt)
+ except Exception as e:
+ raise argparse.ArgumentTypeError(e)
+ finally:
+ return datetime.timedelta(seconds=secs)
+
+
if __name__ == '__main__':
import doctest
doctest.ELLIPSIS_MARKER = '-ANYTHING-'