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
49
50
51
52
53
54
55
56
57
58
59
60
|
import argparse
import asyncio
import sys
from .client import GeminiClient
from .models import list_models
from .types import GeminiOptions
def parse_args():
p = argparse.ArgumentParser(prog="gemini", description="Query Gemini via CLI")
p.add_argument("prompt", nargs="?", help="Prompt to send (reads stdin if omitted)")
p.add_argument("-m", "--model", default="gemini-2.5-pro", help="Model name")
p.add_argument("-c", "--credentials", default=None, help="Path to oauth_creds.json")
p.add_argument("--no-stream", action="store_true", help="Non-streaming mode")
p.add_argument(
"--thinking",
type=int,
default=None,
metavar="BUDGET",
help="Enable thinking mode with given token budget",
)
p.add_argument("--list-models", action="store_true", help="List available models and exit")
return p.parse_args()
async def run(args):
prompt = args.prompt or sys.stdin.read().strip()
if not prompt:
print("Error: no prompt provided", file=sys.stderr)
sys.exit(1)
opts = GeminiOptions(
model=args.model,
thinking_budget=args.thinking,
)
async with GeminiClient(options=opts, credentials_path=args.credentials) as client:
if args.no_stream:
response = await client.send_message(prompt)
print(response.text)
else:
async for chunk in client.send_message_stream(prompt):
if chunk.text_delta:
print(chunk.text_delta, end="", flush=True)
print()
def main():
args = parse_args()
if args.list_models:
for m in list_models():
tag = " [default]" if m.is_default else " [preview]" if m.is_preview else ""
print(f"{m.name}{tag}")
return
asyncio.run(run(args))
if __name__ == "__main__":
main()
|