Cody CLI
Access Cody's AI capabilities directly from your terminal. Leverage powerful code assistance and automation through simple commands.
Cody CLI is currently in experimental stage. Features and functionality may change.
Installation
Install Cody CLI using npm:
npm install -g @sourcegraph/cody
Run cody help
to confirm successful installation
Authentication
Choose your preferred authentication method:
Using Cody CLI
Start interacting with Cody through simple commands:
Basic Chat
Ask questions or get explanations:
cody chat "Explain how promises work in JavaScript"
File Context
Get insights about specific files:
cody chat --context-file main.py "What could be improved?"
Git Integration
Review changes and generate commit messages:
git diff | cody chat "Write a commit message"
Standard Input
Process input from other commands:
echo "What is this code doing?" | cody chat --stdin
Programmatic Usage
Using Cody CLI in JavaScript
Integrate Cody CLI into your Node.js applications:
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
});
}
}
Advanced Usage
Automation Examples
Integrate Cody CLI into your development workflow with these examples:
#!/bin/sh
# .git/hooks/pre-commit
git diff --cached | cody chat "Review this code for security issues" --exit-code
#!/bin/bash
# review.sh
for file in $(git diff --name-only); do
cody chat --context-file "$file" "Review this file for best practices"
done
Troubleshooting
Common solutions for CLI issues:
If you encounter authentication problems, try:
cody auth logout
Then authenticate again using:
cody auth login --web
Get detailed information about any command:
cody help [command]