Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit 495245f

Browse files
committed
docs: document run: false, error handling and global flags
1 parent d12111e commit 495245f

9 files changed

Lines changed: 215 additions & 16 deletions

File tree

docs/.vitepress/config/en.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { join } from "node:path";
22

33
import { defineConfig } from "vitepress";
44

5-
import { getPluginNavigation } from "./plugin-navigation";
5+
import { getNavigation } from "../utils/navigation";
66

7-
const pluginNavigation = await getPluginNavigation(
7+
const pluginNavigation = await getNavigation(
88
join(import.meta.dirname, "..", "..", "official-plugins"),
99
"/official-plugins",
1010
);
@@ -40,18 +40,30 @@ export const enConfig = defineConfig({
4040
text: "Commands",
4141
link: "/commands",
4242
},
43-
{
44-
text: "Interceptors",
45-
link: "/interceptors",
46-
},
4743
{
4844
text: "Context",
4945
link: "/context",
5046
},
47+
{
48+
text: "Global Flags",
49+
link: "/global-flags",
50+
},
51+
{
52+
text: "Interceptors",
53+
link: "/interceptors",
54+
},
5155
{
5256
text: "Plugins",
5357
link: "/plugins",
5458
},
59+
{
60+
text: "Error Handling",
61+
link: "/error-handling",
62+
},
63+
{
64+
text: "Advanced Usage",
65+
link: "/advanced",
66+
},
5567
],
5668
},
5769
{

docs/.vitepress/config/zh.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { join } from "node:path";
22

33
import { defineConfig } from "vitepress";
44

5-
import { getPluginNavigation } from "./plugin-navigation";
5+
import { getNavigation } from "../utils/navigation";
66

7-
const pluginNavigation = await getPluginNavigation(
7+
const pluginNavigation = await getNavigation(
88
join(import.meta.dirname, "..", "..", "zh", "official-plugins"),
99
"/zh/official-plugins",
1010
);
@@ -40,18 +40,30 @@ export const zhConfig = defineConfig({
4040
text: "命令",
4141
link: "/zh/commands",
4242
},
43-
{
44-
text: "拦截器",
45-
link: "/zh/interceptors",
46-
},
4743
{
4844
text: "上下文",
4945
link: "/zh/context",
5046
},
47+
{
48+
text: "全局选项",
49+
link: "/zh/global-flags",
50+
},
51+
{
52+
text: "拦截器",
53+
link: "/zh/interceptors",
54+
},
5155
{
5256
text: "插件",
5357
link: "/zh/plugins",
5458
},
59+
{
60+
text: "错误处理",
61+
link: "/zh/error-handling",
62+
},
63+
{
64+
text: "进阶用法",
65+
link: "/zh/advanced",
66+
},
5567
],
5668
},
5769
{

docs/.vitepress/config/plugin-navigation.ts renamed to docs/.vitepress/utils/navigation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function extractTitleFromMarkdown(content: string): string | null {
1818
return titleMatch ? titleMatch[1].trim() : null;
1919
}
2020

21-
export async function getPluginTitles(
21+
export async function getTitles(
2222
path: string,
2323
): Promise<{ filename: string; title: string }[]> {
2424
try {
@@ -55,13 +55,13 @@ export async function getPluginTitles(
5555
}
5656
}
5757

58-
export async function getPluginNavigation(
58+
export async function getNavigation(
5959
path: string,
6060
webRoot: string,
6161
): Promise<{ text: string; link: string }[]> {
62-
const pluginTitles = await getPluginTitles(path);
62+
const titles = await getTitles(path);
6363

64-
return pluginTitles.map(({ filename, title }) => {
64+
return titles.map(({ filename, title }) => {
6565
const link =
6666
filename === "index.md"
6767
? webRoot

docs/advanced.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
title: Advanced Usage
3+
---
4+
5+
# Advanced Usage
6+
7+
## Passing Custom Arguments
8+
9+
Clerc allows you to pass a custom array of arguments instead of using the default `process.argv` / `Deno.args`. This is useful for testing or specific environments.
10+
11+
```ts
12+
Clerc.create().parse(["node", "my-cli", "greet"]); // Pass a custom array of arguments
13+
```
14+
15+
Alternatively, you can also pass an argument object:
16+
17+
```ts
18+
Clerc.create().parse({
19+
argv: ["greet"],
20+
});
21+
```
22+
23+
## Parse Only Without Execution
24+
25+
Sometimes you may want to parse commands and flags without immediately executing the command handler. Clerc provides an option to achieve this:
26+
27+
```ts
28+
const result = Clerc.create().parse({
29+
run: false, // Parse only, do not execute
30+
});
31+
```
32+
33+
When you need to run, you can call:
34+
35+
```ts
36+
result.run(); // Execute the parsed command

docs/error-handling.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: Error Handling
3+
---
4+
5+
# Error Handling
6+
7+
Clerc supports registering an error handler function to handle errors that occur during command parsing, command runtime, and other processes.
8+
9+
## Example
10+
11+
```ts
12+
Clerc.create()
13+
.scriptName("my-cli")
14+
.description("My CLI application")
15+
.version("1.0.0")
16+
.errorHandler((error: any) => {
17+
console.error("An error occurred:", error.message);
18+
// You can perform other actions as needed, such as logging the error or cleaning up resources
19+
})
20+
.command("run", "Run the application")
21+
.on("run", (ctx) => {
22+
throw new Error("Testing error handling");
23+
})
24+
.parse();

docs/global-flags.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: Global Flags
3+
---
4+
5+
# Global Flags
6+
7+
Clerc supports registering one or more global flags that can be used across all commands.
8+
9+
## Example
10+
11+
```ts
12+
Clerc.create()
13+
.scriptName("my-cli")
14+
.description("My CLI application")
15+
.version("1.0.0")
16+
.globalFlag("verbose", "Enable verbose output", {
17+
type: Boolean,
18+
}) // Global flag
19+
.command("run", "Run the application")
20+
.on("run", (ctx) => {
21+
if (ctx.flags.verbose) {
22+
console.log("Verbose mode enabled");
23+
}
24+
console.log("Running the application...");
25+
})
26+
.parse();

docs/zh/advanced.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: 进阶用法
3+
---
4+
5+
# 进阶用法
6+
7+
## 传入自定义参数
8+
9+
Clerc 允许您传入自定义参数数组,而不是默认使用 `process.argv` / `Deno.args`。这在测试或特定环境下非常有用。
10+
11+
```ts
12+
Clerc.create().parse(["node", "my-cli", "greet"]); // 传入自定义参数数组
13+
```
14+
15+
或者您也可以传入一个参数对象:
16+
17+
```ts
18+
Clerc.create().parse({
19+
argv: ["greet"],
20+
});
21+
```
22+
23+
## 仅解析命令,不执行
24+
25+
有时您可能只想解析命令和选项,而不立即执行命令处理程序。Clerc 提供了一个选项来实现这一点:
26+
27+
```ts
28+
const result = Clerc.create().parse({
29+
run: false, // 仅解析,不执行
30+
});
31+
```
32+
33+
在需要运行时,您可以调用:
34+
35+
```ts
36+
result.run(); // 运行解析后的命令
37+
```

docs/zh/error-handling.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: 错误处理
3+
---
4+
5+
# 错误处理
6+
7+
Clerc 支持注册一个错误处理函数,用于处理捕获的命令解析、命令运行时等过程中发生的错误。
8+
9+
## 示例
10+
11+
```ts
12+
Clerc.create()
13+
.scriptName("my-cli")
14+
.description("My CLI application")
15+
.version("1.0.0")
16+
.errorHandler((error: any) => {
17+
console.error("发生错误:", error.message);
18+
// 您可以根据需要执行其他操作,例如记录错误、清理资源等
19+
})
20+
.command("run", "Run the application")
21+
.on("run", (ctx) => {
22+
throw new Error("测试错误处理");
23+
})
24+
.parse();
25+
```

docs/zh/global-flags.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: 全局选项
3+
---
4+
5+
# 全局选项
6+
7+
Clerc 支持全局注册一个或多个选项,这些选项可以在所有命令中使用。
8+
9+
## 示例
10+
11+
```ts
12+
Clerc.create()
13+
.scriptName("my-cli")
14+
.description("My CLI application")
15+
.version("1.0.0")
16+
.globalFlag("verbose", "Enable verbose output", {
17+
type: Boolean,
18+
}) // 全局选项
19+
.command("run", "Run the application")
20+
.on("run", (ctx) => {
21+
if (ctx.flags.verbose) {
22+
console.log("Verbose mode enabled");
23+
}
24+
console.log("Running the application...");
25+
})
26+
.parse();
27+
```

0 commit comments

Comments
 (0)