Mastering Claude Code CLI:
Your Ultimate Command Guide

I am a developer with 36+ years of rigorous business operations experience. I don't just write Python; I build tools that save businesses 20+ hours a week. Specialising in custom dashboards (Streamlit), high-performance APIs (FastAPI), and automated data gathering (Scraping).
Mastering Claude Code CLI: Your Ultimate Command Guide
I'm happy to share my comprehensive compilation of commands for the Claude Code CLI. This guide I hope will help you navigate and utilize the Claude Code CLI's full potential, whether you're automating tasks, managing permissions, or working across multiple directories. With this, you'll be able to handle everything from basic commands to advanced workflows, ensuring you can optimize your use of Claude Code CLI for any project.
Key Highlights:
๐ฏ Most Useful Flags
-p, --print- Headless mode (perfect for scripts/CI)-y, --dangerously-skip-permissions- YOLO mode--model- Switch between Sonnet/Opus--add-dir- Work across multiple directories--output-format json- Parse responses programmatically--max-turns N- Control cost and prevent runaway agents--permission-mode plan- Review before execution
๐ฅ Power Combos
For Automation:
claude -p --output-format json --max-turns 3 "task"
For Safe Refactoring:
claude --permission-mode plan --allowedTools "Read" "Edit" "task"
For Complex Multi-repo Tasks:
claude -y --model opus --add-dir ../backend ../frontend "task"
๐ Interactive Commands
/help,/undo,/retry,/model,/history@filename,@folder/,@url,@gitmentions
๐ก๏ธ Permission Control
--allowedTools- Whitelist safe operations--disallowedTools- Blacklist dangerous commands--permission-mode- Choose approval level
The artifact above has everything organized with examples for CI/CD, pre-commit hooks, batch processing, and more!
Basic Commands
| Command | Description | Example | ||
claude | Start interactive REPL | claude | ||
claude "query" | Start REPL with initial prompt | claude "explain this project" | ||
claude -p "query" | Query via SDK, then exit (headless) | claude -p "explain this function" | ||
| `cat file | claude -p "query"` | Process piped content | `cat logs.txt | claude -p "explain"` |
claude -c | Continue most recent conversation | claude -c | ||
claude -c -p "query" | Continue via SDK | claude -c -p "Check for type errors" | ||
claude -r "<session-id>" | Resume session by ID | claude -r "abc123" "Finish this PR" | ||
claude update | Update to latest version | claude update | ||
claude mcp | Configure Model Context Protocol servers | claude mcp |
Essential Flags
๐ Automation & Output
| Flag | Description | Example |
-p, --print | Headless mode - execute and exit | claude -p "refactor this" |
--output-format | Output format: text, json, stream-json | claude -p "query" --output-format json |
--input-format | Input format: text, stream-json | claude -p --input-format stream-json |
--include-partial-messages | Include partial messages in streaming output | claude -p --output-format stream-json --include-partial-messages |
--verbose | Enable detailed logging | claude --verbose |
โ๏ธ Model & Behavior
| Flag | Description | Example |
--model | Specify model (sonnet, opus, or full name) | claude --model claude-sonnet-4-20250514 |
--max-turns | Limit conversation turns | claude -p --max-turns 3 "query" |
--append-system-prompt | Add custom system instructions | claude --append-system-prompt "Be concise" |
๐ Working Directories
| Flag | Description | Example |
--add-dir | Add additional working directories | claude --add-dir ../apps ../lib |
๐ Permissions & Safety
| Flag | Description | Example |
--permission-mode | Permission mode: auto, plan, prompt | claude --permission-mode plan |
--dangerously-skip-permissions | Skip ALL permission prompts (YOLO mode) | claude --dangerously-skip-permissions |
-y | Short alias for skip permissions | claude -y "task" |
--permission-prompt-tool | Always prompt for specific tool | claude --permission-prompt-tool mcp_auth_tool |
--allowedTools | Whitelist tools to auto-approve | claude --allowedTools "Bash(git *)" "Read" |
--disallowedTools | Blacklist tools to always block | claude --disallowedTools "Bash(rm *)" "Edit" |
๐ Session Management
| Flag | Description | Example |
-c, --continue | Continue most recent conversation | claude -c |
-r, --resume | Resume specific session by ID | claude --resume abc123 |
Interactive Slash Commands
Use these inside the Claude Code REPL:
| Command | Description |
/help | Show help information |
/exit | Exit Claude Code |
/clear | Clear conversation history |
/reset | Reset to new conversation |
/model | Switch model |
/undo | Undo last action |
/retry | Retry last message |
/logout | Logout from Claude |
/copy | Copy last response |
/history | Show conversation history |
/search | Search conversation |
@ Mentions
Reference context in your prompts:
| Mention | Description | Example |
@filename | Reference specific file | @app.py explain this |
@folder/ | Reference entire folder | @src/ refactor all files |
@url | Reference URL content | @https://example.com summarize |
@git | Reference git context | @git review recent commits |
@search | Search codebase | @search handleAuth fix the bug |
Practical Examples
1. Headless Mode (CI/CD, Scripts)
# Simple one-shot
claude -p "run tests and fix failures"
# With JSON output for parsing
claude -p "analyze code" --output-format json
# Streaming JSON
claude -p "refactor" --output-format stream-json
2. YOLO Mode (Auto-approve everything)
# Skip all confirmations
claude -y "migrate from Jest to Vitest"
# Or full flag
claude --dangerously-skip-permissions "rebuild the API"
3. Custom Models
# Use Opus
claude --model opus "complex refactoring task"
# Use specific version
claude --model claude-sonnet-4.5-20250929 "task"
4. Multi-directory Projects
# Work across multiple dirs
claude --add-dir ../backend ../frontend "sync API types"
5. Permission Control
# Auto-approve safe operations
claude --allowedTools "Read" "Bash(git log:*)" "task"
# Block dangerous operations
claude --disallowedTools "Bash(rm *)" "Bash(sudo *)" "task"
# Plan mode (review before executing)
claude --permission-mode plan "refactor database"
6. Session Management
# Continue last conversation
claude -c
# Continue with new prompt
claude -c -p "now add error handling"
# Resume specific session
claude -r "abc123" "continue the migration"
7. Piped Input
# Analyze logs
cat error.log | claude -p "find the root cause"
# Process command output
git diff | claude -p "write commit message"
# Multiple files
cat *.py | claude -p "find security issues"
8. Custom Instructions
# Add system-level instructions
claude --append-system-prompt "Use TypeScript strict mode" "build API"
# Combine with other flags
claude -y --append-system-prompt "Write tests for everything" "task"
9. Limit Turns (Cost Control)
# Max 3 back-and-forth turns
claude -p --max-turns 3 "fix the bug"
# Prevent runaway agents
claude -y --max-turns 5 "complex task"
10. Verbose Debugging
# See detailed logs
claude --verbose "task"
# Combine with headless
claude -p --verbose "task" 2> debug.log
Advanced Workflows
Pre-commit Hook
#!/bin/bash
# .git/hooks/pre-commit
changed_files=$(git diff --cached --name-only)
echo "$changed_files" | claude -p --max-turns 1 \
"Review these changes for issues" --output-format json
CI/CD Integration
# In GitHub Actions
- name: Code Review
run: |
git diff main..HEAD | claude -p \
--output-format json \
"Review changes and suggest improvements"
Automated Refactoring
# Safe refactoring with plan mode
claude --permission-mode plan \
--allowedTools "Read" "Edit" \
"refactor all components to use hooks"
Batch Processing
# Process multiple files
for file in src/*.ts; do
claude -p "add JSDoc to $file" --max-turns 1
done
Configuration Tips
Environment Variables
# Set default model
export CLAUDE_CODE_MODEL="claude-sonnet-4.5-20250929"
# Set API key
export ANTHROPIC_API_KEY="your-key"
# Set base URL (for proxies)
export ANTHROPIC_BASE_URL="http://localhost:8000"
Create Aliases
# Add to ~/.bashrc or ~/.zshrc
alias cc="claude"
alias ccp="claude -p"
alias ccy="claude -y"
alias ccplan="claude --permission-mode plan"
alias ccverbose="claude --verbose"
Permission Modes Explained
| Mode | Description | Use Case |
auto | Auto-approve safe operations | Default, balanced |
plan | Show plan before executing | Review changes first |
prompt | Prompt for every action | Maximum control |
--dangerously-skip-permissions | Skip all prompts | Automation, trusted tasks |
Tool Patterns for allowedTools/disallowedTools
# Git operations
"Bash(git log:*)" "Bash(git diff:*)" "Bash(git status:*)"
# File operations
"Read" "Edit" "Write"
# Safe commands
"Bash(ls:*)" "Bash(cat:*)" "Bash(grep:*)"
# Package managers
"Bash(npm *)" "Bash(pip *)" "Bash(cargo *)"
# Dangerous (for disallowedTools)
"Bash(rm *)" "Bash(sudo *)" "Bash(chmod *)"
Cost Optimization
# Limit turns to control cost
claude -p --max-turns 2 "task"
# Use Sonnet for most tasks (cheaper than Opus)
claude --model sonnet "task"
# Headless mode for automation (no interactive overhead)
claude -p "task"
Troubleshooting
Check Version
claude --version
View Logs
# Enable verbose logging
claude --verbose "task" 2>&1 | tee debug.log
Test Connection
# Simple test
claude -p "echo hello"
Clear Cache
# Remove session data
rm -rf ~/.claude-code/sessions/
Quick Reference Card
Most Used Flags:
-p # Headless mode
-y # Skip confirmations
-c # Continue conversation
--model opus # Use Opus model
--add-dir # Add directories
--verbose # Debug logging
--max-turns N # Limit turns
Permission Control:
--permission-mode plan # Review first
--allowedTools "Read" "Edit" # Whitelist
--disallowedTools "Bash(rm *)" # Blacklist
Output Formats:
--output-format text # Human readable
--output-format json # Structured
--output-format stream-json # Streaming
Resources
Official Docs: https://docs.claude.com/en/docs/claude-code
Best Practices: https://www.anthropic.com/engineering/claude-code-best-practices
Pro Tip: Combine flags for powerful workflows:
claude -y --model opus --add-dir ../shared --max-turns 5 \
--allowedTools "Read" "Edit" "Bash(git *)" \
"migrate entire codebase to TypeScript"

