3 """Global configuration driven by commandline arguments (even across
4 different modules). Usage:
10 parser = config.add_commandline_args(
12 "Args related to module doing the thing.",
15 "--module_do_the_thing",
18 help="Should the module do the thing?"
26 parser = config.add_commandline_args(
28 "A program that does the thing.",
34 help="Should we really do the thing?"
36 config.parse() # Very important, this must be invoked!
38 If you set this up and remember to invoke config.parse(), all commandline
39 arguments will play nicely together:
43 [--module_do_the_thing MODULE_DO_THE_THING]
47 Args related to module doing the thing.
49 --module_do_the_thing MODULE_DO_THE_THING
50 Should the module do the thing?
53 A program that does the thing
56 Should we really do the thing?
58 Arguments themselves should be accessed via config.config['arg_name']. e.g.
60 if not config.config['dry_run']:
68 from typing import Dict, Any
70 # Note: at this point in time, logging hasn't been configured and
71 # anything we log will come out the root logger.
74 class LoadFromFile(argparse.Action):
75 """Helper to load a config file into argparse."""
76 def __call__ (self, parser, namespace, values, option_string = None):
80 for line in buf.split(','):
82 line = line.strip('{')
83 line = line.strip('}')
84 m = re.match(r"^'([a-zA-Z_\-]+)'\s*:\s*(.*)$", line)
88 value = value.strip("'")
89 if value not in ('None', 'True', 'False'):
90 argv.append(f'--{key}')
92 parser.parse_args(argv, namespace)
95 # A global parser that we will collect arguments into.
96 args = argparse.ArgumentParser(
97 description=f"This program uses config.py ({__file__}) for global, cross-module configuration.",
98 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
99 fromfile_prefix_chars="@"
101 config_parse_called = False
103 # A global configuration dictionary that will contain parsed arguments
104 # It is also this variable that modules use to access parsed arguments
105 config: Dict[str, Any] = {}
108 def add_commandline_args(title: str, description: str = ""):
109 """Create a new context for arguments and return a handle."""
110 return args.add_argument_group(title, description)
113 group = add_commandline_args(
114 f'Global Config ({__file__})',
115 'Args that control the global config itself; how meta!',
123 help='Config file from which to read args in lieu or in addition to commandline.',
129 help='Display the global configuration on STDERR at program startup.',
136 help='Populate config file compatible --config_loadfile to save config for later use.',
140 def parse() -> Dict[str, Any]:
141 """Main program should call this early in main()"""
142 global config_parse_called
143 config_parse_called = True
144 config.update(vars(args.parse_args()))
146 if config['config_savefile']:
147 with open(config['config_savefile'], 'w') as wf:
148 wf.write("\n".join(sys.argv[1:]))
150 if config['config_dump']:
156 def has_been_parsed() -> bool:
157 global config_parse_called
158 return config_parse_called
162 """Print the current config to stdout."""
163 print("Global Configuration:", file=sys.stderr)
164 pprint.pprint(config, stream=sys.stderr)