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="@",