diff --git a/README.md b/README.md index 4d47f0c..73ff589 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Elegant CLI Builder - Lazy and Async commands - Pluggable and composable API - Auto generated usage and help +- Shell autocompletion support via [tab](https://github.com/bombshell-dev/tab) integration 🚧 This project is under heavy development. More features are coming soon! @@ -104,6 +105,91 @@ Renders command usage to a string value. Renders usage and prints to the console +## Autocompletions + +Citty works with [tab](https://github.com/bombshell-dev/tab) to provide shell autocompletions across `zsh`, `bash`, `fish`, and `powershell`. + +```sh +npm install @bomb.sh/tab +``` + +Add autocompletions to your citty CLI: + +```ts +import { defineCommand, createMain } from "citty"; +import tab from "@bomb.sh/tab/citty"; + +const main = defineCommand({ + meta: { + name: "my-cli", + description: "My CLI tool", + }, + subCommands: { + dev: defineCommand({ + meta: { + name: "dev", + description: "Start dev server", + }, + args: { + port: { + type: "string", + description: "Specify port", + }, + host: { + type: "string", + description: "Specify host", + }, + }, + }), + }, +}); + +const completion = await tab(main); + +// Add custom completions for option values +const devCommand = completion.commands.get("dev"); +const portOption = devCommand.options.get("port"); +if (portOption) { + portOption.handler = (complete) => { + complete("3000", "Development port"); + complete("8080", "Production port"); + }; +} + +const cli = createMain(main); +cli(); +``` + +### Testing Completions + +Test your completions locally: + +```sh +node my-cli.js complete -- dev --port= +# Output: --port=3000 Development port +# --port=8080 Production port +``` + +### Installing Completions + +For end users, completions can be installed: + +```sh +# One-time setup (zsh example) +source <(my-cli complete zsh) + +# Permanent setup +my-cli complete zsh > ~/.my-cli-completion.zsh +echo 'source ~/.my-cli-completion.zsh' >> ~/.zshrc +``` + +### Package Manager Completions + +When users install tab, they also get autocompletions for package managers! Tab provides completions for `pnpm`, `npm`, `yarn`, and `bun` out of the box. +This means your users will enjoy autocompletions not only for your citty CLI but also for their everyday package manager commands! + +Learn more about tab at [github.com/bombshell-dev/tab](https://github.com/bombshell-dev/tab). + ## Development - Clone this repository