Skip to content

Commit b69ff49

Browse files
authored
feat: Add documentation for Tab (#17)
1 parent 255c6ea commit b69ff49

File tree

7 files changed

+1675
-1
lines changed

7 files changed

+1675
-1
lines changed

astro.config.mjs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,18 @@ export default defineConfig({
101101
link: "args/api",
102102
},
103103
],
104-
}
104+
},
105+
{
106+
label: "Tab",
107+
id: "tab",
108+
icon: "right-caret",
109+
link: "/tab",
110+
items: [
111+
{ label: "Basics", autogenerate: { directory: "tab/basics" } },
112+
{ label: "API", autogenerate: { directory: "tab/api" } },
113+
{ label: "Guides", autogenerate: { directory: "tab/guides" } },
114+
]
115+
},
105116
]),
106117
],
107118
}),

src/content/docs/tab/api/core.mdx

Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
---
2+
title: Core API
3+
description: Complete API reference for the Tab core package
4+
---
5+
6+
This page provides the complete API reference for the Tab core package, including all classes, methods, types, and interfaces.
7+
8+
## Classes
9+
10+
### Completion
11+
12+
The main class for managing command and option completions.
13+
14+
```ts
15+
import { Completion } from '@bombsh/tab';
16+
17+
const completion = new Completion();
18+
```
19+
20+
#### Methods
21+
22+
##### addCommand
23+
24+
Adds a completion handler for a command.
25+
26+
**Parameters:**
27+
- `name` (string): The command name
28+
- `description` (string): Command description
29+
- `args` (boolean[]): Array indicating which arguments are required (false) or optional (true)
30+
- `handler` (Handler): Function that returns completion suggestions
31+
- `parent` (string, optional): Parent command name for nested commands
32+
33+
**Returns:** string - The command key
34+
35+
**Example:**
36+
```ts
37+
// Command with required argument: "vite <entry>"
38+
completion.addCommand('dev', 'Start development server', [false], async () => {
39+
return [
40+
{ value: 'src/main.ts', description: 'Main entry point' },
41+
{ value: 'src/index.ts', description: 'Index entry point' },
42+
];
43+
});
44+
45+
// Command with optional argument: "vite [entry]"
46+
completion.addCommand('serve', 'Start the server', [true], async () => {
47+
return [
48+
{ value: 'dist', description: 'Distribution directory' },
49+
];
50+
});
51+
52+
// Nested command: "vite dev build"
53+
completion.addCommand('build', 'Build project', [], async () => {
54+
return [
55+
{ value: 'build', description: 'Build command' },
56+
];
57+
}, 'dev');
58+
```
59+
60+
##### addOption
61+
62+
Adds a completion handler for an option of a specific command.
63+
64+
**Parameters:**
65+
- `command` (string): The command name
66+
- `option` (string): The option name (e.g., '--port')
67+
- `description` (string): Option description
68+
- `handler` (Handler): Function that returns completion suggestions
69+
- `alias` (string, optional): Short flag alias (e.g., 'p' for '--port')
70+
71+
**Returns:** string - The option name
72+
73+
**Example:**
74+
```ts
75+
completion.addOption('dev', '--port', 'Port number', async () => {
76+
return [
77+
{ value: '3000', description: 'Development port' },
78+
{ value: '8080', description: 'Production port' },
79+
];
80+
}, 'p');
81+
```
82+
83+
##### parse
84+
85+
Parses command line arguments and outputs completion suggestions to stdout.
86+
87+
**Parameters:**
88+
- `args` (string[]): Command line arguments
89+
90+
**Example:**
91+
```ts
92+
await completion.parse(['--port']);
93+
// Outputs completion suggestions to stdout
94+
```
95+
96+
### script
97+
98+
Generates shell completion scripts.
99+
100+
**Parameters:**
101+
- `shell` (string): Target shell ('zsh', 'bash', 'fish', 'powershell')
102+
- `name` (string): CLI tool name
103+
- `execPath` (string): Executable path for the CLI tool
104+
105+
**Example:**
106+
```ts
107+
import { script } from '@bombsh/tab';
108+
109+
script('zsh', 'my-cli', '/usr/bin/node /path/to/my-cli');
110+
```
111+
112+
## Types
113+
114+
### Handler
115+
116+
Function type for completion handlers.
117+
118+
```ts
119+
type Handler = (
120+
previousArgs: string[],
121+
toComplete: string,
122+
endsWithSpace: boolean
123+
) => Item[] | Promise<Item[]>;
124+
```
125+
126+
**Parameters:**
127+
- `previousArgs` (string[]): Previously typed arguments
128+
- `toComplete` (string): The text being completed
129+
- `endsWithSpace` (boolean): Whether the input ends with a space
130+
131+
**Returns:** Item[] | Promise&lt;Item[]&gt;
132+
133+
**Example:**
134+
```ts
135+
const handler: Handler = async (previousArgs, toComplete, endsWithSpace) => {
136+
// Check if user is typing 'prod' to suggest production
137+
if (toComplete.startsWith('prod')) {
138+
return [
139+
{ value: 'production', description: 'Production environment' },
140+
];
141+
}
142+
143+
return [
144+
{ value: 'development', description: 'Development environment' },
145+
{ value: 'staging', description: 'Staging environment' },
146+
{ value: 'production', description: 'Production environment' },
147+
];
148+
};
149+
```
150+
151+
### Item
152+
153+
Object representing a completion suggestion.
154+
155+
```ts
156+
type Item = {
157+
value: string;
158+
description: string;
159+
};
160+
```
161+
162+
**Properties:**
163+
- `value` (string): The completion value
164+
- `description` (string): Description of the completion
165+
166+
**Example:**
167+
```ts
168+
const item: Item = {
169+
value: '3000',
170+
description: 'Development port'
171+
};
172+
```
173+
174+
### `Positional`
175+
176+
Type for positional argument configuration.
177+
178+
```ts
179+
type Positional = {
180+
required: boolean;
181+
variadic: boolean;
182+
completion: Handler;
183+
};
184+
```
185+
186+
## Constants
187+
188+
### ShellCompRequestCmd
189+
190+
The name of the hidden command used to request completion results.
191+
192+
```ts
193+
export const ShellCompRequestCmd: string = '__complete';
194+
```
195+
196+
### ShellCompNoDescRequestCmd
197+
198+
The name of the hidden command used to request completion results without descriptions.
199+
200+
```ts
201+
export const ShellCompNoDescRequestCmd: string = '__completeNoDesc';
202+
```
203+
204+
### ShellCompDirective
205+
206+
Bit map representing different behaviors the shell can be instructed to have.
207+
208+
```ts
209+
export const ShellCompDirective = {
210+
ShellCompDirectiveError: 1 << 0,
211+
ShellCompDirectiveNoSpace: 1 << 1,
212+
ShellCompDirectiveNoFileComp: 1 << 2,
213+
ShellCompDirectiveFilterFileExt: 1 << 3,
214+
ShellCompDirectiveFilterDirs: 1 << 4,
215+
ShellCompDirectiveKeepOrder: 1 << 5,
216+
ShellCompDirectiveDefault: 0,
217+
};
218+
```
219+
220+
## Complete Example
221+
222+
Here's a complete example showing all the core API features:
223+
224+
```ts
225+
#!/usr/bin/env node
226+
import { Completion, script } from '@bombsh/tab';
227+
228+
const name = 'my-cli';
229+
const completion = new Completion();
230+
231+
// Add command completions with positional arguments
232+
completion.addCommand('dev', 'Start development server', [false], async (previousArgs, toComplete, endsWithSpace) => {
233+
if (toComplete.startsWith('dev')) {
234+
return [
235+
{ value: 'dev', description: 'Start in development mode' },
236+
];
237+
}
238+
239+
return [
240+
{ value: 'dev', description: 'Start in development mode' },
241+
{ value: 'prod', description: 'Start in production mode' },
242+
];
243+
});
244+
245+
// Add option completions
246+
completion.addOption('dev', '--port', 'Port number', async (previousArgs, toComplete, endsWithSpace) => {
247+
if (toComplete.startsWith('30')) {
248+
return [
249+
{ value: '3000', description: 'Development port' },
250+
];
251+
}
252+
253+
return [
254+
{ value: '3000', description: 'Development port' },
255+
{ value: '8080', description: 'Production port' },
256+
];
257+
}, 'p');
258+
259+
// Helper function to quote paths with spaces
260+
function quoteIfNeeded(path: string) {
261+
return path.includes(' ') ? `'${path}'` : path;
262+
}
263+
264+
// Get the executable path for shell completion
265+
const execPath = process.execPath;
266+
const processArgs = process.argv.slice(1);
267+
const quotedExecPath = quoteIfNeeded(execPath);
268+
const quotedProcessArgs = processArgs.map(quoteIfNeeded);
269+
const quotedProcessExecArgs = process.execArgv.map(quoteIfNeeded);
270+
const x = `${quotedExecPath} ${quotedProcessExecArgs.join(' ')} ${quotedProcessArgs[0]}`;
271+
272+
// Handle completion requests
273+
if (process.argv[2] === '--') {
274+
try {
275+
await completion.parse(process.argv.slice(2));
276+
} catch (error) {
277+
console.error('Completion error:', error.message);
278+
process.exit(1);
279+
}
280+
} else {
281+
const shell = process.argv[2];
282+
if (['zsh', 'bash', 'fish', 'powershell'].includes(shell)) {
283+
script(shell, name, x);
284+
} else {
285+
console.error(`Unsupported shell: ${shell}`);
286+
process.exit(1);
287+
}
288+
}
289+
```
290+
291+
## Next Steps
292+
293+
- Learn about [Framework Adapters](/docs/tab/guides/adapters/) for easier integration
294+
- Check out [Examples](/docs/tab/guides/examples/) for practical use cases
295+
- Explore [Best Practices](/docs/tab/guides/best-practices/) for effective implementations

0 commit comments

Comments
 (0)