Lift easter() function from dateutils and remove that dependency from
[pyutils.git] / src / pyutils / config.py
index be11a779507c5ff741c67771b7511fc4b24984b1..ed9a8212c0c8d1c40a6ecabd933eb23f1bf5cbb1 100644 (file)
@@ -7,7 +7,8 @@ optionally, from saved (local or Zookeeper) configuration files... with
 optional support for dynamic arguments (i.e. that can change during runtime).
 
 Let's start with an example of how to use :py:mod:`pyutils.config`.  It's
-pretty easy for normal commandline arguments because it uses :py:mod:`argparse`:
+pretty easy for normal commandline arguments because it wraps :py:mod:`argparse`
+(see https://docs.python.org/3/library/argparse.html):
 
     In your file.py::
 
@@ -139,13 +140,15 @@ ORIG_ARGV: List[str] = sys.argv.copy()
 
 
 class OptionalRawFormatter(argparse.HelpFormatter):
-    """This formatter has the same bahavior as the normal argparse text
-    formatter except when the help text of an argument begins with
-    "RAW|".  In that case, the line breaks are preserved and the text
-    is not wrapped.
+    """This formatter has the same bahavior as the normal argparse
+    text formatter except when the help text of an argument begins
+    with "RAW|".  In that case, the line breaks are preserved and the
+    text is not wrapped.  It is enabled automatically if you use
+    :py:mod:`pyutils.config`.
 
-    Use this, for example, when you need the helptext of an argument
-    to have its spacing preserved exactly, e.g.::
+    Use this by prepending "RAW|" in your help message to disable
+    word wrapping and indicate that the help message is already
+    formatted and should be preserved.  Here's an example usage::
 
         args.add_argument(
             '--mode',
@@ -170,6 +173,7 @@ class OptionalRawFormatter(argparse.HelpFormatter):
           PRECOMPUTE = populate hash table with optimal guesses.
             ''',
         )
+
     """
 
     def _split_lines(self, text, width):
@@ -220,6 +224,7 @@ GROUP.add_argument(
 GROUP.add_argument(
     '--config_allow_dynamic_updates',
     default=False,
+    action='store_true',
     help='If enabled, allow config flags with the string "dynamic" in their names to change at runtime when a new Zookeeper based configuration is created.  See the --config_savefile help message for more information about this option.',
 )
 GROUP.add_argument(
@@ -698,6 +703,15 @@ def parse(entry_module: Optional[str]) -> Dict[str, Any]:
     return CONFIG.parse(entry_module)
 
 
+def error(message: str, exit_code: int = 1) -> None:
+    """
+    Convenience method for indicating a configuration error.
+    """
+    logging.error(message)
+    print(message, file=sys.stderr)
+    sys.exit(exit_code)
+
+
 def has_been_parsed() -> bool:
     """Returns True iff the global config has already been parsed"""
     return CONFIG.has_been_parsed()