AgentSkillsCN

claude-code

Claude Code CLI 完整使用指南。用于代码编写、调试、重构、测试、提交等开发任务。 支持终端、VS Code、桌面应用、Web、JetBrains 等多种环境。

中文原作
SKILL.md
--- frontmatter
name: claude-code
description: |
  Claude Code CLI 完整使用指南。用于代码编写、调试、重构、测试、提交等开发任务。
  支持终端、VS Code、桌面应用、Web、JetBrains 等多种环境。
version: 1.0.0
author: 0xCryptoZen
tags: [claude, code, cli, development, ai, coding]
triggers:
  - claude code
  - claude cli
  - code assistant

Claude Code CLI 使用指南

Claude Code 是一个强大的 AI 编码工具,支持读取代码库、编辑文件、运行命令。

安装

macOS / Linux / WSL

bash
curl -fsSL https://claude.ai/install.sh | bash

Windows PowerShell

powershell
irm https://claude.ai/install.ps1 | iex

Homebrew (macOS)

bash
brew install --cask claude-code

npm

bash
npm install -g @anthropic-ai/claude-code

基本命令

命令说明
claude启动交互式 REPL
claude "query"带初始提示启动
claude -p "query"单次查询后退出
claude -c继续最近的会话
claude -r "session" "query"恢复指定会话
claude update更新到最新版本

常用 Flags

Flag说明
--continue, -c继续最近的会话
--dangerously-skip-permissions跳过所有权限提示
--permission-mode plan计划模式(只读分析)
--allowedTools允许的工具列表
--add-dir添加额外工作目录
--debug启用调试模式
--chrome启用 Chrome 浏览器集成

常见工作流

1. 理解新代码库

bash
cd /path/to/project
claude
> give me an overview of this codebase
> explain the main architecture patterns
> what are the key data models?

2. 修复 Bug

bash
claude "I'm seeing an error when I run npm test"
claude "suggest ways to fix the @ts-ignore in user.ts"
claude "update user.ts to add the null check"

3. 重构代码

bash
claude "find deprecated API usage"
claude "refactor utils.js to use ES2024 features"
claude "run tests for the refactored code"

4. 编写测试

bash
claude "write tests for the auth module"
claude "run the tests and fix any failures"

5. 创建 PR

bash
claude "commit my changes with a descriptive message"
claude "create a PR for this feature"

6. 代码审查

bash
claude "review my recent code changes for security issues"
claude "check for type errors"

Plan Mode(计划模式)

用于复杂重构或多文件修改前的计划。

bash
# 启动计划模式
claude --permission-mode plan

# 单次查询
claude --permission-mode plan -p "Analyze the auth system"

在计划模式下:

  • Claude 只执行只读操作
  • 使用 Shift+Tab 切换模式
  • 适合探索代码库和规划复杂变更

管道和脚本

bash
# 处理管道内容
cat logs.txt | claude -p "explain these errors"

# 监控日志
tail -f app.log | claude -p "alert if you see anomalies"

# 批量操作
git diff main --name-only | claude -p "review for security issues"

CLAUDE.md 配置

在项目根目录创建 CLAUDE.md 文件来设置编码规范:

markdown
# Project Guidelines

## Coding Standards
- Use TypeScript for all new files
- Follow ESLint rules
- Write tests for new features

## Architecture
- Use React functional components
- Follow the service/repository pattern

## Review Checklist
- [ ] Tests pass
- [ ] No TypeScript errors
- [ ] Code is documented

Subagents(子代理)

创建专用子代理处理特定任务:

bash
# 查看可用子代理
> /agents

# 使用特定子代理
> use the code-reviewer subagent to check auth module

# 创建自定义子代理
> /agents
# 选择 "Create New subagent"

MCP 集成

Model Context Protocol 用于连接外部数据源:

bash
# 配置 MCP
claude mcp

# 常见 MCP 服务器
- Google Drive
- Jira
- Slack
- 自定义工具

最佳实践

  1. 明确描述任务 - 越具体越好
  2. 提供上下文 - 包含错误信息、文件路径
  3. 分步骤执行 - 复杂任务分解为小步骤
  4. 验证结果 - 让 Claude 运行测试验证
  5. 使用 CLAUDE.md - 设置项目规范

调试技巧

bash
# 启用调试
claude --debug "api,mcp"

# 查看 API 请求
claude --debug "api"

# 排除某些调试
claude --debug "!statsig,!file"

故障排除

问题解决方案
登录失败运行 claude 重新登录
权限问题检查 --allowedTools 设置
更新失败手动运行 claude update
性能慢使用更具体的提示

资源