aboutsummaryrefslogtreecommitdiffstats
path: root/src/claude/cli.py
blob: 47d3d0d96715bbdec2d7d81916572be2387198bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from __future__ import annotations

import argparse
import asyncio
import sys

from .chat import ChatClient


def parse_args() -> argparse.Namespace:
    p = argparse.ArgumentParser(prog="claude-py", description="Chat with Claude from the terminal")
    p.add_argument("prompt", nargs="*", help="Prompt text (reads stdin if omitted)")
    p.add_argument("-m", "--model", default="claude-sonnet-4-6")
    p.add_argument("-s", "--system", default=None, help="System prompt")
    p.add_argument("--max-tokens", type=int, default=8192)
    p.add_argument("--no-stream", action="store_true", help="Disable streaming")
    p.add_argument("--list-models", action="store_true", help="List available models and exit")
    return p.parse_args()


async def run(args: argparse.Namespace) -> None:
    if args.list_models:
        for model_id, info in ChatClient.list_models().items():
            print(f"  {model_id:<35} {info['display']}")
        return

    if args.prompt:
        prompt = " ".join(args.prompt)
    elif not sys.stdin.isatty():
        prompt = sys.stdin.read().strip()
    else:
        print("error: no prompt provided (pass as argument or pipe to stdin)", file=sys.stderr)
        sys.exit(1)

    async with ChatClient(model=args.model, max_tokens=args.max_tokens, system=args.system) as c:
        if args.no_stream:
            resp = await c.chat(prompt)
            print(resp.text)
        else:
            async for chunk in c.stream(prompt):
                if chunk.text_delta:
                    sys.stdout.write(chunk.text_delta)
                    sys.stdout.flush()
            print()


def main() -> None:
    asyncio.run(run(parse_args()))