forked from disler/claude-code-is-programmable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaude_code_is_programmable_4.py
More file actions
executable file
·94 lines (75 loc) · 3.06 KB
/
claude_code_is_programmable_4.py
File metadata and controls
executable file
·94 lines (75 loc) · 3.06 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env -S uv run --script
# Script demonstrating different output formats for Claude Code CLI responses with format selection options
"""
# Programmable Claude Code script that allows different output formats
Usage:
uv run claude_code_is_programmable_4.py
uv run claude_code_is_programmable_4.py --output-format text
uv run claude_code_is_programmable_4.py --output-format json
uv run claude_code_is_programmable_4.py --output-format stream-json
The script passes the selected output format directly to the Claude Code CLI using
the --output-format flag, which controls how Claude's responses are formatted.
"""
import subprocess
import json
import argparse
def output_text(content):
"""Output in plain text format"""
print(content)
def output_json(content):
"""Output in JSON format"""
try:
# If content is already JSON-formatted string, parse it first
try:
parsed = json.loads(content)
print(json.dumps(parsed, indent=2))
except json.JSONDecodeError:
# If not valid JSON, wrap as a simple message object
print(json.dumps({"message": content}, indent=2))
except Exception as e:
print(json.dumps({"error": str(e)}, indent=2))
def output_stream_json(content):
"""Output in streaming JSON format (one JSON object per line)"""
try:
# If content is already JSON-formatted string, parse it first
try:
parsed = json.loads(content)
print(json.dumps(parsed))
except json.JSONDecodeError:
# If not valid JSON, wrap as a simple message object
print(json.dumps({"message": content}))
except Exception as e:
print(json.dumps({"error": str(e)}))
def main():
parser = argparse.ArgumentParser(description="Claude Code runner script")
parser.add_argument(
"--output-format",
choices=["text", "json", "stream-json"],
default="text",
help="Specify output format",
)
args = parser.parse_args()
prompt = """
RUN the following commands:
1. Run git status
2. Add a brief comment about the changes at the top of claude_code_is_programmable_4.py
3. Summarize what you've done
"""
# Build command with appropriate output format flag
command = ["claude", "-p", prompt, "--allowedTools", "Edit", "Bash", "Write"]
# Add the output format flag if specified
if args.output_format == "json":
command.extend(["--output-format", "json"])
elif args.output_format == "stream-json":
command.extend(["--output-format", "stream-json"])
process = subprocess.run(command, capture_output=True, text=True, check=True)
output = process.stdout or "No output captured"
# Output the result directly since Claude CLI already formats it
# We'll only use our formatting functions for text mode or if needed for processing
if args.output_format == "text":
output_text(output)
else:
# For JSON modes, Claude CLI already returns formatted output
print(output)
if __name__ == "__main__":
main()