命令系统总览
Claude Code 提供了 101+ 斜杠命令,用户通过 /command 的方式在对话中调用。命令系统与工具系统不同——工具是 LLM 主动调用的,命令是用户主动调用的。
命令 vs 工具
| 特性 | 命令 (Commands) | 工具 (Tools) |
|---|---|---|
| 调用者 | 用户 | LLM |
| 触发方式 | /command | LLM Tool Use |
| 权限检查 | 无(用户主动) | 有 |
| 位置 | src/commands/ | src/tools/ |
| 数量 | 101+ | 43+ |
命令架构
用户输入 "/commit"
↓
命令解析器 (commands.ts, 754 行)
↓
查找命令注册表
↓
执行命令处理器
↓
返回结果到对话
命令注册
每个命令通过 commands.ts 注册:
interface Command {
name: string; // 命令名
description: string; // 描述
aliases?: string[]; // 别名
args?: string; // 参数说明
handler: (args: string) => Promise<void>;
}
命令分类
| 类别 | 数量 | 代表命令 |
|---|---|---|
| 开发命令 | ~20 | /commit, /diff, /review, /refactor |
| 工作流命令 | ~30 | /tasks, /memory, /skills, /mcp |
| 工具命令 | ~50 | /clear, /compact, /help, /config |
命令补全
输入 / 后,Claude Code 会显示可用命令列表,并支持模糊搜索和自动补全。