Skip to content

Conversation

@Ahmed-AdelB
Copy link

Summary

This PR adds shell completion support to the OpenAI CLI, addressing issue #843.

Features

  • Bash completion: Tab completion with compgen and bash-completion library
  • Zsh completion: Native _arguments and _describe completion framework
  • Fish completion: Declarative completions with conditions
  • PowerShell completion: Register-ArgumentCompleter pattern

Usage

# Bash - add to ~/.bashrc
eval "$(openai completion bash)"

# Zsh - add to ~/.zshrc
eval "$(openai completion zsh)"

# Fish - save to completions directory
openai completion fish > ~/.config/fish/completions/openai.fish

# PowerShell - add to $PROFILE
. (openai completion powershell | Out-String)

Commands Supported

  • Main commands: api, tools, completion
  • API subcommands: chat.completions.create, models.list, models.retrieve, files.*, audio.*, images.*, embeddings.create, moderations.create
  • Tools subcommands: fine_tunes.prepare_data
  • Global options: -v, -k, -o, -t, --api-key, --organization, etc.

Implementation Notes

  1. No external dependencies: Uses native shell completion primitives rather than libraries like argcomplete
  2. Security: All completion values are hardcoded static strings - no dynamic content or user input processing
  3. Minimal changes: Only adds one new file (_completion.py) and minimal modifications to _cli.py

Changes

  • src/openai/cli/_completion.py (new): Shell completion script generator
  • src/openai/cli/_cli.py: Added import and registration for completion subcommand

Test Plan

  • Generated bash completion script and verified syntax
  • Generated zsh completion script and verified syntax
  • Generated fish completion script and verified syntax
  • Generated PowerShell completion script and verified syntax
  • Verified all API commands are included
  • Verified all global options are included

Fixes #843


Author: Ahmed Adel Bakr Alderai

Add shell completion script generator for bash, zsh, fish, and PowerShell.

Usage:
- Bash: eval "$(openai completion bash)"
- Zsh: eval "$(openai completion zsh)"
- Fish: openai completion fish > ~/.config/fish/completions/openai.fish
- PowerShell: . (openai completion powershell | Out-String)

This adds a new `completion` subcommand that generates shell-specific
completion scripts. The scripts provide tab completion for:
- Main commands (api, tools, completion)
- API subcommands (chat.completions.create, models.list, etc.)
- Global options (-v, -k, -o, --api-key, etc.)

Fixes openai#843

Author: Ahmed Adel Bakr Alderai
@Ahmed-AdelB Ahmed-AdelB requested a review from a team as a code owner December 31, 2025 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add shell auto completion for different shell, like bash, zsh, fish, powershell

1 participant