Skip to content

feat: add shell auto-completion for bash, zsh, fish, powershell#3370

Open
Oxygen56 wants to merge 1 commit into
openai:mainfrom
Oxygen56:feat/shell-completion-843
Open

feat: add shell auto-completion for bash, zsh, fish, powershell#3370
Oxygen56 wants to merge 1 commit into
openai:mainfrom
Oxygen56:feat/shell-completion-843

Conversation

@Oxygen56
Copy link
Copy Markdown

@Oxygen56 Oxygen56 commented Jun 5, 2026

Adds shell completion support via 'openai completion' subcommand.

What this does

  • Adds an openai completion subcommand that generates shell completion scripts for bash, zsh, fish, and PowerShell
  • Completion scripts provide context-aware tab completions by calling the CLI itself as a completion source
  • Users install by running eval "$(openai completion -s <shell>)" or saving the output to their shell config

Usage

# Bash
eval "$(openai completion -s bash)"

# Zsh
eval "$(openai completion -s zsh)"

# Fish
openai completion -s fish > ~/.config/fish/completions/openai.fish

# PowerShell
openai completion -s powershell | Out-String | Invoke-Expression

Files changed

  • src/openai/cli/__init__.py — CLI package entry point
  • src/openai/cli/_cli.py — argparse-based CLI with completion subcommand and dynamic completion source handlers
  • src/openai/__main__.py — enables python -m openai
  • pyproject.toml — adds [project.scripts] entry point for the openai command
  • tests/test_cli.py — tests for the completion subcommand

Closes #843

Adds 'openai completion' subcommand that generates shell completion
scripts for supported shells.

Closes openai#843

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@Oxygen56 Oxygen56 requested a review from a team as a code owner June 5, 2026 16:54
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f5caea9929

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/openai/cli/_cli.py
# eval "$(openai completion -s bash)"

_openai_completion() {
local cur prev words cword
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove the bash-completion-only helper call

In vanilla bash environments where the separate bash-completion package has not been loaded (for example a default macOS shell or a minimal container), _init_completion is not defined, so this line returns before the function ever invokes the CLI. The generated setup instructions only ask users to eval "$(openai completion -s bash)", so those users get no bash completions at all unless they happen to have sourced bash-completion separately.

Useful? React with 👍 / 👎.

Comment thread src/openai/cli/_cli.py
Comment on lines +63 to +65
set -l args (commandline -opc)
set -l current (commandline -ct)
set -l completions (OPENAI_COMPLETE=fish_source openai $args 2>/dev/null)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Drop the command name before fish completion lookup

fish documents commandline -opc as the current process tokenized up to the cursor, which includes the command itself. Passing $args unchanged means openai <TAB> calls the helper as openai openai, and _handle_fish_source treats openai as the current word and filters out every top-level completion; this makes fish completions empty at the first argument. The script needs to remove the first token before invoking the Python completion source, as the bash path does with COMP_WORDS[@]:1.

Useful? React with 👍 / 👎.

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