Skip to content

Commit

Permalink
tbot/main.py: Allow Shell variables also in argumentfiles
Browse files Browse the repository at this point in the history
Make it possible to use shell environment vairiables
in argumentfiles.

Signed-off-by: Heiko Schocher <[email protected]>
  • Loading branch information
hsdenx committed Jun 21, 2023
1 parent 2462710 commit e1a11e0
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion tbot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pathlib
import argparse
import inspect
import re
import typing

try:
Expand Down Expand Up @@ -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="@",
Expand Down

0 comments on commit e1a11e0

Please sign in to comment.