Skip to content

6.3 --allowedTools - 允许的工具列表

--allowedTools 标志允许您指定应允许的工具列表,无需提示用户获得权限。这可以简化工作流程,减少权限提示的频率。

标志语法

bash


claude --allowedTools <tool1> <tool2> ... [其他选项]

功能描述

--allowedTools 标志会:

  1. 指定允许的工具列表
  2. 这些工具在使用时不会提示用户获得权限
  3. 其他工具仍会按正常权限流程处理

使用示例

基本用法

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

允许读取和编辑工具,并添加工作目录。

工具格式

工具的格式为:工具名称(模式)

  • 工具名称 :如 BashReadEdit
  • 模式 :可选,指定工具的使用模式或参数

常用工具

工具名称描述

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:*)"

只允许构建相关命令。

注意事项

  1. 权限覆盖--allowedTools 会覆盖 settings.json 中的设置
  2. 工具存在 :指定的工具必须存在且可用
  3. 模式匹配 :工具模式应该准确,避免过于宽泛
  4. 安全考虑 :只允许真正需要的工具,避免安全风险
  5. 权限提示 :未在列表中的工具仍会提示用户获得权限

最佳实践

  1. 最小权限 :只允许真正需要的工具,遵循最小权限原则
  2. 明确模式 :使用明确的工具模式,避免过于宽泛的匹配
  3. 定期审查 :定期审查允许的工具列表,移除不需要的
  4. 文档记录 :记录允许的工具及其原因
  5. 测试验证 :测试允许的工具是否按预期工作

--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" \
      "分析这个项目的结构"

基于 MIT 许可发布 | 永久导航