Shell Completion
pok generates shell completion scripts for bash, zsh, and fish. The completion command returns command names, subcommands, flags, and enum or boolean flag values for the current argument position.
Installation
Generate and install the completion script for your shell:
Bash
Add to your ~/.bashrc or ~/.bash_profile:
source <(mycli completion bash)Then reload your shell:
source ~/.bashrcZsh
Add to your ~/.zshrc:
source <(mycli completion zsh)Then reload your shell:
source ~/.zshrcFish
Run once to install:
mycli completion fish > ~/.config/fish/completions/mycli.fishInteractive use
Once installed, press Tab to complete:
# Complete commands
mycli d<Tab>
# → deploy, dev, db
# Complete subcommands
mycli db <Tab>
# → migrate, seed, reset
# Complete flags
mycli deploy --<Tab>
# → --env, --dry-run, --help
# Complete flag values (for enums)
mycli deploy --env <Tab>
# → dev, staging, prodCompletion protocol
pok uses a hidden __complete command to generate completions dynamically. When you press Tab, your shell calls:
mycli __complete <current-args>The CLI returns matching completions from:
- Commands: available commands at the current level.
- Aliases: command aliases defined on the current command.
- Flags: context flags defined on the command.
- Flag values: enum choices and boolean values.
Completion surfaces
Command Completions
Top-level and nested commands are completed:
mycli <Tab> # Shows: build, deploy, db, dev
mycli db <Tab> # Shows: migrate, seed, reset, allAlias Completions
Command aliases appear alongside command names:
// commands/deploy.ts
export const command = defineCommand({
label: 'Deploy to environment',
aliases: ['d', 'dep'],
// ...
});mycli <Tab> # Shows: deploy, d, dep, build, ...Flag Completions
Flags are completed when typing --:
mycli deploy --<Tab> # Shows: --env, --dry-run, --verboseFlag Value Completions
For enum and boolean flags, possible values are suggested:
context: {
env: {
from: 'flag',
schema: z.enum(['dev', 'staging', 'prod']),
},
dryRun: {
from: 'flag',
schema: z.boolean().default(false),
},
}mycli deploy --env <Tab> # Shows: dev, staging, prod
mycli deploy --dry-run <Tab> # Shows: true, falseExplicit Choices
You can provide explicit choices that override schema inference:
context: {
region: {
from: 'flag',
schema: z.string(),
choices: ['us-east-1', 'us-west-2', 'eu-west-1'],
},
}Detecting the Current Shell
pok can detect your shell from the $SHELL environment variable:
import { detectShell } from '@pokit/core';
const shell = detectShell(); // 'bash' | 'zsh' | 'fish'Programmatic Script Generation
You can generate completion scripts programmatically:
import { generateCompletionScript } from '@pokit/core';
const bashScript = generateCompletionScript('mycli', 'bash');
const zshScript = generateCompletionScript('mycli', 'zsh');
const fishScript = generateCompletionScript('mycli', 'fish');Troubleshooting
Completions not working
- Ensure the completion script is sourced in your shell config
- Open a new terminal or reload your shell config
- Verify with:
type _mycli_completions(bash) orwhich _mycli(zsh)
Stale completions
Completions are generated dynamically, so they always reflect the current command structure. If completions seem stale, try:
# Reload shell config
source ~/.bashrc # or ~/.zshrcFish completions not updating
For fish, regenerate the completions file:
mycli completion fish > ~/.config/fish/completions/mycli.fish