"""Global configuration driven by commandline arguments, environment variables
and saved configuration files. This works across several modules.
"""Global configuration driven by commandline arguments, environment variables
and saved configuration files. This works across several modules.
# taking any unnecessary dependencies back on them.
# Defer logging messages until later when logging has been initialized.
# taking any unnecessary dependencies back on them.
# Defer logging messages until later when logging has been initialized.
description=None,
formatter_class=OptionalRawFormatter,
fromfile_prefix_chars="@",
description=None,
formatter_class=OptionalRawFormatter,
fromfile_prefix_chars="@",
- epilog=f'{program_name} uses config.py ({__file__}) for global, cross-module configuration setup and parsing.',
+ epilog=f'{PROGRAM_NAME} uses config.py ({__file__}) for global, cross-module configuration setup and parsing.',
# This is the data that is most interesting to our callers; it will hold
# the configuration result.
config: Dict[str, Any] = {}
# This is the data that is most interesting to our callers; it will hold
# the configuration result.
config: Dict[str, Any] = {}
# It would be really nice if this shit worked from interactive python
def add_commandline_args(title: str, description: str = ""):
"""Create a new context for arguments and return a handle."""
# It would be really nice if this shit worked from interactive python
def add_commandline_args(title: str, description: str = ""):
"""Create a new context for arguments and return a handle."""
- for group in args._action_groups:
- if entry_module is not None and entry_module in group.title: # type: ignore
- reordered_action_groups.append(group)
- elif program_name in group.title: # type: ignore
- reordered_action_groups.append(group)
+ for grp in ARGS._action_groups:
+ if entry_module is not None and entry_module in grp.title: # type: ignore
+ reordered_action_groups.append(grp)
+ elif PROGRAM_NAME in group.title: # type: ignore
+ reordered_action_groups.append(grp)
else:
msg = f'Reading commandline arguments from {loadfile}.'
print(msg, file=sys.stderr)
else:
msg = f'Reading commandline arguments from {loadfile}.'
print(msg, file=sys.stderr)
# If we're about to do the usage message dump, put the main
# module's argument group last in the list (if possible) so that
# when the user passes -h or --help, it will be visible on the
# screen w/o scrolling.
for arg in sys.argv:
# If we're about to do the usage message dump, put the main
# module's argument group last in the list (if possible) so that
# when the user passes -h or --help, it will be visible on the
# screen w/o scrolling.
for arg in sys.argv:
- if arg == '--help' or arg == '-h':
- args._action_groups = reorder_arg_action_groups(entry_module)
+ if arg in ('--help', '-h'):
+ if entry_module is not None:
+ entry_module = os.path.basename(entry_module)
+ ARGS._action_groups = reorder_arg_action_groups_before_help(entry_module)
# Examine the environment for variables that match known flags.
# For a flag called --example_flag the corresponding environment
# Examine the environment for variables that match known flags.
# For a flag called --example_flag the corresponding environment
# Parse (possibly augmented, possibly completely overwritten)
# commandline args with argparse normally and populate config.
# Parse (possibly augmented, possibly completely overwritten)
# commandline args with argparse normally and populate config.
sys.argv = sys.argv[:1] + unknown
# Check for savefile and populate it if requested.
savefile = config['config_savefile']
if savefile and len(savefile) > 0:
with open(savefile, 'w') as wf:
sys.argv = sys.argv[:1] + unknown
# Check for savefile and populate it if requested.
savefile = config['config_savefile']
if savefile and len(savefile) > 0:
with open(savefile, 'w') as wf:
def late_logging():
"""Log messages saved earlier now that logging has been initialized."""
logger = logging.getLogger(__name__)
def late_logging():
"""Log messages saved earlier now that logging has been initialized."""
logger = logging.getLogger(__name__)