6.6 --system-prompt - 替換整個系統提示
--system-prompt 標誌允許您用自定義文字替換整個系統提示。這為您提供了完全控制 Claude 行為的能力。
标志语法
bash
claude --system-prompt "<prompt>" [其他选项]
功能描述
--system-prompt 標誌會:
- 用您提供的自定義文字替換整個預設系統提示
- 完全控制 Claude 的行為和指令
- 適用於互動模式和列印模式
使用示例
基本用法
bash
claude --system-prompt "You are a Python expert"
將 Claude 設定為 Python 專家。
代码风格指定
bash
claude --system-prompt "You only write type-annotated Python code"
指定只編寫型別註解的 Python 程式碼。
领域专家
bash
claude --system-prompt "You are a machine learning expert specializing in PyTorch"
將 Claude 設定為 PyTorch 機器學習專家。
结合其他标志
bash
claude --system-prompt "You are a security-focused code reviewer" --model opus
使用指定的系統提示和模型。
使用场景
1. 特定编程语言
bash
claude --system-prompt "You are a Rust expert who follows the Rust API guidelines"
專注於 Rust 程式設計。
2. 特定框架
bash
claude --system-prompt "You are a React expert who uses functional components and hooks"
專注於 React 開發。
3. 特定领域
bash
claude --system-prompt "You are a DevOps expert specializing in Kubernetes"
專注於 DevOps 和 Kubernetes。
4. 特定风格
bash
claude --system-prompt "You write clean, well-documented code with comprehensive tests"
強調程式碼質量和文件。
系统提示的影响
完全替换
--system-prompt 會完全替換預設系統提示,這意味著:
- 刪除所有預設 Claude Code 指令
- 為您提供一個空白的開始
- 完全由您控制 Claude 的行為
適用模式
--system-prompt 在以下模式中有效:
- 互動模式 :
claude --system-prompt "..." - 列印模式 :
claude -p --system-prompt "..." - 繼續模式 :
claude -c --system-prompt "..."
注意事項
- 與
--system-prompt-file互斥:不能同時使用兩個標誌 - 完全替換 :會刪除所有預設指令,確保您的提示足夠完整
- 提示長度 :系統提示有長度限制,保持簡潔明確
- 行為改變 :會顯著改變 Claude 的行為,確保這是您想要的
- 測試驗證 :使用前測試自定義提示,確保行為符合預期
與其他系統提示標誌的對比
| 標誌 | 行為 | 模式 | 用例 |
|---|
--system-prompt| 替換 整個預設提示| 互動 + 列印| 完全控制 Claude 的行為和指令 --system-prompt-file| 替換 為檔案內容| 僅列印| 從檔案載入提示以實現可重現性和版本控制 --append-system-prompt| 附加 到預設提示| 互動 + 列印| 新增特定指令,同時保持預設 Claude Code 行為
最佳實踐
- 明確具體 :系統提示應該明確具體,指導 Claude 的行為
- 保持簡潔 :避免過長的提示,保持簡潔明確
- 測試驗證 :使用前測試自定義提示,確保行為符合預期
- 文件記錄 :記錄自定義提示及其用途
- 版本控制 :將重要的自定義提示放入版本控制
## 常见问题
### Q: 可以在打印模式中使用吗?
A: 是的,`--system-prompt` 在交互模式和打印模式中都有效。
### Q: 会影响会话历史吗?
A: 不会,系统提示只影响 Claude 的行为,不影响会话历史。
### Q: 可以动态更改吗?
A: 不可以,系统提示在会话开始时设置,会话期间不能更改。
### Q: 与 `--append-system-prompt` 有什么区别?
A: `--system-prompt` 完全替换默认提示,而 `--append-system-prompt` 附加到默认提示。
## 相关标志
* `--system-prompt-file`:從檔案載入系統提示
* `--append-system-prompt`:附加到預設系統提示
* `--model`:設定會話模型
## 实际应用示例
### 项目特定提示
bash
#!/bin/bash
# 为项目定义特定的系统提示
PROJECT_PROMPT="You are a developer working on the XYZ project. Follow our coding standards: use TypeScript, include JSDoc comments, and write unit tests for all functions."
claude --system-prompt "$PROJECT_PROMPT" "实现这个功能"
### 团队标准提示
bash
#!/bin/bash
# 使用团队标准的系统提示
TEAM_PROMPT=$(cat .claude/team-prompt.txt)
claude --system-prompt "$TEAM_PROMPT" "审查这个 PR"