Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tbot/main.py: Allow Shell variables also in argumentfiles #96

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion tbot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import string
import sys
import os
import pathlib
import argparse
import inspect
import tbot.error
import typing
from typing import List

try:
from tbot import _version # type: ignore
Expand Down Expand Up @@ -57,11 +60,26 @@ def _import_hightlighter() -> typing.Callable[[str], str]:
# }}}


class TbotArgumentParser(argparse.ArgumentParser):
def convert_arg_line_to_args(self, arg_line: str) -> List[str]:
"""
Make it possible to use shell variables also in argumentsfiles.
"""
try:
arg_line_expanded = string.Template(arg_line).substitute(os.environ)
except KeyError as e:
raise tbot.error.TbotException(
f"Could not find environment variable: {e.args[0]!r}"
) from None

return [arg_line_expanded]


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
20 changes: 18 additions & 2 deletions tbot/newbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import itertools
import os
import pathlib
import string
import sys
import typing
from typing import Iterable, Optional, Sequence
from typing import Iterable, List, Optional, Sequence

if typing.TYPE_CHECKING:
import tbot
Expand Down Expand Up @@ -129,7 +130,7 @@ def get_version() -> str:


def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
parser = TbotArgumentParser(
prog="tbot",
description="Test and development automation tool, tailored for embedded"
+ " (experimental new CLI)",
Expand Down Expand Up @@ -186,6 +187,21 @@ def build_parser() -> argparse.ArgumentParser:
return parser


class TbotArgumentParser(argparse.ArgumentParser):
def convert_arg_line_to_args(self, arg_line: str) -> List[str]:
"""
Make it possible to use shell variables also in argumentsfiles.
"""
try:
arg_line_expanded = string.Template(arg_line).substitute(os.environ)
except KeyError as e:
raise tbot.error.TbotException(
f"Could not find environment variable: {e.args[0]!r}"
) from None

return [arg_line_expanded]


def main(argv: Optional[Sequence[str]] = None) -> None:
parser = build_parser()

Expand Down
Loading