本指南提供 Claude Code 常見工作流程的逐步教學。每個教學都包含清晰的說明、範例命令和最佳實踐,幫助您充分利用 Claude Code。

目錄

了解新的程式碼庫

快速了解程式碼庫概況

使用時機: 您剛加入新專案,需要快速了解其結構。

1

導航到專案根目錄

$ cd /path/to/project
2

啟動 Claude Code

$ claude
3

請求高層次概述

> give me an overview of this codebase
4

深入了解特定組件

> explain the main architecture patterns used here
> what are the key data models?
> how is authentication handled?

提示:

  • 從廣泛的問題開始,然後縮小到特定領域
  • 詢問專案中使用的編碼慣例和模式
  • 請求專案特定術語的詞彙表

尋找相關程式碼

使用時機: 您需要找到與特定功能相關的程式碼。

1

請 Claude 找到相關文件

> find the files that handle user authentication
2

了解組件之間的互動

> how do these authentication files work together?
3

了解執行流程

> trace the login process from front-end to database

提示:

  • 明確說明您要尋找的內容
  • 使用專案中的領域語言

高效修復錯誤

診斷錯誤訊息

使用時機: 您遇到錯誤訊息,需要找到並修復其來源。

1

與 Claude 分享錯誤

> I'm seeing an error when I run npm test
2

請求修復建議

> suggest a few ways to fix the @ts-ignore in user.ts
3

應用修復

> update user.ts to add the null check you suggested

提示:

  • 告訴 Claude 重現問題的命令並獲取堆疊追蹤
  • 提及重現錯誤的步驟
  • 讓 Claude 知道錯誤是間歇性的還是持續性的

重構程式碼

現代化遺留程式碼

使用時機: 您需要更新舊程式碼以使用現代模式和實踐。

1

識別需要重構的遺留程式碼

> find deprecated API usage in our codebase
2

獲取重構建議

> suggest how to refactor utils.js to use modern JavaScript features
3

安全地應用更改

> refactor utils.js to use ES2024 features while maintaining the same behavior
4

驗證重構

> run tests for the refactored code

提示:

  • 請 Claude 解釋現代方法的好處
  • 在需要時請求保持向後相容性
  • 以小的、可測試的增量進行重構

處理測試

增加測試覆蓋率

使用時機: 您需要為未覆蓋的程式碼添加測試。

1

識別未測試的程式碼

> find functions in NotificationsService.swift that are not covered by tests
2

生成測試框架

> add tests for the notification service
3

添加有意義的測試案例

> add test cases for edge conditions in the notification service
4

運行並驗證測試

> run the new tests and fix any failures

提示:

  • 請求涵蓋邊緣案例和錯誤條件的測試
  • 在適當時請求單元測試和整合測試
  • 讓 Claude 解釋測試策略

建立拉取請求

生成全面的拉取請求

使用時機: 您需要為您的更改創建一個文檔完善的拉取請求。

1

總結您的更改

> summarize the changes I've made to the authentication module
2

使用 Claude 生成拉取請求

> create a pr
3

審查和改進

> enhance the PR description with more context about the security improvements
4

添加測試詳情

> add information about how these changes were tested

提示:

  • 直接請求 Claude 為您製作拉取請求
  • 在提交前審查 Claude 生成的拉取請求
  • 請 Claude 突出潛在風險或考慮事項

處理文件

生成程式碼文件

使用時機: 您需要為您的程式碼添加或更新文件。

1

識別未文檔化的程式碼

> find functions without proper JSDoc comments in the auth module
2

生成文件

> add JSDoc comments to the undocumented functions in auth.js
3

審查和增強

> improve the generated documentation with more context and examples
4

驗證文件

> check if the documentation follows our project standards

提示:

  • 指定您想要的文件風格(JSDoc、docstrings 等)
  • 在文件中請求範例
  • 為公共 API、介面和複雜邏輯請求文件

處理圖片

分析圖片和截圖

使用時機: 您需要在程式碼庫中處理圖片或獲取 Claude 的幫助分析圖片內容。

1

將圖片添加到對話中

您可以使用以下任何方法:

# 1. 將圖片拖放到 Claude Code 視窗中

# 2. 複製圖片並使用 ctrl+v 將其貼到 CLI 中

# 3. 提供圖片路徑
$ claude
> Analyze this image: /path/to/your/image.png
2

