forked from disler/claude-code-is-programmable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaude_testing_v1.py
More file actions
34 lines (30 loc) · 934 Bytes
/
claude_testing_v1.py
File metadata and controls
34 lines (30 loc) · 934 Bytes
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
import json
import subprocess
from typing import List, Optional
def run_claude(
prompt: str,
output_format: str = "json",
allowed_tools: Optional[List[str]] = None,
cli: str = "claude",
) -> str:
"""Run Claude Code in headless mode with the given output format."""
cmd = [cli, "-p", prompt, "--output-format", output_format]
if allowed_tools:
cmd.extend(["--allowedTools", *allowed_tools])
result = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
if result.returncode != 0:
raise RuntimeError(f"claude failed: {result.stderr}")
return result.stdout
def run_claude_json(
prompt: str,
allowed_tools: Optional[List[str]] = None,
cli: str = "claude",
) -> dict:
"""Run Claude and parse JSON output."""
output = run_claude(prompt, "json", allowed_tools, cli)
return json.loads(output)