Skip to main content

Command Palette

Search for a command to run...

Mastering Claude Code CLI:

Your Ultimate Command Guide

Updated
โ€ข8 min read
Mastering Claude Code CLI:
M

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, @git mentions

๐Ÿ›ก๏ธ 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

CommandDescriptionExample
claudeStart interactive REPLclaude
claude "query"Start REPL with initial promptclaude "explain this project"
claude -p "query"Query via SDK, then exit (headless)claude -p "explain this function"
`cat fileclaude -p "query"`Process piped content`cat logs.txtclaude -p "explain"`
claude -cContinue most recent conversationclaude -c
claude -c -p "query"Continue via SDKclaude -c -p "Check for type errors"
claude -r "<session-id>"Resume session by IDclaude -r "abc123" "Finish this PR"
claude updateUpdate to latest versionclaude update
claude mcpConfigure Model Context Protocol serversclaude mcp

Essential Flags

๐Ÿš€ Automation & Output

FlagDescriptionExample
-p, --printHeadless mode - execute and exitclaude -p "refactor this"
--output-formatOutput format: text, json, stream-jsonclaude -p "query" --output-format json
--input-formatInput format: text, stream-jsonclaude -p --input-format stream-json
--include-partial-messagesInclude partial messages in streaming outputclaude -p --output-format stream-json --include-partial-messages
--verboseEnable detailed loggingclaude --verbose

โš™๏ธ Model & Behavior

FlagDescriptionExample
--modelSpecify model (sonnet, opus, or full name)claude --model claude-sonnet-4-20250514
--max-turnsLimit conversation turnsclaude -p --max-turns 3 "query"
--append-system-promptAdd custom system instructionsclaude --append-system-prompt "Be concise"

๐Ÿ“ Working Directories

FlagDescriptionExample
--add-dirAdd additional working directoriesclaude --add-dir ../apps ../lib

๐Ÿ” Permissions & Safety

FlagDescriptionExample
--permission-modePermission mode: auto, plan, promptclaude --permission-mode plan
--dangerously-skip-permissionsSkip ALL permission prompts (YOLO mode)claude --dangerously-skip-permissions
-yShort alias for skip permissionsclaude -y "task"
--permission-prompt-toolAlways prompt for specific toolclaude --permission-prompt-tool mcp_auth_tool
--allowedToolsWhitelist tools to auto-approveclaude --allowedTools "Bash(git *)" "Read"
--disallowedToolsBlacklist tools to always blockclaude --disallowedTools "Bash(rm *)" "Edit"

๐Ÿ”„ Session Management

FlagDescriptionExample
-c, --continueContinue most recent conversationclaude -c
-r, --resumeResume specific session by IDclaude --resume abc123

Interactive Slash Commands

Use these inside the Claude Code REPL:

CommandDescription
/helpShow help information
/exitExit Claude Code
/clearClear conversation history
/resetReset to new conversation
/modelSwitch model
/undoUndo last action
/retryRetry last message
/logoutLogout from Claude
/copyCopy last response
/historyShow conversation history
/searchSearch conversation

@ Mentions

Reference context in your prompts:

MentionDescriptionExample
@filenameReference specific file@app.py explain this
@folder/Reference entire folder@src/ refactor all files
@urlReference URL content@https://example.com summarize
@gitReference git context@git review recent commits
@searchSearch 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

ModeDescriptionUse Case
autoAuto-approve safe operationsDefault, balanced
planShow plan before executingReview changes first
promptPrompt for every actionMaximum control
--dangerously-skip-permissionsSkip all promptsAutomation, 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


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"
Mastering Claude Code CLI: