Easier and more self documenting patterns for loading/saving Persistent
[python_utils.git] / argparse_utils.py
index 5a270f6ef22c1845be1bd6c59ab7fde1cb4cfe21..f73a8936d3eb268b96ea387a543608d24d0ceb51 100644 (file)
@@ -1,5 +1,9 @@
 #!/usr/bin/python3
 
+# © Copyright 2021-2022, Scott Gasch
+
+"""Helpers for commandline argument parsing."""
+
 import argparse
 import datetime
 import logging
@@ -15,13 +19,31 @@ logger = logging.getLogger(__name__)
 
 
 class ActionNoYes(argparse.Action):
+    """An argparse Action that allows for commandline arguments like this::
+
+        cfg.add_argument(
+            '--enable_the_thing',
+            action=ActionNoYes,
+            default=False,
+            help='Should we enable the thing?'
+        )
+
+    This creates the following cmdline arguments::
+
+        --enable_the_thing
+        --no_enable_the_thing
+
+    These arguments can be used to indicate the inclusion or exclusion of
+    binary exclusive behaviors.
+    """
+
     def __init__(self, option_strings, dest, default=None, required=False, help=None):
         if default is None:
             msg = 'You must provide a default with Yes/No action'
             logger.critical(msg)
             raise ValueError(msg)
         if len(option_strings) != 1:
-            msg = 'Only single argument is allowed with YesNo action'
+            msg = 'Only single argument is allowed with NoYes action'
             logger.critical(msg)
             raise ValueError(msg)
         opt = option_strings[0]
@@ -81,8 +103,8 @@ def valid_bool(v: Any) -> bool:
 
     try:
         return to_bool(v)
-    except Exception:
-        raise argparse.ArgumentTypeError(v)
+    except Exception as e:
+        raise argparse.ArgumentTypeError(v) from e
 
 
 def valid_ip(ip: str) -> str:
@@ -247,10 +269,10 @@ def valid_duration(txt: str) -> datetime.timedelta:
 
     try:
         secs = parse_duration(txt)
-    except Exception as e:
-        raise argparse.ArgumentTypeError(e)
-    finally:
         return datetime.timedelta(seconds=secs)
+    except Exception as e:
+        logger.exception(e)
+        raise argparse.ArgumentTypeError(e) from e
 
 
 if __name__ == '__main__':