Command Line Interface Experimental

Cody CLI

Access Cody's AI capabilities directly from your terminal. Leverage powerful code assistance and automation through simple commands.

Setup Time: ~2 minutes
Latest Version: v0.8.0
For: Free, Pro, Enterprise
Experimental Feature

Cody CLI is currently in experimental stage. Features and functionality may change.

1

Installation

Install Cody CLI using npm:

Terminal
npm install -g @sourcegraph/cody
Verify Installation

Run cody help to confirm successful installation

2

Authentication

Choose your preferred authentication method:

Browser Authentication (Recommended)

Quick and secure authentication through your web browser:

Browser Auth
cody auth login --web

Token Authentication

Use environment variables for automated workflows:

Environment Variables
export SRC_ENDPOINT="https://sourcegraph.com" export SRC_ACCESS_TOKEN="your_access_token"
3

Using Cody CLI

Start interacting with Cody through simple commands:

Basic Chat

Ask questions or get explanations:

Chat Command
cody chat "Explain how promises work in JavaScript"

File Context

Get insights about specific files:

File Analysis
cody chat --context-file main.py "What could be improved?"

Git Integration

Review changes and generate commit messages:

Git Integration
git diff | cody chat "Write a commit message"

Standard Input

Process input from other commands:

Pipe Input
echo "What is this code doing?" | cody chat --stdin
4

Programmatic Usage

API-like Usage

Using Cody CLI in JavaScript

Integrate Cody CLI into your Node.js applications:

JavaScript Integration
const util = require('util');
const exec = util.promisify(require('child_process').exec);

class CodyClient {
    constructor(options = {}) {
        this.format = options.format || 'text';
    }

    async chat(message, options = {}) {
        const args = ['cody', 'chat'];
        if (this.format === 'json') {
            args.push('-f', 'json');
        }
        if (options.contextFile) {
            args.push('--context-file', options.contextFile);
        }
        args.push('-m', message);
        const { stdout } = await exec(args.join(' '));
        return this.format === 'json' ? JSON.parse(stdout) : stdout;
    }

    async reviewCode(filePath) {
        return this.chat('Review this code for improvements', {
            contextFile: filePath
        });
    }

    async explainCode(filePath) {
        return this.chat('Explain what this code does', {
            contextFile: filePath
        });
    }
}
5

Advanced Usage

Code Examples

Automation Examples

Integrate Cody CLI into your development workflow with these examples:

Pre-commit Hook
#!/bin/sh # .git/hooks/pre-commit git diff --cached | cody chat "Review this code for security issues" --exit-code
Code Review Script
#!/bin/bash # review.sh for file in $(git diff --name-only); do cody chat --context-file "$file" "Review this file for best practices" done
6

Troubleshooting

Common solutions for CLI issues:

Authentication Issues

If you encounter authentication problems, try:

cody auth logout

Then authenticate again using:

cody auth login --web
Command Help

Get detailed information about any command:

cody help [command]