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

Commit aece7a0

Browse files
committed
docs: add more about flags
1 parent 99a4ffc commit aece7a0

File tree

8 files changed

+283
-137
lines changed

8 files changed

+283
-137
lines changed

docs/.vitepress/config/en.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export const enConfig = defineConfig({
4444
text: "Context",
4545
link: "/context",
4646
},
47+
{
48+
text: "Flags",
49+
link: "/flags",
50+
},
4751
{
4852
text: "Global Flags",
4953
link: "/global-flags",

docs/.vitepress/config/zh.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export const zhConfig = defineConfig({
4444
text: "上下文",
4545
link: "/zh/context",
4646
},
47+
{
48+
text: "选项",
49+
link: "/zh/flags",
50+
},
4751
{
4852
text: "全局选项",
4953
link: "/zh/global-flags",

docs/commands.md

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -171,74 +171,7 @@ const cli = Clerc.create()
171171

172172
## Flags
173173

174-
_Clerc_'s flag parsing is powered by [`@clerc/parser`](https://github.com/clercjs/clerc/blob/main/packages/parser) and has many features:
175-
176-
- Array and custom types
177-
- Flag delimiters: `--flag value`, `--flag=value`, `--flag:value` and `--flag.value`
178-
- Combined aliases: `-abcd 2``-a -b -c -d 2`
179-
- [End-of-file](https://unix.stackexchange.com/a/11382): pass `--` to end parsing
180-
181-
Flags can be specified in the `flags` object property, where the key is the flag name and the value is either an flag type function or an object describing the flag.
182-
183-
It's recommended to use camelCase for flag names as it will be interpreted as parsing the equivalent kebab-case flag.
184-
185-
The flag type function can be any function that accepts a string and returns the parsed value. The default JavaScript constructors should cover most use cases: [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/String), [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number), [Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/Boolean), etc.
186-
187-
The flag description object can be used to store additional information about the flag, such as `alias`, `default`, and `description`. To accept multiple values for a flag, wrap the type function in an array.
188-
189-
All provided information will be used to generate better help documentation.
190-
191-
Example:
192-
193-
```ts
194-
// $ node ./foo-cli.mjs echo --some-boolean --some-string hello --some-number 1 -n 2
195-
196-
const cli = Clerc.create()
197-
.scriptName("foo-cli")
198-
.description("A simple CLI")
199-
.version("1.0.0")
200-
.command("echo", "Echo", {
201-
flags: {
202-
someBoolean: {
203-
type: Boolean,
204-
description: "Some boolean flag",
205-
},
206-
207-
someString: {
208-
type: String,
209-
description: "Some string flag",
210-
default: "n/a",
211-
},
212-
213-
someNumber: {
214-
// Wrap the type function in an array to allow multiple values
215-
type: [Number],
216-
alias: "n",
217-
description: "Array of numbers. (e.g. -n 1 -n 2 -n 3)",
218-
},
219-
220-
object: {
221-
type: Object,
222-
description: "An object flag. (e.g. --object.key value)",
223-
},
224-
225-
counter: {
226-
type: [Boolean],
227-
description: "A counter flag. (e.g. -c -c -c)",
228-
},
229-
},
230-
})
231-
.on("echo", (ctx) => {
232-
ctx.flags;
233-
// ^?
234-
ctx.flags.someBoolean; // => true
235-
ctx.flags.someString; // => "hello"
236-
ctx.flags.someNumber; // => [1, 2]
237-
ctx.flags.object; // => { key: "value" }
238-
ctx.flags.counter; // => 2
239-
})
240-
.parse();
241-
```
174+
Please refer to the [Flags Documentation](./flags).
242175

243176
## Ignore
244177

docs/flags.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
title: flags
3+
---
4+
5+
# Flags
6+
7+
Flags are used to provide additional configuration and parameters for commands. Clerc supports various types of options, including built-in JavaScript types such as Boolean, String, Number, and also custom types.
8+
9+
_Clerc_'s flag parsing is powered by [`@clerc/parser`](https://github.com/clercjs/clerc/blob/main/packages/parser) and has many features:
10+
11+
- Array and custom types
12+
- Flag delimiters: `--flag value`, `--flag=value`, `--flag:value` and `--flag.value`
13+
- Combined aliases: `-abcd 2``-a -b -c -d 2`
14+
- [End-of-file](https://unix.stackexchange.com/a/11382): pass `--` to end parsing
15+
16+
Flags can be specified in the `flags` object property, where the key is the flag name and the value is either an flag type function or an object describing the flag.
17+
18+
It's recommended to use camelCase for flag names as it will be interpreted as parsing the equivalent kebab-case flag.
19+
20+
The flag type function can be any function that accepts a string and returns the parsed value. The default JavaScript constructors should cover most use cases: [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/String), [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number), [Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/Boolean), etc.
21+
22+
The flag description object can be used to store additional information about the flag, such as `alias`, `default`, and `description`. To accept multiple values for a flag, wrap the type function in an array.
23+
24+
All provided information will be used to generate better help documentation.
25+
26+
Example:
27+
28+
```ts
29+
// $ node ./foo-cli.mjs echo --some-boolean --some-string hello --some-number 1 -n 2
30+
31+
const cli = Clerc.create()
32+
.scriptName("foo-cli")
33+
.description("A simple CLI")
34+
.version("1.0.0")
35+
.command("echo", "Echo", {
36+
flags: {
37+
someBoolean: {
38+
type: Boolean,
39+
description: "Some boolean flag",
40+
},
41+
42+
someString: {
43+
type: String,
44+
description: "Some string flag",
45+
default: "n/a",
46+
},
47+
48+
someNumber: {
49+
// Wrap the type function in an array to allow multiple values
50+
type: [Number],
51+
alias: "n",
52+
description: "Array of numbers. (e.g. -n 1 -n 2 -n 3)",
53+
},
54+
55+
object: {
56+
type: Object,
57+
description: "An object flag. (e.g. --object.key value)",
58+
},
59+
60+
counter: {
61+
type: [Boolean],
62+
description: "A counter flag. (e.g. -c -c -c)",
63+
},
64+
},
65+
})
66+
.on("echo", (ctx) => {
67+
ctx.flags;
68+
// ^?
69+
ctx.flags.someBoolean; // => true
70+
ctx.flags.someString; // => "hello"
71+
ctx.flags.someNumber; // => [1, 2]
72+
ctx.flags.object; // => { key: "value" }
73+
ctx.flags.counter; // => 2
74+
})
75+
.parse();
76+
```
77+
78+
## Built-in Advanced Types
79+
80+
Clerc provides some built-in advanced flag types to facilitate common needs:
81+
82+
- `Choices`: Restrict flag values to a predefined set.
83+
84+
```ts
85+
import { Choices } from "clerc";
86+
87+
Clerc.create()
88+
.command("serve", "Start the server", {
89+
flags: {
90+
mode: {
91+
type: Choices("development", "production", "test"),
92+
default: "development" as const,
93+
description: "Set the application mode",
94+
},
95+
},
96+
})
97+
.on("serve", (ctx) => {
98+
ctx.flags.mode;
99+
// ^?
100+
})
101+
.parse();
102+
```
103+
104+
## Custom Flag Types
105+
106+
You can create custom flag types by providing a custom type function. The type function accepts a string argument and returns the parsed value.
107+
108+
```ts
109+
// Custom type function that parses a comma-separated string into an array of strings
110+
const CommaSeparatedList = (value: string): string[] =>
111+
value.split(",").map((item) => item.trim());
112+
113+
const cli = Clerc.create()
114+
.scriptName("custom-cli")
115+
.description("A CLI using a custom flag type")
116+
.version("1.0.0")
117+
.command("list", "Display list", {
118+
flags: {
119+
items: {
120+
type: CommaSeparatedList,
121+
default: [] as string[],
122+
description: "Comma-separated list of strings",
123+
},
124+
},
125+
})
126+
.on("list", (ctx) => {
127+
console.log("Items:", ctx.flags.items);
128+
// ^?
129+
})
130+
.parse();
131+
```

docs/global-flags.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ title: Global Flags
66

77
Clerc supports registering one or more global flags that can be used across all commands.
88

9+
More details about flags can be found in the [Flags Documentation](./flags).
10+
911
## Example
1012

1113
```ts
@@ -23,4 +25,5 @@ Clerc.create()
2325
}
2426
console.log("Running the application...");
2527
})
26-
.parse();
28+
.parse();
29+
```

docs/zh/commands.md

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -173,74 +173,7 @@ const cli = Clerc.create()
173173

174174
## 选项
175175

176-
_Clerc_ 的选项解析由 [`@clerc/parser`](https://github.com/clercjs/clerc/blob/main/packages/parser) 提供支持,并具有许多功能:
177-
178-
- 数组和自定义类型
179-
- 选项分隔符:`--flag value``--flag=value``--flag:value``--flag.value`
180-
- 组合别名:`-abcd 2``-a -b -c -d 2`
181-
- [选项结束符](https://unix.stackexchange.com/a/11382):传递 `--` 来结束选项解析
182-
183-
可以在 `flags` 对象属性中指定选项,其中键是选项名称,值是选项类型函数或描述选项的对象。
184-
185-
建议使用驼峰命名法作为选项名称,因为它将被解释为解析短横线分隔的等效选项。
186-
187-
选项类型函数可以是任何接受字符串并返回解析后的值的函数。默认的 JavaScript 构造函数应该涵盖大多数用例:[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/String)[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number)[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/Boolean) 等。
188-
189-
选项描述对象可用于存储有关选项的其他信息,例如 `alias``default``description`。要接受选项的多个值,请将类型函数包装在数组中。
190-
191-
所有提供的信息将用于生成更好的帮助文档。
192-
193-
示例:
194-
195-
```ts
196-
// $ node ./foo-cli.mjs echo --some-boolean --some-string hello --some-number 1 -n 2
197-
198-
const cli = Clerc.create()
199-
.scriptName("foo-cli")
200-
.description("一个简单的 CLI")
201-
.version("1.0.0")
202-
.command("echo", "回显", {
203-
flags: {
204-
someBoolean: {
205-
type: Boolean,
206-
description: "一些布尔选项",
207-
},
208-
209-
someString: {
210-
type: String,
211-
description: "一些字符串选项",
212-
default: "n/a",
213-
},
214-
215-
someNumber: {
216-
// 将类型函数包装在数组中以允许多个值
217-
type: [Number],
218-
alias: "n",
219-
description: "数字数组。 (例如 -n 1 -n 2 -n 3)",
220-
},
221-
222-
object: {
223-
type: Object,
224-
description: "一个对象选项。 (例如 --object.key value)",
225-
},
226-
227-
counter: {
228-
type: [Boolean],
229-
description: "一个计数器选项。 (例如 -c -c -c)",
230-
},
231-
},
232-
})
233-
.on("echo", (ctx) => {
234-
ctx.flags;
235-
// ^?
236-
ctx.flags.someBoolean; // => true
237-
ctx.flags.someString; // => "hello"
238-
ctx.flags.someNumber; // => [1, 2]
239-
ctx.flags.object; // => { key: "value" }
240-
ctx.flags.counter; // => 2
241-
})
242-
.parse();
243-
```
176+
请参见[选项文档](./flags)
244177

245178
## 忽略
246179

0 commit comments

Comments
 (0)