slash-command-builder

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Slash Command Builder - Claude Code Command Expert

斜杠命令构建器 - Claude Code命令专家

Use this skill when creating, improving, or troubleshooting Claude Code slash commands. Provides expert guidance on command structure, syntax, frontmatter configuration, and best practices.
当你需要创建、改进或排查Claude Code斜杠命令时,可使用此技能。它提供关于命令结构、语法、frontmatter配置以及最佳实践的专业指导。

When to Use This Skill

何时使用此技能

Activate this skill when:
  • User asks to create a new slash command
  • User wants to improve an existing command
  • User needs help with command arguments or frontmatter
  • User is troubleshooting command invocation issues
  • User wants to understand slash command capabilities
  • User asks about the difference between commands and skills
在以下场景激活此技能:
  • 用户请求创建新的斜杠命令
  • 用户想要改进现有命令
  • 用户需要命令参数或frontmatter相关帮助
  • 用户在排查命令调用问题
  • 用户想要了解斜杠命令的功能
  • 用户询问命令与技能的区别

Quick Reference

快速参考

Command File Structure

命令文件结构

markdown
---
description: Brief description shown in autocomplete
argument-hint: [arg1] [arg2] <optional-arg>
allowed-tools: Bash(git *), Read, Write
model: claude-3-5-sonnet-20241022
disable-model-invocation: false
---

Your command prompt here with $ARGUMENTS or $1, $2, etc.

Use !`command` for bash execution
Use @file.txt for file references
markdown
---
description: Brief description shown in autocomplete
argument-hint: [arg1] [arg2] <optional-arg>
allowed-tools: Bash(git *), Read, Write
model: claude-3-5-sonnet-20241022
disable-model-invocation: false
---

Your command prompt here with $ARGUMENTS or $1, $2, etc.

Use !`command` for bash execution
Use @file.txt for file references

File Locations

文件位置

Project commands (shared with team):
.claude/commands/my-command.md
Personal commands (individual use):
~/.claude/commands/my-command.md
Organized commands (namespaced):
.claude/commands/frontend/component.md  → /component (project:frontend)
项目命令(与团队共享):
.claude/commands/my-command.md
个人命令(个人使用):
~/.claude/commands/my-command.md
分类命令(命名空间式):
.claude/commands/frontend/component.md  → /component (project:frontend)

Creating Effective Slash Commands

创建高效的斜杠命令

Step 1: Identify the Use Case

步骤1:确定使用场景

Good candidates for slash commands:
  • Frequently repeated prompts
  • Simple, single-purpose tasks
  • Quick code reviews or analyses
  • Common git workflows
  • Standard documentation tasks
NOT good for slash commands (use Skills instead):
  • Complex multi-step workflows
  • Context-aware behavior
  • Team standardization needs
  • Workflows requiring multiple files
适合作为斜杠命令的场景:
  • 频繁重复的提示词
  • 简单、单一目标的任务
  • 快速代码审查或分析
  • 常见Git工作流
  • 标准化文档任务
不适合作为斜杠命令的场景(建议使用Skills):
  • 复杂的多步骤工作流
  • 上下文感知的行为
  • 团队标准化需求
  • 需要处理多个文件的工作流

Step 2: Choose Command Name

步骤2:选择命令名称

Best practices:
  • Use lowercase with hyphens:
    /review-pr
    ,
    /optimize-code
  • Make it memorable and intuitive
  • Avoid conflicts with built-in commands
  • Keep it short (2-3 words max)
Examples:
  • /review-pr
    - Clear, concise
  • /fix-lint
    - Action-oriented
  • /review-pull-request-thoroughly
    - Too verbose
  • /rpr
    - Too cryptic
最佳实践:
  • 使用小写加连字符:
    /review-pr
    /optimize-code
  • 名称要易记且直观
  • 避免与内置命令冲突
  • 保持简短(最多2-3个词)
示例:
  • /review-pr
    - 清晰、简洁
  • /fix-lint
    - 面向动作
  • /review-pull-request-thoroughly
    - 过于冗长
  • /rpr
    - 过于晦涩

Step 3: Design the Prompt

步骤3:设计提示词

Simple command (no arguments):
markdown
---
description: Analyze code for performance bottlenecks
---

Analyze the current file for performance issues:
1. Identify O(n²) or worse algorithms
2. Find unnecessary re-renders or computations
3. Check for memory leaks
4. Suggest optimizations with code examples
Command with all arguments:
markdown
---
description: Generate component boilerplate
argument-hint: <component-name> <type>
---

Create a $ARGUMENTS component following our style guide:
- Use TypeScript with strict types
- Include prop interfaces
- Add JSDoc comments
- Export as default
Command with positional arguments:
markdown
---
description: Review pull request
argument-hint: [pr-number] [reviewer]
---

Review PR #$1 and assign to @$2:
1. Check code quality and style
2. Verify tests are included
3. Look for security issues
4. Suggest improvements
5. Add comments in GitHub
无参数的简单命令:
markdown
---
description: Analyze code for performance bottlenecks
---

Analyze the current file for performance issues:
1. Identify O(n²) or worse algorithms
2. Find unnecessary re-renders or computations
3. Check for memory leaks
4. Suggest optimizations with code examples
带所有参数的命令:
markdown
---
description: Generate component boilerplate
argument-hint: <component-name> <type>
---

Create a $ARGUMENTS component following our style guide:
- Use TypeScript with strict types
- Include prop interfaces
- Add JSDoc comments
- Export as default
带位置参数的命令:
markdown
---
description: Review pull request
argument-hint: [pr-number] [reviewer]
---

Review PR #$1 and assign to @$2:
1. Check code quality and style
2. Verify tests are included
3. Look for security issues
4. Suggest improvements
5. Add comments in GitHub

Step 4: Add Frontmatter Configuration

步骤4:添加frontmatter配置

See
FRONTMATTER.md
in this skill directory for complete frontmatter options.
Minimal frontmatter:
yaml
---
description: What this command does
---
Full-featured frontmatter:
yaml
---
description: Complete command with all options
argument-hint: [required] <optional>
allowed-tools: Bash(git *), Read(**/*.ts), Write
model: claude-3-5-sonnet-20241022
disable-model-invocation: false
---
查看此技能目录中的
FRONTMATTER.md
获取完整的frontmatter选项。
最简frontmatter:
yaml
---
description: What this command does
---
全功能frontmatter:
yaml
---
description: Complete command with all options
argument-hint: [required] <optional>
allowed-tools: Bash(git *), Read(**/*.ts), Write
model: claude-3-5-sonnet-20241022
disable-model-invocation: false
---

Step 5: Test the Command

步骤5:测试命令

  1. Save the file to
    .claude/commands/
  2. Invoke with
    /command-name
  3. Check argument substitution works
  4. Verify tool permissions if using
    allowed-tools
  5. Test edge cases (missing args, wrong types)
  1. 将文件保存到
    .claude/commands/
    目录
  2. 使用
    /command-name
    调用命令
  3. 检查参数替换是否正常工作
  4. 如果使用了
    allowed-tools
    ,验证工具权限是否正确
  5. 测试边缘情况(缺失参数、错误类型)

Advanced Features

高级功能

Bash Execution

Bash执行

Execute shell commands inline with
!
prefix:
markdown
---
description: Show git status
allowed-tools: Bash(git status:*)
---

Current repository status:

!`git status`

Recent commits:

!`git log --oneline -5`
使用
!
前缀在命令中执行shell命令:
markdown
---
description: Show git status
allowed-tools: Bash(git status:*)
---

Current repository status:

!`git status`

Recent commits:

!`git log --oneline -5`

File References

文件引用

Include file contents with
@
prefix:
markdown
---
description: Review specific file
argument-hint: <file-path>
---

Review this file for code quality:

@$1

Focus on:
- Type safety
- Error handling
- Performance
- Maintainability
使用
@
前缀包含文件内容:
markdown
---
description: Review specific file
argument-hint: <file-path>
---

Review this file for code quality:

@$1

Focus on:
- Type safety
- Error handling
- Performance
- Maintainability

Tool Permissions

工具权限

Restrict which tools Claude can use:
markdown
---
description: Safe git status check
allowed-tools: Bash(git status:*), Bash(git diff:*)
---

Show current changes:

!`git status`
!`git diff --stat`
Tool permission syntax:
  • Bash(command:*)
    - Allow specific command with any args
  • Read(path/to/*.ts)
    - Allow reading TypeScript files in path
  • Write
    - Allow writing any file
  • Glob
    ,
    Grep
    ,
    Edit
    - Other available tools
限制Claude可使用的工具:
markdown
---
description: Safe git status check
allowed-tools: Bash(git status:*), Bash(git diff:*)
---

Show current changes:

!`git status`
!`git diff --stat`
工具权限语法:
  • Bash(command:*)
    - 允许特定命令使用任意参数
  • Read(path/to/*.ts)
    - 允许读取路径下的TypeScript文件
  • Write
    - 允许写入任意文件
  • Glob
    ,
    Grep
    ,
    Edit
    - 其他可用工具

Model Selection

模型选择

Override default model for specific commands:
markdown
---
description: Quick syntax fix
model: claude-3-5-haiku-20241022
---

Fix syntax errors in the current file quickly.
When to use different models:
  • claude-3-5-haiku-20241022
    - Fast, simple tasks
  • claude-3-5-sonnet-20241022
    - General purpose (default)
  • claude-opus-4-20250514
    - Complex reasoning
为特定命令覆盖默认模型:
markdown
---
description: Quick syntax fix
model: claude-3-5-haiku-20241022
---

Fix syntax errors in the current file quickly.
何时使用不同模型:
  • claude-3-5-haiku-20241022
    - 快速处理简单任务
  • claude-3-5-sonnet-20241022
    - 通用场景(默认)
  • claude-opus-4-20250514
    - 复杂推理任务

Disable Auto-Invocation

禁用自动调用

Prevent Claude from calling command automatically:
markdown
---
description: Destructive operation
disable-model-invocation: true
---

!`rm -rf node_modules`
!`npm install`
阻止Claude自动调用命令:
markdown
---
description: Destructive operation
disable-model-invocation: true
---

!`rm -rf node_modules`
!`npm install`

Common Patterns

常见模式

Code Review Command

代码审查命令

markdown
---
description: Review code changes
argument-hint: [file-or-pr]
allowed-tools: Bash(git *), Read, Grep
---

Review $ARGUMENTS for:

1. **Code Quality**
   - Clean, readable code
   - Proper naming conventions
   - DRY principle

2. **Security**
   - Input validation
   - SQL injection risks
   - XSS vulnerabilities

3. **Performance**
   - Inefficient algorithms
   - Unnecessary computations
   - Memory leaks

4. **Tests**
   - Unit test coverage
   - Edge cases handled
   - Integration tests

Provide specific file:line references for all issues.
markdown
---
description: Review code changes
argument-hint: [file-or-pr]
allowed-tools: Bash(git *), Read, Grep
---

Review $ARGUMENTS for:

1. **Code Quality**
   - Clean, readable code
   - Proper naming conventions
   - DRY principle

2. **Security**
   - Input validation
   - SQL injection risks
   - XSS vulnerabilities

3. **Performance**
   - Inefficient algorithms
   - Unnecessary computations
   - Memory leaks

4. **Tests**
   - Unit test coverage
   - Edge cases handled
   - Integration tests

Provide specific file:line references for all issues.

Git Workflow Command

Git工作流命令

markdown
---
description: Create feature branch
argument-hint: <feature-name>
allowed-tools: Bash(git *)
---

Create and switch to feature branch:

!`git checkout -b feature/$1`
!`git push -u origin feature/$1`

Branch feature/$1 created and pushed to origin.
markdown
---
description: Create feature branch
argument-hint: <feature-name>
allowed-tools: Bash(git *)
---

Create and switch to feature branch:

!`git checkout -b feature/$1`
!`git push -u origin feature/$1`

Branch feature/$1 created and pushed to origin.

Documentation Generator

文档生成器

markdown
---
description: Generate API docs
argument-hint: <file-path>
allowed-tools: Read
---

Generate comprehensive API documentation for:

@$1

Include:
- Function signatures with types
- Parameter descriptions
- Return value documentation
- Usage examples
- Error cases
markdown
---
description: Generate API docs
argument-hint: <file-path>
allowed-tools: Read
---

Generate comprehensive API documentation for:

@$1

Include:
- Function signatures with types
- Parameter descriptions
- Return value documentation
- Usage examples
- Error cases

Test Generator

测试用例生成器

markdown
---
description: Generate test cases
argument-hint: <file-to-test>
allowed-tools: Read, Write
---

Generate test cases for:

@$1

Create tests covering:
- Happy path scenarios
- Edge cases
- Error conditions
- Boundary values

Use the existing test framework style.
markdown
---
description: Generate test cases
argument-hint: <file-to-test>
allowed-tools: Read, Write
---

Generate test cases for:

@$1

Create tests covering:
- Happy path scenarios
- Edge cases
- Error conditions
- Boundary values

Use the existing test framework style.

Troubleshooting

故障排除

Command Not Found

命令未找到

Problem:
/my-command
doesn't autocomplete
Solutions:
  1. Check file is in
    .claude/commands/
    or
    ~/.claude/commands/
  2. Verify filename matches command (
    .claude/commands/my-command.md
    )
  3. Restart Claude Code to reload commands
  4. Check for syntax errors in frontmatter
问题:
/my-command
无法自动补全
解决方案:
  1. 检查文件是否位于
    .claude/commands/
    ~/.claude/commands/
    目录
  2. 验证文件名与命令名称匹配(
    .claude/commands/my-command.md
  3. 重启Claude Code以重新加载命令
  4. 检查frontmatter中是否存在语法错误

Arguments Not Substituting

参数未替换

Problem:
$1
appears literally instead of being replaced
Solutions:
  1. Ensure arguments are passed:
    /command arg1 arg2
  2. Check you're using
    $1
    ,
    $2
    (not
    ${1}
    )
  3. For all args, use
    $ARGUMENTS
    instead
  4. Verify
    argument-hint
    frontmatter is correct
问题:
$1
直接显示为文本而非被替换
解决方案:
  1. 确保已传入参数:
    /command arg1 arg2
  2. 检查是否使用了
    $1
    $2
    (而非
    ${1}
  3. 若要获取所有参数,使用
    $ARGUMENTS
    替代
  4. 验证
    argument-hint
    frontmatter配置正确

Tool Permission Denied

工具权限被拒绝

Problem: Command can't execute bash or read files
Solutions:
  1. Add
    allowed-tools
    frontmatter
  2. Use specific tool patterns:
    Bash(git *)
  3. Check tool name capitalization (e.g.,
    Bash
    , not
    bash
    )
  4. For file operations, use glob patterns:
    Read(**/*.ts)
问题: 命令无法执行bash或读取文件
解决方案:
  1. 添加
    allowed-tools
    frontmatter配置
  2. 使用特定工具模式:
    Bash(git *)
  3. 检查工具名称的大小写(例如
    Bash
    而非
    bash
  4. 对于文件操作,使用通配符模式:
    Read(**/*.ts)

Command Invokes at Wrong Time

命令在错误时间被调用

Problem: Claude calls command when you don't want it to
Solutions:
  1. Add
    disable-model-invocation: true
    to frontmatter
  2. Ensure
    description
    is specific to avoid false triggers
  3. Use more explicit command names
问题: Claude在你不需要时调用了命令
解决方案:
  1. 在frontmatter中添加
    disable-model-invocation: true
  2. 确保
    description
    足够具体,避免误触发
  3. 使用更明确的命令名称

Commands vs. Skills

命令与技能的区别

Use Slash Commands When:

适合使用斜杠命令的场景:

  • ✅ Task is simple and focused
  • ✅ Prompt fits in one Markdown file
  • ✅ Need quick, explicit invocation
  • ✅ Personal productivity shortcuts
  • ✅ 任务简单且聚焦
  • ✅ 提示词可放入单个Markdown文件
  • ✅ 需要快速、显式的调用
  • ✅ 个人生产力快捷方式

Use Skills When:

适合使用技能的场景:

  • ✅ Complex, multi-step workflows
  • ✅ Context-aware automatic activation
  • ✅ Team needs standardization
  • ✅ Multiple supporting files needed
  • ✅ Sophisticated conditional logic
Example:
  • Command:
    /review-pr
    - Quick PR review prompt
  • Skill:
    code-reviewer
    - Comprehensive review framework with security.md, performance.md, style.md, and scripts
  • ✅ 复杂的多步骤工作流
  • ✅ 上下文感知的自动激活
  • ✅ 团队标准化需求
  • ✅ 需要多个支持文件
  • ✅ 复杂的条件逻辑
示例:
  • 命令:
    /review-pr
    - 快速PR审查提示词
  • 技能:
    code-reviewer
    - 包含security.md、performance.md、style.md和脚本的全面审查框架

Best Practices

最佳实践

1. Keep Commands Focused

1. 保持命令聚焦

Each command should do ONE thing well. Don't create Swiss Army knife commands.
每个命令应专注于完成一件事。不要创建“瑞士军刀”式的命令。

2. Use Clear Argument Hints

2. 使用清晰的参数提示

yaml
argument-hint: [required] <optional> [choices: a|b|c]
yaml
argument-hint: [required] <optional> [choices: a|b|c]

3. Document Expected Output

3. 记录预期输出

Tell Claude what format you want:
markdown
Generate a JSON response with this structure:
{
  "issues": [],
  "suggestions": []
}
告诉Claude你想要的输出格式:
markdown
Generate a JSON response with this structure:
{
  "issues": [],
  "suggestions": []
}

4. Include Examples

4. 包含示例

Show Claude what good output looks like:
markdown
Example output:
向Claude展示优质输出的样子:
markdown
Example output:

Security Issues

Security Issues

  • SQL Injection (file.ts:42) - Use parameterized queries
undefined
  • SQL Injection (file.ts:42) - Use parameterized queries
undefined

5. Be Specific with Tool Permissions

5. 工具权限要具体

Don't use
allowed-tools: Bash
- use
allowed-tools: Bash(git *)
不要使用
allowed-tools: Bash
,应使用
allowed-tools: Bash(git *)

6. Test with Edge Cases

6. 测试边缘情况

  • Missing arguments
  • Wrong argument types
  • Files that don't exist
  • Empty repositories
  • 缺失参数
  • 错误的参数类型
  • 不存在的文件
  • 空仓库

7. Version Control Commands

7. 对命令进行版本控制

Store
.claude/commands/
in git for team collaboration
.claude/commands/
目录存入Git以实现团队协作

8. Organize with Namespaces

8. 使用命名空间组织命令

Use subdirectories for related commands:
.claude/commands/
├── git/
│   ├── feature.md
│   ├── fix.md
│   └── release.md
└── testing/
    ├── unit.md
    └── e2e.md
使用子目录管理相关命令:
.claude/commands/
├── git/
│   ├── feature.md
│   ├── fix.md
│   └── release.md
└── testing/
    ├── unit.md
    └── e2e.md

Related Documentation

相关文档

  • FRONTMATTER.md - Complete frontmatter reference
  • EXAMPLES.md - Real-world command examples
  • PATTERNS.md - Common command patterns
  • FRONTMATTER.md - 完整的frontmatter参考
  • EXAMPLES.md - 真实场景的命令示例
  • PATTERNS.md - 常见命令模式

Checklist for New Commands

新命令检查清单

Before finalizing a slash command:
  • Command name is clear and concise
  • Description frontmatter is specific
  • Argument hints are provided if needed
  • Tool permissions are minimal and specific
  • Prompt is focused on one task
  • Examples are included in prompt
  • Expected output format is specified
  • Edge cases are considered
  • Command has been tested
  • File is in correct directory
Remember: Great slash commands are simple, focused, and make frequent tasks effortless. If you find yourself adding complexity, consider creating a Skill instead.
在最终确定斜杠命令前,请检查:
  • 命令名称清晰简洁
  • 已添加具体的description frontmatter
  • 若需要,已提供参数提示
  • 工具权限最小化且具体
  • 提示词聚焦于单个任务
  • 提示词中包含示例
  • 已指定预期输出格式
  • 考虑了边缘情况
  • 命令已测试
  • 文件位于正确目录
记住: 优秀的斜杠命令简单、聚焦,能让频繁执行的任务变得轻松。如果发现需要添加复杂功能,建议考虑创建一个Skill而非命令。