@bemoje/cli 3.0.0 → 3.1.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 +475 -0
- package/lib/index.cjs +61 -0
- package/lib/index.d.cts +6 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +8 -0
- package/lib/lib/Command.cjs +450 -0
- package/lib/lib/Command.d.cts +1 -0
- package/lib/lib/Command.d.ts +1 -0
- package/lib/lib/Command.js +443 -0
- package/lib/lib/Help.cjs +552 -0
- package/lib/lib/Help.d.cts +1 -0
- package/lib/lib/Help.d.ts +1 -0
- package/lib/lib/Help.js +545 -0
- package/lib/lib/helpers/findCommand.cjs +14 -0
- package/lib/lib/helpers/findCommand.d.cts +8 -0
- package/lib/lib/helpers/findCommand.d.ts +8 -0
- package/lib/lib/helpers/findCommand.js +11 -0
- package/lib/lib/helpers/findOption.cjs +16 -0
- package/lib/lib/helpers/findOption.d.cts +8 -0
- package/lib/lib/helpers/findOption.d.ts +8 -0
- package/lib/lib/helpers/findOption.js +14 -0
- package/lib/lib/helpers/getCommandAncestors.cjs +12 -0
- package/lib/lib/helpers/getCommandAncestors.d.cts +8 -0
- package/lib/lib/helpers/getCommandAncestors.d.ts +8 -0
- package/lib/lib/helpers/getCommandAncestors.js +9 -0
- package/lib/lib/helpers/getCommandAndAncestors.cjs +15 -0
- package/lib/lib/helpers/getCommandAndAncestors.d.cts +8 -0
- package/lib/lib/helpers/getCommandAndAncestors.d.ts +8 -0
- package/lib/lib/helpers/getCommandAndAncestors.js +13 -0
- package/lib/lib/helpers/parseOptionFlags.cjs +28 -0
- package/lib/lib/helpers/parseOptionFlags.d.cts +13 -0
- package/lib/lib/helpers/parseOptionFlags.d.ts +13 -0
- package/lib/lib/helpers/parseOptionFlags.js +26 -0
- package/lib/lib/types.cjs +2 -0
- package/lib/lib/types.d.cts +1 -0
- package/lib/lib/types.d.ts +1 -0
- package/lib/lib/types.js +1 -0
- package/package.json +144 -24
- package/index.d.ts +0 -711
- package/index.mjs +0 -1537
package/README.md
ADDED
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
# @bemoje/cli
|
|
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.
|
|
5
|
+
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
[](https://nodejs.org/api/esm.html)
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @bemoje/cli
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- **Full type inference** - Arguments, options, and parsed results are fully typed. The types update as you chain
|
|
18
|
+
`.addArgument()` and `.addOption()` calls.
|
|
19
|
+
- **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
|
+
auto-generated.
|
|
22
|
+
- **Auto-generated help** - Colorized, grouped, and wrapped help output from your command definitions. Fully
|
|
23
|
+
customizable via the `Help` class.
|
|
24
|
+
- **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
|
+
help and sets `process.exitCode`).
|
|
27
|
+
- **Parse-only design** - `parseArgv()` returns a result object with `args`, `opts`, `errors`, `hooks`, and an
|
|
28
|
+
`execute()` method. You control when and whether execution happens.
|
|
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.
|
|
33
|
+
- **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.
|
|
36
|
+
- **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`.
|
|
39
|
+
- **Hidden commands and options** - Exclude items from help output while keeping them functional.
|
|
40
|
+
- **Grouped help sections** - Organize options and subcommands into named groups in the help output.
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
### Basic Command
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { Command } from '@bemoje/cli'
|
|
48
|
+
|
|
49
|
+
const cli = new Command('greet')
|
|
50
|
+
.setDescription('Greet someone')
|
|
51
|
+
.addArgument('<name>')
|
|
52
|
+
.addOption('-l, --loud', { description: 'Shout the greeting' })
|
|
53
|
+
.setAction((name, opts) => {
|
|
54
|
+
const msg = `Hello, ${name}!`
|
|
55
|
+
console.log(opts.loud ? msg.toUpperCase() : msg)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
cli.parseArgv(process.argv.slice(2))
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Arguments
|
|
62
|
+
|
|
63
|
+
Arguments are positional values parsed from `argv`. They support required (`<name>`), optional (`[name]`), and
|
|
64
|
+
variadic (`<name...>`, `[name...]`) forms.
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
const cmd = new Command('copy')
|
|
68
|
+
.addArgument('<source>') // required string
|
|
69
|
+
.addArgument('[destination]', { defaultValue: '.' }) // optional with default
|
|
70
|
+
.setAction((source, destination, opts) => {
|
|
71
|
+
console.log(`Copying ${source} to ${destination}`)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
cmd.parseArgv(['file.txt'])
|
|
75
|
+
// args: ['file.txt', '.']
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**Variadic arguments** collect all remaining positional values into an array. Only the last argument may be
|
|
79
|
+
variadic.
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
const cmd = new Command('concat').addArgument('<files...>').setAction((files, opts) => {
|
|
83
|
+
// files is string[]
|
|
84
|
+
console.log(`Concatenating: ${files.join(', ')}`)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
cmd.parseArgv(['a.txt', 'b.txt', 'c.txt'])
|
|
88
|
+
// args: [['a.txt', 'b.txt', 'c.txt']]
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
**Choices** can restrict which values are accepted:
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
const cmd = new Command('deploy').addArgument('<env>', { choices: ['dev', 'staging', 'prod'] })
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**Ordering rules** are enforced at both the type level and at runtime:
|
|
98
|
+
|
|
99
|
+
- Required arguments must come before optional ones
|
|
100
|
+
- Only the last argument may be variadic
|
|
101
|
+
- A command with arguments cannot have subcommands (and vice versa)
|
|
102
|
+
|
|
103
|
+
### Options
|
|
104
|
+
|
|
105
|
+
Options are named flags parsed from `argv`. The format is always `-<short>, --<long> [value-syntax]`.
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
const cmd = new Command('build')
|
|
109
|
+
.addOption('-o, --output <dir>', { description: 'Output directory' }) // required string
|
|
110
|
+
.addOption('-m, --minify', { description: 'Minify output' }) // boolean flag
|
|
111
|
+
.addOption('-w, --watch [mode]', { description: 'Watch mode', defaultValue: 'poll' }) // optional string with default
|
|
112
|
+
.setAction((opts) => {
|
|
113
|
+
console.log(opts.output) // string
|
|
114
|
+
console.log(opts.minify) // boolean | undefined
|
|
115
|
+
console.log(opts.watch) // string
|
|
116
|
+
})
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
**Variadic options** collect multiple values into an array:
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
const cmd = new Command('lint')
|
|
123
|
+
.addOption('-i, --include <patterns...>', { description: 'Include globs' }) // required: string[]
|
|
124
|
+
.addOption('-e, --exclude [patterns...]', { description: 'Exclude globs', defaultValue: ['node_modules'] })
|
|
125
|
+
|
|
126
|
+
cmd.parseArgv(['-i', 'src', 'lib', '-e', 'test'])
|
|
127
|
+
// opts.include: ['src', 'lib']
|
|
128
|
+
// opts.exclude: ['test']
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
**Option choices** restrict valid values:
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
const cmd = new Command('format').addOption('-f, --format <type>', {
|
|
135
|
+
description: 'Output format',
|
|
136
|
+
choices: ['json', 'xml', 'yaml'],
|
|
137
|
+
})
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Environment variable defaults** let options fall back to env vars:
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
const cmd = new Command('deploy').addOption('-t, --token <value>', {
|
|
144
|
+
description: 'Auth token',
|
|
145
|
+
env: 'AUTH_TOKEN',
|
|
146
|
+
})
|
|
147
|
+
// If AUTH_TOKEN is set and --token is not passed, opts.token defaults to process.env.AUTH_TOKEN
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
**Hidden options** are excluded from help output:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
const cmd = new Command('tool').addOption('-x, --experimental', {
|
|
154
|
+
description: 'Experimental feature',
|
|
155
|
+
hidden: true,
|
|
156
|
+
})
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
**Grouped options** appear under custom headings in help:
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
const cmd = new Command('server')
|
|
163
|
+
.addOption('-p, --port <n>', { description: 'Port number', group: 'Network:' })
|
|
164
|
+
.addOption('-H, --host [addr]', { description: 'Host address', group: 'Network:' })
|
|
165
|
+
.addOption('-v, --verbose', { description: 'Verbose logging', group: 'Logging:' })
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
**Negatable flags** - any boolean option can be negated with `--no-<name>`:
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
const cmd = new Command('build').addOption('-c, --color', { description: 'Colorize output' })
|
|
172
|
+
|
|
173
|
+
cmd.parseArgv(['--no-color'])
|
|
174
|
+
// opts.color: false
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**kebab-case to camelCase** - multi-word option names are automatically camelCased:
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
const cmd = new Command('build').addOption('-o, --output-dir <path>', { description: 'Output directory' })
|
|
181
|
+
|
|
182
|
+
cmd.parseArgv(['--output-dir', '/tmp'])
|
|
183
|
+
// opts.outputDir: '/tmp'
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Subcommands
|
|
187
|
+
|
|
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.
|
|
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`).
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
const cli = new Command('git')
|
|
196
|
+
.setDescription('A version control system')
|
|
197
|
+
.addOption('-v, --verbose', { description: 'Verbose output' })
|
|
198
|
+
|
|
199
|
+
.addCommand('clone', (sub) =>
|
|
200
|
+
sub
|
|
201
|
+
.setDescription('Clone a repository')
|
|
202
|
+
.addArgument('<url>')
|
|
203
|
+
.addOption('-d, --depth <n>', { description: 'Shallow clone depth' })
|
|
204
|
+
.setAction((url, opts) => {
|
|
205
|
+
// opts.verbose is inherited from parent
|
|
206
|
+
console.log(`Cloning ${url} with depth ${opts.depth ?? 'full'}`)
|
|
207
|
+
})
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
.addCommand('status', (sub) =>
|
|
211
|
+
sub
|
|
212
|
+
.setDescription('Show working tree status')
|
|
213
|
+
.addOption('-s, --short', { description: 'Short format output' })
|
|
214
|
+
.setAction((opts) => {
|
|
215
|
+
console.log('Status:', opts.short ? 'clean' : 'On branch main...')
|
|
216
|
+
})
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
cli.parseArgv(['clone', 'https://github.com/user/repo', '-d', '1'])
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
**Aliases** can also be set manually:
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
const cli = new Command('app')
|
|
226
|
+
cli.command('install').setAliases('i', 'add')
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Parsing and Execution
|
|
230
|
+
|
|
231
|
+
`parseArgv()` does not execute the action - it returns a result object:
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
const cli = new Command('tool')
|
|
235
|
+
.addArgument('<input>')
|
|
236
|
+
.addOption('-v, --verbose', { description: 'Verbose' })
|
|
237
|
+
.setAction((input, opts) => {
|
|
238
|
+
console.log(`Processing ${input}`)
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
const result = cli.parseArgv(['file.txt', '-v'])
|
|
242
|
+
|
|
243
|
+
result.path // string[] - subcommand path segments
|
|
244
|
+
result.name // string - command name
|
|
245
|
+
result.argv // string[] - the argv that was parsed
|
|
246
|
+
result.args // [string] - typed parsed arguments
|
|
247
|
+
result.opts // { verbose: true } - typed parsed options
|
|
248
|
+
result.errors // string[] | undefined - validation errors
|
|
249
|
+
result.hooks // HookDefinition[] - triggered hooks
|
|
250
|
+
result.cmd // Command - the matched command instance
|
|
251
|
+
|
|
252
|
+
// Execute the action (and any triggered hooks) when you're ready:
|
|
253
|
+
await result.execute()
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
**Validation errors** are collected, not thrown. Check `result.errors` before executing:
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
const result = cli.parseArgv([])
|
|
260
|
+
if (result.errors) {
|
|
261
|
+
console.error(result.errors.join('\n'))
|
|
262
|
+
process.exitCode = 1
|
|
263
|
+
} else {
|
|
264
|
+
await result.execute()
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
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.
|
|
270
|
+
|
|
271
|
+
### Option Hooks
|
|
272
|
+
|
|
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.
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
const cli = new Command('tool')
|
|
278
|
+
.addOption('-c, --clean', { description: 'Clean before running' })
|
|
279
|
+
.addOptionHook('clean', ({ cmd, opts }) => {
|
|
280
|
+
console.log('Cleaning build artifacts...')
|
|
281
|
+
// Hook runs before the main action
|
|
282
|
+
})
|
|
283
|
+
.setAction((opts) => {
|
|
284
|
+
console.log('Running tool...')
|
|
285
|
+
})
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
Hooks are evaluated in registration order. If a hook sets `process.exitCode`, subsequent hooks and the main action
|
|
289
|
+
are skipped.
|
|
290
|
+
|
|
291
|
+
### Help Output
|
|
292
|
+
|
|
293
|
+
Help is auto-generated with ANSI color support, word wrapping, and section grouping.
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
const cli = new Command('my-tool')
|
|
297
|
+
.setVersion('1.0.0')
|
|
298
|
+
.setDescription('A great CLI tool')
|
|
299
|
+
.addArgument('<input>')
|
|
300
|
+
.addOption('-v, --verbose', { description: 'Verbose output' })
|
|
301
|
+
|
|
302
|
+
// Render help string (with ANSI colors)
|
|
303
|
+
console.log(cli.renderHelp())
|
|
304
|
+
|
|
305
|
+
// Render plain text (no ANSI)
|
|
306
|
+
console.log(cli.renderHelp({ noColor: true }))
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
**Customize help rendering** via `.helpConfiguration()`:
|
|
310
|
+
|
|
311
|
+
```ts
|
|
312
|
+
cli.helpConfiguration((help) => {
|
|
313
|
+
help.sortSubcommands = false
|
|
314
|
+
help.sortOptions = false
|
|
315
|
+
help.helpWidth = 120
|
|
316
|
+
})
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
The `Help` class exposes many styleable and overridable methods for full control over the output format:
|
|
320
|
+
|
|
321
|
+
| Method | Purpose |
|
|
322
|
+
| --------------------------- | --------------------------------------------- |
|
|
323
|
+
| `styleTitle(str)` | Style section headings ("Usage:", "Options:") |
|
|
324
|
+
| `styleUsage(str)` | Style the usage line |
|
|
325
|
+
| `styleOptionTerm(str)` | Style option flags |
|
|
326
|
+
| `styleSubcommandTerm(str)` | Style subcommand entries |
|
|
327
|
+
| `styleArgumentTerm(str)` | Style argument entries |
|
|
328
|
+
| `styleDescriptionText(str)` | Base style for all descriptions |
|
|
329
|
+
| `formatItem(term, w, desc)` | Format a term/description pair with padding |
|
|
330
|
+
| `boxWrap(str, width)` | Wrap text at whitespace to fit width |
|
|
331
|
+
|
|
332
|
+
### Type Safety
|
|
333
|
+
|
|
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:
|
|
336
|
+
|
|
337
|
+
```ts
|
|
338
|
+
const cmd = new Command('example')
|
|
339
|
+
.addArgument('<name>') // A = [string]
|
|
340
|
+
.addArgument('[count]', { defaultValue: '1' }) // A = [string, string]
|
|
341
|
+
.addOption('-v, --verbose', {}) // O = { verbose?: boolean, ... }
|
|
342
|
+
.addOption('-f, --format <type>', {}) // O = { verbose?: boolean, format: string, ... }
|
|
343
|
+
.setAction((name, count, opts) => {
|
|
344
|
+
// name: string, count: string, opts: { verbose?: boolean, format: string, ... }
|
|
345
|
+
})
|
|
346
|
+
```
|
|
347
|
+
|
|
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.
|
|
350
|
+
|
|
351
|
+
### Helpers
|
|
352
|
+
|
|
353
|
+
```ts
|
|
354
|
+
import {
|
|
355
|
+
findCommand,
|
|
356
|
+
findOption,
|
|
357
|
+
parseOptionFlags,
|
|
358
|
+
getCommandAncestors,
|
|
359
|
+
getCommandAndAncestors,
|
|
360
|
+
} from '@bemoje/cli'
|
|
361
|
+
|
|
362
|
+
// Find a subcommand by name or alias
|
|
363
|
+
const sub = findCommand(cli, 'clone') // Command | undefined
|
|
364
|
+
const sub2 = findCommand(cli, 'c') // also works with aliases
|
|
365
|
+
|
|
366
|
+
// Find an option by name, short flag, or long flag
|
|
367
|
+
const opt = findOption(cli, 'verbose') // Option | undefined
|
|
368
|
+
const opt2 = findOption(cli, '-v') // by short flag
|
|
369
|
+
const opt3 = findOption(cli, '--verbose') // by long flag
|
|
370
|
+
|
|
371
|
+
// Parse raw flag syntax into components
|
|
372
|
+
parseOptionFlags('-o, --output <dir>')
|
|
373
|
+
// => { short: 'o', long: 'output', name: 'output', argName: 'dir' }
|
|
374
|
+
|
|
375
|
+
// Navigate command hierarchy
|
|
376
|
+
getCommandAncestors(sub) // parent commands (excluding self)
|
|
377
|
+
getCommandAndAncestors(sub) // [self, parent, grandparent, ...]
|
|
378
|
+
```
|
|
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
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var Command = require('./lib/Command');
|
|
4
|
+
var Help = require('./lib/Help');
|
|
5
|
+
var findCommand = require('./lib/helpers/findCommand');
|
|
6
|
+
var findOption = require('./lib/helpers/findOption');
|
|
7
|
+
var getCommandAncestors = require('./lib/helpers/getCommandAncestors');
|
|
8
|
+
var getCommandAndAncestors = require('./lib/helpers/getCommandAndAncestors');
|
|
9
|
+
var parseOptionFlags = require('./lib/helpers/parseOptionFlags');
|
|
10
|
+
var types = require('./lib/types');
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
Object.keys(Command).forEach(function (k) {
|
|
15
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return Command[k]; }
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
Object.keys(Help).forEach(function (k) {
|
|
21
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () { return Help[k]; }
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
Object.keys(findCommand).forEach(function (k) {
|
|
27
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () { return findCommand[k]; }
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
Object.keys(findOption).forEach(function (k) {
|
|
33
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
get: function () { return findOption[k]; }
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
Object.keys(getCommandAncestors).forEach(function (k) {
|
|
39
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
get: function () { return getCommandAncestors[k]; }
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
Object.keys(getCommandAndAncestors).forEach(function (k) {
|
|
45
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
46
|
+
enumerable: true,
|
|
47
|
+
get: function () { return getCommandAndAncestors[k]; }
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
Object.keys(parseOptionFlags).forEach(function (k) {
|
|
51
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
52
|
+
enumerable: true,
|
|
53
|
+
get: function () { return parseOptionFlags[k]; }
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
Object.keys(types).forEach(function (k) {
|
|
57
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
58
|
+
enumerable: true,
|
|
59
|
+
get: function () { return types[k]; }
|
|
60
|
+
});
|
|
61
|
+
});
|
package/lib/index.d.cts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { A as ActionHandler, a as AllowedArgumentUsage, b as Argument, c as ArgumentOptions, d as ArgumentUsage, e as Arguments, B as BooleanOptionOptions, C as Command, H as Help, f as HookActionHandler, g as HookDefinition, h as HookPredicate, I as ICommand, i as IHelp, j as InferAddOptionResult, k as InferAddedArgumentType, L as Logger, O as Option, l as OptionOptions, m as OptionUsage, n as OptionalArgumentOptions, o as OptionalArgumentOptionsWithDefaultValue, p as OptionalArgumentUsage, q as OptionalOptionOptions, r as OptionalVariadicArgumentOptions, s as OptionalVariadicArgumentUsage, t as OptionalVariadicOptionOptions, u as Options, P as ParseArgvResult, R as RequiredArgumentOptions, v as RequiredArgumentUsage, w as RequiredOptionOptions, x as RequiredVariadicArgumentOptions, y as RequiredVariadicArgumentUsage, z as RequiredVariadicOptionOptions, S as SubCommands } from './Command--Ojy3KHE.cjs';
|
|
2
|
+
export { findCommand } from './lib/helpers/findCommand.cjs';
|
|
3
|
+
export { findOption } from './lib/helpers/findOption.cjs';
|
|
4
|
+
export { getCommandAncestors } from './lib/helpers/getCommandAncestors.cjs';
|
|
5
|
+
export { getCommandAndAncestors } from './lib/helpers/getCommandAndAncestors.cjs';
|
|
6
|
+
export { parseOptionFlags } from './lib/helpers/parseOptionFlags.cjs';
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { A as ActionHandler, a as AllowedArgumentUsage, b as Argument, c as ArgumentOptions, d as ArgumentUsage, e as Arguments, B as BooleanOptionOptions, C as Command, H as Help, f as HookActionHandler, g as HookDefinition, h as HookPredicate, I as ICommand, i as IHelp, j as InferAddOptionResult, k as InferAddedArgumentType, L as Logger, O as Option, l as OptionOptions, m as OptionUsage, n as OptionalArgumentOptions, o as OptionalArgumentOptionsWithDefaultValue, p as OptionalArgumentUsage, q as OptionalOptionOptions, r as OptionalVariadicArgumentOptions, s as OptionalVariadicArgumentUsage, t as OptionalVariadicOptionOptions, u as Options, P as ParseArgvResult, R as RequiredArgumentOptions, v as RequiredArgumentUsage, w as RequiredOptionOptions, x as RequiredVariadicArgumentOptions, y as RequiredVariadicArgumentUsage, z as RequiredVariadicOptionOptions, S as SubCommands } from './Command--Ojy3KHE.js';
|
|
2
|
+
export { findCommand } from './lib/helpers/findCommand.js';
|
|
3
|
+
export { findOption } from './lib/helpers/findOption.js';
|
|
4
|
+
export { getCommandAncestors } from './lib/helpers/getCommandAncestors.js';
|
|
5
|
+
export { getCommandAndAncestors } from './lib/helpers/getCommandAndAncestors.js';
|
|
6
|
+
export { parseOptionFlags } from './lib/helpers/parseOptionFlags.js';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './lib/Command';
|
|
2
|
+
export * from './lib/Help';
|
|
3
|
+
export * from './lib/helpers/findCommand';
|
|
4
|
+
export * from './lib/helpers/findOption';
|
|
5
|
+
export * from './lib/helpers/getCommandAncestors';
|
|
6
|
+
export * from './lib/helpers/getCommandAndAncestors';
|
|
7
|
+
export * from './lib/helpers/parseOptionFlags';
|
|
8
|
+
export * from './lib/types';
|