Skip to content

Commit 18588b8

Browse files
committed
Print help when command is empty and no input redirect
1 parent d654c95 commit 18588b8

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

llm/cli.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ def prompt(
143143
144144
Documentation: https://llm.datasette.io/en/stable/usage.html
145145
"""
146+
# If no prompt is provided, and we are in a TTY, show the help
147+
if sys.stdin.isatty() and not prompt:
148+
click.echo(click.Context(cli).get_help())
149+
return
150+
146151
if log and no_log:
147152
raise click.ClickException("--log and --no-log are mutually exclusive")
148153

@@ -153,18 +158,14 @@ def read_prompt():
153158

154159
# Is there extra prompt available on stdin?
155160
stdin_prompt = None
156-
if not sys.stdin.isatty():
157-
stdin_prompt = sys.stdin.read()
161+
stdin_prompt = sys.stdin.read()
158162

159163
if stdin_prompt:
160164
bits = [stdin_prompt]
161165
if prompt:
162166
bits.append(prompt)
163167
prompt = " ".join(bits)
164168

165-
if prompt is None and not save and sys.stdin.isatty():
166-
# Hang waiting for input to stdin (unless --save)
167-
prompt = sys.stdin.read()
168169
return prompt
169170

170171
if save:

tests/test_llm.py

+13
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,16 @@ def test_model_defaults(tmpdir, monkeypatch):
570570
assert config_path.exists()
571571
assert llm.get_default_model() == "gpt-4o"
572572
assert llm.get_model().model_id == "gpt-4o"
573+
574+
575+
def test_interactive_llm_empty_prompt():
576+
runner = CliRunner()
577+
# use a pty for stdin
578+
master, _ = os.openpty()
579+
input = os.fdopen(master, "r")
580+
args = ["--no-stream"]
581+
result = runner.invoke(cli, args, input=input, catch_exceptions=False)
582+
assert result.exit_code == 0
583+
# ensure the result is the help message
584+
assert result.output.startswith("Usage:")
585+
input.close()

0 commit comments

Comments
 (0)