Advanced Hook Use Cases
Hookby davila7 · Added 5mo ago
Claude
Install
npx claude-code-templates@latest --hook=cli-tool/components/skills/development/hook-development/references --yesAdd to Claude
claude mcp add advanced-hook-use-casesAbout
Advanced Hook Use Cases
This reference covers advanced hook patterns and techniques for sophisticated automation workflows.
Multi-Stage Validation
Combine command and prompt hooks for layered validation:
{
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "bash ${CLAUDE_PLUGIN_ROOT}/scripts/quick-check.sh",
"timeout": 5
},
{
"type": "prompt",
"prompt": "Deep analysis of bash command: $TOOL_INPUT",
"timeout": 15
}
]
}
]
}
Use case: Fast deterministic checks followed by intelligent analysis
Example quick-check.sh:
#!/bin/bash
input=$(cat)
command=$(echo "$input" | jq -r '.tool_input.command')
# Immediate approval for safe commands
if [[ "$command" =~ ^(ls|pwd|echo|date|whoami)$ ]]; then
exit 0
fi
# Let prompt hook handle complex cases
exit 0
The command hook quickly approves obviously safe commands, while the prompt hook analyzes everything else.
Conditional Hook Execution
Execute hooks based on environment or context:
#!/bin/bash
# Only run in CI environment
if [ -z "$CI" ]; then
echo '{"continue": true}' # Skip in non-CI
exit 0
fi
# Run validation logic in CI
input=$(cat)
# ... validation code ...
Use cases:
- Different behavior in CI vs local development
- Project-specific validation
- User-specific rules
Example: Skip certain checks for trusted users:
#!/bin/bash
# Skip detailed checks for admin users
if [ "$USER" = "admin" ]; then
exit 0
fi
# Full validation for other users
input=$(cat)
# ... validation code ...
Hook Chaining via State
Share state between hooks using temporary files:
# Hook 1: Analyze and save state
#!/bin/bash
input=$(cat)
command=$(echo "$input" | jq -r '.tool_input.command')
# Analyze command
risk_level=$(calculate_risk "$command")
echo "$risk_level" > /tmp/hook-state-$$
exit 0
`
Tags
PreCommitaitmplclaude-code-templates