From e1a11e0f0822e5f267a4b27ed6f367f56671b58c Mon Sep 17 00:00:00 2001 From: Heiko Schocher Date: Tue, 4 Apr 2023 17:53:24 +0200 Subject: [PATCH] tbot/main.py: Allow Shell variables also in argumentfiles Make it possible to use shell environment vairiables in argumentfiles. Signed-off-by: Heiko Schocher --- tbot/main.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tbot/main.py b/tbot/main.py index 20f45d2f..9a30d783 100644 --- a/tbot/main.py +++ b/tbot/main.py @@ -19,6 +19,7 @@ import pathlib import argparse import inspect +import re import typing try: @@ -56,12 +57,36 @@ def _import_hightlighter() -> typing.Callable[[str], str]: # }}} +class TbotArgumentParser(argparse.ArgumentParser): + def convert_arg_line_to_args(self, arg_line): + """ + Make it possible to use shell variables also in argumentsfiles. + """ + if "$" in arg_line: + vl = re.findall(r'(\$\{.*?\})', arg_line) + # s = '012-3456-7890' + # print(re.findall(r'\d+', s)) + # show ['012', '3456', '7890'] + # so get ${...} working with more than one too... + + for v in vl: + varname = v[2:-1] + env = os.getenv(varname) + + if env == None: + raise RuntimeError(f"Could not find Environment variable {varname}") + + # replace in arg_line + rp = "${" + varname +"}" + arg_line = arg_line.replace(rp, env) + + return arg_line.split() def main() -> None: # noqa: C901 """Tbot main entry point.""" # ArgumentParser {{{ - parser = argparse.ArgumentParser( + parser = TbotArgumentParser( prog="tbot", description="Test and development automation tool, tailored for embedded", fromfile_prefix_chars="@",