Add shell autocompletion to CLI tools
✓Works with OpenClaudeYou are a CLI tool developer. The user wants to add shell autocompletion (bash, zsh, fish) to their command-line application.
What to check first
- Run
echo $SHELLto identify the user's current shell - Check if your CLI tool is installed globally or available in PATH via
which <tool-name> - Verify Node.js version supports
process.argvandprocess.env.COMP_*variables (Node 12+)
Steps
- Create a completion script generator function that reads
COMP_WORDS,COMP_CWORD, andCOMP_LINEenvironment variables (bash) or equivalent (zsh/fish) - Parse the current command line input to determine what completions to suggest based on subcommands, flags, and arguments
- Implement a
--completionorcompletionsubcommand that outputs completion options line-by-line to stdout - Register completion script in shell config files:
~/.bashrc(bash),~/.zshrc(zsh), or~/.config/fish/config.fish(fish) - Use
sourcedirective orevalto load completion function on shell startup - Test with TAB key by typing partial command and pressing TAB twice
- For npm packages, add
postinstallscript to automatically register completions or use completion libraries likeocliforyargs-completion
Code
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
// Main CLI with completion support
const cli = {
commands: ['start', 'stop', 'status', 'deploy', 'logs'],
flags: ['--help', '--version', '--config', '--verbose', '--dry-run'],
getCompletions(line) {
const parts = line.trim().split(/\s+/);
const current = parts[parts.length - 1] || '';
const previous = parts[parts.length - 2];
// If typing a flag
if (current.startsWith('-')) {
return this.flags.filter(f => f.startsWith(current));
}
// If typing a command
if (parts.length === 1 || !this.commands.includes(parts[0])) {
return this.commands.filter(c => c.startsWith(current));
}
// Context-aware completions based on command
if (previous === 'deploy') {
return ['staging', 'production'].filter(e => e.startsWith(current));
}
if (previous === '--config') {
const configDir = path.join(process.env.HOME, '.config');
try {
return fs.readdirSync(configDir).filter(f => f.startsWith(current));
} catch (e) {
return [];
}
}
return [];
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related CLI Tools Skills
Other Claude Code skills in the same category — free to download.
CLI App Builder
Build CLI applications with Commander.js
Interactive CLI
Create interactive CLI prompts (Inquirer.js)
CLI Progress Bar
Add progress bars and spinners to CLI
CLI Config Manager
Build CLI configuration management
CLI Help Generator
Generate help text and man pages
CLI Testing
Test CLI applications
CLI Packaging
Package CLI for npm/brew distribution
CLI Color Output
Add colored and formatted CLI output
Want a CLI Tools skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.