spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

Script polishBrowse files
Dickson Tsai authored on Jul 2, 2025 parent 3d2166e · commit 74ba615503bb
1 changed file+8 -9compared with 3d2166e
Mexamples/hooks/bash_command_validator_example.py
@@ -18,7 +18,7 @@
1818 "hooks": [
1919 {
2020 "type": "command",
21- "command": "python -m ./bash_command_validator_example"
21+ "command": "python3 /path/to/claude-code/examples/hooks/bash_command_validator_example.py"
2222 }
2323 ]
2424 }
@@ -33,21 +33,21 @@
3333 import sys
3434
3535 # Define validation rules as a list of (regex pattern, message) tuples
36-VALIDATION_RULES = [
36+_VALIDATION_RULES = [
3737 (
38- r"\bgrep\b(?!.*\|)",
38+ r"^grep\b(?!.*\|)",
3939 "Use 'rg' (ripgrep) instead of 'grep' for better performance and features",
4040 ),
4141 (
42- r"\bfind\s+\S+\s+-name\b",
42+ r"^find\s+\S+\s+-name\b",
4343 "Use 'rg --files | rg pattern' or 'rg --files -g pattern' instead of 'find -name' for better performance",
4444 ),
4545 ]
4646
4747
48-def validate_command(command: str) -> list[str]:
48+def _validate_command(command: str) -> list[str]:
4949 issues = []
50- for pattern, message in VALIDATION_RULES:
50+ for pattern, message in _VALIDATION_RULES:
5151 if re.search(pattern, command):
5252 issues.append(message)
5353 return issues
@@ -58,6 +58,7 @@
5858 input_data = json.load(sys.stdin)
5959 except json.JSONDecodeError as e:
6060 print(f"Error: Invalid JSON input: {e}", file=sys.stderr)
61+ # Exit code 1 shows stderr to the user but not to Claude
6162 sys.exit(1)
6263
6364 tool_name = input_data.get("tool_name", "")
@@ -70,9 +71,7 @@
7071 if not command:
7172 sys.exit(0)
7273
73- # Validate the command
74- issues = validate_command(command)
75-
74+ issues = _validate_command(command)
7675 if issues:
7776 for message in issues:
7877 print(f"• {message}", file=sys.stderr)