請 Claude 分析圖片

> What does this image show?
> Describe the UI elements in this screenshot
> Are there any problematic elements in this diagram?
3

使用圖片作為上下文

> Here's a screenshot of the error. What's causing it?
> This is our current database schema. How should we modify it for the new feature?
4

從視覺內容獲取程式碼建議

> Generate CSS to match this design mockup
> What HTML structure would recreate this component?

提示:

  • 當文字描述不清晰或繁瑣時使用圖片
  • 包含錯誤、UI 設計或圖表的截圖以提供更好的上下文
  • 您可以在對話中處理多個圖片
  • 圖片分析適用於圖表、截圖、模型等

設置專案記憶

創建有效的 CLAUDE.md 文件

使用時機: 您想要設置 CLAUDE.md 文件來存儲重要的專案信息、慣例和常用命令。

1

為您的程式碼庫建立 CLAUDE.md

> /init

提示:

  • 包含常用命令(構建、測試、lint)以避免重複搜索
  • 記錄程式碼風格偏好和命名慣例
  • 添加專案特定的重要架構模式
  • 您可以將 CLAUDE.md 文件添加到運行 Claude 的資料夾、父目錄(Claude 自動讀取這些)或子目錄(Claude 按需拉取這些)中

將 Claude 作為類 Unix 工具使用

將 Claude 添加到您的驗證流程

使用時機: 您想要使用 Claude Code 作為 linter 或程式碼審查者。

步驟:

1

將 Claude 添加到您的構建腳本

// package.json
{
    ...
    "scripts": {
        ...
        "lint:claude": "claude -p 'you are a linter. please look at the changes vs. main and report any issues related to typos. report the filename and line number on one line, and a description of the issue on the second line. do not return any other text.'"
    }
}

管道輸入,管道輸出

使用時機: 您想要將數據管道輸入到 Claude,並以結構化格式獲取數據。

1

通過 Claude 管道傳輸數據

$ cat build-error.txt | claude -p 'concisely explain the root cause of this build error' > output.txt

設置模型上下文協議 (MCP)

模型上下文協議 (MCP) 是一個開放協議,使 LLM 能夠訪問外部工具和數據源。更多詳情,請參見 MCP 文件

使用第三方 MCP 伺服器時請自行承擔風險。確保您信任 MCP 伺服器,並且在使用與互聯網通信的 MCP 伺服器時要特別小心,因為這些可能會使您面臨提示注入風險。

配置 MCP 伺服器

使用時機: 您想要通過使用模型上下文協議連接到專門的工具和外部伺服器來增強 Claude 的功能。

1

添加 MCP Stdio 伺服器

# 基本語法
$ claude mcp add <name> <command> [args...]

# 範例:添加本地伺服器
$ claude mcp add my-server -e API_KEY=123 -- /path/to/server arg1 arg2
2

管理您的 MCP 伺服器

# 列出所有配置的伺服器
$ claude mcp list

# 獲取特定伺服器的詳細信息
$ claude mcp get my-server

# 移除伺服器
$ claude mcp remove my-server

提示:

  • 使用 -s--scope 標誌與 project(預設)或 global 來指定配置存儲位置
  • 使用 -e--env 標誌設置環境變量(例如,-e KEY=value
  • MCP 遵循客戶端-伺服器架構,其中 Claude Code(客戶端)可以連接到多個專門的伺服器

連接到 Postgres MCP 伺服器

使用時機: 您想要給 Claude 只讀訪問 PostgreSQL 數據庫的權限,用於查詢和架構檢查。

1

添加 Postgres MCP 伺服器

$ claude mcp add postgres-server /path/to/postgres-mcp-server --connection-string "postgresql://user:pass@localhost:5432/mydb"
2

使用 Claude 查詢您的數據庫

# 在您的 Claude 會話中,您可以詢問有關您的數據庫的問題
$ claude
> describe the schema of our users table
> what are the most recent orders in the system?
> show me the relationship between customers and invoices

提示:

  • Postgres MCP 伺服器為了安全提供只讀訪問
  • Claude 可以幫助您探索數據庫結構並運行分析查詢
  • 您可以使用這個來快速了解不熟悉專案中的數據庫架構
  • 確保您的連接字符串使用具有最小所需權限的適當憑證

下一步

Claude Code 參考實現

克隆我們的開發容器參考實現。