feat(cli): add shell completion support for bash, zsh, fish, powershell#3198
feat(cli): add shell completion support for bash, zsh, fish, powershell#3198simer11-jing wants to merge 2 commits intoopenai:mainfrom
Conversation
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>
There was a problem hiding this comment.
💡 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".
| case ${words[1]} in | ||
| api) | ||
| # API subcommands | ||
| local api_commands="chat completions models files fine-tuning embeddings audio images moderations assistants threads runs messages" |
There was a problem hiding this comment.
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 👍 / 👎.
| ;; | ||
| tools) | ||
| # Tools subcommands | ||
| local tools_commands="" |
There was a problem hiding this comment.
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 👍 / 👎.
|
|
||
| $commandElements = $commandAst.CommandElements | ||
| $command = @( | ||
| 'complete' |
There was a problem hiding this comment.
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>
Summary
Adds shell completion support for the
openaiCLI, addressing #843.Supported shells:
Usage
Implementation
src/openai/cli/_completion.pywith completion script templatessrc/openai/cli/_cli.pyto register thecompletesubcommandapi,tools,complete-v,-b,-k,-p, etc.Testing
Tested manually with:
Fixes #843
🤖 Generated with Claude Code