From 95a0a29055837c783647536baca14af9ec346381 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 10 Apr 2024 08:32:49 -0500 Subject: [PATCH] Add -@ for reading system prompt from a file `-S @...` did not have good ergonomics for completion. --- src/chap/core.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/chap/core.py b/src/chap/core.py index 00c81d1..665d682 100644 --- a/src/chap/core.py +++ b/src/chap/core.py @@ -177,12 +177,23 @@ def colonstr(arg: str) -> tuple[str, str]: def set_system_message(ctx: click.Context, param: click.Parameter, value: str) -> None: - if value and value.startswith("@"): + if value is None: + return + if value.startswith("@"): with open(value[1:], "r", encoding="utf-8") as f: - value = f.read().rstrip() + value = f.read().strip() ctx.obj.system_message = value +def set_system_message_from_file( + ctx: click.Context, param: click.Parameter, value: click.File +) -> None: + if value is None: + return + content = value.read().strip() + ctx.obj.system_message = content + + def set_backend(ctx: click.Context, param: click.Parameter, value: str) -> None: if value == "list": formatter = ctx.make_formatter() @@ -369,6 +380,13 @@ main = MyCLI( help="Show the version and exit", callback=version_callback, ), + click.Option( + ("--system-message-file", "-@"), + type=click.File("r"), + default=None, + callback=set_system_message_from_file, + expose_value=False, + ), click.Option( ("--system-message", "-S"), type=str,