跳轉到主要內容

CLI 命令

命令描述範例
claude啟動互動式 REPLclaude
claude "query"使用初始提示啟動 REPLclaude "explain this project"
claude -p "query"透過 SDK 查詢,然後退出claude -p "explain this function"
cat file | claude -p "query"處理管道內容cat logs.txt | claude -p "explain"
claude -c繼續最近的對話claude -c
claude -c -p "query"透過 SDK 繼續claude -c -p "Check for type errors"
claude -r "<session-id>" "query"按 ID 恢復工作階段claude -r "abc123" "Finish this PR"
claude update更新到最新版本claude update
claude mcp設定 Model Context Protocol (MCP) 伺服器請參閱 Claude Code MCP 文件

CLI 旗標

使用這些命令列旗標自訂 Claude Code 的行為:
旗標描述範例
--add-dir新增額外的工作目錄供 Claude 存取(驗證每個路徑是否存在為目錄)claude --add-dir ../apps ../lib
--agents透過 JSON 動態定義自訂 subagents(請參閱下面的格式)claude --agents '{"reviewer":{"description":"Reviews code","prompt":"You are a code reviewer"}}'
--allowedTools應允許的工具清單,無需提示使用者獲得許可,除了 settings.json 檔案"Bash(git log:*)" "Bash(git diff:*)" "Read"
--disallowedTools應禁止的工具清單,無需提示使用者獲得許可,除了 settings.json 檔案"Bash(git log:*)" "Bash(git diff:*)" "Edit"
--print, -p列印回應而不進入互動模式(請參閱 SDK 文件 以了解程式設計用法詳細資訊)claude -p "query"
--system-prompt用自訂文字取代整個系統提示(在互動和列印模式中都有效;在 v2.0.14 中新增)claude --system-prompt "You are a Python expert"
--system-prompt-file從檔案載入系統提示,取代預設提示(僅列印模式;在 v1.0.54 中新增)claude -p --system-prompt-file ./custom-prompt.txt "query"
--append-system-prompt將自訂文字附加到預設系統提示的末尾(在互動和列印模式中都有效;在 v1.0.55 中新增)claude --append-system-prompt "Always use TypeScript"
--output-format指定列印模式的輸出格式(選項:textjsonstream-jsonclaude -p "query" --output-format json
--input-format指定列印模式的輸入格式(選項:textstream-jsonclaude -p --output-format json --input-format stream-json
--include-partial-messages在輸出中包含部分串流事件(需要 --print--output-format=stream-jsonclaude -p --output-format stream-json --include-partial-messages "query"
--verbose啟用詳細日誌記錄,顯示完整的逐輪輸出(有助於在列印和互動模式中進行偵錯)claude --verbose
--max-turns限制非互動模式中的代理轉數claude -p --max-turns 3 "query"
--model使用最新模型的別名(sonnetopus)或模型的完整名稱設定目前工作階段的模型claude --model claude-sonnet-4-5-20250929
--permission-mode以指定的 permission mode 開始claude --permission-mode plan
--permission-prompt-tool指定 MCP 工具以在非互動模式中處理許可提示claude -p --permission-prompt-tool mcp_auth_tool "query"
--resume按 ID 恢復特定工作階段,或在互動模式中選擇claude --resume abc123 "query"
--continue在目前目錄中載入最近的對話claude --continue
--dangerously-skip-permissions跳過許可提示(謹慎使用)claude --dangerously-skip-permissions
--output-format json 旗標對於指令碼和自動化特別有用, 允許您以程式設計方式解析 Claude 的回應。

Agents 旗標格式

--agents 旗標接受定義一個或多個自訂 subagents 的 JSON 物件。每個 subagent 需要一個唯一的名稱(作為鍵)和一個具有以下欄位的定義物件:
欄位必需描述
description何時應呼叫 subagent 的自然語言描述
prompt指導 subagent 行為的系統提示
toolssubagent 可以使用的特定工具陣列(例如 ["Read", "Edit", "Bash"])。如果省略,繼承所有工具
model要使用的模型別名:sonnetopushaiku。如果省略,使用預設 subagent 模型
範例:
claude --agents '{
  "code-reviewer": {
    "description": "Expert code reviewer. Use proactively after code changes.",
    "prompt": "You are a senior code reviewer. Focus on code quality, security, and best practices.",
    "tools": ["Read", "Grep", "Glob", "Bash"],
    "model": "sonnet"
  },
  "debugger": {
    "description": "Debugging specialist for errors and test failures.",
    "prompt": "You are an expert debugger. Analyze errors, identify root causes, and provide fixes."
  }
}'
如需有關建立和使用 subagents 的更多詳細資訊,請參閱 subagents 文件

系統提示旗標

Claude Code 提供三個旗標用於自訂系統提示,每個旗標都有不同的用途:
旗標行為模式使用案例
--system-prompt取代整個預設提示互動 + 列印完全控制 Claude 的行為和指示
--system-prompt-file取代為檔案內容僅列印從檔案載入提示以實現可重現性和版本控制
--append-system-prompt附加到預設提示互動 + 列印新增特定指示,同時保持預設 Claude Code 行為
何時使用每一個:
  • --system-prompt:當您需要完全控制 Claude 的系統提示時使用。這會移除所有預設 Claude Code 指示,為您提供一個空白的狀態。
    claude --system-prompt "You are a Python expert who only writes type-annotated code"
    
  • --system-prompt-file:當您想從檔案載入自訂提示時使用,對於團隊一致性或版本控制的提示範本很有用。
    claude -p --system-prompt-file ./prompts/code-review.txt "Review this PR"
    
  • --append-system-prompt:當您想新增特定指示,同時保持 Claude Code 的預設功能完整時使用。這是大多數使用案例的最安全選項。
    claude --append-system-prompt "Always use TypeScript and include JSDoc comments"
    
--system-prompt--system-prompt-file 是互斥的。您不能同時使用這兩個旗標。
對於大多數使用案例,建議使用 --append-system-prompt,因為它保留了 Claude Code 的內建功能,同時新增了您的自訂需求。只有當您需要完全控制系統提示時,才使用 --system-prompt--system-prompt-file
如需有關列印模式 (-p) 的詳細資訊,包括輸出格式、 串流、詳細日誌記錄和程式設計用法,請參閱 SDK 文件

另請參閱