X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=config.py;h=1a362a381eeaac36b6b8fabafa2db548ee82b1f0;hb=e46158e49121b8a955bb07b73f5bcf9928b79c90;hp=0c4016675d7124c3f48e1a9def75794f9b86409d;hpb=aafec9ed642bcbd6fa8ba14656111eb98ebf0b29;p=python_utils.git diff --git a/config.py b/config.py index 0c40166..1a362a3 100644 --- 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. @@ -303,7 +312,6 @@ class Config: @staticmethod def _parse_arg_into_env(arg: str) -> Optional[Tuple[str, str, List[str]]]: """Internal helper to parse commandling args into environment vars.""" - arg = arg.strip() if not arg.startswith('['): return None @@ -314,7 +322,7 @@ class Config: chunks = arg.split() if len(chunks) > 1: - var = arg.split()[0] + var = chunks[0] else: var = arg @@ -323,7 +331,7 @@ class Config: env = var.upper() while env[0] == '-': env = env[1:] - return arg, env, chunks + return var, env, chunks def _augment_sys_argv_from_environment_variables(self): """Internal. Look at the system environment for variables that match @@ -364,9 +372,9 @@ class Config: from string_utils import to_bool if len(chunks) == 1 and to_bool(value): - sys.argv.append(arg) + sys.argv.append(var) elif len(chunks) > 1: - sys.argv.append(arg) + sys.argv.append(var) sys.argv.append(value) arg = '' @@ -572,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: