Creating Custom Commands
Creating Custom Commands
Section titled “Creating Custom Commands”Custom commands let you automate project-specific workflows with simple slash commands. This guide shows you how to create commands that integrate seamlessly with Claude Kit.
Command File Structure
Section titled “Command File Structure”Commands are markdown files in .claude/commands/:
.claude/commands/├── your-command.md # /your-command├── deploy-prod.md # /deploy-prod└── weekly-report.md # /weekly-reportThe filename (without .md) becomes the command name.
Command Template
Section titled “Command Template”Here’s the complete template for a command file:
# /command-name - Short Description
## Purpose
Brief description of what this command does and why it exists.
## Usage
\`\`\`/command-name [arguments]\`\`\`
## Arguments
- `$ARGUMENTS`: Description of what arguments this command accepts
---
Execute workflow for: **$ARGUMENTS**
## Workflow
### Phase 1: First Step
Description of what happens in this phase.
1. **Task 1** - Details about task 1 - What to check or do
2. **Task 2** - Details about task 2 - Expected outcomes
### Phase 2: Second Step
Description of next phase.
1. **Task 1** - Details - Actions
## Output
Describe what the command should produce:
\`\`\`markdown## [Command Name] Complete
### Summary[What was done]
### Results- Item 1- Item 2
### Next Steps1. Next action2. Another action\`\`\`
## Example
**Input**: `/command-name example input`
**Output**: Description of what happens with this example
## Flags
| Flag | Description | Example ||------|-------------|---------|| `--flag-name` | What this flag does | `--flag-name=value` |
<!-- CUSTOMIZATION POINT -->## Variations
Optional section for project-specific variations.Command Anatomy
Section titled “Command Anatomy”1. Header Section
Section titled “1. Header Section”# /deploy - Deploy Application
## Purpose
Deploy the application to specified environment with pre-deployment checks,database migrations, and post-deployment verification.The header includes:
- Title: Command name and one-line description
- Purpose: Detailed explanation of what and why
2. Usage Section
Section titled “2. Usage Section”## Usage
\`\`\`/deploy [environment] [--skip-tests]\`\`\`
## Arguments
- `environment`: Target environment (staging, production)- `--skip-tests`: Skip running tests before deploymentDefine:
- How to invoke the command
- What arguments it accepts
- Optional flags
3. Separator Line
Section titled “3. Separator Line”---
Deploy to **$ARGUMENTS** environment:The --- separator is important:
- Everything above is documentation
- Everything below is the instruction Claude executes
- Use
$ARGUMENTSto inject user input
4. Workflow Section
Section titled “4. Workflow Section”## Workflow
### Phase 1: Pre-deployment Checks
1. **Verify Environment** - Check environment exists - Verify credentials - Confirm deployment is allowed
2. **Run Tests** - Execute full test suite - Check code coverage - Fail if tests don't passBreak down the command into:
- Logical phases
- Numbered steps within phases
- Clear success criteria
5. Output Section
Section titled “5. Output Section”## Output
\`\`\`markdown## Deployment Complete
### EnvironmentProduction
### Changes Deployed- API v2.3.1- Database migration: add_user_preferences- Updated 15 files
### Verification- Health check: ✓ Passed- Smoke tests: ✓ Passed
### Rollback Command/rollback production v2.3.0\`\`\`Show what the successful output should look like.
6. Examples Section
Section titled “6. Examples Section”## Example
**Input**: `/deploy production`
**Output**:1. Runs test suite (2 min)2. Builds production bundle (1 min)3. Deploys to production servers4. Runs smoke tests5. Reports deployment statusProvide concrete examples of usage.
7. Flags Section
Section titled “7. Flags Section”## Flags
| Flag | Description | Example ||------|-------------|---------|| `--skip-tests` | Skip pre-deployment tests | `/deploy staging --skip-tests` || `--force` | Force deployment without confirmation | `/deploy prod --force` || `--rollback` | Rollback to previous version | `/deploy prod --rollback` |Document all available flags.
Complete Example: Custom Deploy Command
Section titled “Complete Example: Custom Deploy Command”Here’s a real-world example:
# /deploy - Deploy Application
## Purpose
Deploy the application to specified environment with automated checks,database migrations, and verification steps.
## Usage
\`\`\`/deploy [environment]\`\`\`
## Arguments
- `environment`: Target environment (staging, production)
---
Deploy to **$ARGUMENTS** environment:
## Workflow
### Phase 1: Pre-deployment Checks
1. **Verify Environment Configuration** - Check `.env.[environment]` file exists - Verify required secrets are set - Confirm environment is accessible
2. **Run Test Suite** ```bash pnpm test pnpm test:e2e- All tests must pass
- Coverage must be >= 80%
- Check for Breaking Changes
- Review CHANGELOG.md
- Identify migration requirements
- Confirm database backup exists
Phase 2: Build
Section titled “Phase 2: Build”-
Build Production Bundle
Terminal window pnpm build- Verify build completes without errors
- Check bundle size
- Validate output directory
-
Run Database Migrations
Terminal window pnpm migrate:$ARGUMENTS- Apply pending migrations
- Verify migration success
- Create rollback script
Phase 3: Deploy
Section titled “Phase 3: Deploy”-
Deploy to Environment
Terminal window pnpm deploy:$ARGUMENTS- Upload built assets
- Update environment variables
- Restart services
-
Health Checks
- Wait for services to start (30s)
- Verify API health endpoint
- Check database connectivity
Phase 4: Verification
Section titled “Phase 4: Verification”-
Run Smoke Tests
Terminal window pnpm smoke:$ARGUMENTS- Test critical user paths
- Verify external integrations
- Check error rates
-
Monitor Deployment
- Check application logs for errors
- Monitor performance metrics
- Verify no spike in error rate
Phase 5: Documentation
Section titled “Phase 5: Documentation”-
Update Deployment Log
- Record deployment time
- Note version deployed
- List any manual steps taken
-
Notify Team
- Post to Slack #deployments channel
- Include deployment summary
- Provide rollback instructions
Output
Section titled “Output”```markdown
Deployment Complete: $ARGUMENTS
Section titled “Deployment Complete: $ARGUMENTS”Version
Section titled “Version”v2.3.1 (commit: abc1234)
Deployment Time
Section titled “Deployment Time”2024-01-15 14:30 UTC (Duration: 4m 32s)
Changes Deployed
Section titled “Changes Deployed”- Feature: User profile redesign
- Fix: Payment processing timeout
- Database: Migration #045 (add_user_preferences)
Verification Results
Section titled “Verification Results”- Health Check: ✓ Passed
- Smoke Tests: ✓ All passed (12/12)
- Error Rate: 0.01% (normal)
Rollback Command
Section titled “Rollback Command”If issues arise, rollback with: `/rollback $ARGUMENTS v2.3.0`
Monitoring
Section titled “Monitoring”- Dashboard: https://monitoring.example.com
- Logs: https://logs.example.com ```
Example
Section titled “Example”Input: /deploy production
Output:
- Runs full test suite (3 min)
- Builds production bundle (2 min)
- Applies database migrations (30s)
- Deploys to production cluster (1 min)
- Runs smoke tests (1 min)
- Posts deployment notification to Slack
- Provides rollback command
| Flag | Description | Example |
|---|---|---|
--skip-tests | Skip test suite (not recommended for production) | --skip-tests |
--skip-migrations | Skip database migrations | --skip-migrations |
--dry-run | Simulate deployment without executing | --dry-run |
--force | Skip confirmation prompts | --force |
Flag Usage
Section titled “Flag Usage”/deploy staging --skip-tests # Deploy to staging without tests/deploy production --dry-run # Simulate production deployment/deploy production --skip-migrations # Deploy without DB changesProject-Specific Variations
Section titled “Project-Specific Variations”Modify the workflow based on your deployment strategy:
- Add CDN cache invalidation steps
- Include blue-green deployment logic
- Add feature flag configuration
- Include monitoring alert setup
## Best Practices
### 1. Keep Commands Focused
Each command should do ONE thing well:
```markdown# Good: Focused commands/deploy [env] # Just deployment/migrate [env] # Just migrations/rollback [env] # Just rollback
# Bad: Doing too much/deploy-and-test-and-notify [env]2. Use Clear Phase Names
Section titled “2. Use Clear Phase Names”# Good: Clear phases### Phase 1: Pre-deployment Checks### Phase 2: Build and Test### Phase 3: Deploy
# Bad: Vague phases### Phase 1: Setup### Phase 2: Main stuff### Phase 3: Finish3. Provide Verification Steps
Section titled “3. Provide Verification Steps”## Workflow
### Phase 2: Build
1. **Build Application** ```bash pnpm buildVerification:
- Check
dist/folder exists - Verify no build errors in output
- Confirm bundle size < 500KB
### 4. Include Error Handling
```markdown### Phase 3: Deploy
1. **Deploy to Server** - Upload build artifacts
**If deployment fails**: - Check server connectivity - Verify deployment credentials - Review server logs at `/var/log/app.log` - Consider using `--force` flag5. Add Real Examples
Section titled “5. Add Real Examples”## Example
**Scenario**: Deploy new feature to staging
**Input**: `/deploy staging`
**What happens**:1. ✓ Tests pass (2m 15s)2. ✓ Build succeeds (45s)3. ✓ Migrations applied (2 migrations)4. ✓ Deployment complete (30s)5. ✓ Smoke tests pass (5/5)
**Output**: Deployment summary with rollback commandCommon Command Patterns
Section titled “Common Command Patterns”Pattern 1: Multi-Step Workflow
Section titled “Pattern 1: Multi-Step Workflow”# /release - Create Release
## Workflow
### Phase 1: Prepare1. Verify clean working directory2. Pull latest changes3. Run full test suite
### Phase 2: Version1. Bump version number2. Update CHANGELOG.md3. Commit version changes
### Phase 3: Release1. Create git tag2. Push to remote3. Trigger CI/CD pipeline
### Phase 4: Verify1. Wait for CI build2. Verify release artifacts3. Post release notesPattern 2: Interactive Workflow
Section titled “Pattern 2: Interactive Workflow”# /onboard - Onboard New Team Member
## Workflow
### Phase 1: Environment Setup1. **Ask for GitHub username** - Add to organization - Add to relevant teams
2. **Ask for preferred IDE** - Provide setup instructions - Share configuration filesPattern 3: Conditional Workflow
Section titled “Pattern 3: Conditional Workflow”# /optimize - Optimize Performance
## Workflow
### Phase 1: Analyze1. Run performance profiler2. Identify bottlenecks
**If database is bottleneck**: - Go to Phase 2: Database Optimization
**If API is bottleneck**: - Go to Phase 3: API OptimizationTesting Your Command
Section titled “Testing Your Command”After creating a command, test it:
- Invoke it:
/your-command test input - Verify behavior: Check that Claude follows the workflow
- Test flags: Try each flag combination
- Check output: Ensure output matches template
Next Steps
Section titled “Next Steps”- Create Custom Modes — Define behavioral patterns
- Create Custom Skills — Add knowledge modules
- Configure CLAUDE.md — Set project standards
Examples to Study
Section titled “Examples to Study”Check these built-in commands for inspiration:
/feature— Complex multi-phase workflow/fix— Debugging workflow with error handling/ship— Git workflow automation/tdd— Methodology-driven development