cli-kiss 0.2.3 → 0.2.4
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 +1 -1
- package/dist/index.d.ts +643 -667
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/docs/.vitepress/config.mts +1 -3
- package/docs/.vitepress/theme/index.ts +4 -0
- package/docs/.vitepress/theme/style.css +4 -0
- package/docs/guide/02_commands.md +67 -31
- package/docs/guide/03_options.md +11 -10
- package/docs/guide/05_types.md +1 -1
- package/docs/guide/06_run.md +1 -1
- package/docs/index.md +8 -3
- package/docs/public/hero.png +0 -0
- package/package.json +1 -1
- package/src/lib/Command.ts +36 -93
- package/src/lib/Operation.ts +12 -30
- package/src/lib/Option.ts +120 -96
- package/src/lib/Positional.ts +9 -34
- package/src/lib/Reader.ts +122 -98
- package/src/lib/Run.ts +30 -17
- package/src/lib/Type.ts +26 -22
- package/src/lib/Typo.ts +62 -83
- package/src/lib/Usage.ts +174 -78
- package/tests/unit.Reader.aliases.ts +31 -15
- package/tests/unit.Reader.commons.ts +99 -43
- package/tests/unit.Reader.shortBig.ts +75 -31
- package/tests/unit.command.execute.ts +76 -33
- package/tests/unit.command.usage.ts +35 -35
- package/tests/unit.runner.cycle.ts +13 -13
- package/tests/unit.runner.errors.ts +19 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Opaque key
|
|
3
|
-
* Returned by {@link ReaderArgs.registerOption}; passed to {@link ReaderArgs.getOptionValues}.
|
|
2
|
+
* Opaque key returned by {@link ReaderArgs.registerOption}.
|
|
4
3
|
*/
|
|
5
4
|
type ReaderOptionKey = (string | {
|
|
6
5
|
__brand: "ReaderOptionKey";
|
|
@@ -8,35 +7,44 @@ type ReaderOptionKey = (string | {
|
|
|
8
7
|
__brand: "ReaderOptionKey";
|
|
9
8
|
};
|
|
10
9
|
/**
|
|
11
|
-
*
|
|
12
|
-
|
|
10
|
+
* Parsing behaviour for a registered option, passed to {@link ReaderArgs.registerOption}.
|
|
11
|
+
*/
|
|
12
|
+
type ReaderOptionParsing = {
|
|
13
|
+
consumeShortGroup: boolean;
|
|
14
|
+
consumeNextArg: (inlined: string | null, separated: Array<string>, next: string | undefined) => boolean;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Result of parsing an option, including its inlined value and any following separated values.
|
|
18
|
+
*/
|
|
19
|
+
type ReaderOptionValue = {
|
|
20
|
+
inlined: string | null;
|
|
21
|
+
separated: Array<string>;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Option registration/query interface. Subset of {@link ReaderArgs}.
|
|
13
25
|
*/
|
|
14
26
|
type ReaderOptions = {
|
|
15
27
|
/**
|
|
16
|
-
* Registers an option
|
|
28
|
+
* Registers an option; all `longs` and `shorts` share the same key.
|
|
17
29
|
*
|
|
18
30
|
* @param definition.longs - Long-form names (without `--`).
|
|
19
31
|
* @param definition.shorts - Short-form names (without `-`).
|
|
20
|
-
* @param definition.
|
|
32
|
+
* @param definition.parsing - Parsing behaviour.
|
|
21
33
|
* @returns A {@link ReaderOptionKey} for later retrieval.
|
|
22
34
|
* @throws `Error` if a name is already registered or short names overlap.
|
|
23
35
|
*/
|
|
24
36
|
registerOption(definition: {
|
|
25
37
|
longs: Array<string>;
|
|
26
38
|
shorts: Array<string>;
|
|
27
|
-
|
|
39
|
+
parsing: ReaderOptionParsing;
|
|
28
40
|
}): ReaderOptionKey;
|
|
29
41
|
/**
|
|
30
|
-
* Returns all values collected for
|
|
31
|
-
*
|
|
32
|
-
* @param key - Key from {@link ReaderOptions.registerOption}.
|
|
33
|
-
* @returns Raw string values, one per occurrence; empty if never provided.
|
|
34
|
-
* @throws `Error` if `key` was not registered.
|
|
42
|
+
* Returns all values collected for `key`.
|
|
35
43
|
*/
|
|
36
|
-
getOptionValues(key: ReaderOptionKey): Array<
|
|
44
|
+
getOptionValues(key: ReaderOptionKey): Array<ReaderOptionValue>;
|
|
37
45
|
};
|
|
38
46
|
/**
|
|
39
|
-
* Positional
|
|
47
|
+
* Positional consumption interface. Subset of {@link ReaderArgs}.
|
|
40
48
|
*/
|
|
41
49
|
type ReaderPositionals = {
|
|
42
50
|
/**
|
|
@@ -64,31 +72,29 @@ declare class ReaderArgs {
|
|
|
64
72
|
constructor(args: ReadonlyArray<string>);
|
|
65
73
|
/**
|
|
66
74
|
* Registers an option; all `longs` and `shorts` share the same key.
|
|
67
|
-
* Short names
|
|
68
|
-
* but must not be prefixes of one another.
|
|
75
|
+
* Short names must not be prefixes of one another.
|
|
69
76
|
*
|
|
70
77
|
* @param definition.longs - Long-form names (without `--`).
|
|
71
78
|
* @param definition.shorts - Short-form names (without `-`).
|
|
72
|
-
* @param definition.
|
|
79
|
+
* @param definition.parsing - Parsing behaviour.
|
|
73
80
|
* @returns A {@link ReaderOptionKey} for {@link ReaderArgs.getOptionValues}.
|
|
74
81
|
* @throws `Error` if any name is already registered or short names overlap.
|
|
75
82
|
*/
|
|
76
83
|
registerOption(definition: {
|
|
77
84
|
longs: Array<string>;
|
|
78
85
|
shorts: Array<string>;
|
|
79
|
-
|
|
86
|
+
parsing: ReaderOptionParsing;
|
|
80
87
|
}): ReaderOptionKey;
|
|
81
88
|
/**
|
|
82
|
-
* Returns all values collected for
|
|
89
|
+
* Returns all values collected for `key`.
|
|
83
90
|
*
|
|
84
91
|
* @param key - Key from {@link ReaderArgs.registerOption}.
|
|
85
|
-
* @returns
|
|
92
|
+
* @returns One entry per occurrence.
|
|
86
93
|
* @throws `Error` if `key` was not registered.
|
|
87
94
|
*/
|
|
88
|
-
getOptionValues(key: ReaderOptionKey): Array<
|
|
95
|
+
getOptionValues(key: ReaderOptionKey): Array<ReaderOptionValue>;
|
|
89
96
|
/**
|
|
90
|
-
* Returns the next
|
|
91
|
-
* Parse intervening options as side-effects.
|
|
97
|
+
* Returns the next positional token; parses intervening options as a side-effect.
|
|
92
98
|
* All tokens after `--` are treated as positionals.
|
|
93
99
|
*
|
|
94
100
|
* @returns The next positional, or `undefined` when exhausted.
|
|
@@ -109,11 +115,11 @@ declare class ReaderArgs {
|
|
|
109
115
|
*/
|
|
110
116
|
type Type<Value> = {
|
|
111
117
|
/**
|
|
112
|
-
* Human-readable name shown in help and
|
|
118
|
+
* Human-readable name shown in help and errors (e.g. `"String"`, `"Number"`).
|
|
113
119
|
*/
|
|
114
120
|
content: string;
|
|
115
121
|
/**
|
|
116
|
-
*
|
|
122
|
+
* Decodes a raw CLI string into `Value`.
|
|
117
123
|
*
|
|
118
124
|
* @param input - Raw string from the command line.
|
|
119
125
|
* @returns The decoded value.
|
|
@@ -122,19 +128,22 @@ type Type<Value> = {
|
|
|
122
128
|
decoder(input: string): Value;
|
|
123
129
|
};
|
|
124
130
|
/**
|
|
125
|
-
* Decodes
|
|
126
|
-
* Used
|
|
131
|
+
* Decodes a string to `boolean` (case-insensitive).
|
|
132
|
+
* Used by {@link optionFlag} for `--flag=<value>`.
|
|
127
133
|
*
|
|
128
134
|
* @example
|
|
129
135
|
* ```ts
|
|
136
|
+
* typeBoolean.decoder("true") // → true
|
|
130
137
|
* typeBoolean.decoder("yes") // → true
|
|
138
|
+
* typeBoolean.decoder("y") // → true
|
|
131
139
|
* typeBoolean.decoder("false") // → false
|
|
132
|
-
* typeBoolean.decoder("
|
|
140
|
+
* typeBoolean.decoder("no") // → false
|
|
141
|
+
* typeBoolean.decoder("n") // → false
|
|
133
142
|
* ```
|
|
134
143
|
*/
|
|
135
144
|
declare const typeBoolean: Type<boolean>;
|
|
136
145
|
/**
|
|
137
|
-
* Parses a date/time string via `Date.parse
|
|
146
|
+
* Parses a date/time string via `Date.parse`.
|
|
138
147
|
* Accepts any format supported by `Date.parse`, including ISO 8601.
|
|
139
148
|
*
|
|
140
149
|
* @example
|
|
@@ -146,8 +155,7 @@ declare const typeBoolean: Type<boolean>;
|
|
|
146
155
|
*/
|
|
147
156
|
declare const typeDate: Type<Date>;
|
|
148
157
|
/**
|
|
149
|
-
* Parses a string
|
|
150
|
-
* Accepts integers, floats, and scientific notation; `NaN` throws a {@link TypoError}.
|
|
158
|
+
* Parses a string to `number` via `Number()`; `NaN` throws {@link TypoError}.
|
|
151
159
|
*
|
|
152
160
|
* @example
|
|
153
161
|
* ```ts
|
|
@@ -158,8 +166,8 @@ declare const typeDate: Type<Date>;
|
|
|
158
166
|
*/
|
|
159
167
|
declare const typeNumber: Type<number>;
|
|
160
168
|
/**
|
|
161
|
-
* Parses an integer string
|
|
162
|
-
* Floats and non-numeric strings throw
|
|
169
|
+
* Parses an integer string to `bigint` via `BigInt()`.
|
|
170
|
+
* Floats and non-numeric strings throw {@link TypoError}.
|
|
163
171
|
*
|
|
164
172
|
* @example
|
|
165
173
|
* ```ts
|
|
@@ -170,8 +178,8 @@ declare const typeNumber: Type<number>;
|
|
|
170
178
|
*/
|
|
171
179
|
declare const typeInteger: Type<bigint>;
|
|
172
180
|
/**
|
|
173
|
-
* Parses an absolute URL string
|
|
174
|
-
* Relative or malformed URLs throw
|
|
181
|
+
* Parses an absolute URL string to a `URL` object.
|
|
182
|
+
* Relative or malformed URLs throw {@link TypoError}.
|
|
175
183
|
*
|
|
176
184
|
* @example
|
|
177
185
|
* ```ts
|
|
@@ -191,7 +199,7 @@ declare const typeUrl: Type<URL>;
|
|
|
191
199
|
*/
|
|
192
200
|
declare const typeString: Type<string>;
|
|
193
201
|
/**
|
|
194
|
-
*
|
|
202
|
+
* Chains `before`'s decoder with an `after` transformation.
|
|
195
203
|
* `before` errors are prefixed with `"from: <content>"` for traceability.
|
|
196
204
|
*
|
|
197
205
|
* @typeParam Before - Intermediate type from `before.decoder`.
|
|
@@ -221,8 +229,8 @@ declare function typeMapped<Before, After>(before: Type<Before>, after: {
|
|
|
221
229
|
decoder: (value: Before) => After;
|
|
222
230
|
}): Type<After>;
|
|
223
231
|
/**
|
|
224
|
-
* Creates a {@link Type}`<string>`
|
|
225
|
-
* Out-of-set inputs throw
|
|
232
|
+
* Creates a {@link Type}`<string>` that only accepts a fixed set of values.
|
|
233
|
+
* Out-of-set inputs throw {@link TypoError} listing up to 5 valid options.
|
|
226
234
|
*
|
|
227
235
|
* @param content - Name shown in help and errors (e.g. `"Environment"`).
|
|
228
236
|
* @param values - Ordered list of accepted values.
|
|
@@ -237,7 +245,7 @@ declare function typeMapped<Before, After>(before: Type<Before>, after: {
|
|
|
237
245
|
*/
|
|
238
246
|
declare function typeOneOf<const Value extends string>(content: string, values: Array<Value>): Type<Value>;
|
|
239
247
|
/**
|
|
240
|
-
* Splits a delimited string into a
|
|
248
|
+
* Splits a delimited string into a typed tuple.
|
|
241
249
|
* Each part is decoded by the corresponding element type; wrong count or decode failure throws {@link TypoError}.
|
|
242
250
|
*
|
|
243
251
|
* @typeParam Elements - Tuple of decoded value types (inferred from `elementTypes`).
|
|
@@ -258,7 +266,7 @@ declare function typeTuple<const Elements extends Array<any>>(elementTypes: {
|
|
|
258
266
|
[K in keyof Elements]: Type<Elements[K]>;
|
|
259
267
|
}, separator?: string): Type<Elements>;
|
|
260
268
|
/**
|
|
261
|
-
* Splits a delimited string into a
|
|
269
|
+
* Splits a delimited string into a typed array.
|
|
262
270
|
* Each part is decoded by `elementType`; failed decodes throw {@link TypoError}.
|
|
263
271
|
* Note: splitting an empty string yields one empty element — prefer {@link optionRepeatable} for a zero-default.
|
|
264
272
|
*
|
|
@@ -281,257 +289,580 @@ declare function typeTuple<const Elements extends Array<any>>(elementTypes: {
|
|
|
281
289
|
declare function typeList<Value>(elementType: Type<Value>, separator?: string): Type<Array<Value>>;
|
|
282
290
|
|
|
283
291
|
/**
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
* Created with {@link optionFlag}, {@link optionSingleValue}, or
|
|
287
|
-
* {@link optionRepeatable} and passed via the `options` map of {@link operation}.
|
|
288
|
-
*
|
|
289
|
-
* @typeParam Value - Decoded value type.
|
|
292
|
+
* Color names for terminal styling, used by {@link TypoStyle}.
|
|
293
|
+
* `dark*` = standard ANSI (30–37); `bright*` = high-intensity (90–97).
|
|
290
294
|
*/
|
|
291
|
-
type
|
|
292
|
-
/**
|
|
293
|
-
* Returns metadata used to render the `Options:` section of help.
|
|
294
|
-
*/
|
|
295
|
-
generateUsage(): OptionUsage;
|
|
296
|
-
/**
|
|
297
|
-
* Registers the option on `readerOptions` and returns an {@link OptionDecoder}.
|
|
298
|
-
*/
|
|
299
|
-
registerAndMakeDecoder(readerOptions: ReaderArgs): OptionDecoder<Value>;
|
|
300
|
-
};
|
|
295
|
+
type TypoColor = "darkBlack" | "darkRed" | "darkGreen" | "darkYellow" | "darkBlue" | "darkMagenta" | "darkCyan" | "darkWhite" | "brightBlack" | "brightRed" | "brightGreen" | "brightYellow" | "brightBlue" | "brightMagenta" | "brightCyan" | "brightWhite";
|
|
301
296
|
/**
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
* @typeParam Value - Decoded value type.
|
|
297
|
+
* Visual styling applied by a {@link TypoSupport} instance.
|
|
298
|
+
* All fields are optional; ignored entirely in `"none"` mode.
|
|
305
299
|
*/
|
|
306
|
-
type
|
|
300
|
+
type TypoStyle = {
|
|
307
301
|
/**
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
* @throws {@link TypoError} if decoding failed.
|
|
302
|
+
* Foreground (text) color.
|
|
311
303
|
*/
|
|
312
|
-
|
|
313
|
-
};
|
|
314
|
-
/**
|
|
315
|
-
* Human-readable metadata for a single option, used to render the `Options:` section
|
|
316
|
-
* of the help output produced by {@link usageToStyledLines}.
|
|
317
|
-
*/
|
|
318
|
-
type OptionUsage = {
|
|
304
|
+
fgColor?: TypoColor;
|
|
319
305
|
/**
|
|
320
|
-
*
|
|
306
|
+
* Background color.
|
|
321
307
|
*/
|
|
322
|
-
|
|
308
|
+
bgColor?: TypoColor;
|
|
323
309
|
/**
|
|
324
|
-
*
|
|
310
|
+
* Reduced intensity.
|
|
325
311
|
*/
|
|
326
|
-
|
|
312
|
+
dim?: boolean;
|
|
327
313
|
/**
|
|
328
|
-
*
|
|
314
|
+
* Bold.
|
|
329
315
|
*/
|
|
330
|
-
|
|
316
|
+
bold?: boolean;
|
|
331
317
|
/**
|
|
332
|
-
*
|
|
318
|
+
* Italic.
|
|
333
319
|
*/
|
|
334
|
-
|
|
320
|
+
italic?: boolean;
|
|
335
321
|
/**
|
|
336
|
-
*
|
|
322
|
+
* Underline.
|
|
337
323
|
*/
|
|
338
|
-
|
|
324
|
+
underline?: boolean;
|
|
325
|
+
/**
|
|
326
|
+
* Strikethrough.
|
|
327
|
+
*/
|
|
328
|
+
strikethrough?: boolean;
|
|
339
329
|
};
|
|
340
330
|
/**
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
* Parsing: absent → `false`; `--flag` / `--flag=yes` → `true`; `--flag=no` → `false`;
|
|
344
|
-
* specified more than once → {@link TypoError}.
|
|
345
|
-
*
|
|
346
|
-
* @param definition - Flag configuration.
|
|
347
|
-
* @param definition.long - Long-form name (without `--`).
|
|
348
|
-
* @param definition.short - Short-form name (without `-`).
|
|
349
|
-
* @param definition.description - Help text.
|
|
350
|
-
* @param definition.hint - Short note shown in parentheses.
|
|
351
|
-
* @param definition.aliases - Additional names.
|
|
352
|
-
* @param definition.default - Default when absent. Defaults to `() => false`.
|
|
353
|
-
* @returns An {@link Option}`<boolean>`.
|
|
354
|
-
*
|
|
355
|
-
* @example
|
|
356
|
-
* ```ts
|
|
357
|
-
* const verboseFlag = optionFlag({
|
|
358
|
-
* long: "verbose",
|
|
359
|
-
* short: "v",
|
|
360
|
-
* description: "Enable verbose output",
|
|
361
|
-
* });
|
|
362
|
-
* ```
|
|
331
|
+
* Section title style. Bold dark-green.
|
|
363
332
|
*/
|
|
364
|
-
declare
|
|
365
|
-
long: Lowercase<string>;
|
|
366
|
-
short?: string;
|
|
367
|
-
description?: string;
|
|
368
|
-
hint?: string;
|
|
369
|
-
aliases?: {
|
|
370
|
-
longs?: Array<Lowercase<string>>;
|
|
371
|
-
shorts?: Array<string>;
|
|
372
|
-
};
|
|
373
|
-
default?: () => boolean;
|
|
374
|
-
}): Option<boolean>;
|
|
333
|
+
declare const typoStyleTitle: TypoStyle;
|
|
375
334
|
/**
|
|
376
|
-
*
|
|
377
|
-
*
|
|
378
|
-
* Parsing: absent → `default()`; once → decoded with `type`; more than once → {@link TypoError}.
|
|
379
|
-
* Value syntax: `--long value`, `--long=value`, `-s value`, `-s=value`, `-svalue`.
|
|
380
|
-
*
|
|
381
|
-
* @typeParam Value - Type produced by the decoder.
|
|
382
|
-
*
|
|
383
|
-
* @param definition - Option configuration.
|
|
384
|
-
* @param definition.long - Long-form name (without `--`).
|
|
385
|
-
* @param definition.short - Short-form name (without `-`).
|
|
386
|
-
* @param definition.description - Help text.
|
|
387
|
-
* @param definition.hint - Short note shown in parentheses.
|
|
388
|
-
* @param definition.aliases - Additional names.
|
|
389
|
-
* @param definition.label - Value placeholder in help. Defaults to uppercased `type.content`.
|
|
390
|
-
* @param definition.type - Decoder for the raw string value.
|
|
391
|
-
* @param definition.default - Default when absent. Throw to make the option required.
|
|
392
|
-
* @returns An {@link Option}`<Value>`.
|
|
393
|
-
*
|
|
394
|
-
* @example
|
|
395
|
-
* ```ts
|
|
396
|
-
* const outputOption = optionSingleValue({
|
|
397
|
-
* long: "output",
|
|
398
|
-
* short: "o",
|
|
399
|
-
* type: typeString,
|
|
400
|
-
* label: "PATH",
|
|
401
|
-
* description: "Output directory",
|
|
402
|
-
* default: () => "dist/",
|
|
403
|
-
* });
|
|
404
|
-
* ```
|
|
335
|
+
* Logic/type identifier style. Bold dark-magenta.
|
|
405
336
|
*/
|
|
406
|
-
declare
|
|
407
|
-
long: Lowercase<string>;
|
|
408
|
-
short?: string;
|
|
409
|
-
description?: string;
|
|
410
|
-
hint?: string;
|
|
411
|
-
aliases?: {
|
|
412
|
-
longs?: Array<Lowercase<string>>;
|
|
413
|
-
shorts?: Array<string>;
|
|
414
|
-
};
|
|
415
|
-
label?: Uppercase<string>;
|
|
416
|
-
type: Type<Value>;
|
|
417
|
-
default: () => Value;
|
|
418
|
-
}): Option<Value>;
|
|
337
|
+
declare const typoStyleLogic: TypoStyle;
|
|
419
338
|
/**
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
* Parsing: absent → `[]`; N occurrences → array of N decoded values in order.
|
|
423
|
-
* Value syntax: `--long value`, `--long=value`, `-s value`, `-s=value`, `-svalue`.
|
|
424
|
-
*
|
|
425
|
-
* @typeParam Value - Type produced by the decoder for each occurrence.
|
|
426
|
-
*
|
|
427
|
-
* @param definition - Option configuration.
|
|
428
|
-
* @param definition.long - Long-form name (without `--`).
|
|
429
|
-
* @param definition.short - Short-form name (without `-`).
|
|
430
|
-
* @param definition.description - Help text.
|
|
431
|
-
* @param definition.hint - Short note shown in parentheses.
|
|
432
|
-
* @param definition.aliases - Additional names.
|
|
433
|
-
* @param definition.label - Value placeholder in help. Defaults to uppercased `type.content`.
|
|
434
|
-
* @param definition.type - Decoder applied to each raw string value.
|
|
435
|
-
* @returns An {@link Option}`<Array<Value>>`.
|
|
436
|
-
*
|
|
437
|
-
* @example
|
|
438
|
-
* ```ts
|
|
439
|
-
* const filesOption = optionRepeatable({
|
|
440
|
-
* long: "file",
|
|
441
|
-
* short: "f",
|
|
442
|
-
* type: typeString,
|
|
443
|
-
* label: "PATH",
|
|
444
|
-
* description: "Input file (may be repeated)",
|
|
445
|
-
* });
|
|
446
|
-
* // Usage: my-cli --file a.ts --file b.ts → ["a.ts", "b.ts"]
|
|
447
|
-
* ```
|
|
339
|
+
* Quoted user-input style. Bold dark-yellow.
|
|
448
340
|
*/
|
|
449
|
-
declare
|
|
450
|
-
long: Lowercase<string>;
|
|
451
|
-
short?: string;
|
|
452
|
-
description?: string;
|
|
453
|
-
hint?: string;
|
|
454
|
-
aliases?: {
|
|
455
|
-
longs?: Array<Lowercase<string>>;
|
|
456
|
-
shorts?: Array<string>;
|
|
457
|
-
};
|
|
458
|
-
label?: Uppercase<string>;
|
|
459
|
-
type: Type<Value>;
|
|
460
|
-
}): Option<Array<Value>>;
|
|
461
|
-
|
|
341
|
+
declare const typoStyleQuote: TypoStyle;
|
|
462
342
|
/**
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
* Created with {@link positionalRequired}, {@link positionalOptional}, or
|
|
466
|
-
* {@link positionalVariadics} and passed via the `positionals` array of
|
|
467
|
-
* {@link operation}, consumed in declaration order.
|
|
468
|
-
*
|
|
469
|
-
* @typeParam Value - Decoded value type.
|
|
343
|
+
* Error label style. Bold dark-red.
|
|
470
344
|
*/
|
|
471
|
-
|
|
345
|
+
declare const typoStyleFailure: TypoStyle;
|
|
346
|
+
/**
|
|
347
|
+
* Option/command name style. Bold dark-cyan.
|
|
348
|
+
*/
|
|
349
|
+
declare const typoStyleConstants: TypoStyle;
|
|
350
|
+
/**
|
|
351
|
+
* Positional/user-input label style. Bold dark-blue.
|
|
352
|
+
*/
|
|
353
|
+
declare const typoStyleUserInput: TypoStyle;
|
|
354
|
+
/**
|
|
355
|
+
* Strong text style. Bold.
|
|
356
|
+
*/
|
|
357
|
+
declare const typoStyleRegularStrong: TypoStyle;
|
|
358
|
+
/**
|
|
359
|
+
* Subtle text style. Italic and dim.
|
|
360
|
+
*/
|
|
361
|
+
declare const typoStyleRegularWeaker: TypoStyle;
|
|
362
|
+
/**
|
|
363
|
+
* Immutable styled string segment. Compose into a {@link TypoText}.
|
|
364
|
+
*/
|
|
365
|
+
declare class TypoString {
|
|
366
|
+
#private;
|
|
472
367
|
/**
|
|
473
|
-
*
|
|
368
|
+
* @param value - Raw text content.
|
|
369
|
+
* @param typoStyle - Style to apply when rendering. Defaults to `{}` (no style).
|
|
474
370
|
*/
|
|
475
|
-
|
|
371
|
+
constructor(value: string, typoStyle?: TypoStyle);
|
|
476
372
|
/**
|
|
477
|
-
*
|
|
478
|
-
*
|
|
373
|
+
* Returns the text styled by `typoSupport`.
|
|
374
|
+
*
|
|
375
|
+
* @param typoSupport - Rendering mode.
|
|
479
376
|
*/
|
|
480
|
-
|
|
481
|
-
|
|
377
|
+
computeStyledString(typoSupport: TypoSupport): string;
|
|
378
|
+
/**
|
|
379
|
+
* Returns the raw text.
|
|
380
|
+
*/
|
|
381
|
+
getRawString(): string;
|
|
382
|
+
}
|
|
482
383
|
/**
|
|
483
|
-
*
|
|
484
|
-
*
|
|
485
|
-
* @typeParam Value - Decoded value type.
|
|
384
|
+
* Mutable sequence of {@link TypoString} segments.
|
|
486
385
|
*/
|
|
487
|
-
|
|
386
|
+
declare class TypoText {
|
|
387
|
+
#private;
|
|
488
388
|
/**
|
|
489
|
-
*
|
|
389
|
+
* @param segments - Initial text segments
|
|
390
|
+
*/
|
|
391
|
+
constructor(...segments: Array<TypoText | Array<TypoString> | TypoString | string>);
|
|
392
|
+
/**
|
|
393
|
+
* Appends new text segment(s).
|
|
490
394
|
*
|
|
491
|
-
* @
|
|
395
|
+
* @param segment - Text segment(s) to append.
|
|
492
396
|
*/
|
|
493
|
-
|
|
494
|
-
};
|
|
495
|
-
/**
|
|
496
|
-
* Human-readable metadata for a single positional argument, used to render the
|
|
497
|
-
* `Positionals:` section of the help output produced by {@link usageToStyledLines}.
|
|
498
|
-
*/
|
|
499
|
-
type PositionalUsage = {
|
|
397
|
+
push(segment: TypoText | Array<TypoString> | TypoString | string): void;
|
|
500
398
|
/**
|
|
501
|
-
*
|
|
399
|
+
* Renders all segments into a single styled string.
|
|
400
|
+
*
|
|
401
|
+
* @param typoSupport - Rendering mode.
|
|
402
|
+
* @returns Concatenated styled string.
|
|
502
403
|
*/
|
|
503
|
-
|
|
404
|
+
computeStyledString(typoSupport: TypoSupport): string;
|
|
504
405
|
/**
|
|
505
|
-
*
|
|
406
|
+
* Returns the concatenated raw text.
|
|
506
407
|
*/
|
|
507
|
-
|
|
408
|
+
computeRawString(): string;
|
|
508
409
|
/**
|
|
509
|
-
*
|
|
510
|
-
* Required: `<NAME>`, optional: `[NAME]`, variadic: `[NAME]...`.
|
|
410
|
+
* Returns the total raw character count.
|
|
511
411
|
*/
|
|
512
|
-
|
|
513
|
-
}
|
|
412
|
+
computeRawLength(): number;
|
|
413
|
+
}
|
|
514
414
|
/**
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
415
|
+
* Column-aligned grid of {@link TypoText} cells.
|
|
416
|
+
* Each column is padded to the widest cell (raw chars); the last column is not padded.
|
|
417
|
+
*/
|
|
418
|
+
declare class TypoGrid {
|
|
419
|
+
#private;
|
|
420
|
+
constructor();
|
|
421
|
+
/**
|
|
422
|
+
* Appends a row. All rows should have the same cell count.
|
|
423
|
+
*
|
|
424
|
+
* @param cells - Ordered {@link TypoText} cells.
|
|
425
|
+
*/
|
|
426
|
+
pushRow(cells: Array<TypoText>): void;
|
|
427
|
+
/**
|
|
428
|
+
* Renders as an array of styled, column-padded strings.
|
|
429
|
+
*
|
|
430
|
+
* @param typoSupport - Rendering mode.
|
|
431
|
+
* @returns 2-D array of styled strings.
|
|
432
|
+
*/
|
|
433
|
+
computeStyledLines(typoSupport: TypoSupport): Array<string>;
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* `Error` subclass with a {@link TypoText} styled message for rich terminal output.
|
|
437
|
+
* Chains `TypoError` sources after `": "`.
|
|
438
|
+
*/
|
|
439
|
+
declare class TypoError extends Error {
|
|
440
|
+
#private;
|
|
441
|
+
/**
|
|
442
|
+
* @param currentTypoText - Styled message for this error.
|
|
443
|
+
* @param source - Optional cause; `TypoError` chains styled text, `Error` appends `.message`, else `String()`.
|
|
444
|
+
*/
|
|
445
|
+
constructor(currentTypoText: TypoText, source?: unknown);
|
|
446
|
+
/**
|
|
447
|
+
* Renders the styled message (without `"Error:"` prefix).
|
|
448
|
+
*
|
|
449
|
+
* @param typoSupport - Rendering mode.
|
|
450
|
+
* @returns Styled error string.
|
|
451
|
+
*/
|
|
452
|
+
computeStyledString(typoSupport: TypoSupport): string;
|
|
453
|
+
/**
|
|
454
|
+
* Runs `thrower`; wraps any thrown error as a `TypoError` with `context()` prepended.
|
|
455
|
+
*
|
|
456
|
+
* @typeParam Value - Return type of `thrower`.
|
|
457
|
+
* @param thrower - Function to execute; result passed through on success.
|
|
458
|
+
* @param context - Produces the {@link TypoText} prepended to the caught error.
|
|
459
|
+
* @returns Value from `thrower`.
|
|
460
|
+
* @throws `TypoError` wrapping the original error with context prepended.
|
|
461
|
+
*/
|
|
462
|
+
static tryWithContext<Value>(thrower: () => Value, context: () => TypoText): Value;
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Controls ANSI terminal styling. Create via the static factory methods.
|
|
466
|
+
*/
|
|
467
|
+
declare class TypoSupport {
|
|
468
|
+
#private;
|
|
469
|
+
private constructor();
|
|
470
|
+
/**
|
|
471
|
+
* Plain text — no ANSI codes.
|
|
472
|
+
*/
|
|
473
|
+
static none(): TypoSupport;
|
|
474
|
+
/**
|
|
475
|
+
* ANSI escape codes for color terminals.
|
|
476
|
+
*/
|
|
477
|
+
static tty(): TypoSupport;
|
|
478
|
+
/**
|
|
479
|
+
* Deterministic textual styling for snapshot tests.
|
|
480
|
+
* Style flags appear as suffixes: `{text}@color`, `{text}+` (bold), `{text}-` (dim),
|
|
481
|
+
* `{text}*` (italic), `{text}_` (underline), `{text}~` (strikethrough).
|
|
482
|
+
*/
|
|
483
|
+
static mock(): TypoSupport;
|
|
484
|
+
/**
|
|
485
|
+
* Auto-detects styling mode from the process environment.
|
|
486
|
+
* `FORCE_COLOR=0` / `NO_COLOR` → none; `FORCE_COLOR` (truthy) / `isTTY` → tty; else → none.
|
|
487
|
+
* Falls back to none if `process` is unavailable.
|
|
488
|
+
*/
|
|
489
|
+
static inferFromProcess(): TypoSupport;
|
|
490
|
+
/**
|
|
491
|
+
* Applies `typoStyle` to `value` according to the current mode.
|
|
492
|
+
*
|
|
493
|
+
* @param value - Raw text.
|
|
494
|
+
* @param typoStyle - Style to apply.
|
|
495
|
+
* @returns Styled string.
|
|
496
|
+
*/
|
|
497
|
+
computeStyledString(value: string, typoStyle: TypoStyle): string;
|
|
498
|
+
/**
|
|
499
|
+
* Formats any thrown value as `"Error: <message>"` with {@link typoStyleFailure} on the prefix.
|
|
500
|
+
*
|
|
501
|
+
* @param error - Any thrown value.
|
|
502
|
+
* @returns Styled error string.
|
|
503
|
+
*/
|
|
504
|
+
computeStyledErrorMessage(error: unknown): string;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Usage model. Produced by {@link CommandDecoder.generateUsage}, consumed by {@link usageToStyledLines}.
|
|
509
|
+
*/
|
|
510
|
+
type UsageCommand = {
|
|
511
|
+
/**
|
|
512
|
+
* Segments of the usage line
|
|
513
|
+
* (e.g. `my-cli <POSITIONAL> subcommand <ANOTHER_POSITIONAL>`).
|
|
514
|
+
*/
|
|
515
|
+
segments: Array<UsageSegment>;
|
|
516
|
+
/**
|
|
517
|
+
* Command metadata.
|
|
518
|
+
*/
|
|
519
|
+
information: CommandInformation;
|
|
520
|
+
/**
|
|
521
|
+
* Positionals in declaration order.
|
|
522
|
+
*/
|
|
523
|
+
positionals: Array<UsagePositional>;
|
|
524
|
+
/**
|
|
525
|
+
* Subcommands, populated when none was selected.
|
|
526
|
+
*/
|
|
527
|
+
subcommands: Array<UsageSubcommand>;
|
|
528
|
+
/**
|
|
529
|
+
* Options in registration order.
|
|
530
|
+
*/
|
|
531
|
+
options: Array<UsageOption>;
|
|
532
|
+
};
|
|
533
|
+
/**
|
|
534
|
+
* One segment of the usage line.
|
|
535
|
+
*/
|
|
536
|
+
type UsageSegment = {
|
|
537
|
+
positional: string;
|
|
538
|
+
} | {
|
|
539
|
+
subcommand: string;
|
|
540
|
+
};
|
|
541
|
+
/**
|
|
542
|
+
* Usage metadata. Produced by {@link Operation.generateUsage}, consumed when building {@link UsageCommand}.
|
|
543
|
+
*/
|
|
544
|
+
type UsageOperation = {
|
|
545
|
+
/**
|
|
546
|
+
* Registered options.
|
|
547
|
+
*/
|
|
548
|
+
options: Array<UsageOption>;
|
|
549
|
+
/**
|
|
550
|
+
* Declared positionals, in order.
|
|
551
|
+
*/
|
|
552
|
+
positionals: Array<UsagePositional>;
|
|
553
|
+
};
|
|
554
|
+
/**
|
|
555
|
+
* Positional metadata for the `Positionals:` section of help.
|
|
556
|
+
*/
|
|
557
|
+
type UsagePositional = {
|
|
558
|
+
/**
|
|
559
|
+
* Help text.
|
|
560
|
+
*/
|
|
561
|
+
description: string | undefined;
|
|
562
|
+
/**
|
|
563
|
+
* Short note shown in parentheses.
|
|
564
|
+
*/
|
|
565
|
+
hint: string | undefined;
|
|
566
|
+
/**
|
|
567
|
+
* Placeholder label shown in the usage line and the `Positionals:` section.
|
|
568
|
+
* Required: `<NAME>`, optional: `[NAME]`, variadic: `[NAME]...`.
|
|
569
|
+
*/
|
|
570
|
+
label: Uppercase<string>;
|
|
571
|
+
};
|
|
572
|
+
/**
|
|
573
|
+
* Entry in the `Subcommands:` section.
|
|
574
|
+
*/
|
|
575
|
+
type UsageSubcommand = {
|
|
576
|
+
/**
|
|
577
|
+
* Token the user types (e.g. `"deploy"`).
|
|
578
|
+
*/
|
|
579
|
+
name: string;
|
|
580
|
+
/**
|
|
581
|
+
* From {@link CommandInformation.description}.
|
|
582
|
+
*/
|
|
583
|
+
description: string | undefined;
|
|
584
|
+
/**
|
|
585
|
+
* From {@link CommandInformation.hint}.
|
|
586
|
+
*/
|
|
587
|
+
hint: string | undefined;
|
|
588
|
+
};
|
|
589
|
+
/**
|
|
590
|
+
* Option metadata for the `Options:` section of help.
|
|
591
|
+
*/
|
|
592
|
+
type UsageOption = {
|
|
593
|
+
/**
|
|
594
|
+
* Short-form name without `-` (e.g. `"v"`).
|
|
595
|
+
*/
|
|
596
|
+
short: string | undefined;
|
|
597
|
+
/**
|
|
598
|
+
* Long-form name without `--` (e.g. `"verbose"`).
|
|
599
|
+
*/
|
|
600
|
+
long: Lowercase<string>;
|
|
601
|
+
/**
|
|
602
|
+
* Extra annotation appended to the option label in help (e.g. `[=no]`, ` [*]`).
|
|
603
|
+
*/
|
|
604
|
+
annotation: string | undefined;
|
|
605
|
+
/**
|
|
606
|
+
* Help text.
|
|
607
|
+
*/
|
|
608
|
+
description: string | undefined;
|
|
609
|
+
/**
|
|
610
|
+
* Short note shown in parentheses.
|
|
611
|
+
*/
|
|
612
|
+
hint: string | undefined;
|
|
613
|
+
/**
|
|
614
|
+
* Value placeholder in help (e.g. `"<FILE>"`). `undefined` for flags.
|
|
615
|
+
*/
|
|
616
|
+
label: Uppercase<string> | undefined;
|
|
617
|
+
};
|
|
618
|
+
/**
|
|
619
|
+
* Converts a {@link UsageCommand} model into an array of styled lines ready to be
|
|
620
|
+
* joined with `"\n"` and printed to the terminal.
|
|
621
|
+
*
|
|
622
|
+
* The output format is:
|
|
623
|
+
* ```
|
|
624
|
+
* Usage: <cliName> [segments...]
|
|
625
|
+
*
|
|
626
|
+
* <description> (<hint>)
|
|
627
|
+
* <detail lines...>
|
|
628
|
+
*
|
|
629
|
+
* Positionals:
|
|
630
|
+
* <LABEL> <description> (<hint>)
|
|
631
|
+
*
|
|
632
|
+
* Subcommands:
|
|
633
|
+
* <name> <description> (<hint>)
|
|
634
|
+
*
|
|
635
|
+
* Options:
|
|
636
|
+
* -s, --long <LABEL><annotation> <description> (<hint>)
|
|
637
|
+
*
|
|
638
|
+
* Examples:
|
|
639
|
+
* <description>
|
|
640
|
+
* <command line>
|
|
641
|
+
*
|
|
642
|
+
* ```
|
|
643
|
+
* Sections that have no entries are omitted. The trailing empty line is always included.
|
|
644
|
+
*
|
|
645
|
+
* Column alignment per section via {@link TypoGrid}.
|
|
646
|
+
*
|
|
647
|
+
* @param params.cliName - Program name for the usage line.
|
|
648
|
+
* @param params.usage - From {@link CommandDecoder.generateUsage}.
|
|
649
|
+
* @param params.typoSupport - Rendering mode.
|
|
650
|
+
* @returns Styled lines; includes a trailing empty string.
|
|
651
|
+
*
|
|
652
|
+
* @example
|
|
653
|
+
* ```ts
|
|
654
|
+
* const lines = usageToStyledLines({
|
|
655
|
+
* cliName: "my-cli",
|
|
656
|
+
* usage: commandDecoder.generateUsage(),
|
|
657
|
+
* typoSupport: TypoSupport.tty(),
|
|
658
|
+
* });
|
|
659
|
+
* process.stdout.write(lines.join("\n"));
|
|
660
|
+
* ```
|
|
661
|
+
*/
|
|
662
|
+
declare function usageToStyledLines(params: {
|
|
663
|
+
cliName: Lowercase<string>;
|
|
664
|
+
usage: UsageCommand;
|
|
665
|
+
typoSupport: TypoSupport;
|
|
666
|
+
}): string[];
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* A CLI option. Created with {@link optionFlag}, {@link optionSingleValue},
|
|
670
|
+
* or {@link optionRepeatable}.
|
|
671
|
+
*
|
|
672
|
+
* @typeParam Value - Decoded value type.
|
|
673
|
+
*/
|
|
674
|
+
type Option<Value> = {
|
|
675
|
+
/**
|
|
676
|
+
* Returns metadata for the `Options:` section.
|
|
677
|
+
*/
|
|
678
|
+
generateUsage(): UsageOption;
|
|
679
|
+
/**
|
|
680
|
+
* Registers the option on `readerOptions` and returns an {@link OptionDecoder}.
|
|
681
|
+
*/
|
|
682
|
+
registerAndMakeDecoder(readerOptions: ReaderArgs): OptionDecoder<Value>;
|
|
683
|
+
};
|
|
684
|
+
/**
|
|
685
|
+
* Produced by {@link Option.registerAndMakeDecoder}.
|
|
686
|
+
*
|
|
687
|
+
* @typeParam Value - Decoded value type.
|
|
688
|
+
*/
|
|
689
|
+
type OptionDecoder<Value> = {
|
|
690
|
+
/**
|
|
691
|
+
* Returns the decoded option value.
|
|
692
|
+
*
|
|
693
|
+
* @throws {@link TypoError} if decoding failed.
|
|
694
|
+
*/
|
|
695
|
+
getAndDecodeValue(): Value;
|
|
696
|
+
};
|
|
697
|
+
/**
|
|
698
|
+
* Creates a boolean flag option (`--verbose`, optionally `--flag=no`).
|
|
699
|
+
*
|
|
700
|
+
* Parsing: absent → `false`; `--flag` / `--flag=yes` → `true`; `--flag=no` → `false`;
|
|
701
|
+
* specified more than once → {@link TypoError}.
|
|
702
|
+
*
|
|
703
|
+
* @param definition.long - Long-form name (without `--`).
|
|
704
|
+
* @param definition.short - Short-form name (without `-`).
|
|
705
|
+
* @param definition.description - Help text.
|
|
706
|
+
* @param definition.hint - Short note shown in parentheses.
|
|
707
|
+
* @param definition.aliases - Additional names.
|
|
708
|
+
* @param definition.default - Default when absent. Defaults to `false`.
|
|
709
|
+
* @returns An {@link Option}`<boolean>`.
|
|
710
|
+
*
|
|
711
|
+
* @example
|
|
712
|
+
* ```ts
|
|
713
|
+
* const verboseFlag = optionFlag({
|
|
714
|
+
* long: "verbose",
|
|
715
|
+
* short: "v",
|
|
716
|
+
* description: "Enable verbose output",
|
|
717
|
+
* });
|
|
718
|
+
* ```
|
|
719
|
+
*/
|
|
720
|
+
declare function optionFlag(definition: {
|
|
721
|
+
long: Lowercase<string>;
|
|
722
|
+
short?: string;
|
|
723
|
+
description?: string;
|
|
724
|
+
hint?: string;
|
|
725
|
+
aliases?: {
|
|
726
|
+
longs?: Array<Lowercase<string>>;
|
|
727
|
+
shorts?: Array<string>;
|
|
728
|
+
};
|
|
729
|
+
default?: boolean;
|
|
730
|
+
}): Option<boolean>;
|
|
731
|
+
/**
|
|
732
|
+
* Creates an option that accepts exactly one value (e.g. `--output dist/`).
|
|
733
|
+
*
|
|
734
|
+
* Parsing: absent → `default()`; once → decoded with `type`; more than once → {@link TypoError}.
|
|
735
|
+
* Value syntax: `--long value`, `--long=value`, `-s value`, `-s=value`, `-svalue`.
|
|
736
|
+
*
|
|
737
|
+
* @typeParam Value - Type produced by the decoder.
|
|
738
|
+
*
|
|
739
|
+
* @param definition.long - Long-form name (without `--`).
|
|
740
|
+
* @param definition.short - Short-form name (without `-`).
|
|
741
|
+
* @param definition.description - Help text.
|
|
742
|
+
* @param definition.hint - Short note shown in parentheses.
|
|
743
|
+
* @param definition.aliases - Additional names.
|
|
744
|
+
* @param definition.label - Value placeholder in help. Defaults to uppercased `type.content`.
|
|
745
|
+
* @param definition.type - Decoder for the raw string value.
|
|
746
|
+
* @param definition.default - Default when absent. Throw to make the option required.
|
|
747
|
+
* @returns An {@link Option}`<Value>`.
|
|
748
|
+
*
|
|
749
|
+
* @example
|
|
750
|
+
* ```ts
|
|
751
|
+
* const outputOption = optionSingleValue({
|
|
752
|
+
* long: "output",
|
|
753
|
+
* short: "o",
|
|
754
|
+
* type: typeString,
|
|
755
|
+
* label: "PATH",
|
|
756
|
+
* description: "Output directory",
|
|
757
|
+
* default: () => "dist/",
|
|
758
|
+
* });
|
|
759
|
+
* ```
|
|
760
|
+
*/
|
|
761
|
+
declare function optionSingleValue<Value>(definition: {
|
|
762
|
+
long: Lowercase<string>;
|
|
763
|
+
short?: string;
|
|
764
|
+
description?: string;
|
|
765
|
+
hint?: string;
|
|
766
|
+
aliases?: {
|
|
767
|
+
longs?: Array<Lowercase<string>>;
|
|
768
|
+
shorts?: Array<string>;
|
|
769
|
+
};
|
|
770
|
+
label?: Uppercase<string>;
|
|
771
|
+
type: Type<Value>;
|
|
772
|
+
default: () => Value;
|
|
773
|
+
}): Option<Value>;
|
|
774
|
+
/**
|
|
775
|
+
* Creates an option that collects every occurrence into an array (e.g. `--file a.ts --file b.ts`).
|
|
776
|
+
*
|
|
777
|
+
* Parsing: absent → `[]`; N occurrences → array of N decoded values in order.
|
|
778
|
+
* Value syntax: `--long value`, `--long=value`, `-s value`, `-s=value`, `-svalue`.
|
|
779
|
+
*
|
|
780
|
+
* @typeParam Value - Type produced by the decoder for each occurrence.
|
|
781
|
+
*
|
|
782
|
+
* @param definition.long - Long-form name (without `--`).
|
|
783
|
+
* @param definition.short - Short-form name (without `-`).
|
|
784
|
+
* @param definition.description - Help text.
|
|
785
|
+
* @param definition.hint - Short note shown in parentheses.
|
|
786
|
+
* @param definition.aliases - Additional names.
|
|
787
|
+
* @param definition.label - Value placeholder in help. Defaults to uppercased `type.content`.
|
|
788
|
+
* @param definition.type - Decoder applied to each raw string value.
|
|
789
|
+
* @returns An {@link Option}`<Array<Value>>`.
|
|
790
|
+
*
|
|
791
|
+
* @example
|
|
792
|
+
* ```ts
|
|
793
|
+
* const filesOption = optionRepeatable({
|
|
794
|
+
* long: "file",
|
|
795
|
+
* short: "f",
|
|
796
|
+
* type: typeString,
|
|
797
|
+
* label: "PATH",
|
|
798
|
+
* description: "Input file (may be repeated)",
|
|
799
|
+
* });
|
|
800
|
+
* // Usage: my-cli --file a.ts --file b.ts → ["a.ts", "b.ts"]
|
|
801
|
+
* ```
|
|
802
|
+
*/
|
|
803
|
+
declare function optionRepeatable<Value>(definition: {
|
|
804
|
+
long: Lowercase<string>;
|
|
805
|
+
short?: string;
|
|
806
|
+
description?: string;
|
|
807
|
+
hint?: string;
|
|
808
|
+
aliases?: {
|
|
809
|
+
longs?: Array<Lowercase<string>>;
|
|
810
|
+
shorts?: Array<string>;
|
|
811
|
+
};
|
|
812
|
+
label?: Uppercase<string>;
|
|
813
|
+
type: Type<Value>;
|
|
814
|
+
}): Option<Array<Value>>;
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* A positional argument. Created with {@link positionalRequired}, {@link positionalOptional},
|
|
818
|
+
* or {@link positionalVariadics}.
|
|
819
|
+
*
|
|
820
|
+
* @typeParam Value - Decoded value type.
|
|
821
|
+
*/
|
|
822
|
+
type Positional<Value> = {
|
|
823
|
+
/**
|
|
824
|
+
* Returns metadata for the `Positionals:` section.
|
|
825
|
+
*/
|
|
826
|
+
generateUsage(): UsagePositional;
|
|
827
|
+
/**
|
|
828
|
+
* Consumes the next positional token from `readerPositionals`.
|
|
829
|
+
* Returns a decoder that produces the final value.
|
|
830
|
+
*/
|
|
831
|
+
consumeAndMakeDecoder(readerPositionals: ReaderPositionals): PositionalDecoder<Value>;
|
|
832
|
+
};
|
|
833
|
+
/**
|
|
834
|
+
* Produced by {@link Positional.consumeAndMakeDecoder}.
|
|
835
|
+
*
|
|
836
|
+
* @typeParam Value - Decoded value type.
|
|
837
|
+
*/
|
|
838
|
+
type PositionalDecoder<Value> = {
|
|
839
|
+
/**
|
|
840
|
+
* Returns the decoded positional value.
|
|
841
|
+
*
|
|
842
|
+
* @throws {@link TypoError} if decoding failed.
|
|
843
|
+
*/
|
|
844
|
+
decodeValue(): Value;
|
|
845
|
+
};
|
|
846
|
+
/**
|
|
847
|
+
* Creates a required positional — missing token throws {@link TypoError}.
|
|
848
|
+
* Label defaults to uppercased `type.content` in angle brackets (e.g. `<STRING>`).
|
|
849
|
+
*
|
|
850
|
+
* @typeParam Value - Type produced by the decoder.
|
|
851
|
+
*
|
|
852
|
+
* @param definition.description - Help text.
|
|
853
|
+
* @param definition.hint - Short note shown in parentheses.
|
|
854
|
+
* @param definition.label - Label without brackets; defaults to uppercased `type.content`.
|
|
855
|
+
* @param definition.type - Decoder for the raw token.
|
|
856
|
+
* @returns A {@link Positional}`<Value>`.
|
|
857
|
+
*
|
|
858
|
+
* @example
|
|
859
|
+
* ```ts
|
|
860
|
+
* const namePositional = positionalRequired({
|
|
861
|
+
* type: typeString,
|
|
862
|
+
* label: "NAME",
|
|
863
|
+
* description: "The name to greet",
|
|
864
|
+
* });
|
|
865
|
+
* // Parses: my-cli Alice → "Alice"
|
|
535
866
|
* ```
|
|
536
867
|
*/
|
|
537
868
|
declare function positionalRequired<Value>(definition: {
|
|
@@ -546,7 +877,6 @@ declare function positionalRequired<Value>(definition: {
|
|
|
546
877
|
*
|
|
547
878
|
* @typeParam Value - Type produced by the decoder (or the default).
|
|
548
879
|
*
|
|
549
|
-
* @param definition - Positional configuration.
|
|
550
880
|
* @param definition.description - Help text.
|
|
551
881
|
* @param definition.hint - Short note shown in parentheses.
|
|
552
882
|
* @param definition.label - Label without brackets; defaults to uppercased `type.content`.
|
|
@@ -579,7 +909,6 @@ declare function positionalOptional<Value>(definition: {
|
|
|
579
909
|
*
|
|
580
910
|
* @typeParam Value - Type produced by the decoder for each token.
|
|
581
911
|
*
|
|
582
|
-
* @param definition - Positional configuration.
|
|
583
912
|
* @param definition.endDelimiter - Sentinel token that stops collection (consumed, not included).
|
|
584
913
|
* @param definition.description - Help text.
|
|
585
914
|
* @param definition.hint - Short note shown in parentheses.
|
|
@@ -612,14 +941,14 @@ declare function positionalVariadics<Value>(definition: {
|
|
|
612
941
|
* Created with {@link operation} and passed to {@link command},
|
|
613
942
|
* {@link commandWithSubcommands}, or {@link commandChained}.
|
|
614
943
|
*
|
|
615
|
-
* @typeParam Context - Injected at execution
|
|
616
|
-
* @typeParam Result - Value produced on execution; typically `void
|
|
944
|
+
* @typeParam Context - Injected at execution; forwarded to handlers.
|
|
945
|
+
* @typeParam Result - Value produced on execution; typically `void`.
|
|
617
946
|
*/
|
|
618
947
|
type Operation<Context, Result> = {
|
|
619
948
|
/**
|
|
620
949
|
* Returns usage metadata without consuming any arguments.
|
|
621
950
|
*/
|
|
622
|
-
generateUsage():
|
|
951
|
+
generateUsage(): UsageOperation;
|
|
623
952
|
/**
|
|
624
953
|
* Consumes args from `readerArgs` and returns an {@link OperationDecoder}.
|
|
625
954
|
*/
|
|
@@ -651,26 +980,10 @@ type OperationInterpreter<Context, Result> = {
|
|
|
651
980
|
*/
|
|
652
981
|
executeWithContext(context: Context): Promise<Result>;
|
|
653
982
|
};
|
|
654
|
-
/**
|
|
655
|
-
* Usage metadata produced by {@link Operation.generateUsage}.
|
|
656
|
-
* Consumed when building {@link CommandUsage}.
|
|
657
|
-
*/
|
|
658
|
-
type OperationUsage = {
|
|
659
|
-
/**
|
|
660
|
-
* Usage descriptors for all registered options.
|
|
661
|
-
*/
|
|
662
|
-
options: Array<OptionUsage>;
|
|
663
|
-
/**
|
|
664
|
-
* Usage descriptors for all declared positionals, in order.
|
|
665
|
-
*/
|
|
666
|
-
positionals: Array<PositionalUsage>;
|
|
667
|
-
};
|
|
668
983
|
/**
|
|
669
984
|
* Creates an {@link Operation} from options, positionals, and an async handler.
|
|
670
985
|
*
|
|
671
|
-
* The `handler` receives
|
|
672
|
-
* `options` (keyed by the same names declared in `inputs.options`) and
|
|
673
|
-
* `positionals` (a tuple in declaration order).
|
|
986
|
+
* The `handler` receives `context` and `inputs` with decoded `options` and `positionals`.
|
|
674
987
|
*
|
|
675
988
|
* @typeParam Context - Context type accepted by the handler.
|
|
676
989
|
* @typeParam Result - Return type of the handler.
|
|
@@ -681,7 +994,7 @@ type OperationUsage = {
|
|
|
681
994
|
* @param inputs.options - Map of keys to {@link Option} descriptors.
|
|
682
995
|
* @param inputs.positionals - Ordered array of {@link Positional} descriptors.
|
|
683
996
|
* @param handler - Async function implementing the command logic.
|
|
684
|
-
* @returns An {@link Operation}
|
|
997
|
+
* @returns An {@link Operation}.
|
|
685
998
|
*
|
|
686
999
|
* @example
|
|
687
1000
|
* ```ts
|
|
@@ -694,7 +1007,7 @@ type OperationUsage = {
|
|
|
694
1007
|
* positionalRequired({ type: typeString, label: "NAME", description: "Name to greet" }),
|
|
695
1008
|
* ],
|
|
696
1009
|
* },
|
|
697
|
-
* async (_ctx, { options: { loud }, positionals: [name] })
|
|
1010
|
+
* async function (_ctx, { options: { loud }, positionals: [name] }) {
|
|
698
1011
|
* const message = `Hello, ${name}!`;
|
|
699
1012
|
* console.log(loud ? message.toUpperCase() : message);
|
|
700
1013
|
* },
|
|
@@ -716,20 +1029,18 @@ declare function operation<Context, Result, Options extends {
|
|
|
716
1029
|
}) => Promise<Result>): Operation<Context, Result>;
|
|
717
1030
|
|
|
718
1031
|
/**
|
|
719
|
-
* A CLI command
|
|
720
|
-
* Created with {@link command}, {@link commandWithSubcommands}, or {@link commandChained}.
|
|
721
|
-
* Usually passed through {@link runAndExit} to run.
|
|
1032
|
+
* A CLI command. Created with {@link command}, {@link commandWithSubcommands}, or {@link commandChained}.
|
|
722
1033
|
*
|
|
723
|
-
* @typeParam Context - Injected at execution
|
|
724
|
-
* @typeParam Result -
|
|
1034
|
+
* @typeParam Context - Injected at execution; forwarded to handlers.
|
|
1035
|
+
* @typeParam Result - Produced on execution; typically `void`.
|
|
725
1036
|
*/
|
|
726
1037
|
type Command<Context, Result> = {
|
|
727
1038
|
/**
|
|
728
|
-
* Returns
|
|
1039
|
+
* Returns static metadata.
|
|
729
1040
|
*/
|
|
730
1041
|
getInformation(): CommandInformation;
|
|
731
1042
|
/**
|
|
732
|
-
*
|
|
1043
|
+
* Registers options/positionals on `readerArgs`; returns a {@link CommandDecoder}.
|
|
733
1044
|
*/
|
|
734
1045
|
consumeAndMakeDecoder(readerArgs: ReaderArgs): CommandDecoder<Context, Result>;
|
|
735
1046
|
};
|
|
@@ -741,10 +1052,9 @@ type Command<Context, Result> = {
|
|
|
741
1052
|
*/
|
|
742
1053
|
type CommandDecoder<Context, Result> = {
|
|
743
1054
|
/**
|
|
744
|
-
*
|
|
745
|
-
* Used for `--help` and `usageOnError`.
|
|
1055
|
+
* Returns {@link UsageCommand} for the current command path.
|
|
746
1056
|
*/
|
|
747
|
-
generateUsage():
|
|
1057
|
+
generateUsage(): UsageCommand;
|
|
748
1058
|
/**
|
|
749
1059
|
* Creates a ready-to-execute {@link CommandInterpreter}.
|
|
750
1060
|
*
|
|
@@ -755,7 +1065,7 @@ type CommandDecoder<Context, Result> = {
|
|
|
755
1065
|
/**
|
|
756
1066
|
* A fully parsed, decoded and ready-to-execute command.
|
|
757
1067
|
*
|
|
758
|
-
* @typeParam Context -
|
|
1068
|
+
* @typeParam Context - Context passed to the handler.
|
|
759
1069
|
* @typeParam Result - Value produced on success.
|
|
760
1070
|
*/
|
|
761
1071
|
type CommandInterpreter<Context, Result> = {
|
|
@@ -765,99 +1075,46 @@ type CommandInterpreter<Context, Result> = {
|
|
|
765
1075
|
executeWithContext(context: Context): Promise<Result>;
|
|
766
1076
|
};
|
|
767
1077
|
/**
|
|
768
|
-
*
|
|
1078
|
+
* Command metadata shown in `--help` output.
|
|
769
1079
|
*/
|
|
770
1080
|
type CommandInformation = {
|
|
771
1081
|
/**
|
|
772
|
-
*
|
|
773
|
-
*/
|
|
774
|
-
description: string;
|
|
775
|
-
/**
|
|
776
|
-
* Short note shown in parentheses (e.g. `"deprecated"`, `"experimental"`).
|
|
777
|
-
*/
|
|
778
|
-
hint?: string;
|
|
779
|
-
/**
|
|
780
|
-
* Extra lines printed below the description.
|
|
1082
|
+
* Shown in the usage header.
|
|
781
1083
|
*/
|
|
782
|
-
|
|
783
|
-
/**
|
|
784
|
-
* Examples shown in the `Examples:` section of the usage output.
|
|
785
|
-
*/
|
|
786
|
-
examples?: Array<{
|
|
787
|
-
/**
|
|
788
|
-
* Explanation shown above the example.
|
|
789
|
-
*/
|
|
790
|
-
explanation: string;
|
|
791
|
-
/**
|
|
792
|
-
* Command line args to show as an example of usage.
|
|
793
|
-
*/
|
|
794
|
-
commandArgs: Array<string | {
|
|
795
|
-
positional: string;
|
|
796
|
-
} | {
|
|
797
|
-
subcommand: string;
|
|
798
|
-
} | {
|
|
799
|
-
option: {
|
|
800
|
-
long: string;
|
|
801
|
-
value?: string;
|
|
802
|
-
} | {
|
|
803
|
-
short: string;
|
|
804
|
-
value?: string;
|
|
805
|
-
};
|
|
806
|
-
}>;
|
|
807
|
-
}>;
|
|
808
|
-
};
|
|
809
|
-
/**
|
|
810
|
-
* Full usage/help model.
|
|
811
|
-
* Produced by {@link CommandDecoder.generateUsage},
|
|
812
|
-
* Consumed by {@link usageToStyledLines}.
|
|
813
|
-
*/
|
|
814
|
-
type CommandUsage = {
|
|
815
|
-
/**
|
|
816
|
-
* Segments forming the usage line
|
|
817
|
-
* (e.g. `my-cli <POSITIONAL> subcommand <ANOTHER_POSITIONAL>`).
|
|
818
|
-
*/
|
|
819
|
-
segments: Array<CommandUsageSegment>;
|
|
820
|
-
/**
|
|
821
|
-
* Command's static metadata.
|
|
822
|
-
*/
|
|
823
|
-
information: CommandInformation;
|
|
824
|
-
/**
|
|
825
|
-
* Positionals in declaration order.
|
|
826
|
-
*/
|
|
827
|
-
positionals: Array<PositionalUsage>;
|
|
828
|
-
/**
|
|
829
|
-
* Available subcommands. Non-empty when subcommand was not specified.
|
|
830
|
-
*/
|
|
831
|
-
subcommands: Array<CommandUsageSubcommand>;
|
|
832
|
-
/**
|
|
833
|
-
* Options in registration order.
|
|
834
|
-
*/
|
|
835
|
-
options: Array<OptionUsage>;
|
|
836
|
-
};
|
|
837
|
-
/**
|
|
838
|
-
* One element in the usage segment trail.
|
|
839
|
-
*/
|
|
840
|
-
type CommandUsageSegment = {
|
|
841
|
-
positional: string;
|
|
842
|
-
} | {
|
|
843
|
-
command: string;
|
|
844
|
-
};
|
|
845
|
-
/**
|
|
846
|
-
* Subcommand entry shown in the `Subcommands:` section of the usage output.
|
|
847
|
-
*/
|
|
848
|
-
type CommandUsageSubcommand = {
|
|
1084
|
+
description: string;
|
|
849
1085
|
/**
|
|
850
|
-
*
|
|
1086
|
+
* Shown in parentheses, e.g. `"deprecated"`, `"experimental"`.
|
|
851
1087
|
*/
|
|
852
|
-
|
|
1088
|
+
hint?: string;
|
|
853
1089
|
/**
|
|
854
|
-
*
|
|
1090
|
+
* Extra lines printed below the description.
|
|
855
1091
|
*/
|
|
856
|
-
|
|
1092
|
+
details?: Array<string>;
|
|
857
1093
|
/**
|
|
858
|
-
*
|
|
1094
|
+
* Shown in the `Examples:` section.
|
|
859
1095
|
*/
|
|
860
|
-
|
|
1096
|
+
examples?: Array<{
|
|
1097
|
+
/**
|
|
1098
|
+
* Explanation shown above the example.
|
|
1099
|
+
*/
|
|
1100
|
+
explanation: string;
|
|
1101
|
+
/**
|
|
1102
|
+
* Example command args.
|
|
1103
|
+
*/
|
|
1104
|
+
commandArgs: Array<string | {
|
|
1105
|
+
positional: string;
|
|
1106
|
+
} | {
|
|
1107
|
+
subcommand: string;
|
|
1108
|
+
} | {
|
|
1109
|
+
option: {
|
|
1110
|
+
long: string;
|
|
1111
|
+
value?: string;
|
|
1112
|
+
} | {
|
|
1113
|
+
short: string;
|
|
1114
|
+
value?: string;
|
|
1115
|
+
};
|
|
1116
|
+
}>;
|
|
1117
|
+
}>;
|
|
861
1118
|
};
|
|
862
1119
|
/**
|
|
863
1120
|
* Creates a leaf command that directly executes an {@link Operation}.
|
|
@@ -865,8 +1122,8 @@ type CommandUsageSubcommand = {
|
|
|
865
1122
|
* @typeParam Context - Context forwarded to the handler.
|
|
866
1123
|
* @typeParam Result - Value returned by the handler.
|
|
867
1124
|
*
|
|
868
|
-
* @param information - Command metadata
|
|
869
|
-
* @param operation -
|
|
1125
|
+
* @param information - Command metadata.
|
|
1126
|
+
* @param operation - Options, positionals, and handler.
|
|
870
1127
|
* @returns A {@link Command}.
|
|
871
1128
|
*
|
|
872
1129
|
* @example
|
|
@@ -882,17 +1139,16 @@ type CommandUsageSubcommand = {
|
|
|
882
1139
|
*/
|
|
883
1140
|
declare function command<Context, Result>(information: CommandInformation, operation: Operation<Context, Result>): Command<Context, Result>;
|
|
884
1141
|
/**
|
|
885
|
-
* Creates a command that runs
|
|
886
|
-
* then dispatches to a named subcommand based on the next positional token.
|
|
1142
|
+
* Creates a command that runs `operation` first, then dispatches to a named subcommand.
|
|
887
1143
|
*
|
|
888
1144
|
* @typeParam Context - Context accepted by `operation`.
|
|
889
1145
|
* @typeParam Payload - Output of `operation`; becomes the subcommand's context.
|
|
890
1146
|
* @typeParam Result - Value produced by the selected subcommand.
|
|
891
1147
|
*
|
|
892
|
-
* @param information - Command metadata
|
|
893
|
-
* @param operation -
|
|
1148
|
+
* @param information - Command metadata.
|
|
1149
|
+
* @param operation - Runs first; output becomes the subcommand's context.
|
|
894
1150
|
* @param subcommands - Map of subcommand names to their {@link Command}s.
|
|
895
|
-
* @returns A {@link Command}
|
|
1151
|
+
* @returns A dispatching {@link Command}.
|
|
896
1152
|
*
|
|
897
1153
|
* @example
|
|
898
1154
|
* ```ts
|
|
@@ -917,10 +1173,10 @@ declare function commandWithSubcommands<Context, Payload, Result>(information: C
|
|
|
917
1173
|
* @typeParam Payload - Output of `operation`; becomes `subcommand`'s context.
|
|
918
1174
|
* @typeParam Result - Value produced by `subcommand`.
|
|
919
1175
|
*
|
|
920
|
-
* @param information - Command metadata
|
|
921
|
-
* @param operation -
|
|
922
|
-
* @param subcommand -
|
|
923
|
-
* @returns A {@link Command}
|
|
1176
|
+
* @param information - Command metadata.
|
|
1177
|
+
* @param operation - Runs first; output becomes `subcommand`'s context.
|
|
1178
|
+
* @param subcommand - Runs after `operation`.
|
|
1179
|
+
* @returns A {@link Command} composing both stages.
|
|
924
1180
|
*
|
|
925
1181
|
* @example
|
|
926
1182
|
* ```ts
|
|
@@ -940,15 +1196,16 @@ declare function commandChained<Context, Payload, Result>(information: CommandIn
|
|
|
940
1196
|
* Main entry point: parses CLI arguments, executes the matched command, and exits.
|
|
941
1197
|
* Handles `--help`, `--version`, usage-on-error, and exit codes.
|
|
942
1198
|
*
|
|
943
|
-
* Exit codes:
|
|
1199
|
+
* Exit codes:
|
|
1200
|
+
* - `0` on success / `--help` / `--version`
|
|
1201
|
+
* - `1` on parse error or execution error.
|
|
944
1202
|
*
|
|
945
|
-
* @typeParam Context -
|
|
1203
|
+
* @typeParam Context - Forwarded unchanged to the handler.
|
|
946
1204
|
*
|
|
947
1205
|
* @param cliName - Program name used in usage and `--version` output.
|
|
948
1206
|
* @param cliArgs - Raw arguments, typically `process.argv.slice(2)`.
|
|
949
|
-
* @param context - Forwarded to the
|
|
1207
|
+
* @param context - Forwarded to the handler.
|
|
950
1208
|
* @param command - Root {@link Command}.
|
|
951
|
-
* @param options - Optional runner configuration.
|
|
952
1209
|
* @param options.useTtyColors - Color mode: `true` (always), `false` (never),
|
|
953
1210
|
* `"mock"` (snapshot-friendly), `undefined` (auto-detect from env).
|
|
954
1211
|
* @param options.usageOnHelp - Enables `--help` flag (default `true`).
|
|
@@ -967,7 +1224,7 @@ declare function commandChained<Context, Payload, Result>(information: CommandIn
|
|
|
967
1224
|
* { description: "Greet someone" },
|
|
968
1225
|
* operation(
|
|
969
1226
|
* { options: {}, positionals: [positionalRequired({ type: typeString, label: "NAME" })] },
|
|
970
|
-
* async (_ctx, { positionals: [name] })
|
|
1227
|
+
* async function (_ctx, { positionals: [name] }) {
|
|
971
1228
|
* console.log(`Hello, ${name}!`);
|
|
972
1229
|
* },
|
|
973
1230
|
* ),
|
|
@@ -987,285 +1244,4 @@ declare function runAndExit<Context>(cliName: Lowercase<string>, cliArgs: Readon
|
|
|
987
1244
|
onExit?: ((code: number) => never) | undefined;
|
|
988
1245
|
}): Promise<never>;
|
|
989
1246
|
|
|
990
|
-
|
|
991
|
-
* Color names for terminal styling, used by {@link TypoStyle}.
|
|
992
|
-
* `dark*` = standard ANSI (30–37); `bright*` = high-intensity (90–97).
|
|
993
|
-
*/
|
|
994
|
-
type TypoColor = "darkBlack" | "darkRed" | "darkGreen" | "darkYellow" | "darkBlue" | "darkMagenta" | "darkCyan" | "darkWhite" | "brightBlack" | "brightRed" | "brightGreen" | "brightYellow" | "brightBlue" | "brightMagenta" | "brightCyan" | "brightWhite";
|
|
995
|
-
/**
|
|
996
|
-
* Visual styling applied by a {@link TypoSupport} instance.
|
|
997
|
-
* All fields are optional; ignored entirely in `"none"` mode.
|
|
998
|
-
*/
|
|
999
|
-
type TypoStyle = {
|
|
1000
|
-
/**
|
|
1001
|
-
* Foreground (text) color.
|
|
1002
|
-
*/
|
|
1003
|
-
fgColor?: TypoColor;
|
|
1004
|
-
/**
|
|
1005
|
-
* Background color.
|
|
1006
|
-
*/
|
|
1007
|
-
bgColor?: TypoColor;
|
|
1008
|
-
/**
|
|
1009
|
-
* Render with reduced intensity.
|
|
1010
|
-
*/
|
|
1011
|
-
dim?: boolean;
|
|
1012
|
-
/**
|
|
1013
|
-
* Render in bold.
|
|
1014
|
-
*/
|
|
1015
|
-
bold?: boolean;
|
|
1016
|
-
/**
|
|
1017
|
-
* Render in italic.
|
|
1018
|
-
*/
|
|
1019
|
-
italic?: boolean;
|
|
1020
|
-
/**
|
|
1021
|
-
* Render with an underline.
|
|
1022
|
-
*/
|
|
1023
|
-
underline?: boolean;
|
|
1024
|
-
/**
|
|
1025
|
-
* Render with a strikethrough.
|
|
1026
|
-
*/
|
|
1027
|
-
strikethrough?: boolean;
|
|
1028
|
-
};
|
|
1029
|
-
/**
|
|
1030
|
-
* Pre-defined style for section titles (e.g. `"Positionals:"`). Bold dark-green.
|
|
1031
|
-
*/
|
|
1032
|
-
declare const typoStyleTitle: TypoStyle;
|
|
1033
|
-
/**
|
|
1034
|
-
* Pre-defined style for logic/type identifiers in error messages. Bold dark-magenta.
|
|
1035
|
-
*/
|
|
1036
|
-
declare const typoStyleLogic: TypoStyle;
|
|
1037
|
-
/**
|
|
1038
|
-
* Pre-defined style for quoted user-supplied values in error messages. Bold dark-yellow.
|
|
1039
|
-
*/
|
|
1040
|
-
declare const typoStyleQuote: TypoStyle;
|
|
1041
|
-
/**
|
|
1042
|
-
* Pre-defined style for failure/error labels (e.g. `"Error:"`). Bold dark-red.
|
|
1043
|
-
*/
|
|
1044
|
-
declare const typoStyleFailure: TypoStyle;
|
|
1045
|
-
/**
|
|
1046
|
-
* Pre-defined style for CLI flag/option/command names. Bold dark-cyan.
|
|
1047
|
-
*/
|
|
1048
|
-
declare const typoStyleConstants: TypoStyle;
|
|
1049
|
-
/**
|
|
1050
|
-
* Pre-defined style for positional placeholders and user-input labels. Bold dark-blue.
|
|
1051
|
-
*/
|
|
1052
|
-
declare const typoStyleUserInput: TypoStyle;
|
|
1053
|
-
/**
|
|
1054
|
-
* Pre-defined style for strong regular text (e.g. command descriptions). Bold.
|
|
1055
|
-
*/
|
|
1056
|
-
declare const typoStyleRegularStrong: TypoStyle;
|
|
1057
|
-
/**
|
|
1058
|
-
* Pre-defined style for subtle supplementary text (e.g. hints). Italic and dim.
|
|
1059
|
-
*/
|
|
1060
|
-
declare const typoStyleRegularWeaker: TypoStyle;
|
|
1061
|
-
/**
|
|
1062
|
-
* An immutable styled string segment: a raw text value paired with a {@link TypoStyle}.
|
|
1063
|
-
* Compose multiple segments into a {@link TypoText}; rendering is deferred to {@link TypoString.computeStyledString}.
|
|
1064
|
-
*/
|
|
1065
|
-
declare class TypoString {
|
|
1066
|
-
#private;
|
|
1067
|
-
/**
|
|
1068
|
-
* @param value - Raw text content.
|
|
1069
|
-
* @param typoStyle - Style to apply when rendering. Defaults to `{}` (no style).
|
|
1070
|
-
*/
|
|
1071
|
-
constructor(value: string, typoStyle?: TypoStyle);
|
|
1072
|
-
/**
|
|
1073
|
-
* Returns the unstyled raw text content.
|
|
1074
|
-
*/
|
|
1075
|
-
getRawString(): string;
|
|
1076
|
-
/**
|
|
1077
|
-
* Returns the text styled by `typoSupport`.
|
|
1078
|
-
*
|
|
1079
|
-
* @param typoSupport - Rendering mode.
|
|
1080
|
-
*/
|
|
1081
|
-
computeStyledString(typoSupport: TypoSupport): string;
|
|
1082
|
-
}
|
|
1083
|
-
/**
|
|
1084
|
-
* A mutable sequence of {@link TypoString} segments forming a styled multi-part message.
|
|
1085
|
-
* Rendering is deferred to {@link TypoText.computeStyledString}.
|
|
1086
|
-
*/
|
|
1087
|
-
declare class TypoText {
|
|
1088
|
-
#private;
|
|
1089
|
-
/**
|
|
1090
|
-
* @param typoParts - Initial segments; `TypoText` is flattened, `string` is wrapped unstyled.
|
|
1091
|
-
*/
|
|
1092
|
-
constructor(...typoParts: Array<TypoText | TypoString | string>);
|
|
1093
|
-
/**
|
|
1094
|
-
* Appends a {@link TypoString} segment.
|
|
1095
|
-
*
|
|
1096
|
-
* @param typoString - Segment to append.
|
|
1097
|
-
*/
|
|
1098
|
-
pushString(typoString: TypoString | string): void;
|
|
1099
|
-
/**
|
|
1100
|
-
* Appends all segments from another {@link TypoText} (shallow copy).
|
|
1101
|
-
*
|
|
1102
|
-
* @param typoText - Source text.
|
|
1103
|
-
*/
|
|
1104
|
-
pushText(typoText: TypoText | string): void;
|
|
1105
|
-
/**
|
|
1106
|
-
* Renders all segments into a single styled string.
|
|
1107
|
-
*
|
|
1108
|
-
* @param typoSupport - Rendering mode.
|
|
1109
|
-
* @returns Concatenated styled string.
|
|
1110
|
-
*/
|
|
1111
|
-
computeStyledString(typoSupport: TypoSupport): string;
|
|
1112
|
-
/**
|
|
1113
|
-
* Returns the concatenation of all segments' raw (unstyled) text.
|
|
1114
|
-
*/
|
|
1115
|
-
computeRawString(): string;
|
|
1116
|
-
/**
|
|
1117
|
-
* Returns the total character count of the raw (unstyled) text.
|
|
1118
|
-
*/
|
|
1119
|
-
computeRawLength(): number;
|
|
1120
|
-
}
|
|
1121
|
-
/**
|
|
1122
|
-
* A column-aligned grid of {@link TypoText} cells.
|
|
1123
|
-
* Each column is padded to the widest cell (raw chars); the last column is not padded.
|
|
1124
|
-
* Used by {@link usageToStyledLines} to render `Positionals:`, `Subcommands:`, and `Options:`.
|
|
1125
|
-
*/
|
|
1126
|
-
declare class TypoGrid {
|
|
1127
|
-
#private;
|
|
1128
|
-
constructor();
|
|
1129
|
-
/**
|
|
1130
|
-
* Appends a row. All rows should have the same cell count for alignment to be meaningful.
|
|
1131
|
-
*
|
|
1132
|
-
* @param cells - Ordered {@link TypoText} cells.
|
|
1133
|
-
*/
|
|
1134
|
-
pushRow(cells: Array<TypoText>): void;
|
|
1135
|
-
/**
|
|
1136
|
-
* Renders the grid as a 2-D array of styled (and column-padded) strings.
|
|
1137
|
-
* Join each inner array with `""` to get a line.
|
|
1138
|
-
*
|
|
1139
|
-
* @param typoSupport - Rendering mode.
|
|
1140
|
-
* @returns 2-D array of styled strings.
|
|
1141
|
-
*/
|
|
1142
|
-
computeStyledGrid(typoSupport: TypoSupport): Array<Array<string>>;
|
|
1143
|
-
}
|
|
1144
|
-
/**
|
|
1145
|
-
* `Error` subclass with a {@link TypoText} styled message for rich terminal output.
|
|
1146
|
-
* Used throughout the library for parse failures (unknown option, type decode error, etc.).
|
|
1147
|
-
* If `source` is a `TypoError`, its styled text is chained after `": "`.
|
|
1148
|
-
*/
|
|
1149
|
-
declare class TypoError extends Error {
|
|
1150
|
-
#private;
|
|
1151
|
-
/**
|
|
1152
|
-
* @param currentTypoText - Styled message for this error.
|
|
1153
|
-
* @param source - Optional cause; `TypoError` chains styled text, `Error` appends `.message`, else `String()`.
|
|
1154
|
-
*/
|
|
1155
|
-
constructor(currentTypoText: TypoText, source?: unknown);
|
|
1156
|
-
/**
|
|
1157
|
-
* Renders the styled error message (without a `"Error:"` prefix).
|
|
1158
|
-
*
|
|
1159
|
-
* @param typoSupport - Rendering mode.
|
|
1160
|
-
* @returns Styled error string.
|
|
1161
|
-
*/
|
|
1162
|
-
computeStyledString(typoSupport: TypoSupport): string;
|
|
1163
|
-
/**
|
|
1164
|
-
* Runs `thrower`; on any throw wraps it as a `TypoError` with `context()` prepended.
|
|
1165
|
-
* Useful for adding call-chain context (e.g. `"at 0: Number: ..."`).
|
|
1166
|
-
*
|
|
1167
|
-
* @typeParam Value - Return type of `thrower`.
|
|
1168
|
-
* @param thrower - Function to execute; result passed through on success.
|
|
1169
|
-
* @param context - Produces the {@link TypoText} prepended to the caught error.
|
|
1170
|
-
* @returns Value from `thrower`.
|
|
1171
|
-
* @throws `TypoError` wrapping the original error with context prepended.
|
|
1172
|
-
*/
|
|
1173
|
-
static tryWithContext<Value>(thrower: () => Value, context: () => TypoText): Value;
|
|
1174
|
-
}
|
|
1175
|
-
/**
|
|
1176
|
-
* Controls ANSI terminal styling for {@link TypoString}, {@link TypoText}, and error rendering.
|
|
1177
|
-
* Create via {@link TypoSupport.none}, {@link TypoSupport.tty}, {@link TypoSupport.mock},
|
|
1178
|
-
* or {@link TypoSupport.inferFromProcess}.
|
|
1179
|
-
*/
|
|
1180
|
-
declare class TypoSupport {
|
|
1181
|
-
#private;
|
|
1182
|
-
private constructor();
|
|
1183
|
-
/**
|
|
1184
|
-
* Returns a `TypoSupport` that strips all styling (plain text, no ANSI codes).
|
|
1185
|
-
*/
|
|
1186
|
-
static none(): TypoSupport;
|
|
1187
|
-
/**
|
|
1188
|
-
* Returns a `TypoSupport` that applies ANSI escape codes (for color-capable terminals).
|
|
1189
|
-
*/
|
|
1190
|
-
static tty(): TypoSupport;
|
|
1191
|
-
/**
|
|
1192
|
-
* Returns a `TypoSupport` with deterministic textual styling for snapshot tests.
|
|
1193
|
-
* Style flags appear as suffixes: `{text}@color`, `{text}+` (bold), `{text}-` (dim),
|
|
1194
|
-
* `{text}*` (italic), `{text}_` (underline), `{text}~` (strikethrough).
|
|
1195
|
-
*/
|
|
1196
|
-
static mock(): TypoSupport;
|
|
1197
|
-
/**
|
|
1198
|
-
* Auto-detects styling mode from the process environment.
|
|
1199
|
-
* `FORCE_COLOR=0` / `NO_COLOR` → none; `FORCE_COLOR` (truthy) / `isTTY` → tty; else → none.
|
|
1200
|
-
* Falls back to none if `process` is unavailable.
|
|
1201
|
-
*/
|
|
1202
|
-
static inferFromProcess(): TypoSupport;
|
|
1203
|
-
/**
|
|
1204
|
-
* Applies `typoStyle` to `value` according to the current mode.
|
|
1205
|
-
*
|
|
1206
|
-
* @param value - Raw text.
|
|
1207
|
-
* @param typoStyle - Style to apply.
|
|
1208
|
-
* @returns Styled string.
|
|
1209
|
-
*/
|
|
1210
|
-
computeStyledString(value: string, typoStyle: TypoStyle): string;
|
|
1211
|
-
/**
|
|
1212
|
-
* Formats any thrown value as `"Error: <message>"` with {@link typoStyleFailure} on the prefix.
|
|
1213
|
-
*
|
|
1214
|
-
* @param error - Any thrown value.
|
|
1215
|
-
* @returns Styled error string.
|
|
1216
|
-
*/
|
|
1217
|
-
computeStyledErrorMessage(error: unknown): string;
|
|
1218
|
-
}
|
|
1219
|
-
|
|
1220
|
-
/**
|
|
1221
|
-
* Converts a {@link CommandUsage} model into an array of styled lines ready to be
|
|
1222
|
-
* joined with `"\n"` and printed to the terminal.
|
|
1223
|
-
*
|
|
1224
|
-
* The output format is:
|
|
1225
|
-
* ```
|
|
1226
|
-
* Usage: <cliName> [segments...]
|
|
1227
|
-
*
|
|
1228
|
-
* <description> (<hint>)
|
|
1229
|
-
* <detail lines...>
|
|
1230
|
-
*
|
|
1231
|
-
* Positionals:
|
|
1232
|
-
* <LABEL> <description> (<hint>)
|
|
1233
|
-
*
|
|
1234
|
-
* Subcommands:
|
|
1235
|
-
* <name> <description> (<hint>)
|
|
1236
|
-
*
|
|
1237
|
-
* Options:
|
|
1238
|
-
* -s, --long <LABEL> <description> (<hint>)
|
|
1239
|
-
*
|
|
1240
|
-
* Examples:
|
|
1241
|
-
* <description>
|
|
1242
|
-
* <command line>
|
|
1243
|
-
*
|
|
1244
|
-
* ```
|
|
1245
|
-
* Sections that have no entries are omitted. The trailing empty line is always included.
|
|
1246
|
-
*
|
|
1247
|
-
* Column alignment within each section is handled by {@link TypoGrid}: the widest entry
|
|
1248
|
-
* in each column sets the width for the entire section.
|
|
1249
|
-
*
|
|
1250
|
-
* @param params.cliName - Program name for the usage line.
|
|
1251
|
-
* @param params.commandUsage - From {@link CommandDecoder.generateUsage}.
|
|
1252
|
-
* @param params.typoSupport - Rendering mode.
|
|
1253
|
-
* @returns One string per output line; trailing empty string for the blank line at the end.
|
|
1254
|
-
*
|
|
1255
|
-
* @example
|
|
1256
|
-
* ```ts
|
|
1257
|
-
* const lines = usageToStyledLines({
|
|
1258
|
-
* cliName: "my-cli",
|
|
1259
|
-
* commandUsage: commandDecoder.generateUsage(),
|
|
1260
|
-
* typoSupport: TypoSupport.tty(),
|
|
1261
|
-
* });
|
|
1262
|
-
* process.stdout.write(lines.join("\n"));
|
|
1263
|
-
* ```
|
|
1264
|
-
*/
|
|
1265
|
-
declare function usageToStyledLines(params: {
|
|
1266
|
-
cliName: Lowercase<string>;
|
|
1267
|
-
commandUsage: CommandUsage;
|
|
1268
|
-
typoSupport: TypoSupport;
|
|
1269
|
-
}): string[];
|
|
1270
|
-
|
|
1271
|
-
export { type Command, type CommandDecoder, type CommandInformation, type CommandInterpreter, type CommandUsage, type CommandUsageSegment, type CommandUsageSubcommand, type Operation, type OperationDecoder, type OperationInterpreter, type OperationUsage, type Option, type OptionDecoder, type OptionUsage, type Positional, type PositionalDecoder, type PositionalUsage, ReaderArgs, type ReaderOptionKey, type ReaderOptions, type ReaderPositionals, type Type, type TypoColor, TypoError, TypoGrid, TypoString, type TypoStyle, TypoSupport, TypoText, command, commandChained, commandWithSubcommands, operation, optionFlag, optionRepeatable, optionSingleValue, positionalOptional, positionalRequired, positionalVariadics, runAndExit, typeBoolean, typeDate, typeInteger, typeList, typeMapped, typeNumber, typeOneOf, typeString, typeTuple, typeUrl, typoStyleConstants, typoStyleFailure, typoStyleLogic, typoStyleQuote, typoStyleRegularStrong, typoStyleRegularWeaker, typoStyleTitle, typoStyleUserInput, usageToStyledLines };
|
|
1247
|
+
export { type Command, type CommandDecoder, type CommandInformation, type CommandInterpreter, type Operation, type OperationDecoder, type OperationInterpreter, type Option, type OptionDecoder, type Positional, type PositionalDecoder, ReaderArgs, type ReaderOptionKey, type ReaderOptionParsing, type ReaderOptionValue, type ReaderOptions, type ReaderPositionals, type Type, type TypoColor, TypoError, TypoGrid, TypoString, type TypoStyle, TypoSupport, TypoText, type UsageCommand, type UsageOperation, type UsageOption, type UsagePositional, type UsageSegment, type UsageSubcommand, command, commandChained, commandWithSubcommands, operation, optionFlag, optionRepeatable, optionSingleValue, positionalOptional, positionalRequired, positionalVariadics, runAndExit, typeBoolean, typeDate, typeInteger, typeList, typeMapped, typeNumber, typeOneOf, typeString, typeTuple, typeUrl, typoStyleConstants, typoStyleFailure, typoStyleLogic, typoStyleQuote, typoStyleRegularStrong, typoStyleRegularWeaker, typoStyleTitle, typoStyleUserInput, usageToStyledLines };
|