Skip to content

feat(cli): add shell completion support for bash, zsh, fish, powershell#3198

Open
simer11-jing wants to merge 2 commits intoopenai:mainfrom
simer11-jing:feature/add-shell-completion
Open

feat(cli): add shell completion support for bash, zsh, fish, powershell#3198
simer11-jing wants to merge 2 commits intoopenai:mainfrom
simer11-jing:feature/add-shell-completion

Conversation

@simer11-jing
Copy link
Copy Markdown

Summary

Adds shell completion support for the openai CLI, addressing #843.

Supported shells:

  • bash
  • zsh
  • fish
  • powershell

Usage

# bash - add to ~/.bashrc
source <(openai complete bash)

# zsh - add to ~/.zshrc
source <(openai complete zsh)

# fish - add to ~/.config/fish/config.fish
openai complete fish | source

# powershell - add to $PROFILE
openai complete powershell | Out-String | Invoke-Expression

Implementation

  • Added src/openai/cli/_completion.py with completion script templates
  • Modified src/openai/cli/_cli.py to register the complete subcommand
  • Completion scripts include support for:
    • Main commands: api, tools, complete
    • Global options: -v, -b, -k, -p, etc.
    • Subcommand options

Testing

Tested manually with:

python -m openai complete bash  # outputs bash completion script
python -m openai complete zsh   # outputs zsh completion script
python -m openai complete fish  # outputs fish completion script
python -m openai complete powershell  # outputs powershell completion script

Fixes #843

🤖 Generated with Claude Code

Add `openai complete` command to generate shell completion scripts for:
- bash
- zsh
- fish
- powershell

Usage:
  # bash
  source <(openai complete bash)

  # zsh
  source <(openai complete zsh)

  # fish
  openai complete fish | source

  # powershell
  openai complete powershell | Out-String | Invoke-Expression

Fixes openai#843

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@simer11-jing simer11-jing requested a review from a team as a code owner May 3, 2026 14:20
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: 92f9f91452

ℹ️ 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/_completion.py Outdated
case ${words[1]} in
api)
# API subcommands
local api_commands="chat completions models files fine-tuning embeddings audio images moderations assistants threads runs messages"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Complete api with real parser command names

This completion list proposes tokens like chat, models, and files, but the CLI parser actually registers full subcommand names such as chat.completions.create, models.list, and files.create (via add_parser(...) in src/openai/cli/_api/*). In bash, accepting one of these suggestions produces an invalid command that argparse rejects, so tab completion for the primary openai api ... workflow is misleading and often unusable.

Useful? React with 👍 / 👎.

Comment thread src/openai/cli/_completion.py Outdated
;;
tools)
# Tools subcommands
local tools_commands=""
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 Populate tools completions instead of empty list

The bash script sets tools_commands to an empty string, so openai tools <TAB> never suggests any valid subcommands even though the CLI registers fine_tunes.prepare_data under tools (src/openai/cli/_tools/fine_tunes.py). This makes completion incomplete for an existing command path and degrades discoverability of supported tool operations.

Useful? React with 👍 / 👎.

Comment thread src/openai/cli/_completion.py Outdated

$commandElements = $commandAst.CommandElements
$command = @(
'complete'
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Detect PowerShell complete subcommand correctly

The PowerShell completer prepends a literal 'complete' to $command before appending parsed arguments, so when users type openai complete ... the computed value becomes complete;complete (or longer) and never equals 'complete'. As a result, the shell-name completions branch is not entered and openai complete <TAB> returns top-level command suggestions instead of bash|zsh|fish|powershell.

Useful? React with 👍 / 👎.

- Fix api subcommands to use correct full names (e.g., chat.completions.create)
- Add tools subcommands (fine_tunes.prepare_data, migrate, grit)
- Fix PowerShell complete subcommand detection logic

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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