Skip to content

Commit a75179d

Browse files
committed
docs
1 parent 08d634a commit a75179d

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

README.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Tools like git and their autocompletion experience inspired us to build this too
99
```ts
1010
import { Completion, script } from "@bombsh/tab";
1111

12-
const name = "vite"
12+
const name = "my-cli"
1313
const completion = new Completion()
1414

1515
completion.addCommand("start", "Start the application", async (previousArgs, toComplete, endsWithSpace) => {
@@ -48,7 +48,7 @@ if (process.argv[2] === "--") {
4848
}
4949
```
5050

51-
Now your user can run `source <(vite complete zsh)` and they will get completions for the `vite` command using the [autocompletion server](#autocompletion-server).
51+
Now your user can run `source <(my-cli complete zsh)` and they will get completions for the `my-cli` command using the [autocompletion server](#autocompletion-server).
5252

5353
## Adapters
5454

@@ -60,7 +60,7 @@ Since we are heavy users of tools like `cac` and `citty`, we have created adapte
6060
import cac from "cac";
6161
import tab from "@bombsh/tab/cac";
6262

63-
const cli = cac("vite");
63+
const cli = cac("my-cli");
6464

6565
cli
6666
.command("dev", "Start dev server")
@@ -82,7 +82,7 @@ portOptionCompletion.handler = async (previousArgs, toComplete, endsWithSpace) =
8282

8383
cli.parse();
8484
```
85-
Now autocompletion will be available for any specified command and option in your cac instance. If your user writes `vite dev --po`, they will get suggestions for the `--port` option. Or if they write `vite d` they will get suggestions for the `dev` command.
85+
Now autocompletion will be available for any specified command and option in your cac instance. If your user writes `my-cli dev --po`, they will get suggestions for the `--port` option. Or if they write `my-cli d` they will get suggestions for the `dev` command.
8686

8787
Suggestions are missing in the adapters since yet cac or citty do not have a way to provide suggestions (tab just came out!), we'd have to provide them manually. Mutations do not hurt in this situation.
8888

@@ -94,8 +94,8 @@ import tab from "@bombsh/tab/citty";
9494

9595
const main = defineCommand({
9696
meta: {
97-
name: "vite",
98-
description: "Vite CLI tool",
97+
name: "my-cli",
98+
description: "My CLI tool",
9999
},
100100
});
101101

@@ -131,19 +131,30 @@ const cli = createMain(main);
131131
cli();
132132
```
133133

134+
## Recipe
135+
136+
`source <(my-cli complete zsh)` won't be enough since the user would have to run this command each time they spin up a new shell instance.
137+
138+
We suggest this approach for the end user that you as a maintainer might want to push.
139+
140+
```
141+
my-cli completion zsh > ~/completion-for-my-cli.zsh
142+
echo 'source ~/completion-for-my-cli.zsh' >> ~/.zshrc
143+
```
144+
134145
## Autocompletion Server
135146

136147
By integrating tab into your cli, your cli would have a new command called `complete`. This is where all the magic happens. And the shell would contact this command to get completions. That's why we call it the autocompletion server.
137148

138149
```zsh
139-
vite complete -- --po
150+
my-cli complete -- --po
140151
--port Specify the port number
141152
:0
142153
```
143154

144155
The autocompletion server can be a standard to identify whether a package provides autocompletions. Whether running `tool complete --` would result in an output that ends with `:{Number}` (matching the pattern `/:\d+$/`).
145156

146-
In situations like `vite dev --po` you'd have autocompletions! But in the case of `pnpm vite dev --po` which is what most of us use, tab does not inject autocompletions for a tool like pnpm.
157+
In situations like `my-cli dev --po` you'd have autocompletions! But in the case of `pnpm my-cli dev --po` which is what most of us use, tab does not inject autocompletions for a tool like pnpm.
147158

148159
Since pnpm already has its own autocompletion [script](https://pnpm.io/completion), this provides the opportunity to check whether a package provides autocompletions and use those autocompletions if available.
149160

@@ -153,8 +164,7 @@ Other package managers like `npm` and `yarn` can decide whether to support this
153164

154165
## Inspiration
155166
- git
156-
- [cobra](https://github.com/spf13/cobra/blob/main/shell_completions.go)
157-
167+
- [cobra](https://github.com/spf13/cobra/blob/main/shell_completions.go), without cobra, tab would have took 10x longer to build
158168

159169
## TODO
160170
- [] fish

0 commit comments

Comments
 (0)