Claude Code CLI Cheatsheet - Anthropic AI Coding Agent Full Reference
Claude Code targets developers who want the model to read, edit, and run commands inside a repo from the terminal, plus automation engineers wiring it into CI. As Anthropic official agentic coding tool it differs from chat-style assistants, and drives pipelines headlessly via -p + --output-format json or iterative multi-turn REPL sessions. This sheet surfaces the key commands for install/auth, session resume, permission modes, and MCP extensibility so you can start on real projects immediately.
Typical Use Case
Claude Code is most useful in two situations: daily coding where the AI directly edits repo code (refactoring, bug fixes, tests, new features) via the interactive REPL, and CI/script automation where -p print mode with --output-format json handles one-shot reviews, docs, or codegen. It shines as a pair engineer when build/test/lint live in CLAUDE.md and context comes via @file — far fewer wasted turns. When you do not want the model touching files, use /plan read-only planning first, then release permissions.
Install & Auth 7
npm install -g @anthropic-ai/claude-codeclaude --versionnpm update -g @anthropic-ai/claude-codeclaude updateclaude auth loginclaude auth statusclaude auth logoutLaunch & Run 6
claudeclaude "Refactor the user-auth module to use the new session API"cat error.log | claude -p "Explain this error and suggest a fix"claude -cclaude -r "abc123" "Finish the PR"claude -c -p "Check for type errors"CLI Flags 13
claude -p -m claude-sonnet-4 "Write a Python function to parse CSV"claude -p --output-format json "List all TODO comments in this project"claude -p --max-turns 5 "Refactor the auth module"claude --max-budget-usd 2 "Implement the login page"claude --add-dir ../frontend ../backendclaude --system-prompt "You are a security auditor"claude --append-system-prompt "Use TypeScript strict mode"claude --permission-mode planclaude --dangerously-skip-permissionsclaude -w feat/loginclaude --effort high "Design the cache layer architecture"claude --bg "Run the test suite"claude --verboseSession Slash Commands 7
/clear/compact/context/resume/branch/rewind/exitProject & Dev Slash Commands 10
/init/memory/mcp/agents/permissions/hooks/plan/model/diff/code-review --fixMCP Server Management 7
claude mcp add --transport http <name> <url>claude mcp add <name> -- <args...>claude mcp add-json '<json>'claude mcp listclaude mcp get <name>claude mcp remove <name>claude mcp add-from-claude-desktopPrompt File References 3
Review @./src/components/Button.tsxCompare @src/old.js with @src/new.jsExplain the structure of @./srcCommand Examples
Fix a failing test in print mode
claude -p --output-format json "运行测试,根据失败信息定位根因并给出修复方案"
cat log.txt | claude -p "分析这个错误的根因并给出修复步骤"-p 只输出结果后退出,加 --output-format json 便于脚本直接解析,是自动化调用的事实标准。
Output
{"status":"root cause identified","cause":"user-auth 模块未处理空 token","fix":"在 session 校验前补充 null 检查"}Resume last session to continue a refactor
claude -c
echo "错误日志" | claude -c -p "检查类型错误"用 -c(最近会话)或 -r <session-id>(指定会话)在终端重启后即可无痛续接。
Output
(进入 REPL,恢复上文中的上下文与已做改动,可继续对话或改文件)
Plan before a large change
claude --permission-mode plan "重构支付模块,先给出改动清单和风险点"plan 模式下模型只做读与分析,确认方案后再按默认权限执行,能显著降低返工。
Output
(仅输出规划报告,不修改任何文件)
Add a Postgres MCP server
claude mcp add --transport http my-pg http://localhost:8080/mcp
claude mcp list接入后可在会话里让 Claude 直接查询数据库并基于结果写代码,省去手动复制查询结果。
Output
✔ Added MCP server "my-pg" (HTTP)
Common Pitfalls
- --dangerously-skip-permissions skips all prompts so the model can edit files and run commands directly — only in trusted/throwaway environments.
- --max-budget-usd only caps -p print mode; an interactive REPL needs another budget mechanism, so do not rely on it to stop long sessions.
- Changes are diffed before applying, but editing CLAUDE.md/AGENTS.md or dependencies changes future behavior — review those memory files before big changes.
- stdio MCP servers are spawned by the claude subprocess; if one dies it can slow the agent, so check /mcp on stalls.
Tips
- For scripts and CI prefer `claude -p --output-format json`: print mode + JSON is the de-facto standard for automation.
- Cap long tasks or bounded budgets with both `--max-turns` and `--max-budget-usd` to avoid runaway API spend.
- Give Claude context with `@file` / `@dir` references — more accurate and maintainable than pasting file contents into the prompt.
- Before big changes, enter read-only `/plan` mode to confirm the approach, then grant permissions to execute — far less rework.
FAQ
What is the difference between Claude Code -p (print mode) and the interactive REPL?
The interactive REPL suits exploratory development with multi-turn conversations and file edits; -p print mode suits scripts and pipelines — it runs once, prints the result, and exits, and supports --output-format json for programmatic parsing.
How do I let Claude Code auto-approve all actions without confirmation prompts?
Use --dangerously-skip-permissions to skip all prompts, or set --permission-mode to acceptEdits / bypassPermissions. Only use these in trusted environments, since they let the agent modify files and run commands directly.
How does Claude Code connect to MCP servers to extend its capabilities?
Manage connections interactively via the /mcp slash command, or add external MCP servers with claude mcp add <name> <command> (e.g. database, filesystem, API). Once connected, the agent can call those tools during a session.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Aug 5, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us