Reduce the doctest lease duration...
[python_utils.git] / config.py
index a83433c68733178b634dad288cc4c14428a6b81b..1a362a381eeaac36b6b8fabafa2db548ee82b1f0 100644 (file)
--- a/config.py
+++ b/config.py
@@ -188,6 +188,12 @@ GROUP.add_argument(
     action='store_true',
     help='If present, config will raise an exception if it doesn\'t recognize an argument.  The default behavior is to ignore unknown arguments so as to allow interoperability with programs that want to use their own argparse calls to parse their own, separate commandline args.',
 )
+GROUP.add_argument(
+    '--config_exit_after_parse',
+    default=False,
+    action='store_true',
+    help='If present, halt the program after parsing config.  Useful, for example, to write a --config_savefile and then terminate.',
+)
 
 
 class Config:
@@ -234,6 +240,9 @@ class Config:
     def __contains__(self, key: str) -> bool:
         return key in self.config
 
+    def get(self, key: str, default: Any = None) -> Optional[Any]:
+        return self.config.get(key, default)
+
     @staticmethod
     def add_commandline_args(title: str, description: str = "") -> argparse._ArgumentGroup:
         """Create a new context for arguments and return a handle.
@@ -571,6 +580,11 @@ class Config:
             self.dump_config()
 
         self.config_parse_called = True
+        if config['config_exit_after_parse']:
+            print("Exiting because of --config_exit_after_parse.")
+            if self.zk:
+                self.zk.stop()
+            sys.exit(0)
         return self.config
 
     def has_been_parsed(self) -> bool: