Skip to content

Commit

Permalink
adds ENV_OPTION_PREFIX to use for forming defaults for args if none; …
Browse files Browse the repository at this point in the history
…todo note; ups argparser object trace level
  • Loading branch information
Tom O'Hara committed Oct 8, 2023
1 parent ea423be commit 692eb35
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion mezcla/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@
"Add negation option for each boolean option")
SHORT_OPTIONS = system.getenv_bool("SHORT_OPTIONS", False,
"Automatically derive short options")
ENV_OPTION_PREFIX = system.getenv_value("ENV_OPTION_PREFIX", None,
"Environment variable prefix to check for default")
## TEST
## TEMP_BASE = system.getenv_value("TEMP_BASE", None,
## "Override for temporary file basename")
Expand Down Expand Up @@ -262,6 +264,7 @@ def __init__(self, runtime_args=None, description=None, skip_args=False,
if system.is_regular_file(self.temp_base):
gh.delete_file(self.temp_base)
gh.run("mkdir -p {dir}", dir=self.temp_base)
## TODO3: main-temp.txt???
default_temp_file = gh.form_path(self.temp_base, "temp.txt")
else:
default_temp_file = self.temp_base
Expand Down Expand Up @@ -437,6 +440,11 @@ def get_parsed_option(self, label, default=None, positional=False):
value = self.parsed_args.get(opt_label)
# Override null value with default
if value is None:
if ((default is None) and ENV_OPTION_PREFIX):
env_var = f"{ENV_OPTION_PREFIX}_{opt_label}".upper()
default = system.getenv(env_var)
if default:
debug.trace(4, f"FYI: Using option {label} from env ({opt_label}: {default!r})")
value = default
under_label = label.replace("-", "_")
# Do sanity check for positional argument being checked by mistake
Expand Down Expand Up @@ -599,7 +607,7 @@ def add_argument(opt_label, add_short=None, **kwargs): # pylint: disable=redef

# Parse the command line and get result
tpo.debug_format("parser={p}", 6, p=parser)
debug.trace_object(7, parser, max_depth=2)
debug.trace_object(8, parser, max_depth=2)
self.parser = parser
# note: not trapped to allow for early exit
self.parsed_args = vars(parser.parse_args(runtime_args))
Expand Down

0 comments on commit 692eb35

Please sign in to comment.