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
| @@ -18,7 +18,7 @@ | ||
| 18 | 18 | "hooks": [ |
| 19 | 19 | { |
| 20 | 20 | "type": "command", |
| 21 | - "command": "python -m ./bash_command_validator_example" | |
| 21 | + "command": "python3 /path/to/claude-code/examples/hooks/bash_command_validator_example.py" | |
| 22 | 22 | } |
| 23 | 23 | ] |
| 24 | 24 | } |
| @@ -33,21 +33,21 @@ | ||
| 33 | 33 | import sys |
| 34 | 34 | |
| 35 | 35 | # Define validation rules as a list of (regex pattern, message) tuples |
| 36 | -VALIDATION_RULES = [ | |
| 36 | +_VALIDATION_RULES = [ | |
| 37 | 37 | ( |
| 38 | - r"\bgrep\b(?!.*\|)", | |
| 38 | + r"^grep\b(?!.*\|)", | |
| 39 | 39 | "Use 'rg' (ripgrep) instead of 'grep' for better performance and features", |
| 40 | 40 | ), |
| 41 | 41 | ( |
| 42 | - r"\bfind\s+\S+\s+-name\b", | |
| 42 | + r"^find\s+\S+\s+-name\b", | |
| 43 | 43 | "Use 'rg --files | rg pattern' or 'rg --files -g pattern' instead of 'find -name' for better performance", |
| 44 | 44 | ), |
| 45 | 45 | ] |
| 46 | 46 | |
| 47 | 47 | |
| 48 | -def validate_command(command: str) -> list[str]: | |
| 48 | +def _validate_command(command: str) -> list[str]: | |
| 49 | 49 | issues = [] |
| 50 | - for pattern, message in VALIDATION_RULES: | |
| 50 | + for pattern, message in _VALIDATION_RULES: | |
| 51 | 51 | if re.search(pattern, command): |
| 52 | 52 | issues.append(message) |
| 53 | 53 | return issues |
| @@ -58,6 +58,7 @@ | ||
| 58 | 58 | input_data = json.load(sys.stdin) |
| 59 | 59 | except json.JSONDecodeError as e: |
| 60 | 60 | print(f"Error: Invalid JSON input: {e}", file=sys.stderr) |
| 61 | + # Exit code 1 shows stderr to the user but not to Claude | |
| 61 | 62 | sys.exit(1) |
| 62 | 63 | |
| 63 | 64 | tool_name = input_data.get("tool_name", "") |
| @@ -70,9 +71,7 @@ | ||
| 70 | 71 | if not command: |
| 71 | 72 | sys.exit(0) |
| 72 | 73 | |
| 73 | - # Validate the command | |
| 74 | - issues = validate_command(command) | |
| 75 | - | |
| 74 | + issues = _validate_command(command) | |
| 76 | 75 | if issues: |
| 77 | 76 | for message in issues: |
| 78 | 77 | print(f"• {message}", file=sys.stderr) |