@bemoje/cli 3.1.3 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -137
- package/lib/index.cjs +2 -1
- package/lib/index.js +2 -1
- package/package.json +14 -44
package/README.md
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
# @bemoje/cli
|
|
2
2
|
|
|
3
|
-
A type-safe CLI framework for building command-line interfaces with typed arguments, options, subcommands, and
|
|
4
|
-
auto-generated help - without execution coupling.
|
|
3
|
+
A type-safe CLI framework for building command-line interfaces with typed arguments, options, subcommands, and auto-generated help - without execution coupling.
|
|
5
4
|
|
|
6
|
-
[](https://www.typescriptlang.org/)
|
|
7
|
-
|
|
5
|
+
[](https://www.typescriptlang.org/) [](https://nodejs.org/api/esm.html)
|
|
6
|
+
|
|
7
|
+
## Exports
|
|
8
|
+
|
|
9
|
+
<!-- EXPORTS_START -->
|
|
10
|
+
|
|
11
|
+
- **Command**: a type-safe CLI composer that can parse argv and generate help without execution coupling.
|
|
12
|
+
- **Help**: This is a fork of the Help class from the 'commander' npm package. The Help class method names as well as the expected interface of the Command instance to parse, are both similar, but different and not compatible without custom adaptations,
|
|
13
|
+
- **findCommand**: Finds subcommand by name or alias
|
|
14
|
+
- **findOption**: Finds option by name, short name or long name
|
|
15
|
+
- **getCommandAncestors**: Returns all ancestor commands excluding this command
|
|
16
|
+
- **getCommandAndAncestors**: Returns command and all ancestor commands in hierarchy
|
|
17
|
+
- **parseOptionFlags**: Parses option flags string into its components
|
|
18
|
+
|
|
19
|
+
<!-- EXPORTS_END -->
|
|
8
20
|
|
|
9
21
|
## Installation
|
|
10
22
|
|
|
@@ -14,28 +26,19 @@ npm install @bemoje/cli
|
|
|
14
26
|
|
|
15
27
|
## Features
|
|
16
28
|
|
|
17
|
-
- **Full type inference** - Arguments, options, and parsed results are fully typed. The types update as you chain
|
|
18
|
-
`.addArgument()` and `.addOption()` calls.
|
|
29
|
+
- **Full type inference** - Arguments, options, and parsed results are fully typed. The types update as you chain `.addArgument()` and `.addOption()` calls.
|
|
19
30
|
- **Declarative, chainable API** - Build commands fluently with method chaining; each call returns a narrowed type.
|
|
20
|
-
- **Subcommand hierarchies** - Nest commands arbitrarily deep. Options are inherited by subcommands. Aliases are
|
|
21
|
-
|
|
22
|
-
- **Auto-generated help** - Colorized, grouped, and wrapped help output from your command definitions. Fully
|
|
23
|
-
customizable via the `Help` class.
|
|
31
|
+
- **Subcommand hierarchies** - Nest commands arbitrarily deep. Options are inherited by subcommands. Aliases are auto-generated.
|
|
32
|
+
- **Auto-generated help** - Colorized, grouped, and wrapped help output from your command definitions. Fully customizable via the `Help` class.
|
|
24
33
|
- **Built-in `--help`, `--debug`, and `--version` flags** - Automatically added to root commands.
|
|
25
|
-
- **Option hooks** - Register side-effect actions that trigger when specific options are set (e.g., `--help` prints
|
|
26
|
-
|
|
27
|
-
- **
|
|
28
|
-
|
|
29
|
-
- **Validation** - Missing required arguments, unknown options, and invalid choices are reported as errors on the
|
|
30
|
-
result.
|
|
31
|
-
- **Variadic arguments and options** - Both arguments and options support variadic (`...`) syntax to collect
|
|
32
|
-
multiple values into arrays.
|
|
34
|
+
- **Option hooks** - Register side-effect actions that trigger when specific options are set (e.g., `--help` prints help and sets `process.exitCode`).
|
|
35
|
+
- **Parse-only design** - `parseArgv()` returns a result object with `args`, `opts`, `errors`, `hooks`, and an `execute()` method. You control when and whether execution happens.
|
|
36
|
+
- **Validation** - Missing required arguments, unknown options, and invalid choices are reported as errors on the result.
|
|
37
|
+
- **Variadic arguments and options** - Both arguments and options support variadic (`...`) syntax to collect multiple values into arrays.
|
|
33
38
|
- **Environment variable defaults** - Options can fall back to environment variables via the `env` option.
|
|
34
|
-
- **Argument and option choices** - Restrict allowed values with `choices` arrays; violations are reported as
|
|
35
|
-
validation errors.
|
|
39
|
+
- **Argument and option choices** - Restrict allowed values with `choices` arrays; violations are reported as validation errors.
|
|
36
40
|
- **Negatable boolean flags** - Pass `--no-<flag>` to set a boolean option to `false`.
|
|
37
|
-
- **kebab-case to camelCase mapping** - Multi-word option names like `--output-dir` are automatically available as
|
|
38
|
-
`opts.outputDir`.
|
|
41
|
+
- **kebab-case to camelCase mapping** - Multi-word option names like `--output-dir` are automatically available as `opts.outputDir`.
|
|
39
42
|
- **Hidden commands and options** - Exclude items from help output while keeping them functional.
|
|
40
43
|
- **Grouped help sections** - Organize options and subcommands into named groups in the help output.
|
|
41
44
|
|
|
@@ -60,8 +63,7 @@ cli.parseArgv(process.argv.slice(2))
|
|
|
60
63
|
|
|
61
64
|
### Arguments
|
|
62
65
|
|
|
63
|
-
Arguments are positional values parsed from `argv`. They support required (`<name>`), optional (`[name]`), and
|
|
64
|
-
variadic (`<name...>`, `[name...]`) forms.
|
|
66
|
+
Arguments are positional values parsed from `argv`. They support required (`<name>`), optional (`[name]`), and variadic (`<name...>`, `[name...]`) forms.
|
|
65
67
|
|
|
66
68
|
```ts
|
|
67
69
|
const cmd = new Command('copy')
|
|
@@ -75,8 +77,7 @@ cmd.parseArgv(['file.txt'])
|
|
|
75
77
|
// args: ['file.txt', '.']
|
|
76
78
|
```
|
|
77
79
|
|
|
78
|
-
**Variadic arguments** collect all remaining positional values into an array. Only the last argument may be
|
|
79
|
-
variadic.
|
|
80
|
+
**Variadic arguments** collect all remaining positional values into an array. Only the last argument may be variadic.
|
|
80
81
|
|
|
81
82
|
```ts
|
|
82
83
|
const cmd = new Command('concat').addArgument('<files...>').setAction((files, opts) => {
|
|
@@ -185,11 +186,9 @@ cmd.parseArgv(['--output-dir', '/tmp'])
|
|
|
185
186
|
|
|
186
187
|
### Subcommands
|
|
187
188
|
|
|
188
|
-
Use `.command()` to create a subcommand and get it back, or `.addCommand()` to add one via callback and get the
|
|
189
|
-
parent back for chaining.
|
|
189
|
+
Use `.command()` to create a subcommand and get it back, or `.addCommand()` to add one via callback and get the parent back for chaining.
|
|
190
190
|
|
|
191
|
-
Options defined on the parent are automatically inherited by subcommands. Aliases are auto-generated from
|
|
192
|
-
subcommand name initials (e.g., `build-project` gets alias `bp`).
|
|
191
|
+
Options defined on the parent are automatically inherited by subcommands. Aliases are auto-generated from subcommand name initials (e.g., `build-project` gets alias `bp`).
|
|
193
192
|
|
|
194
193
|
```ts
|
|
195
194
|
const cli = new Command('git')
|
|
@@ -265,13 +264,11 @@ if (result.errors) {
|
|
|
265
264
|
}
|
|
266
265
|
```
|
|
267
266
|
|
|
268
|
-
When `execute()` is called, hooks run first (in order). If any hook sets `process.exitCode`, execution stops. Then
|
|
269
|
-
the main action runs inside a timer that provides a logger.
|
|
267
|
+
When `execute()` is called, hooks run first (in order). If any hook sets `process.exitCode`, execution stops. Then the main action runs inside a timer that provides a logger.
|
|
270
268
|
|
|
271
269
|
### Option Hooks
|
|
272
270
|
|
|
273
|
-
Hooks let you attach side-effect actions that run when a specific option is set. The built-in `--help`, `--debug`,
|
|
274
|
-
and `--version` flags are all implemented as hooks.
|
|
271
|
+
Hooks let you attach side-effect actions that run when a specific option is set. The built-in `--help`, `--debug`, and `--version` flags are all implemented as hooks.
|
|
275
272
|
|
|
276
273
|
```ts
|
|
277
274
|
const cli = new Command('tool')
|
|
@@ -285,8 +282,7 @@ const cli = new Command('tool')
|
|
|
285
282
|
})
|
|
286
283
|
```
|
|
287
284
|
|
|
288
|
-
Hooks are evaluated in registration order. If a hook sets `process.exitCode`, subsequent hooks and the main action
|
|
289
|
-
are skipped.
|
|
285
|
+
Hooks are evaluated in registration order. If a hook sets `process.exitCode`, subsequent hooks and the main action are skipped.
|
|
290
286
|
|
|
291
287
|
### Help Output
|
|
292
288
|
|
|
@@ -331,8 +327,7 @@ The `Help` class exposes many styleable and overridable methods for full control
|
|
|
331
327
|
|
|
332
328
|
### Type Safety
|
|
333
329
|
|
|
334
|
-
The `Command` class tracks argument and option types through generics that update with each chained call. This
|
|
335
|
-
means your action handler receives correctly typed parameters:
|
|
330
|
+
The `Command` class tracks argument and option types through generics that update with each chained call. This means your action handler receives correctly typed parameters:
|
|
336
331
|
|
|
337
332
|
```ts
|
|
338
333
|
const cmd = new Command('example')
|
|
@@ -345,8 +340,7 @@ const cmd = new Command('example')
|
|
|
345
340
|
})
|
|
346
341
|
```
|
|
347
342
|
|
|
348
|
-
Invalid argument orderings (e.g., required after optional, anything after variadic) are caught at the **type
|
|
349
|
-
level** - TypeScript will reject the code before it runs.
|
|
343
|
+
Invalid argument orderings (e.g., required after optional, anything after variadic) are caught at the **type level** - TypeScript will reject the code before it runs.
|
|
350
344
|
|
|
351
345
|
### Helpers
|
|
352
346
|
|
|
@@ -376,100 +370,3 @@ parseOptionFlags('-o, --output <dir>')
|
|
|
376
370
|
getCommandAncestors(sub) // parent commands (excluding self)
|
|
377
371
|
getCommandAndAncestors(sub) // [self, parent, grandparent, ...]
|
|
378
372
|
```
|
|
379
|
-
|
|
380
|
-
## API Reference
|
|
381
|
-
|
|
382
|
-
### `Command` Class
|
|
383
|
-
|
|
384
|
-
#### Constructor
|
|
385
|
-
|
|
386
|
-
| Method | Description |
|
|
387
|
-
| ------------------- | ----------------------------------------- |
|
|
388
|
-
| `new Command(name)` | Create a new root command with given name |
|
|
389
|
-
|
|
390
|
-
#### Configuration
|
|
391
|
-
|
|
392
|
-
| Method | Returns | Description |
|
|
393
|
-
| --------------------------- | ------- | ------------------------------------------------- |
|
|
394
|
-
| `.setName(name)` | `void` | Rename the command |
|
|
395
|
-
| `.setDescription(...lines)` | `this` | Set full description (lines are joined with `\n`) |
|
|
396
|
-
| `.setSummary(text)` | `this` | Set brief one-line summary for command listings |
|
|
397
|
-
| `.setVersion(version)` | `this` | Set version and add `--version` flag |
|
|
398
|
-
| `.setHidden(hidden?)` | `this` | Hide from help listings |
|
|
399
|
-
| `.setGroup(group)` | `this` | Set category for help organization |
|
|
400
|
-
| `.setAliases(...aliases)` | `this` | Replace all aliases |
|
|
401
|
-
| `.addAliases(...aliases)` | `this` | Append aliases |
|
|
402
|
-
| `.helpConfiguration(cb?)` | `this` | Configure `Help` instance via callback |
|
|
403
|
-
|
|
404
|
-
#### Arguments
|
|
405
|
-
|
|
406
|
-
| Method | Returns | Description |
|
|
407
|
-
| ------------------------------- | -------------- | ------------------------- |
|
|
408
|
-
| `.addArgument(usage, options?)` | `Command<...>` | Add a positional argument |
|
|
409
|
-
|
|
410
|
-
Argument usage patterns: `<name>` (required), `[name]` (optional), `<name...>` (required variadic), `[name...]`
|
|
411
|
-
(optional variadic).
|
|
412
|
-
|
|
413
|
-
Argument options: `description`, `choices`, `defaultValue`, `defaultValueDescription`.
|
|
414
|
-
|
|
415
|
-
#### Options
|
|
416
|
-
|
|
417
|
-
| Method | Returns | Description |
|
|
418
|
-
| ----------------------------- | -------------- | -------------------------------------- |
|
|
419
|
-
| `.addOption(flags, options?)` | `Command<...>` | Add a named option with type inference |
|
|
420
|
-
|
|
421
|
-
Option flag patterns: `-s, --long` (boolean), `-s, --long <val>` (required string), `-s, --long [val]` (optional
|
|
422
|
-
string), `-s, --long <val...>` (required variadic), `-s, --long [val...]` (optional variadic).
|
|
423
|
-
|
|
424
|
-
Option properties: `description`, `defaultValue`, `defaultValueDescription`, `choices`, `env`, `hidden`, `group`.
|
|
425
|
-
|
|
426
|
-
#### Subcommands
|
|
427
|
-
|
|
428
|
-
| Method | Returns | Description |
|
|
429
|
-
| ----------------------- | --------- | --------------------------------------------- |
|
|
430
|
-
| `.command(name, cb?)` | `Command` | Create subcommand, return the subcommand |
|
|
431
|
-
| `.addCommand(name, cb)` | `this` | Create subcommand via callback, return parent |
|
|
432
|
-
|
|
433
|
-
#### Actions and Hooks
|
|
434
|
-
|
|
435
|
-
| Method | Returns | Description |
|
|
436
|
-
| -------------------------------- | ------- | ------------------------------------------ |
|
|
437
|
-
| `.setAction(fn)` | `this` | Set main action handler |
|
|
438
|
-
| `.addOptionHook(optionName, fn)` | `this` | Register hook triggered when option is set |
|
|
439
|
-
|
|
440
|
-
#### Parsing
|
|
441
|
-
|
|
442
|
-
| Method | Returns | Description |
|
|
443
|
-
| ------------------- | ----------------- | ----------------------------------------- |
|
|
444
|
-
| `.parseArgv(argv?)` | `ParseArgvResult` | Parse argv and return result with execute |
|
|
445
|
-
| `.renderHelp(cfg?)` | `string` | Render help text |
|
|
446
|
-
|
|
447
|
-
### Helper Functions
|
|
448
|
-
|
|
449
|
-
| Export | Description |
|
|
450
|
-
| ------------------------ | -------------------------------------------------------------- |
|
|
451
|
-
| `findCommand(cmd, name)` | Find subcommand by name or alias |
|
|
452
|
-
| `findOption(cmd, name)` | Find option by name, `-short`, or `--long` |
|
|
453
|
-
| `getCommandAncestors` | Get all parent commands (excluding self) |
|
|
454
|
-
| `getCommandAndAncestors` | Get self and all parent commands |
|
|
455
|
-
| `parseOptionFlags` | Parse flag syntax string into `{ short, long, name, argName }` |
|
|
456
|
-
|
|
457
|
-
### `Help` Class
|
|
458
|
-
|
|
459
|
-
Renders formatted, colorized help text. Instantiated automatically by `Command`. Override style methods by
|
|
460
|
-
subclassing or via `helpConfiguration()`.
|
|
461
|
-
|
|
462
|
-
Key properties: `helpWidth`, `sortSubcommands`, `sortOptions`, `minWidthToWrap`.
|
|
463
|
-
|
|
464
|
-
### Types
|
|
465
|
-
|
|
466
|
-
| Type | Description |
|
|
467
|
-
| ------------------- | ------------------------------------------------------------ |
|
|
468
|
-
| `ParseArgvResult` | Return type of `parseArgv()` with args, opts, execute, etc. |
|
|
469
|
-
| `ActionHandler` | Action function signature receiving args, opts, and metadata |
|
|
470
|
-
| `HookActionHandler` | Hook function signature receiving parse context |
|
|
471
|
-
| `HookDefinition` | Hook with name, predicate, and action |
|
|
472
|
-
| `Argument` | Argument descriptor |
|
|
473
|
-
| `Option` | Option descriptor |
|
|
474
|
-
| `ICommand` | Command interface for loose coupling |
|
|
475
|
-
| `IHelp` | Help interface |
|
package/lib/index.cjs
CHANGED
|
@@ -396,7 +396,8 @@ var Help = class {
|
|
|
396
396
|
const args = sub.arguments.map((arg) => {
|
|
397
397
|
return arg.usage;
|
|
398
398
|
}).join(" ");
|
|
399
|
-
return (sub.aliases[0] ? `${sub.aliases[0].padEnd(this.longestSubcommandAliasLength(), " ")} | ` : "") + sub.name + (sub.options.length ? ` ${this.usageDisplayOptionsAs}` : "") +
|
|
399
|
+
return (sub.aliases[0] ? `${sub.aliases[0].padEnd(this.longestSubcommandAliasLength(), " ")} | ` : "") + sub.name + (sub.options.length ? ` ${this.usageDisplayOptionsAs}` : "") + // simplistic check for non-help option
|
|
400
|
+
(args ? ` ${args}` : "");
|
|
400
401
|
}
|
|
401
402
|
/**
|
|
402
403
|
* Get the option term to show in the list of options.
|
package/lib/index.js
CHANGED
|
@@ -388,7 +388,8 @@ var Help = class {
|
|
|
388
388
|
const args = sub.arguments.map((arg) => {
|
|
389
389
|
return arg.usage;
|
|
390
390
|
}).join(" ");
|
|
391
|
-
return (sub.aliases[0] ? `${sub.aliases[0].padEnd(this.longestSubcommandAliasLength(), " ")} | ` : "") + sub.name + (sub.options.length ? ` ${this.usageDisplayOptionsAs}` : "") +
|
|
391
|
+
return (sub.aliases[0] ? `${sub.aliases[0].padEnd(this.longestSubcommandAliasLength(), " ")} | ` : "") + sub.name + (sub.options.length ? ` ${this.usageDisplayOptionsAs}` : "") + // simplistic check for non-help option
|
|
392
|
+
(args ? ` ${args}` : "");
|
|
392
393
|
}
|
|
393
394
|
/**
|
|
394
395
|
* Get the option term to show in the list of options.
|
package/package.json
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemoje/cli",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "A type-safe CLI framework for building command-line interfaces with typed arguments, options, subcommands, and auto-generated help - without execution coupling.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"ansi-colors": "^4.1.3",
|
|
12
|
+
"es-toolkit": "^1.45.1"
|
|
13
|
+
},
|
|
7
14
|
"keywords": [
|
|
8
15
|
"cli",
|
|
9
16
|
"command-line",
|
|
@@ -40,37 +47,7 @@
|
|
|
40
47
|
"default": "./lib/index.cjs"
|
|
41
48
|
}
|
|
42
49
|
},
|
|
43
|
-
"
|
|
44
|
-
"import": {
|
|
45
|
-
"types": "./lib/index.d.ts",
|
|
46
|
-
"default": "./lib/index.js"
|
|
47
|
-
},
|
|
48
|
-
"require": {
|
|
49
|
-
"types": "./lib/index.d.cts",
|
|
50
|
-
"default": "./lib/index.cjs"
|
|
51
|
-
}
|
|
52
|
-
},
|
|
53
|
-
"./Command": {
|
|
54
|
-
"import": {
|
|
55
|
-
"types": "./lib/index.d.ts",
|
|
56
|
-
"default": "./lib/index.js"
|
|
57
|
-
},
|
|
58
|
-
"require": {
|
|
59
|
-
"types": "./lib/index.d.cts",
|
|
60
|
-
"default": "./lib/index.cjs"
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
"./Help": {
|
|
64
|
-
"import": {
|
|
65
|
-
"types": "./lib/index.d.ts",
|
|
66
|
-
"default": "./lib/index.js"
|
|
67
|
-
},
|
|
68
|
-
"require": {
|
|
69
|
-
"types": "./lib/index.d.cts",
|
|
70
|
-
"default": "./lib/index.cjs"
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
"./types": {
|
|
50
|
+
"./*": {
|
|
74
51
|
"import": {
|
|
75
52
|
"types": "./lib/index.d.ts",
|
|
76
53
|
"default": "./lib/index.js"
|
|
@@ -81,22 +58,15 @@
|
|
|
81
58
|
}
|
|
82
59
|
}
|
|
83
60
|
},
|
|
84
|
-
"
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
|
|
88
|
-
"publishConfig": {
|
|
89
|
-
"access": "public"
|
|
61
|
+
"repository": {
|
|
62
|
+
"type": "git",
|
|
63
|
+
"url": "git+https://github.com/bemoje/mono.git",
|
|
64
|
+
"directory": "libs/cli"
|
|
90
65
|
},
|
|
91
|
-
"license": "MIT",
|
|
92
66
|
"author": {
|
|
93
67
|
"name": "Benjamin Møller Jensen",
|
|
94
68
|
"email": "bemoje@bemoje.net",
|
|
95
69
|
"url": "https://github.com/bemoje/"
|
|
96
70
|
},
|
|
97
|
-
"
|
|
98
|
-
"type": "git",
|
|
99
|
-
"url": "git+https://github.com/bemoje/mono.git",
|
|
100
|
-
"directory": "libs/cli"
|
|
101
|
-
}
|
|
71
|
+
"license": "MIT"
|
|
102
72
|
}
|