Task: Convert Project Rules to Claude Code Hooks
Attribution: This command is based on the excellent work by zxdxjtu from the claudecode-rule2hook repository. Licensed under MIT License. Integrated into Claude Command Suite with permission to enhance developer workflows.
You are an expert at converting natural language project rules into Claude Code hook configurations. Your task is to analyze the given rules and generate appropriate hook configurations following the official Claude Code hooks specification.
Instructions
- If rules are provided as arguments, analyze those rules
- If no arguments are provided, read and analyze the CLAUDE.md file from these locations:
./CLAUDE.md
(project memory)./CLAUDE.local.md
(local project memory)-
~/.claude/CLAUDE.md
(user memory) -
For each rule, determine:
- The appropriate hook event (PreToolUse, PostToolUse, Stop, Notification)
- The tool matcher pattern (exact tool names or regex)
-
The command to execute
-
Generate the complete hook configuration following the exact JSON structure
- Save it to
~/.claude/hooks.json
(merge with existing hooks if present) - Provide a summary of what was configured
Hook Events
PreToolUse
- When: Runs BEFORE a tool is executed
- Common Keywords: "before", "check", "validate", "prevent", "scan", "verify"
- Available Tool Matchers:
Task
- Before launching agent tasksBash
- Before running shell commandsGlob
- Before file pattern matchingGrep
- Before content searchingRead
- Before reading filesEdit
- Before editing single filesMultiEdit
- Before batch editing filesWrite
- Before writing/creating filesWebFetch
- Before fetching web contentWebSearch
- Before web searchingTodoRead
- Before reading todo listTodoWrite
- Before updating todo list- Special Feature: Can block tool execution if command returns non-zero exit code
PostToolUse
- When: Runs AFTER a tool completes successfully
- Common Keywords: "after", "following", "once done", "when finished"
- Available Tool Matchers: Same as PreToolUse
- Common Uses: Formatting, linting, building, testing after file changes
Stop
- When: Runs when Claude Code finishes responding
- Common Keywords: "finish", "complete", "end task", "done", "wrap up"
- No matcher needed: Applies to all completions
- Common Uses: Final status checks, summaries, cleanup
Notification
- When: Runs when Claude Code sends notifications
- Common Keywords: "notify", "alert", "inform", "message"
- Special: Rarely used for rule conversion
Hook Configuration Structure
{
"hooks": {
"EventName": [
{
"matcher": "ToolName|AnotherTool|Pattern.*",
"hooks": [
{
"type": "command",
"command": "your-command-here"
}
]
}
]
}
}
Matcher Patterns
- Exact match:
"Edit"
- matches only Edit tool - Multiple tools:
"Edit|MultiEdit|Write"
- matches any of these - Regex patterns:
".*Edit"
- matches Edit and MultiEdit - All tools: Omit matcher field entirely
Examples with Analysis
Example 1: Python Formatting
Rule: "Format Python files with black after editing" Analysis: - Keyword "after" → PostToolUse - "editing" → Edit|MultiEdit|Write tools - "Python files" → command should target .py files
{
"hooks": {
"PostToolUse": [{
"matcher": "Edit|MultiEdit|Write",
"hooks": [{
"type": "command",
"command": "black . --quiet 2>/dev/null || true"
}]
}]
}
}
Example 2: Git Status Check
Rule: "Run git status when finishing a task" Analysis: - "finishing" → Stop event - No specific tool mentioned → no matcher needed
{
"hooks": {
"Stop": [{
"hooks": [{
"type": "command",
"command": "git status"
}]
}]
}
}
Example 3: Security Scan
Rule: "Check for hardcoded secrets before saving any file" Analysis: - "before" → PreToolUse - "saving any file" → Write|Edit|MultiEdit
{
"hooks": {
"PreToolUse": [{
"matcher": "Write|Edit|MultiEdit",
"hooks": [{
"type": "command",
"command": "git secrets --scan 2>/dev/null || echo 'No secrets found'"
}]
}]
}
}
Example 4: Test Runner
Rule: "Run npm test after modifying files in tests/ directory" Analysis: - "after modifying" → PostToolUse - "files" → Edit|MultiEdit|Write - Note: Path filtering happens in the command, not the matcher
{
"hooks": {
"PostToolUse": [{
"matcher": "Edit|MultiEdit|Write",
"hooks": [{
"type": "command",
"command": "npm test 2>/dev/null || echo 'Tests need attention'"
}]
}]
}
}
Example 5: Command Logging
Rule: "Log all bash commands before execution" Analysis: - "before execution" → PreToolUse - "bash commands" → Bash tool specifically
{
"hooks": {
"PreToolUse": [{
"matcher": "Bash",
"hooks": [{
"type": "command",
"command": "echo \"[$(date)] Executing bash command\" >> ~/.claude/command.log"
}]
}]
}
}
Best Practices for Command Generation
- Error Handling: Add
|| true
or2>/dev/null
to prevent hook failures from blocking Claude - Quiet Mode: Use quiet flags (--quiet, -q) when available
- Path Safety: Use relative paths or check existence
- Performance: Keep commands fast to avoid slowing down Claude
- Logging: Redirect verbose output to avoid cluttering Claude's interface
Common Rule Patterns
- "Format [language] files after editing" → PostToolUse + Edit|MultiEdit|Write
- "Run [command] before committing" → PreToolUse + Bash (when git commit detected)
- "Check for [pattern] before saving" → PreToolUse + Write|Edit|MultiEdit
- "Execute [command] when done" → Stop event
- "Validate [something] before running commands" → PreToolUse + Bash
- "Clear cache after modifying config" → PostToolUse + Edit|MultiEdit|Write
- "Notify when [condition]" → Usually PostToolUse with specific matcher
Important Notes
- Always merge with existing hooks - don't overwrite
- Test commands work before adding to hooks
- Consider performance impact of hooks
- Use specific matchers when possible to avoid unnecessary executions
- Commands run with full user permissions - be careful with destructive operations
User Input
$ARGUMENTS