feat: add shell auto-completion for bash, zsh, fish, powershell#3370
feat: add shell auto-completion for bash, zsh, fish, powershell#3370Oxygen56 wants to merge 1 commit into
Conversation
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>
There was a problem hiding this comment.
💡 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".
| # eval "$(openai completion -s bash)" | ||
|
|
||
| _openai_completion() { | ||
| local cur prev words cword |
There was a problem hiding this comment.
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 👍 / 👎.
| set -l args (commandline -opc) | ||
| set -l current (commandline -ct) | ||
| set -l completions (OPENAI_COMPLETE=fish_source openai $args 2>/dev/null) |
There was a problem hiding this comment.
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 👍 / 👎.
Adds shell completion support via 'openai completion' subcommand.
What this does
openai completionsubcommand that generates shell completion scripts for bash, zsh, fish, and PowerShelleval "$(openai completion -s <shell>)"or saving the output to their shell configUsage
Files changed
src/openai/cli/__init__.py— CLI package entry pointsrc/openai/cli/_cli.py— argparse-based CLI with completion subcommand and dynamic completion source handlerssrc/openai/__main__.py— enablespython -m openaipyproject.toml— adds[project.scripts]entry point for theopenaicommandtests/test_cli.py— tests for the completion subcommandCloses #843