diff --git a/README.md b/README.md index 3c103f036c..d8bd704cd2 100644 --- a/README.md +++ b/README.md @@ -779,6 +779,36 @@ In addition to the options provided in the base `OpenAI` client, the following o An example of using the client with Microsoft Entra ID (formerly known as Azure Active Directory) can be found [here](https://github.com/openai/openai-python/blob/main/examples/azure_ad.py). +## Activating CLI Autocomplete +Use the following commands to activate CLI autocompletion for each shell. + +Install `openai` with `[cli]` option to install argcomplete. +```shell +pip install openai[cli] +``` + +### Argument Completion Activation +See argcomplete's [documentation](https://github.com/kislyuk/argcomplete/tree/develop?tab=readme-ov-file#synopsis) for temporary Bash/Zsh setup. +See argcomplete's [contributing documentation](https://github.com/kislyuk/argcomplete/tree/develop/contrib) for Fish and Powershell setup. + +#### Permanent Argument Completion Setup for Bash/Zsh +Alternatively, run this command to register `openai` for argument completion permanently, eliminating the need to run the `eval` command each time the terminal is restarted or killed. +For Bash: +```shell +register-python-argcomplete openai >> ~/.bashrc +``` +For Zsh: +```shell +register-python-argcomplete openai >> ~/.zshrc +``` + +### Usage +After completing the appropriate steps for your shell, try the following action: +>_If you have activated permanent argument completion, restart your terminal before proceeding._ +```shell +openai +``` + ## Versioning This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions: diff --git a/pyproject.toml b/pyproject.toml index 7cd583da3f..47fddd3672 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,6 +45,7 @@ openai = "openai.cli:main" [project.optional-dependencies] realtime = ["websockets >= 13, < 15"] datalib = ["numpy >= 1", "pandas >= 1.2.3", "pandas-stubs >= 1.1.0.11"] +cli = ["argcomplete >= 1.12.0"] [tool.rye] managed = true diff --git a/src/openai/cli/_cli.py b/src/openai/cli/_cli.py index fd165f48ab..cc065a342b 100644 --- a/src/openai/cli/_cli.py +++ b/src/openai/cli/_cli.py @@ -1,3 +1,5 @@ +#!usr/bin/env python +# PYTHON_ARGCOMPLETE_OK from __future__ import annotations import sys @@ -161,6 +163,11 @@ def _parse_args(parser: argparse.ArgumentParser) -> tuple[argparse.Namespace, Ar def _main() -> None: parser = _build_parser() + try: + import argcomplete + argcomplete.autocomplete(parser) + except ImportError: + pass parsed, args, unknown = _parse_args(parser) if args.verbosity != 0: