From afe22d0fddde9b4101bd699241b46887b2b3394b Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 10 Apr 2024 08:33:55 -0500 Subject: [PATCH] chap ask: add --stdin flag --- src/chap/commands/ask.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/chap/commands/ask.py b/src/chap/commands/ask.py index 0cbf650..e79ee3a 100644 --- a/src/chap/commands/ask.py +++ b/src/chap/commands/ask.py @@ -100,8 +100,9 @@ async def work() -> None: @command_uses_new_session @click.option("--print-prompt/--no-print-prompt", default=True) -@click.argument("prompt", nargs=-1, required=True) -def main(obj: Obj, prompt: str, print_prompt: bool) -> None: +@click.option("--stdin/--no-stdin", "use_stdin", default=False) +@click.argument("prompt", nargs=-1) +def main(obj: Obj, prompt: list[str], use_stdin: bool, print_prompt: bool) -> None: """Ask a question (command-line argument is passed as prompt)""" session = obj.session assert session is not None @@ -112,9 +113,16 @@ def main(obj: Obj, prompt: str, print_prompt: bool) -> None: api = obj.api assert api is not None + if use_stdin: + if prompt == use_stdin: + raise click.UsageError("Can't use 'prompt' together with --stdin") + + joined_prompt = sys.stdin.read() + else: + joined_prompt = " ".join(prompt) # symlink_session_filename(session_filename) - response = verbose_ask(api, session, " ".join(prompt), print_prompt=print_prompt) + response = verbose_ask(api, session, joined_prompt, print_prompt=print_prompt) print(f"Saving session to {session_filename}", file=sys.stderr) if response is not None: