6.3 --allowedTools - 允许的工具列表
--allowedTools 标志允许您指定应允许的工具列表,无需提示用户获得权限。这可以简化工作流程,减少权限提示的频率。
标志语法
bash
claude --allowedTools <tool1> <tool2> ... [其他选项]
功能描述
--allowedTools 标志会:
- 指定允许的工具列表
- 这些工具在使用时不会提示用户获得权限
- 其他工具仍会按正常权限流程处理
使用示例
基本用法
bash
claude --allowedTools "Bash(git log:*)" "Bash(git diff:*)" "Read"
允许特定的 Bash 命令和 Read 工具。
允许读取工具
bash
claude --allowedTools "Read" "Grep" "Glob"
允许文件读取和搜索工具。
允许特定命令
bash
claude --allowedTools "Bash(npm test:*)" "Bash(npm run build:*)"
允许特定的 npm 命令。
结合其他标志
bash
claude --allowedTools "Read" "Edit" --add-dir ./src
允许读取和编辑工具,并添加工作目录。
工具格式
工具的格式为:工具名称(模式)
- 工具名称 :如
Bash、Read、Edit等 - 模式 :可选,指定工具的使用模式或参数
常用工具
| 工具名称 | 描述 |
|---|
Bash| 执行 shell 命令 Read| 读取文件内容 Edit| 编辑文件 Grep| 搜索文本 Glob| 查找文件 Write| 写入文件
使用场景
1. 只读操作
bash
claude --allowedTools "Read" "Grep" "Glob"
只允许读取操作,避免意外修改。
2. 安全命令
bash
claude --allowedTools "Bash(git log:*)" "Bash(git diff:*)"
只允许安全的 git 只读命令。
3. 测试命令
bash
claude --allowedTools "Bash(npm test:*)" "Bash(python -m pytest:*)"
只允许测试相关命令。
4. 构建命令
bash
claude --allowedTools "Bash(npm run build:*)" "Bash(cmake --build:*)"
只允许构建相关命令。
注意事项
- 权限覆盖 :
--allowedTools会覆盖 settings.json 中的设置 - 工具存在 :指定的工具必须存在且可用
- 模式匹配 :工具模式应该准确,避免过于宽泛
- 安全考虑 :只允许真正需要的工具,避免安全风险
- 权限提示 :未在列表中的工具仍会提示用户获得权限
最佳实践
- 最小权限 :只允许真正需要的工具,遵循最小权限原则
- 明确模式 :使用明确的工具模式,避免过于宽泛的匹配
- 定期审查 :定期审查允许的工具列表,移除不需要的
- 文档记录 :记录允许的工具及其原因
- 测试验证 :测试允许的工具是否按预期工作
与 --disallowedTools 的对比
| 标志 | 行为 | 适用场景 |
|---|
--allowedTools| 白名单,只允许指定的工具| 严格控制,只允许特定操作 --disallowedTools| 黑名单,禁止指定的工具| 灵活控制,禁止危险操作
## 常见问题
### Q: 可以同时使用 `--allowedTools` 和 `--disallowedTools` 吗?
A: 可以,`--allowedTools` 优先级更高,会覆盖 `--disallowedTools`。
### Q: 工具模式支持通配符吗?
A: 是的,支持 `*` 通配符进行模式匹配。
### Q: 如何知道可用的工具列表?
A: 可以使用 `/tool list` 命令查看所有可用工具。
### Q: 允许的工具会持久化吗?
A: 不会,`--allowedTools` 只对当前会话有效。要持久化,请在 settings.json 中设置。
## 相关标志
* `--disallowedTools`:禁止的工具列表
* `--dangerously-skip-permissions`:跳过所有权限提示
* `--permission-mode`:指定权限模式
## 实际应用示例
### CI/CD 环境
bash
#!/bin/bash
# 在 CI/CD 中只允许安全操作
claude \
--allowedTools \
"Bash(git log:*)" \
"Bash(git diff:*)" \
"Read" \
"Grep" \
"审查代码变更"
### 只读分析
bash
#!/bin/bash
# 只读分析,避免意外修改
claude \
--allowedTools "Read" "Grep" "Glob" \
"分析这个项目的结构"