You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 12, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: docs/advanced.md
+5-4Lines changed: 5 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,13 +9,13 @@ title: Advanced Usage
9
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
10
11
11
```ts
12
-
Clerc.create().parse(["node", "my-cli", "greet"]); // Pass a custom array of arguments
12
+
Cli().parse(["node", "my-cli", "greet"]); // Pass a custom array of arguments
13
13
```
14
14
15
15
Alternatively, you can also pass an argument object:
16
16
17
17
```ts
18
-
Clerc.create().parse({
18
+
Cli().parse({
19
19
argv: ["greet"],
20
20
});
21
21
```
@@ -25,12 +25,13 @@ Clerc.create().parse({
25
25
Sometimes you may want to parse commands and flags without immediately executing the command handler. Clerc provides an option to achieve this:
Lazy loading allows you to defer the loading of command handlers until they are actually invoked. This is useful for reducing startup time and memory usage, especially when you have many commands or heavy handlers.
299
+
300
+
You can implement lazy loading by using dynamic imports (`await import()`) within the handler:
301
+
302
+
### Basic Lazy Loading
303
+
304
+
```ts
305
+
const cli =Cli()
306
+
.scriptName("app")
307
+
.description("An application with lazy loading")
308
+
.version("1.0.0")
309
+
.command("build", "Build the project", {
310
+
flags: {
311
+
production: {
312
+
type: Boolean,
313
+
description: "Build for production",
314
+
},
315
+
},
316
+
})
317
+
.on("build", async (ctx) => {
318
+
// Handler is only loaded when the command is invoked
@@ -189,7 +189,7 @@ The `String` type is used for flags that accept string values. This is the most
189
189
**Default value behavior:** If the flag is not specified, its value is `undefined` (unless a `default` property is set).
190
190
191
191
```ts
192
-
const cli =Clerc.create()
192
+
const cli =Cli()
193
193
.command("greet", "Greet someone", {
194
194
flags: {
195
195
name: {
@@ -223,7 +223,7 @@ The `Boolean` type is used for creating boolean switch flags. By default, simply
223
223
**Default value behavior:** If the flag is not specified, its value is `false`.
224
224
225
225
```ts
226
-
const cli =Clerc.create()
226
+
const cli =Cli()
227
227
.command("build", "Build the project", {
228
228
flags: {
229
229
production: {
@@ -255,7 +255,7 @@ const cli = Clerc.create()
255
255
The Boolean type supports a `negatable` property that allows you to decide whether to enable negated flags. By default, `negatable` is `true`, which means `--no-flag` will set the `flag` flag to `false`.
256
256
257
257
```ts
258
-
const cli =Clerc.create()
258
+
const cli =Cli()
259
259
.command("start", "Start the application", {
260
260
flags: {
261
261
color: {
@@ -296,7 +296,7 @@ The `Array` type is used for flags that accept multiple values. Define it by wra
296
296
**Default value behavior:** If the flag is not specified, its value is `[]` (empty array).
297
297
298
298
```ts
299
-
const cli =Clerc.create()
299
+
const cli =Cli()
300
300
.command("copy", "Copy files", {
301
301
flags: {
302
302
// Use [String] to accept multiple string values
@@ -345,7 +345,7 @@ The counter type is used to count how many times a flag is specified. This can b
345
345
**Default value behavior:** If the flag is not specified, its value is `0`.
346
346
347
347
```ts
348
-
const cli =Clerc.create()
348
+
const cli =Cli()
349
349
.command("log", "Display logs", {
350
350
flags: {
351
351
// [Boolean] type counts how many times the flag is used
@@ -379,7 +379,7 @@ The `Object` type is used for flags that accept key-value pairs. Use dots or oth
379
379
**Default value behavior:** If the flag is not specified, its value is `{}` (empty object).
380
380
381
381
```ts
382
-
const cli =Clerc.create()
382
+
const cli =Cli()
383
383
.command("config", "Configure the application", {
384
384
flags: {
385
385
define: {
@@ -408,7 +408,7 @@ Clerc provides some built-in advanced flag types to facilitate common needs:
408
408
```ts
409
409
import { Choices } from"clerc";
410
410
411
-
Clerc.create()
411
+
Cli()
412
412
.command("serve", "Start the server", {
413
413
flags: {
414
414
mode: {
@@ -434,7 +434,7 @@ You can create custom flag types by providing a custom type function. The type f
0 commit comments