X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=argparse_utils.py;h=8c254ae1f995f6aa684134c595e35968ea5b36cd;hb=36fea7f15ed17150691b5b3ead75450e575229ef;hp=1c61b2460650c11872cbaaabf4e7f5a689be746c;hpb=b454ad295eb3024a238d32bf2aef1ebc3c496b44;p=python_utils.git diff --git a/argparse_utils.py b/argparse_utils.py index 1c61b24..8c254ae 100644 --- a/argparse_utils.py +++ b/argparse_utils.py @@ -17,12 +17,7 @@ logger = logging.getLogger(__name__) class ActionNoYes(argparse.Action): def __init__( - self, - option_strings, - dest, - default=None, - required=False, - help=None + self, option_strings, dest, default=None, required=False, help=None ): if default is None: msg = 'You must provide a default with Yes/No action' @@ -47,14 +42,13 @@ class ActionNoYes(argparse.Action): const=None, default=default, required=required, - help=help + help=help, ) @overrides def __call__(self, parser, namespace, values, option_strings=None): - if ( - option_strings.startswith('--no-') or - option_strings.startswith('--no_') + if option_strings.startswith('--no-') or option_strings.startswith( + '--no_' ): setattr(namespace, self.dest, False) else: @@ -89,6 +83,7 @@ def valid_bool(v: Any) -> bool: if isinstance(v, bool): return v from string_utils import to_bool + try: return to_bool(v) except Exception: @@ -110,6 +105,7 @@ def valid_ip(ip: str) -> str: """ from string_utils import extract_ip_v4 + s = extract_ip_v4(ip.strip()) if s is not None: return s @@ -136,6 +132,7 @@ def valid_mac(mac: str) -> str: """ from string_utils import extract_mac_address + s = extract_mac_address(mac) if s is not None: return s @@ -206,6 +203,7 @@ def valid_date(txt: str) -> datetime.date: -ANYTHING- """ from string_utils import to_date + date = to_date(txt) if date is not None: return date @@ -228,6 +226,7 @@ def valid_datetime(txt: str) -> datetime.datetime: -ANYTHING- """ from string_utils import to_datetime + dt = to_datetime(txt) if dt is not None: return dt @@ -250,6 +249,7 @@ def valid_duration(txt: str) -> datetime.timedelta: """ from datetime_utils import parse_duration + try: secs = parse_duration(txt) except Exception as e: @@ -260,5 +260,6 @@ def valid_duration(txt: str) -> datetime.timedelta: if __name__ == '__main__': import doctest + doctest.ELLIPSIS_MARKER = '-ANYTHING-' doctest.testmod()