cli-kiss 0.2.2 → 0.2.3
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/dist/index.d.ts +509 -820
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/docs/.vitepress/config.mts +3 -0
- package/docs/guide/01_getting_started.md +4 -4
- package/docs/guide/02_commands.md +5 -10
- package/docs/guide/03_options.md +4 -6
- package/docs/guide/04_positionals.md +6 -8
- package/docs/guide/05_types.md +8 -10
- package/docs/guide/06_run.md +3 -4
- package/docs/index.md +3 -3
- package/package.json +1 -1
- package/src/lib/Command.ts +178 -245
- package/src/lib/Operation.ts +60 -80
- package/src/lib/Option.ts +112 -123
- package/src/lib/Positional.ts +59 -92
- package/src/lib/Reader.ts +53 -78
- package/src/lib/Run.ts +39 -57
- package/src/lib/Type.ts +81 -141
- package/src/lib/Typo.ts +121 -164
- package/src/lib/Usage.ts +56 -18
- package/tests/unit.command.execute.ts +81 -69
- package/tests/unit.command.usage.ts +228 -107
- package/tests/unit.runner.cycle.ts +37 -7
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* {@link ReaderArgs}
|
|
4
|
-
*
|
|
5
|
-
* Keys are returned by {@link ReaderArgs.registerOption} and passed back to
|
|
6
|
-
* {@link ReaderArgs.getOptionValues} to retrieve the parsed values. The internal
|
|
7
|
-
* representation is intentionally opaque — treat it as a handle, not a string.
|
|
2
|
+
* Opaque key identifying a registered option within a {@link ReaderArgs} instance.
|
|
3
|
+
* Returned by {@link ReaderArgs.registerOption}; passed to {@link ReaderArgs.getOptionValues}.
|
|
8
4
|
*/
|
|
9
5
|
type ReaderOptionKey = (string | {
|
|
10
6
|
__brand: "ReaderOptionKey";
|
|
@@ -12,23 +8,18 @@ type ReaderOptionKey = (string | {
|
|
|
12
8
|
__brand: "ReaderOptionKey";
|
|
13
9
|
};
|
|
14
10
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* {@link ReaderArgs} implements both `ReaderOptions` and {@link ReaderPositionals}.
|
|
18
|
-
* The two interfaces are exposed separately so that option and positional parsing logic
|
|
19
|
-
* can depend only on the capability they need.
|
|
11
|
+
* Option registration and query interface, implemented by {@link ReaderArgs}.
|
|
12
|
+
* Exposed separately from {@link ReaderPositionals} so parsers depend only on what they need.
|
|
20
13
|
*/
|
|
21
14
|
type ReaderOptions = {
|
|
22
15
|
/**
|
|
23
|
-
* Registers
|
|
16
|
+
* Registers an option so the parser can recognise it.
|
|
24
17
|
*
|
|
25
|
-
* @param definition.longs -
|
|
26
|
-
* @param definition.shorts -
|
|
27
|
-
* @param definition.valued -
|
|
28
|
-
*
|
|
29
|
-
* @
|
|
30
|
-
* @throws `Error` if any of the given names has already been registered, or if a
|
|
31
|
-
* short name overlaps (is a prefix of, or has as a prefix, another registered short).
|
|
18
|
+
* @param definition.longs - Long-form names (without `--`).
|
|
19
|
+
* @param definition.shorts - Short-form names (without `-`).
|
|
20
|
+
* @param definition.valued - `true` if the option takes a value; `false` for flags.
|
|
21
|
+
* @returns A {@link ReaderOptionKey} for later retrieval.
|
|
22
|
+
* @throws `Error` if a name is already registered or short names overlap.
|
|
32
23
|
*/
|
|
33
24
|
registerOption(definition: {
|
|
34
25
|
longs: Array<string>;
|
|
@@ -38,71 +29,49 @@ type ReaderOptions = {
|
|
|
38
29
|
/**
|
|
39
30
|
* Returns all values collected for the option identified by `key`.
|
|
40
31
|
*
|
|
41
|
-
* @param key -
|
|
42
|
-
* @returns
|
|
43
|
-
*
|
|
44
|
-
* @throws `Error` if `key` was not previously registered on this instance.
|
|
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.
|
|
45
35
|
*/
|
|
46
36
|
getOptionValues(key: ReaderOptionKey): Array<string>;
|
|
47
37
|
};
|
|
48
38
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* {@link ReaderArgs} implements both {@link ReaderOptions} and `ReaderPositionals`.
|
|
39
|
+
* Positional token consumption interface, implemented by {@link ReaderArgs}.
|
|
52
40
|
*/
|
|
53
41
|
type ReaderPositionals = {
|
|
54
42
|
/**
|
|
55
|
-
*
|
|
56
|
-
* any option tokens (which are parsed as side-effects).
|
|
43
|
+
* Returns the next positional token, parsing intervening options as side-effects.
|
|
57
44
|
*
|
|
58
|
-
* @returns The next positional
|
|
59
|
-
*
|
|
60
|
-
* @throws {@link TypoError} if an unrecognised option token is encountered while
|
|
61
|
-
* scanning for the next positional.
|
|
45
|
+
* @returns The next positional, or `undefined` when exhausted.
|
|
46
|
+
* @throws {@link TypoError} on an unrecognised option.
|
|
62
47
|
*/
|
|
63
48
|
consumePositional(): string | undefined;
|
|
64
49
|
};
|
|
65
50
|
/**
|
|
66
|
-
*
|
|
67
|
-
*
|
|
51
|
+
* Core argument parser: converts raw CLI tokens into named options and positionals.
|
|
52
|
+
* Options must be registered before {@link ReaderArgs.consumePositional} is called.
|
|
68
53
|
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* whether each token is an option name, an option value, or a bare positional.
|
|
54
|
+
* Supported syntax: `--name`, `--name value`, `--name=value`,
|
|
55
|
+
* `-n`, `-n value`, `-nvalue`, `-abc` (stacked), `--` (end-of-options).
|
|
72
56
|
*
|
|
73
|
-
*
|
|
74
|
-
* - Long options: `--name`, `--name value`, `--name=value`
|
|
75
|
-
* - Short options: `-n`, `-n value`, `-n=value`, `-nvalue`, `-abc` (stacked flags)
|
|
76
|
-
* - End-of-options separator: `--` — all subsequent tokens are treated as positionals.
|
|
77
|
-
*
|
|
78
|
-
* In most cases you do not need to use `ReaderArgs` directly; it is created internally
|
|
79
|
-
* by {@link runAndExit}. It is exposed for advanced use cases such as building
|
|
80
|
-
* custom runners.
|
|
57
|
+
* Created internally by {@link runAndExit}; exposed for advanced / custom runners.
|
|
81
58
|
*/
|
|
82
59
|
declare class ReaderArgs {
|
|
83
60
|
#private;
|
|
84
61
|
/**
|
|
85
|
-
* @param args -
|
|
86
|
-
* The array is not modified; a read cursor is maintained internally.
|
|
62
|
+
* @param args - Raw CLI tokens (e.g. `process.argv.slice(2)`). Not mutated.
|
|
87
63
|
*/
|
|
88
64
|
constructor(args: ReadonlyArray<string>);
|
|
89
65
|
/**
|
|
90
|
-
* Registers
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* `getOptionValues(key)` after parsing will return values collected under any of the
|
|
94
|
-
* registered names.
|
|
95
|
-
*
|
|
96
|
-
* Short names support stacking (e.g. `-abc` is parsed as `-a -b -c`) and inline
|
|
97
|
-
* values (e.g. `-nvalue`). Short names must not be a prefix of, nor have as a prefix,
|
|
98
|
-
* any other registered short name — the parser uses prefix matching to parse stacked
|
|
99
|
-
* shorts, so overlapping prefixes would be ambiguous.
|
|
66
|
+
* Registers an option; all `longs` and `shorts` share the same key.
|
|
67
|
+
* Short names support stacking (e.g. `-abc`) and inline values (e.g. `-nvalue`),
|
|
68
|
+
* but must not be prefixes of one another.
|
|
100
69
|
*
|
|
101
70
|
* @param definition.longs - Long-form names (without `--`).
|
|
102
71
|
* @param definition.shorts - Short-form names (without `-`).
|
|
103
|
-
* @param definition.valued - `true` if the option
|
|
104
|
-
* @returns
|
|
105
|
-
* @throws `Error` if any name is already registered or
|
|
72
|
+
* @param definition.valued - `true` if the option takes a value; `false` for flags.
|
|
73
|
+
* @returns A {@link ReaderOptionKey} for {@link ReaderArgs.getOptionValues}.
|
|
74
|
+
* @throws `Error` if any name is already registered or short names overlap.
|
|
106
75
|
*/
|
|
107
76
|
registerOption(definition: {
|
|
108
77
|
longs: Array<string>;
|
|
@@ -110,70 +79,51 @@ declare class ReaderArgs {
|
|
|
110
79
|
valued: boolean;
|
|
111
80
|
}): ReaderOptionKey;
|
|
112
81
|
/**
|
|
113
|
-
* Returns all
|
|
82
|
+
* Returns all values collected for the option key.
|
|
114
83
|
*
|
|
115
|
-
* @param key -
|
|
116
|
-
* @returns
|
|
117
|
-
*
|
|
118
|
-
* literal value strings.
|
|
119
|
-
* @throws `Error` if `key` was not registered on this instance.
|
|
84
|
+
* @param key - Key from {@link ReaderArgs.registerOption}.
|
|
85
|
+
* @returns String values, one per occurrence.
|
|
86
|
+
* @throws `Error` if `key` was not registered.
|
|
120
87
|
*/
|
|
121
88
|
getOptionValues(key: ReaderOptionKey): Array<string>;
|
|
122
89
|
/**
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
* Option tokens encountered during the scan are recorded in the internal results map
|
|
127
|
-
* (equivalent to recording their values against their key). Any unrecognised option token
|
|
128
|
-
* causes a {@link TypoError} to be thrown immediately.
|
|
129
|
-
*
|
|
130
|
-
* After `--` is encountered, all remaining tokens are treated as positionals.
|
|
90
|
+
* Returns the next bare positional token.
|
|
91
|
+
* Parse intervening options as side-effects.
|
|
92
|
+
* All tokens after `--` are treated as positionals.
|
|
131
93
|
*
|
|
132
|
-
* @returns The next positional
|
|
133
|
-
*
|
|
134
|
-
* @throws {@link TypoError} if an unrecognised option (long or short) is encountered.
|
|
94
|
+
* @returns The next positional, or `undefined` when exhausted.
|
|
95
|
+
* @throws {@link TypoError} on an unrecognised option.
|
|
135
96
|
*/
|
|
136
97
|
consumePositional(): string | undefined;
|
|
137
98
|
}
|
|
138
99
|
|
|
139
100
|
/**
|
|
140
|
-
*
|
|
101
|
+
* Decodes a raw CLI string into a typed value.
|
|
102
|
+
* A pair of a human-readable `content` name (e.g. `"Number"`) and a `decoder` function.
|
|
141
103
|
*
|
|
142
|
-
*
|
|
143
|
-
* - a `content` string — a human-readable name shown in help/error messages (e.g.
|
|
144
|
-
* `"String"`, `"Number"`, `"Url"`).
|
|
145
|
-
* - a `decoder` function — converts the raw string or throws a {@link TypoError} on
|
|
146
|
-
* invalid input.
|
|
147
|
-
*
|
|
148
|
-
* Built-in types: {@link typeString}, {@link typeBoolean}, {@link typeNumber},
|
|
104
|
+
* Built-in: {@link typeString}, {@link typeBoolean}, {@link typeNumber},
|
|
149
105
|
* {@link typeInteger}, {@link typeDate}, {@link typeUrl}.
|
|
106
|
+
* Composite: {@link typeOneOf}, {@link typeMapped}, {@link typeTuple}, {@link typeList}.
|
|
150
107
|
*
|
|
151
|
-
*
|
|
152
|
-
* {@link typeList}.
|
|
153
|
-
*
|
|
154
|
-
* @typeParam Value - The TypeScript type that the decoder produces.
|
|
108
|
+
* @typeParam Value - Type produced by the decoder.
|
|
155
109
|
*/
|
|
156
110
|
type Type<Value> = {
|
|
157
111
|
/**
|
|
158
|
-
* Human-readable name
|
|
159
|
-
* Examples: `"String"`, `"Number"`, `"Url"`.
|
|
112
|
+
* Human-readable name shown in help and error messages (e.g. `"String"`, `"Number"`).
|
|
160
113
|
*/
|
|
161
114
|
content: string;
|
|
162
115
|
/**
|
|
163
|
-
*
|
|
116
|
+
* Converts a raw CLI string into `Value`.
|
|
164
117
|
*
|
|
165
|
-
* @param
|
|
118
|
+
* @param input - Raw string from the command line.
|
|
166
119
|
* @returns The decoded value.
|
|
167
|
-
* @throws {@link TypoError}
|
|
120
|
+
* @throws {@link TypoError} on invalid input.
|
|
168
121
|
*/
|
|
169
|
-
decoder(
|
|
122
|
+
decoder(input: string): Value;
|
|
170
123
|
};
|
|
171
124
|
/**
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
* Primarily used internally by {@link optionFlag} for the `--flag=<value>` syntax, but
|
|
176
|
-
* can also be used in positionals or valued options.
|
|
125
|
+
* Decodes `"true"` / `"yes"` → `true` and `"false"` / `"no"` → `false` (case-insensitive).
|
|
126
|
+
* Used internally by {@link optionFlag} for the `--flag=<value>` syntax.
|
|
177
127
|
*
|
|
178
128
|
* @example
|
|
179
129
|
* ```ts
|
|
@@ -184,13 +134,8 @@ type Type<Value> = {
|
|
|
184
134
|
*/
|
|
185
135
|
declare const typeBoolean: Type<boolean>;
|
|
186
136
|
/**
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
* Accepts any format supported by the JavaScript `Date.parse` API, including ISO 8601
|
|
190
|
-
* strings (e.g. `"2024-01-15"`, `"2024-01-15T10:30:00Z"`). Non-parseable values throw
|
|
191
|
-
* a {@link TypoError}.
|
|
192
|
-
*
|
|
193
|
-
* Produces a `Date` object. The decoded value is the result of `new Date(Date.parse(value))`.
|
|
137
|
+
* Parses a date/time string via `Date.parse` into a `Date` object.
|
|
138
|
+
* Accepts any format supported by `Date.parse`, including ISO 8601.
|
|
194
139
|
*
|
|
195
140
|
* @example
|
|
196
141
|
* ```ts
|
|
@@ -201,11 +146,8 @@ declare const typeBoolean: Type<boolean>;
|
|
|
201
146
|
*/
|
|
202
147
|
declare const typeDate: Type<Date>;
|
|
203
148
|
/**
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
* Accepts integers, floating-point values, and scientific notation (e.g. `"3.14"`,
|
|
208
|
-
* `"-1"`, `"1e10"`). Values that produce `NaN` throw a {@link TypoError}.
|
|
149
|
+
* Parses a string into a `number` via `Number()`.
|
|
150
|
+
* Accepts integers, floats, and scientific notation; `NaN` throws a {@link TypoError}.
|
|
209
151
|
*
|
|
210
152
|
* @example
|
|
211
153
|
* ```ts
|
|
@@ -216,11 +158,8 @@ declare const typeDate: Type<Date>;
|
|
|
216
158
|
*/
|
|
217
159
|
declare const typeNumber: Type<number>;
|
|
218
160
|
/**
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
* Only accepts valid integer strings (e.g. `"42"`, `"-100"`, `"9007199254740993"`).
|
|
223
|
-
* Floating-point strings or non-numeric values throw a {@link TypoError}.
|
|
161
|
+
* Parses an integer string into a `bigint` via `BigInt()`.
|
|
162
|
+
* Floats and non-numeric strings throw a {@link TypoError}.
|
|
224
163
|
*
|
|
225
164
|
* @example
|
|
226
165
|
* ```ts
|
|
@@ -231,10 +170,8 @@ declare const typeNumber: Type<number>;
|
|
|
231
170
|
*/
|
|
232
171
|
declare const typeInteger: Type<bigint>;
|
|
233
172
|
/**
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
* The string must be a valid absolute URL (e.g. `"https://example.com/path?q=1"`).
|
|
237
|
-
* Relative URLs and malformed strings throw a {@link TypoError}.
|
|
173
|
+
* Parses an absolute URL string into a `URL` object.
|
|
174
|
+
* Relative or malformed URLs throw a {@link TypoError}.
|
|
238
175
|
*
|
|
239
176
|
* @example
|
|
240
177
|
* ```ts
|
|
@@ -244,9 +181,7 @@ declare const typeInteger: Type<bigint>;
|
|
|
244
181
|
*/
|
|
245
182
|
declare const typeUrl: Type<URL>;
|
|
246
183
|
/**
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
* This is the simplest type and accepts any string value without validation.
|
|
184
|
+
* Identity decoder — passes the raw string through unchanged.
|
|
250
185
|
*
|
|
251
186
|
* @example
|
|
252
187
|
* ```ts
|
|
@@ -256,29 +191,21 @@ declare const typeUrl: Type<URL>;
|
|
|
256
191
|
*/
|
|
257
192
|
declare const typeString: Type<string>;
|
|
258
193
|
/**
|
|
259
|
-
* Creates a
|
|
260
|
-
*
|
|
194
|
+
* Creates a {@link Type} by chaining `before`'s decoder with an `after` transformation.
|
|
195
|
+
* `before` errors are prefixed with `"from: <content>"` for traceability.
|
|
261
196
|
*
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
* prefix so that the full decoding path is visible in error messages.
|
|
197
|
+
* @typeParam Before - Intermediate type from `before.decoder`.
|
|
198
|
+
* @typeParam After - Final type from `after.decoder`.
|
|
265
199
|
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
* @
|
|
271
|
-
* @typeParam After - The final type produced by `after.decoder`.
|
|
272
|
-
*
|
|
273
|
-
* @param before - The base type that decodes the raw CLI string.
|
|
274
|
-
* @param after - The transformation applied to the `Before` value.
|
|
275
|
-
* @param after.content - Human-readable name for the resulting type (shown in errors).
|
|
276
|
-
* @param after.decoder - Function that converts a `Before` value into `After`.
|
|
277
|
-
* @returns A new {@link Type}`<After>` whose `content` is `after.content`.
|
|
200
|
+
* @param before - Base decoder for the raw string.
|
|
201
|
+
* @param after - Transformation applied to the decoded value.
|
|
202
|
+
* @param after.content - Name for the resulting type (shown in errors).
|
|
203
|
+
* @param after.decoder - Converts a `Before` value to `After`.
|
|
204
|
+
* @returns A {@link Type}`<After>`.
|
|
278
205
|
*
|
|
279
206
|
* @example
|
|
280
207
|
* ```ts
|
|
281
|
-
* const typePort =
|
|
208
|
+
* const typePort = typeMapped(typeNumber, {
|
|
282
209
|
* content: "Port",
|
|
283
210
|
* decoder: (n) => {
|
|
284
211
|
* if (n < 1 || n > 65535) throw new Error("Out of range");
|
|
@@ -289,23 +216,17 @@ declare const typeString: Type<string>;
|
|
|
289
216
|
* // "--port 99999" → TypoError: --port: <PORT>: Port: Out of range
|
|
290
217
|
* ```
|
|
291
218
|
*/
|
|
292
|
-
declare function
|
|
219
|
+
declare function typeMapped<Before, After>(before: Type<Before>, after: {
|
|
293
220
|
content: string;
|
|
294
221
|
decoder: (value: Before) => After;
|
|
295
222
|
}): Type<After>;
|
|
296
223
|
/**
|
|
297
|
-
* Creates a {@link Type}`<string>`
|
|
298
|
-
*
|
|
299
|
-
* The decoder performs an exact (case-sensitive) lookup in `values`. If the input is
|
|
300
|
-
* not in the set, a {@link TypoError} is thrown listing up to 5 of the valid options.
|
|
224
|
+
* Creates a {@link Type}`<string>` accepting only a fixed set of string values.
|
|
225
|
+
* Out-of-set inputs throw a {@link TypoError} listing up to 5 valid options.
|
|
301
226
|
*
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
* @
|
|
305
|
-
* messages (e.g. `"Environment"`, `"LogLevel"`).
|
|
306
|
-
* @param values - The ordered list of accepted string values. The order is preserved in
|
|
307
|
-
* the error message preview.
|
|
308
|
-
* @returns A {@link Type}`<string>` that validates membership in `values`.
|
|
227
|
+
* @param content - Name shown in help and errors (e.g. `"Environment"`).
|
|
228
|
+
* @param values - Ordered list of accepted values.
|
|
229
|
+
* @returns A {@link Type}`<string>`.
|
|
309
230
|
*
|
|
310
231
|
* @example
|
|
311
232
|
* ```ts
|
|
@@ -314,25 +235,16 @@ declare function typeConverted<Before, After>(before: Type<Before>, after: {
|
|
|
314
235
|
* typeEnv.decoder("unknown") // throws TypoError: Invalid value: "unknown" (expected one of: "dev" | "staging" | "prod")
|
|
315
236
|
* ```
|
|
316
237
|
*/
|
|
317
|
-
declare function typeOneOf(content: string, values: Array<
|
|
238
|
+
declare function typeOneOf<const Value extends string>(content: string, values: Array<Value>): Type<Value>;
|
|
318
239
|
/**
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
* The raw string is split on `separator` into exactly `elementTypes.length` parts.
|
|
323
|
-
* Each part is decoded by its corresponding element type. If the number of splits does
|
|
324
|
-
* not match, or if any element's decoder fails, a {@link TypoError} is thrown with the
|
|
325
|
-
* index and element type context.
|
|
240
|
+
* Splits a delimited string into a fixed-length typed tuple.
|
|
241
|
+
* Each part is decoded by the corresponding element type; wrong count or decode failure throws {@link TypoError}.
|
|
326
242
|
*
|
|
327
|
-
*
|
|
328
|
-
* (e.g. `"Number,String"` for a `[number, string]` tuple with `","` separator).
|
|
243
|
+
* @typeParam Elements - Tuple of decoded value types (inferred from `elementTypes`).
|
|
329
244
|
*
|
|
330
|
-
* @
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
* @param elementTypes - An ordered array of {@link Type}s, one per tuple element.
|
|
334
|
-
* @param separator - The string used to split the raw value (default: `","`).
|
|
335
|
-
* @returns A {@link Type}`<Elements>` tuple type.
|
|
245
|
+
* @param elementTypes - One {@link Type} per tuple element, in order.
|
|
246
|
+
* @param separator - Delimiter (default `","`).
|
|
247
|
+
* @returns A {@link Type}`<Elements>`.
|
|
336
248
|
*
|
|
337
249
|
* @example
|
|
338
250
|
* ```ts
|
|
@@ -346,26 +258,14 @@ declare function typeTuple<const Elements extends Array<any>>(elementTypes: {
|
|
|
346
258
|
[K in keyof Elements]: Type<Elements[K]>;
|
|
347
259
|
}, separator?: string): Type<Elements>;
|
|
348
260
|
/**
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
* The raw string is split on `separator` and each part is decoded by `elementType`.
|
|
353
|
-
* If any element's decoder fails, a {@link TypoError} is thrown with the index and
|
|
354
|
-
* element type context.
|
|
355
|
-
*
|
|
356
|
-
* Unlike {@link typeTuple}, the number of elements is not fixed; the result array
|
|
357
|
-
* length equals the number of `separator`-delimited parts in the input string. To pass
|
|
358
|
-
* an empty array, the user must pass an empty string (`""`), which splits into one
|
|
359
|
-
* empty-string element — consider using {@link optionRepeatable} instead if you want a
|
|
360
|
-
* naturally empty default.
|
|
261
|
+
* Splits a delimited string into a variable-length typed array.
|
|
262
|
+
* Each part is decoded by `elementType`; failed decodes throw {@link TypoError}.
|
|
263
|
+
* Note: splitting an empty string yields one empty element — prefer {@link optionRepeatable} for a zero-default.
|
|
361
264
|
*
|
|
362
|
-
*
|
|
363
|
-
* signal repeatability.
|
|
265
|
+
* @typeParam Value - Element type produced by `elementType.decoder`.
|
|
364
266
|
*
|
|
365
|
-
* @
|
|
366
|
-
*
|
|
367
|
-
* @param elementType - The {@link Type} used to decode each element.
|
|
368
|
-
* @param separator - The string used to split the raw value (default: `","`).
|
|
267
|
+
* @param elementType - Decoder applied to each element.
|
|
268
|
+
* @param separator - Delimiter (default `","`).
|
|
369
269
|
* @returns A {@link Type}`<Array<Value>>`.
|
|
370
270
|
*
|
|
371
271
|
* @example
|
|
@@ -381,86 +281,75 @@ declare function typeTuple<const Elements extends Array<any>>(elementTypes: {
|
|
|
381
281
|
declare function typeList<Value>(elementType: Type<Value>, separator?: string): Type<Array<Value>>;
|
|
382
282
|
|
|
383
283
|
/**
|
|
384
|
-
*
|
|
385
|
-
* and usage-generation logic.
|
|
284
|
+
* A CLI option (flag or valued) with its parsing and usage-generation logic.
|
|
386
285
|
*
|
|
387
|
-
*
|
|
388
|
-
* {@link optionRepeatable} and
|
|
286
|
+
* Created with {@link optionFlag}, {@link optionSingleValue}, or
|
|
287
|
+
* {@link optionRepeatable} and passed via the `options` map of {@link operation}.
|
|
389
288
|
*
|
|
390
|
-
* @typeParam Value -
|
|
391
|
-
* - `boolean` for flags created with {@link optionFlag}.
|
|
392
|
-
* - `T` for single-value options created with {@link optionSingleValue}.
|
|
393
|
-
* - `Array<T>` for repeatable options created with {@link optionRepeatable}.
|
|
289
|
+
* @typeParam Value - Decoded value type.
|
|
394
290
|
*/
|
|
395
291
|
type Option<Value> = {
|
|
396
|
-
/**
|
|
292
|
+
/**
|
|
293
|
+
* Returns metadata used to render the `Options:` section of help.
|
|
294
|
+
*/
|
|
397
295
|
generateUsage(): OptionUsage;
|
|
398
296
|
/**
|
|
399
|
-
* Registers the option on `readerOptions`
|
|
400
|
-
* returns an {@link OptionParser} that can later retrieve the parsed value(s).
|
|
401
|
-
*
|
|
402
|
-
* @param readerOptions - The shared {@link ReaderArgs} that will parse the raw
|
|
403
|
-
* command-line tokens.
|
|
297
|
+
* Registers the option on `readerOptions` and returns an {@link OptionDecoder}.
|
|
404
298
|
*/
|
|
405
|
-
|
|
299
|
+
registerAndMakeDecoder(readerOptions: ReaderArgs): OptionDecoder<Value>;
|
|
406
300
|
};
|
|
407
301
|
/**
|
|
408
|
-
*
|
|
409
|
-
*
|
|
410
|
-
* Returned by {@link Option.createParser} and called by {@link OperationFactory.createInstance}.
|
|
302
|
+
* Produced by {@link Option.registerAndMakeDecoder}.
|
|
411
303
|
*
|
|
412
|
-
* @typeParam Value -
|
|
304
|
+
* @typeParam Value - Decoded value type.
|
|
413
305
|
*/
|
|
414
|
-
type
|
|
415
|
-
|
|
306
|
+
type OptionDecoder<Value> = {
|
|
307
|
+
/**
|
|
308
|
+
* Returns the decoded option value.
|
|
309
|
+
*
|
|
310
|
+
* @throws {@link TypoError} if decoding failed.
|
|
311
|
+
*/
|
|
312
|
+
getAndDecodeValue(): Value;
|
|
416
313
|
};
|
|
417
314
|
/**
|
|
418
|
-
* Human-readable metadata for a single
|
|
315
|
+
* Human-readable metadata for a single option, used to render the `Options:` section
|
|
419
316
|
* of the help output produced by {@link usageToStyledLines}.
|
|
420
317
|
*/
|
|
421
318
|
type OptionUsage = {
|
|
422
|
-
/** Short description of what the option does. */
|
|
423
|
-
description: string | undefined;
|
|
424
319
|
/**
|
|
425
|
-
*
|
|
426
|
-
* Suitable for short caveats such as `"required"` or `"defaults to 42"`.
|
|
427
|
-
*/
|
|
428
|
-
hint: string | undefined;
|
|
429
|
-
/**
|
|
430
|
-
* The primary long-form name of the option, without the `--` prefix (e.g. `"verbose"`).
|
|
431
|
-
* The user passes this as `--verbose` on the command line.
|
|
320
|
+
* Long-form name without `--` (e.g. `"verbose"`).
|
|
432
321
|
*/
|
|
433
322
|
long: Lowercase<string>;
|
|
434
323
|
/**
|
|
435
|
-
*
|
|
436
|
-
* The user passes this as `-v` on the command line.
|
|
324
|
+
* Short-form name without `-` (e.g. `"v"`).
|
|
437
325
|
*/
|
|
438
326
|
short: string | undefined;
|
|
439
327
|
/**
|
|
440
|
-
*
|
|
441
|
-
|
|
328
|
+
* Help text in usage.
|
|
329
|
+
*/
|
|
330
|
+
description: string | undefined;
|
|
331
|
+
/**
|
|
332
|
+
* Short note shown in parentheses.
|
|
333
|
+
*/
|
|
334
|
+
hint: string | undefined;
|
|
335
|
+
/**
|
|
336
|
+
* Value placeholder in help (e.g. `"<FILE>"`). `undefined` for flags.
|
|
442
337
|
*/
|
|
443
338
|
label: Uppercase<string> | undefined;
|
|
444
339
|
};
|
|
445
340
|
/**
|
|
446
|
-
* Creates a boolean flag option
|
|
447
|
-
*
|
|
448
|
-
*
|
|
449
|
-
*
|
|
450
|
-
*
|
|
451
|
-
*
|
|
452
|
-
* -
|
|
453
|
-
*
|
|
454
|
-
*
|
|
455
|
-
* @param definition -
|
|
456
|
-
* @param definition.
|
|
457
|
-
* @param definition.
|
|
458
|
-
* @param definition.description - Human-readable description for the help output.
|
|
459
|
-
* @param definition.hint - Optional supplementary note shown in parentheses.
|
|
460
|
-
* @param definition.aliases - Additional long/short names that the parser also
|
|
461
|
-
* recognises as this flag.
|
|
462
|
-
* @param definition.default - Factory for the default value when the flag is absent.
|
|
463
|
-
* Defaults to `() => false` when omitted.
|
|
341
|
+
* Creates a boolean flag option (`--verbose`, optionally `--flag=no`).
|
|
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`.
|
|
464
353
|
* @returns An {@link Option}`<boolean>`.
|
|
465
354
|
*
|
|
466
355
|
* @example
|
|
@@ -484,32 +373,22 @@ declare function optionFlag(definition: {
|
|
|
484
373
|
default?: () => boolean;
|
|
485
374
|
}): Option<boolean>;
|
|
486
375
|
/**
|
|
487
|
-
* Creates an option that accepts exactly one value (e.g. `--output dist/`
|
|
488
|
-
*
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
*
|
|
492
|
-
*
|
|
493
|
-
*
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
*
|
|
500
|
-
* @
|
|
501
|
-
*
|
|
502
|
-
* @param definition -
|
|
503
|
-
* @param definition.long - Primary long-form name (without `--`). Must be lowercase.
|
|
504
|
-
* @param definition.short - Optional short-form name (without `-`).
|
|
505
|
-
* @param definition.description - Human-readable description for the help output.
|
|
506
|
-
* @param definition.hint - Optional supplementary note shown in parentheses.
|
|
507
|
-
* @param definition.aliases - Additional long/short names the parser also recognises.
|
|
508
|
-
* @param definition.label - Custom label shown in the help output (e.g. `"FILE"`).
|
|
509
|
-
* Defaults to the uppercased `type.content`.
|
|
510
|
-
* @param definition.type - The {@link Type} used to decode the raw string value.
|
|
511
|
-
* @param definition.default - Factory for the default value when the option is absent.
|
|
512
|
-
* Throw an error from this factory to make the option effectively required.
|
|
376
|
+
* Creates an option that accepts exactly one value (e.g. `--output dist/`).
|
|
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.
|
|
513
392
|
* @returns An {@link Option}`<Value>`.
|
|
514
393
|
*
|
|
515
394
|
* @example
|
|
@@ -538,30 +417,21 @@ declare function optionSingleValue<Value>(definition: {
|
|
|
538
417
|
default: () => Value;
|
|
539
418
|
}): Option<Value>;
|
|
540
419
|
/**
|
|
541
|
-
* Creates an option that
|
|
542
|
-
*
|
|
543
|
-
*
|
|
544
|
-
*
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
*
|
|
550
|
-
*
|
|
551
|
-
*
|
|
552
|
-
*
|
|
553
|
-
* @
|
|
554
|
-
*
|
|
555
|
-
*
|
|
556
|
-
* @param definition - Configuration for the option.
|
|
557
|
-
* @param definition.long - Primary long-form name (without `--`). Must be lowercase.
|
|
558
|
-
* @param definition.short - Optional short-form name (without `-`).
|
|
559
|
-
* @param definition.description - Human-readable description for the help output.
|
|
560
|
-
* @param definition.hint - Optional supplementary note shown in parentheses.
|
|
561
|
-
* @param definition.aliases - Additional long/short names the parser also recognises.
|
|
562
|
-
* @param definition.label - Custom label shown in the help output (e.g. `"FILE"`).
|
|
563
|
-
* Defaults to the uppercased `type.content`.
|
|
564
|
-
* @param definition.type - The {@link Type} used to decode each raw string value.
|
|
420
|
+
* Creates an option that collects every occurrence into an array (e.g. `--file a.ts --file b.ts`).
|
|
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.
|
|
565
435
|
* @returns An {@link Option}`<Array<Value>>`.
|
|
566
436
|
*
|
|
567
437
|
* @example
|
|
@@ -590,80 +460,68 @@ declare function optionRepeatable<Value>(definition: {
|
|
|
590
460
|
}): Option<Array<Value>>;
|
|
591
461
|
|
|
592
462
|
/**
|
|
593
|
-
*
|
|
594
|
-
* line — together with its parsing and usage-generation logic.
|
|
463
|
+
* A bare (non-option) positional argument with its parsing and usage-generation logic.
|
|
595
464
|
*
|
|
596
|
-
*
|
|
597
|
-
* {@link positionalVariadics} and
|
|
598
|
-
* {@link operation},
|
|
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.
|
|
599
468
|
*
|
|
600
|
-
* @typeParam Value -
|
|
469
|
+
* @typeParam Value - Decoded value type.
|
|
601
470
|
*/
|
|
602
471
|
type Positional<Value> = {
|
|
603
|
-
/**
|
|
472
|
+
/**
|
|
473
|
+
* Returns metadata used to render the `Positionals:` section of help.
|
|
474
|
+
*/
|
|
604
475
|
generateUsage(): PositionalUsage;
|
|
605
476
|
/**
|
|
606
|
-
* Consumes the positional from `readerPositionals
|
|
607
|
-
*
|
|
608
|
-
* The parser is created during {@link Operation.createFactory} and may throw a
|
|
609
|
-
* {@link TypoError} if the positional is missing (when required) or if decoding fails.
|
|
610
|
-
* @param readerPositionals - The source of positional arguments to be consumed.
|
|
477
|
+
* Consumes the next positional token from `readerPositionals`.
|
|
478
|
+
* Returns a decoder that produces the final value.
|
|
611
479
|
*/
|
|
612
|
-
|
|
480
|
+
consumeAndMakeDecoder(readerPositionals: ReaderPositionals): PositionalDecoder<Value>;
|
|
613
481
|
};
|
|
614
482
|
/**
|
|
615
|
-
*
|
|
483
|
+
* Produced by {@link Positional.consumeAndMakeDecoder}.
|
|
616
484
|
*
|
|
617
|
-
*
|
|
618
|
-
*
|
|
619
|
-
* @typeParam Value - The TypeScript type of the parsed value.
|
|
485
|
+
* @typeParam Value - Decoded value type.
|
|
620
486
|
*/
|
|
621
|
-
type
|
|
487
|
+
type PositionalDecoder<Value> = {
|
|
622
488
|
/**
|
|
623
|
-
* Returns the
|
|
489
|
+
* Returns the decoded positional value.
|
|
624
490
|
*
|
|
625
|
-
* @throws {@link TypoError} if
|
|
491
|
+
* @throws {@link TypoError} if decoding failed.
|
|
626
492
|
*/
|
|
627
|
-
|
|
493
|
+
decodeValue(): Value;
|
|
628
494
|
};
|
|
629
495
|
/**
|
|
630
496
|
* Human-readable metadata for a single positional argument, used to render the
|
|
631
497
|
* `Positionals:` section of the help output produced by {@link usageToStyledLines}.
|
|
632
498
|
*/
|
|
633
499
|
type PositionalUsage = {
|
|
634
|
-
/**
|
|
500
|
+
/**
|
|
501
|
+
* Help text.
|
|
502
|
+
*/
|
|
635
503
|
description: string | undefined;
|
|
636
504
|
/**
|
|
637
|
-
*
|
|
638
|
-
* Suitable for short caveats such as `"defaults to 'world'"`.
|
|
505
|
+
* Short note shown in parentheses.
|
|
639
506
|
*/
|
|
640
507
|
hint: string | undefined;
|
|
641
508
|
/**
|
|
642
|
-
*
|
|
643
|
-
* Required
|
|
644
|
-
* use square-bracket notation (e.g. `"[FILE]"`); variadic ones append `...`
|
|
645
|
-
* (e.g. `"[ITEM]..."`).
|
|
509
|
+
* Placeholder label shown in the usage line and the `Positionals:` section.
|
|
510
|
+
* Required: `<NAME>`, optional: `[NAME]`, variadic: `[NAME]...`.
|
|
646
511
|
*/
|
|
647
512
|
label: Uppercase<string>;
|
|
648
513
|
};
|
|
649
514
|
/**
|
|
650
|
-
* Creates a required positional
|
|
651
|
-
*
|
|
652
|
-
* The parser consumes the next available positional token and decodes it with
|
|
653
|
-
* `definition.type`. If no token is available, a {@link TypoError} is thrown immediately
|
|
654
|
-
* during parsing (i.e. inside {@link Operation.createFactory}).
|
|
515
|
+
* Creates a required positional — missing token throws {@link TypoError}.
|
|
516
|
+
* Label defaults to uppercased `type.content` in angle brackets (e.g. `<STRING>`).
|
|
655
517
|
*
|
|
656
|
-
*
|
|
657
|
-
* wrapped in angle brackets (e.g. `<STRING>`). Supply `label` to override.
|
|
518
|
+
* @typeParam Value - Type produced by the decoder.
|
|
658
519
|
*
|
|
659
|
-
* @
|
|
660
|
-
*
|
|
661
|
-
* @param definition -
|
|
662
|
-
* @param definition.
|
|
663
|
-
* @param definition.
|
|
664
|
-
* @param definition.label - Custom label shown in the usage line (without angle brackets).
|
|
665
|
-
* Defaults to the uppercased `type.content`.
|
|
666
|
-
* @param definition.type - The {@link Type} used to decode the raw string token.
|
|
520
|
+
* @param definition - Positional configuration.
|
|
521
|
+
* @param definition.description - Help text.
|
|
522
|
+
* @param definition.hint - Short note shown in parentheses.
|
|
523
|
+
* @param definition.label - Label without brackets; defaults to uppercased `type.content`.
|
|
524
|
+
* @param definition.type - Decoder for the raw token.
|
|
667
525
|
* @returns A {@link Positional}`<Value>`.
|
|
668
526
|
*
|
|
669
527
|
* @example
|
|
@@ -683,26 +541,17 @@ declare function positionalRequired<Value>(definition: {
|
|
|
683
541
|
type: Type<Value>;
|
|
684
542
|
}): Positional<Value>;
|
|
685
543
|
/**
|
|
686
|
-
* Creates an optional positional
|
|
687
|
-
*
|
|
688
|
-
*
|
|
689
|
-
*
|
|
690
|
-
*
|
|
691
|
-
*
|
|
692
|
-
*
|
|
693
|
-
*
|
|
694
|
-
*
|
|
695
|
-
*
|
|
696
|
-
* @
|
|
697
|
-
*
|
|
698
|
-
* @param definition - Configuration for the positional.
|
|
699
|
-
* @param definition.description - Human-readable description for the help output.
|
|
700
|
-
* @param definition.hint - Optional supplementary note shown in parentheses.
|
|
701
|
-
* @param definition.label - Custom label shown in the usage line (without square brackets).
|
|
702
|
-
* Defaults to the uppercased `type.content`.
|
|
703
|
-
* @param definition.type - The {@link Type} used to decode the raw string token.
|
|
704
|
-
* @param definition.default - Factory called when the positional is absent to supply the
|
|
705
|
-
* default value. Throw from this factory to make omission an error.
|
|
544
|
+
* Creates an optional positional — absent token falls back to `default()`.
|
|
545
|
+
* Label defaults to uppercased `type.content` in square brackets (e.g. `[STRING]`).
|
|
546
|
+
*
|
|
547
|
+
* @typeParam Value - Type produced by the decoder (or the default).
|
|
548
|
+
*
|
|
549
|
+
* @param definition - Positional configuration.
|
|
550
|
+
* @param definition.description - Help text.
|
|
551
|
+
* @param definition.hint - Short note shown in parentheses.
|
|
552
|
+
* @param definition.label - Label without brackets; defaults to uppercased `type.content`.
|
|
553
|
+
* @param definition.type - Decoder for the raw token.
|
|
554
|
+
* @param definition.default - Value when absent. Throw to make it required.
|
|
706
555
|
* @returns A {@link Positional}`<Value>`.
|
|
707
556
|
*
|
|
708
557
|
* @example
|
|
@@ -725,31 +574,17 @@ declare function positionalOptional<Value>(definition: {
|
|
|
725
574
|
default: () => Value;
|
|
726
575
|
}): Positional<Value>;
|
|
727
576
|
/**
|
|
728
|
-
* Creates a variadic positional
|
|
729
|
-
*
|
|
730
|
-
*
|
|
731
|
-
*
|
|
732
|
-
*
|
|
733
|
-
*
|
|
734
|
-
*
|
|
735
|
-
*
|
|
736
|
-
*
|
|
737
|
-
*
|
|
738
|
-
*
|
|
739
|
-
* `endDelimiter` is configured, the delimiter is also shown (e.g. `[STRING]...["--"]`).
|
|
740
|
-
* Supply `label` to override the base label.
|
|
741
|
-
*
|
|
742
|
-
* @typeParam Value - The TypeScript type produced by the type decoder for each token.
|
|
743
|
-
*
|
|
744
|
-
* @param definition - Configuration for the variadic positional.
|
|
745
|
-
* @param definition.endDelimiter - Optional sentinel string that signals the end of
|
|
746
|
-
* the variadic sequence (e.g. `"--"`). When encountered it is consumed but not
|
|
747
|
-
* included in the result array.
|
|
748
|
-
* @param definition.description - Human-readable description for the help output.
|
|
749
|
-
* @param definition.hint - Optional supplementary note shown in parentheses.
|
|
750
|
-
* @param definition.label - Custom label shown in the usage line (without brackets).
|
|
751
|
-
* Defaults to the uppercased `type.content`.
|
|
752
|
-
* @param definition.type - The {@link Type} used to decode each raw string token.
|
|
577
|
+
* Creates a variadic positional that collects zero or more remaining tokens into an array.
|
|
578
|
+
* Stops at `endDelimiter` (consumed, not included). Label: `[TYPE]...` notation.
|
|
579
|
+
*
|
|
580
|
+
* @typeParam Value - Type produced by the decoder for each token.
|
|
581
|
+
*
|
|
582
|
+
* @param definition - Positional configuration.
|
|
583
|
+
* @param definition.endDelimiter - Sentinel token that stops collection (consumed, not included).
|
|
584
|
+
* @param definition.description - Help text.
|
|
585
|
+
* @param definition.hint - Short note shown in parentheses.
|
|
586
|
+
* @param definition.label - Label without brackets; defaults to uppercased `type.content`.
|
|
587
|
+
* @param definition.type - Decoder applied to each token.
|
|
753
588
|
* @returns A {@link Positional}`<Array<Value>>`.
|
|
754
589
|
*
|
|
755
590
|
* @example
|
|
@@ -772,106 +607,80 @@ declare function positionalVariadics<Value>(definition: {
|
|
|
772
607
|
}): Positional<Array<Value>>;
|
|
773
608
|
|
|
774
609
|
/**
|
|
775
|
-
*
|
|
776
|
-
*
|
|
777
|
-
*
|
|
778
|
-
*
|
|
779
|
-
*
|
|
780
|
-
*
|
|
781
|
-
*
|
|
782
|
-
* @typeParam Input - The context value the handler receives at execution time (forwarded
|
|
783
|
-
* from the parent command's context or from a preceding chained operation).
|
|
784
|
-
* @typeParam Output - The value the handler produces. For leaf operations this is
|
|
785
|
-
* typically `void`; for intermediate stages it is the payload forwarded to the next
|
|
786
|
-
* command in a chain.
|
|
610
|
+
* Options, positionals, and an async handler that together form the logic of a CLI command.
|
|
611
|
+
*
|
|
612
|
+
* Created with {@link operation} and passed to {@link command},
|
|
613
|
+
* {@link commandWithSubcommands}, or {@link commandChained}.
|
|
614
|
+
*
|
|
615
|
+
* @typeParam Context - Injected at execution time; forwarded to handlers. Use to inject dependencies.
|
|
616
|
+
* @typeParam Result - Value produced on execution; typically `void` for leaf commands.
|
|
787
617
|
*/
|
|
788
|
-
type Operation<
|
|
618
|
+
type Operation<Context, Result> = {
|
|
789
619
|
/**
|
|
790
|
-
* Returns usage metadata
|
|
791
|
-
* Called by the parent command factory when building the help/usage output.
|
|
620
|
+
* Returns usage metadata without consuming any arguments.
|
|
792
621
|
*/
|
|
793
622
|
generateUsage(): OperationUsage;
|
|
794
623
|
/**
|
|
795
|
-
*
|
|
796
|
-
* {@link OperationFactory} that can create a ready-to-execute
|
|
797
|
-
* {@link OperationInstance}.
|
|
798
|
-
*
|
|
799
|
-
* Any parse error (unknown option, type mismatch, etc.) is captured and re-thrown
|
|
800
|
-
* when {@link OperationFactory.createInstance} is called.
|
|
801
|
-
*
|
|
802
|
-
* @param readerArgs - The shared argument reader. Options are registered on it and
|
|
803
|
-
* positionals are consumed in declaration order.
|
|
624
|
+
* Consumes args from `readerArgs` and returns an {@link OperationDecoder}.
|
|
804
625
|
*/
|
|
805
|
-
|
|
626
|
+
consumeAndMakeDecoder(readerArgs: ReaderArgs): OperationDecoder<Context, Result>;
|
|
806
627
|
};
|
|
807
628
|
/**
|
|
808
|
-
* Produced by {@link Operation.
|
|
809
|
-
* Instantiating it finalises value extraction and produces an {@link OperationInstance}.
|
|
629
|
+
* Produced by {@link Operation.consumeAndMakeDecoder}.
|
|
810
630
|
*
|
|
811
|
-
* @typeParam
|
|
812
|
-
* @typeParam
|
|
631
|
+
* @typeParam Context - See {@link Operation}.
|
|
632
|
+
* @typeParam Result - See {@link Operation}.
|
|
813
633
|
*/
|
|
814
|
-
type
|
|
634
|
+
type OperationDecoder<Context, Result> = {
|
|
815
635
|
/**
|
|
816
|
-
*
|
|
817
|
-
* {@link OperationInstance} ready for execution.
|
|
636
|
+
* Creates a ready-to-execute {@link OperationInterpreter}.
|
|
818
637
|
*
|
|
819
|
-
* @throws {@link TypoError} if
|
|
820
|
-
* {@link Operation.createFactory}.
|
|
638
|
+
* @throws {@link TypoError} if parsing or decoding failed.
|
|
821
639
|
*/
|
|
822
|
-
|
|
640
|
+
decodeAndMakeInterpreter(): OperationInterpreter<Context, Result>;
|
|
823
641
|
};
|
|
824
642
|
/**
|
|
825
|
-
* A fully parsed, ready-to-execute operation.
|
|
643
|
+
* A fully parsed, decoded and ready-to-execute operation.
|
|
826
644
|
*
|
|
827
|
-
* @typeParam
|
|
828
|
-
* @typeParam
|
|
645
|
+
* @typeParam Context - Caller-supplied context.
|
|
646
|
+
* @typeParam Result - Value produced on success.
|
|
829
647
|
*/
|
|
830
|
-
type
|
|
648
|
+
type OperationInterpreter<Context, Result> = {
|
|
831
649
|
/**
|
|
832
|
-
*
|
|
833
|
-
* option/positional values.
|
|
834
|
-
*
|
|
835
|
-
* @param input - Context from the parent command (or the root context supplied to
|
|
836
|
-
* {@link runAndExit}).
|
|
837
|
-
* @returns A promise resolving to the handler's return value.
|
|
650
|
+
* Executes with the provided context.
|
|
838
651
|
*/
|
|
839
|
-
executeWithContext(
|
|
652
|
+
executeWithContext(context: Context): Promise<Result>;
|
|
840
653
|
};
|
|
841
654
|
/**
|
|
842
|
-
*
|
|
843
|
-
* Consumed
|
|
655
|
+
* Usage metadata produced by {@link Operation.generateUsage}.
|
|
656
|
+
* Consumed when building {@link CommandUsage}.
|
|
844
657
|
*/
|
|
845
658
|
type OperationUsage = {
|
|
846
|
-
/**
|
|
659
|
+
/**
|
|
660
|
+
* Usage descriptors for all registered options.
|
|
661
|
+
*/
|
|
847
662
|
options: Array<OptionUsage>;
|
|
848
|
-
/**
|
|
663
|
+
/**
|
|
664
|
+
* Usage descriptors for all declared positionals, in order.
|
|
665
|
+
*/
|
|
849
666
|
positionals: Array<PositionalUsage>;
|
|
850
667
|
};
|
|
851
668
|
/**
|
|
852
|
-
* Creates an {@link Operation} from
|
|
853
|
-
*
|
|
854
|
-
*
|
|
855
|
-
*
|
|
856
|
-
*
|
|
857
|
-
*
|
|
858
|
-
*
|
|
859
|
-
*
|
|
860
|
-
*
|
|
861
|
-
*
|
|
862
|
-
*
|
|
863
|
-
* @
|
|
864
|
-
* @
|
|
865
|
-
* @
|
|
866
|
-
*
|
|
867
|
-
* @param inputs - Declares the options and positionals this operation accepts.
|
|
868
|
-
* @param inputs.options - A map from arbitrary keys to {@link Option} descriptors.
|
|
869
|
-
* The same keys appear in `handler`'s `inputs.options` argument.
|
|
870
|
-
* @param inputs.positionals - An ordered array of {@link Positional} descriptors.
|
|
871
|
-
* Their parsed values appear in `handler`'s `inputs.positionals` argument, in the
|
|
872
|
-
* same order.
|
|
873
|
-
* @param handler - The async function that implements the command logic. Receives the
|
|
874
|
-
* execution context and all parsed inputs.
|
|
669
|
+
* Creates an {@link Operation} from options, positionals, and an async handler.
|
|
670
|
+
*
|
|
671
|
+
* The `handler` receives the parent `context` and an `inputs` object with
|
|
672
|
+
* `options` (keyed by the same names declared in `inputs.options`) and
|
|
673
|
+
* `positionals` (a tuple in declaration order).
|
|
674
|
+
*
|
|
675
|
+
* @typeParam Context - Context type accepted by the handler.
|
|
676
|
+
* @typeParam Result - Return type of the handler.
|
|
677
|
+
* @typeParam Options - Map of option keys to parsed value types.
|
|
678
|
+
* @typeParam Positionals - Tuple of parsed positional value types, in order.
|
|
679
|
+
*
|
|
680
|
+
* @param inputs - Options and positionals this operation accepts.
|
|
681
|
+
* @param inputs.options - Map of keys to {@link Option} descriptors.
|
|
682
|
+
* @param inputs.positionals - Ordered array of {@link Positional} descriptors.
|
|
683
|
+
* @param handler - Async function implementing the command logic.
|
|
875
684
|
* @returns An {@link Operation} ready to be composed into a command.
|
|
876
685
|
*
|
|
877
686
|
* @example
|
|
@@ -907,168 +716,158 @@ declare function operation<Context, Result, Options extends {
|
|
|
907
716
|
}) => Promise<Result>): Operation<Context, Result>;
|
|
908
717
|
|
|
909
718
|
/**
|
|
910
|
-
*
|
|
911
|
-
*
|
|
719
|
+
* A CLI command — parses arguments and executes within a given context.
|
|
720
|
+
* Created with {@link command}, {@link commandWithSubcommands}, or {@link commandChained}.
|
|
721
|
+
* Usually passed through {@link runAndExit} to run.
|
|
912
722
|
*
|
|
913
|
-
*
|
|
914
|
-
*
|
|
915
|
-
* or {@link commandChained}, and pass it to {@link runAndExit} to run your CLI.
|
|
916
|
-
*
|
|
917
|
-
* @typeParam Context - The value passed into the command when it is executed. It flows
|
|
918
|
-
* from {@link runAndExit}'s `context` argument down through the command chain.
|
|
919
|
-
* @typeParam Result - The value produced by executing the command. For root commands
|
|
920
|
-
* passed to {@link runAndExit} this is always `void`.
|
|
723
|
+
* @typeParam Context - Injected at execution time; forwarded to handlers. Use to inject dependencies.
|
|
724
|
+
* @typeParam Result - Value produced on execution; typically `void` for leaf commands.
|
|
921
725
|
*/
|
|
922
726
|
type Command<Context, Result> = {
|
|
923
727
|
/**
|
|
924
|
-
* Returns the static metadata
|
|
728
|
+
* Returns the command's static metadata.
|
|
925
729
|
*/
|
|
926
730
|
getInformation(): CommandInformation;
|
|
927
731
|
/**
|
|
928
|
-
*
|
|
929
|
-
* information or create a ready-to-run {@link CommandInstance}.
|
|
930
|
-
*
|
|
931
|
-
* Parsing errors are captured and deferred: `createFactory` never throws; instead
|
|
932
|
-
* the error surfaces when {@link CommandFactory.createInstance} is called on the
|
|
933
|
-
* returned factory.
|
|
732
|
+
* Consumes args in a `readerArgs` and returns a {@link CommandDecoder}.
|
|
934
733
|
*/
|
|
935
|
-
|
|
734
|
+
consumeAndMakeDecoder(readerArgs: ReaderArgs): CommandDecoder<Context, Result>;
|
|
936
735
|
};
|
|
937
736
|
/**
|
|
938
|
-
* Produced by {@link Command.
|
|
939
|
-
* been parsed. Provides two capabilities:
|
|
737
|
+
* Produced by {@link Command.consumeAndMakeDecoder}.
|
|
940
738
|
*
|
|
941
|
-
*
|
|
942
|
-
*
|
|
943
|
-
*
|
|
944
|
-
* @typeParam Context - Input passed to the command {@link Command}.
|
|
945
|
-
* @typeParam Result - Result of the command's logic {@link Command}.
|
|
739
|
+
* @typeParam Context - See {@link Command}.
|
|
740
|
+
* @typeParam Result - See {@link Command}.
|
|
946
741
|
*/
|
|
947
|
-
type
|
|
742
|
+
type CommandDecoder<Context, Result> = {
|
|
948
743
|
/**
|
|
949
|
-
* Builds the
|
|
950
|
-
*
|
|
951
|
-
* is enabled.
|
|
744
|
+
* Builds the {@link CommandUsage} for the current command path.
|
|
745
|
+
* Used for `--help` and `usageOnError`.
|
|
952
746
|
*/
|
|
953
747
|
generateUsage(): CommandUsage;
|
|
954
748
|
/**
|
|
955
|
-
* Creates a {@link
|
|
749
|
+
* Creates a ready-to-execute {@link CommandInterpreter}.
|
|
956
750
|
*
|
|
957
|
-
* @throws {@link TypoError} if
|
|
958
|
-
* {@link Command.createFactory} encountered an error (e.g. unknown
|
|
959
|
-
* option, missing required positional, invalid type).
|
|
751
|
+
* @throws {@link TypoError} if parsing or decoding failed.
|
|
960
752
|
*/
|
|
961
|
-
|
|
753
|
+
decodeAndMakeInterpreter(): CommandInterpreter<Context, Result>;
|
|
962
754
|
};
|
|
963
755
|
/**
|
|
964
|
-
* A fully parsed, ready-to-execute command.
|
|
756
|
+
* A fully parsed, decoded and ready-to-execute command.
|
|
965
757
|
*
|
|
966
|
-
* @typeParam Context -
|
|
967
|
-
* @typeParam Result -
|
|
758
|
+
* @typeParam Context - Caller-supplied context.
|
|
759
|
+
* @typeParam Result - Value produced on success.
|
|
968
760
|
*/
|
|
969
|
-
type
|
|
761
|
+
type CommandInterpreter<Context, Result> = {
|
|
970
762
|
/**
|
|
971
|
-
* Executes
|
|
972
|
-
*
|
|
973
|
-
* @param context - Arbitrary value injected by the caller (see {@link runAndExit}).
|
|
974
|
-
* @returns A promise that resolves to the command's result, or rejects if the
|
|
975
|
-
* command handler throws.
|
|
763
|
+
* Executes with the provided context.
|
|
976
764
|
*/
|
|
977
765
|
executeWithContext(context: Context): Promise<Result>;
|
|
978
766
|
};
|
|
979
767
|
/**
|
|
980
|
-
* Static,
|
|
981
|
-
*
|
|
982
|
-
* This information is displayed in the usage/help output produced by {@link usageToStyledLines}.
|
|
768
|
+
* Static metadata for a command, shown in `--help` output via {@link usageToStyledLines}.
|
|
983
769
|
*/
|
|
984
770
|
type CommandInformation = {
|
|
985
|
-
/**
|
|
771
|
+
/**
|
|
772
|
+
* Short description shown in the usage header.
|
|
773
|
+
*/
|
|
986
774
|
description: string;
|
|
987
775
|
/**
|
|
988
|
-
*
|
|
989
|
-
* Suitable for short caveats such as `"deprecated"` or `"experimental"`.
|
|
776
|
+
* Short note shown in parentheses (e.g. `"deprecated"`, `"experimental"`).
|
|
990
777
|
*/
|
|
991
778
|
hint?: string;
|
|
992
779
|
/**
|
|
993
|
-
*
|
|
994
|
-
* Useful for multi-line explanations, examples, or caveats that don't fit in
|
|
995
|
-
* a single sentence.
|
|
780
|
+
* Extra lines printed below the description.
|
|
996
781
|
*/
|
|
997
782
|
details?: Array<string>;
|
|
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
|
+
}>;
|
|
998
808
|
};
|
|
999
809
|
/**
|
|
1000
|
-
*
|
|
1001
|
-
*
|
|
1002
|
-
*
|
|
1003
|
-
* {@link usageToStyledLines} to render the `--help` output.
|
|
810
|
+
* Full usage/help model.
|
|
811
|
+
* Produced by {@link CommandDecoder.generateUsage},
|
|
812
|
+
* Consumed by {@link usageToStyledLines}.
|
|
1004
813
|
*/
|
|
1005
814
|
type CommandUsage = {
|
|
1006
815
|
/**
|
|
1007
|
-
*
|
|
1008
|
-
* `
|
|
1009
|
-
|
|
1010
|
-
|
|
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.
|
|
1011
822
|
*/
|
|
1012
|
-
breadcrumbs: Array<CommandUsageBreadcrumb>;
|
|
1013
|
-
/** The command's static metadata (description, hint, details). */
|
|
1014
823
|
information: CommandInformation;
|
|
1015
824
|
/**
|
|
1016
|
-
*
|
|
1017
|
-
* in the order they must appear on the command line.
|
|
825
|
+
* Positionals in declaration order.
|
|
1018
826
|
*/
|
|
1019
827
|
positionals: Array<PositionalUsage>;
|
|
1020
828
|
/**
|
|
1021
|
-
*
|
|
1022
|
-
* Non-empty only when the command is a {@link commandWithSubcommands} and the
|
|
1023
|
-
* subcommand selection could not be resolved (i.e. on error or `--help`).
|
|
829
|
+
* Available subcommands. Non-empty when subcommand was not specified.
|
|
1024
830
|
*/
|
|
1025
831
|
subcommands: Array<CommandUsageSubcommand>;
|
|
1026
832
|
/**
|
|
1027
|
-
* Options
|
|
1028
|
-
* in the order they were registered.
|
|
833
|
+
* Options in registration order.
|
|
1029
834
|
*/
|
|
1030
835
|
options: Array<OptionUsage>;
|
|
1031
836
|
};
|
|
1032
837
|
/**
|
|
1033
|
-
*
|
|
1034
|
-
*
|
|
1035
|
-
* - `{ positional: string }` — A positional placeholder such as `<NAME>` or `[FILE]`.
|
|
1036
|
-
* - `{ command: string }` — A literal subcommand token such as `deploy`.
|
|
838
|
+
* One element in the usage segment trail.
|
|
1037
839
|
*/
|
|
1038
|
-
type
|
|
840
|
+
type CommandUsageSegment = {
|
|
1039
841
|
positional: string;
|
|
1040
842
|
} | {
|
|
1041
843
|
command: string;
|
|
1042
844
|
};
|
|
1043
845
|
/**
|
|
1044
|
-
*
|
|
1045
|
-
* of the usage output.
|
|
846
|
+
* Subcommand entry shown in the `Subcommands:` section of the usage output.
|
|
1046
847
|
*/
|
|
1047
848
|
type CommandUsageSubcommand = {
|
|
1048
|
-
/**
|
|
849
|
+
/**
|
|
850
|
+
* Literal token the user types (e.g. `"deploy"`).
|
|
851
|
+
*/
|
|
1049
852
|
name: string;
|
|
1050
|
-
/**
|
|
853
|
+
/**
|
|
854
|
+
* Short description from the subcommand's {@link CommandInformation}.
|
|
855
|
+
*/
|
|
1051
856
|
description: string | undefined;
|
|
1052
|
-
/**
|
|
857
|
+
/**
|
|
858
|
+
* Hint from the subcommand's {@link CommandInformation}.
|
|
859
|
+
*/
|
|
1053
860
|
hint: string | undefined;
|
|
1054
861
|
};
|
|
1055
862
|
/**
|
|
1056
|
-
* Creates a leaf command
|
|
1057
|
-
* an {@link Operation}.
|
|
1058
|
-
*
|
|
1059
|
-
* During parsing, `command` reads all positionals and options consumed by `operation`,
|
|
1060
|
-
* then asserts that no extra positionals remain. Any unexpected trailing positional
|
|
1061
|
-
* causes a {@link TypoError} deferred to {@link CommandFactory.createInstance}.
|
|
863
|
+
* Creates a leaf command that directly executes an {@link Operation}.
|
|
1062
864
|
*
|
|
1063
|
-
* @typeParam Context -
|
|
1064
|
-
*
|
|
1065
|
-
* @typeParam Result - The value returned by the operation handler.
|
|
865
|
+
* @typeParam Context - Context forwarded to the handler.
|
|
866
|
+
* @typeParam Result - Value returned by the handler.
|
|
1066
867
|
*
|
|
1067
|
-
* @param information -
|
|
1068
|
-
* @param operation -
|
|
1069
|
-
*
|
|
1070
|
-
* @returns A {@link Command} suitable for passing to {@link runAndExit}
|
|
1071
|
-
* or composing with {@link commandWithSubcommands} / {@link commandChained}.
|
|
868
|
+
* @param information - Command metadata (description, hint, details).
|
|
869
|
+
* @param operation - Defines: options, positionals, and the handler.
|
|
870
|
+
* @returns A {@link Command}.
|
|
1072
871
|
*
|
|
1073
872
|
* @example
|
|
1074
873
|
* ```ts
|
|
@@ -1083,34 +882,17 @@ type CommandUsageSubcommand = {
|
|
|
1083
882
|
*/
|
|
1084
883
|
declare function command<Context, Result>(information: CommandInformation, operation: Operation<Context, Result>): Command<Context, Result>;
|
|
1085
884
|
/**
|
|
1086
|
-
* Creates a command that
|
|
1087
|
-
*
|
|
1088
|
-
*
|
|
1089
|
-
*
|
|
1090
|
-
*
|
|
1091
|
-
*
|
|
1092
|
-
*
|
|
1093
|
-
*
|
|
1094
|
-
*
|
|
1095
|
-
*
|
|
1096
|
-
*
|
|
1097
|
-
*
|
|
1098
|
-
* **Usage on error / `--help`:** when the subcommand cannot be determined, the usage
|
|
1099
|
-
* output lists all available subcommands under a `Subcommands:` section.
|
|
1100
|
-
*
|
|
1101
|
-
* @typeParam Context - The context value accepted by the root operation handler.
|
|
1102
|
-
* @typeParam Payload - The value produced by the root operation and forwarded as the
|
|
1103
|
-
* context to the selected subcommand.
|
|
1104
|
-
* @typeParam Result - The value produced by the selected subcommand.
|
|
1105
|
-
*
|
|
1106
|
-
* @param information - Static metadata shown in the top-level usage when no valid
|
|
1107
|
-
* subcommand has been selected.
|
|
1108
|
-
* @param operation - The operation that is always executed first, before the
|
|
1109
|
-
* subcommand. Its output becomes the subcommand's context.
|
|
1110
|
-
* @param subcommands - A map of lowercase subcommand names to their
|
|
1111
|
-
* {@link Command}s. The keys are the literal tokens the user types.
|
|
1112
|
-
* @returns A {@link Command} that dispatches to one of the provided
|
|
1113
|
-
* subcommands.
|
|
885
|
+
* Creates a command that runs an {@link Operation} to produce a `Payload`,
|
|
886
|
+
* then dispatches to a named subcommand based on the next positional token.
|
|
887
|
+
*
|
|
888
|
+
* @typeParam Context - Context accepted by `operation`.
|
|
889
|
+
* @typeParam Payload - Output of `operation`; becomes the subcommand's context.
|
|
890
|
+
* @typeParam Result - Value produced by the selected subcommand.
|
|
891
|
+
*
|
|
892
|
+
* @param information - Command metadata (description, hint, details).
|
|
893
|
+
* @param operation - Always runs first; its output becomes the subcommand's context.
|
|
894
|
+
* @param subcommands - Map of subcommand names to their {@link Command}s.
|
|
895
|
+
* @returns A {@link Command} that dispatches to one of the provided subcommands.
|
|
1114
896
|
*
|
|
1115
897
|
* @example
|
|
1116
898
|
* ```ts
|
|
@@ -1128,36 +910,17 @@ declare function commandWithSubcommands<Context, Payload, Result>(information: C
|
|
|
1128
910
|
[subcommand: Lowercase<string>]: Command<Payload, Result>;
|
|
1129
911
|
}): Command<Context, Result>;
|
|
1130
912
|
/**
|
|
1131
|
-
*
|
|
1132
|
-
*
|
|
1133
|
-
*
|
|
1134
|
-
*
|
|
1135
|
-
*
|
|
1136
|
-
*
|
|
1137
|
-
*
|
|
1138
|
-
*
|
|
1139
|
-
*
|
|
1140
|
-
*
|
|
1141
|
-
*
|
|
1142
|
-
* remaining unparsed tokens).
|
|
1143
|
-
* 3. At execution time, `operation` runs first; its result is passed as the context to
|
|
1144
|
-
* `nextCommand`.
|
|
1145
|
-
*
|
|
1146
|
-
* **Usage:** breadcrumbs, positionals, and options from both stages are merged into a
|
|
1147
|
-
* single flat usage description. The `information` of `nextCommand` takes precedence in
|
|
1148
|
-
* the generated usage output.
|
|
1149
|
-
*
|
|
1150
|
-
* @typeParam Context - The context value accepted by `operation`.
|
|
1151
|
-
* @typeParam Payload - The value produced by `operation` and used as the context for
|
|
1152
|
-
* `nextCommand`.
|
|
1153
|
-
* @typeParam Result - The value produced by `nextCommand`.
|
|
1154
|
-
*
|
|
1155
|
-
* @param information - Fallback metadata used in the usage output when `nextCommand`'s
|
|
1156
|
-
* factory cannot be created (i.e. on parse error in the next stage).
|
|
1157
|
-
* @param operation - The first stage operation. Its output becomes `nextCommand`'s
|
|
1158
|
-
* context.
|
|
1159
|
-
* @param nextCommand - The second stage command, executed after `operation` succeeds.
|
|
1160
|
-
* @returns A {@link Command} that transparently composes the two stages.
|
|
913
|
+
* Chains an {@link Operation} and a {@link Command}: `operation` runs first, its
|
|
914
|
+
* output becomes `subcommand`'s context. No token is consumed for routing.
|
|
915
|
+
*
|
|
916
|
+
* @typeParam Context - Context accepted by `operation`.
|
|
917
|
+
* @typeParam Payload - Output of `operation`; becomes `subcommand`'s context.
|
|
918
|
+
* @typeParam Result - Value produced by `subcommand`.
|
|
919
|
+
*
|
|
920
|
+
* @param information - Command metadata (description, hint, details).
|
|
921
|
+
* @param operation - First stage; its output is passed as `subcommand`'s context.
|
|
922
|
+
* @param subcommand - Second stage, executed after `operation`.
|
|
923
|
+
* @returns A {@link Command} transparently composing the two stages.
|
|
1161
924
|
*
|
|
1162
925
|
* @example
|
|
1163
926
|
* ```ts
|
|
@@ -1171,56 +934,30 @@ declare function commandWithSubcommands<Context, Payload, Result>(information: C
|
|
|
1171
934
|
* );
|
|
1172
935
|
* ```
|
|
1173
936
|
*/
|
|
1174
|
-
declare function commandChained<Context, Payload, Result>(information: CommandInformation, operation: Operation<Context, Payload>,
|
|
937
|
+
declare function commandChained<Context, Payload, Result>(information: CommandInformation, operation: Operation<Context, Payload>, subcommand: Command<Payload, Result>): Command<Context, Result>;
|
|
1175
938
|
|
|
1176
939
|
/**
|
|
1177
|
-
*
|
|
1178
|
-
*
|
|
1179
|
-
*
|
|
1180
|
-
*
|
|
1181
|
-
*
|
|
1182
|
-
*
|
|
1183
|
-
*
|
|
1184
|
-
*
|
|
1185
|
-
*
|
|
1186
|
-
*
|
|
1187
|
-
*
|
|
1188
|
-
*
|
|
1189
|
-
*
|
|
1190
|
-
* -
|
|
1191
|
-
*
|
|
1192
|
-
* -
|
|
1193
|
-
*
|
|
1194
|
-
*
|
|
1195
|
-
* @
|
|
1196
|
-
*
|
|
1197
|
-
*
|
|
1198
|
-
* @param cliName - The name of the CLI program (e.g. `"my-cli"`). Used in the usage
|
|
1199
|
-
* summary header and in the `--version` output.
|
|
1200
|
-
* @param cliArgs - The raw command-line arguments to parse, typically `process.argv.slice(2)`.
|
|
1201
|
-
* @param context - The context value forwarded to the command's execution handler.
|
|
1202
|
-
* @param command - The root {@link Command} that describes how to parse and execute
|
|
1203
|
-
* the CLI.
|
|
1204
|
-
* @param options - Optional configuration for the runner.
|
|
1205
|
-
* @param options.useTtyColors - Controls terminal color output in styled messages.
|
|
1206
|
-
* - `true` — Always apply ANSI color codes.
|
|
1207
|
-
* - `false` — Never apply color codes (plain text).
|
|
1208
|
-
* - `"mock"` — Use a deterministic mock style useful for snapshot testing.
|
|
1209
|
-
* - `undefined` (default) — Auto-detect based on `process.stdout.isTTY` and the
|
|
1210
|
-
* `FORCE_COLOR` / `NO_COLOR` environment variables.
|
|
1211
|
-
* @param options.usageOnHelp - When `true` (default), registers a `--help` flag that
|
|
1212
|
-
* prints the usage summary and exits with code `0`.
|
|
1213
|
-
* @param options.usageOnError - When `true` (default), prints the usage summary to
|
|
1214
|
-
* stderr before the error message whenever argument parsing fails.
|
|
1215
|
-
* @param options.buildVersion - When provided, registers a `--version` flag that prints
|
|
1216
|
-
* `<cliName> <buildVersion>` to stdout and exits with code `0`.
|
|
1217
|
-
* @param options.onError - Custom handler for errors thrown during command execution.
|
|
1218
|
-
* If omitted, the error is printed to stderr via {@link TypoSupport}.
|
|
1219
|
-
* @param options.onExit - Overrides the process exit function (default: `process.exit`).
|
|
1220
|
-
* Useful for testing — supply a function that throws or captures the exit code instead
|
|
1221
|
-
* of actually terminating the process.
|
|
1222
|
-
*
|
|
1223
|
-
* @returns A `Promise<never>` because the function always terminates by calling `onExit`.
|
|
940
|
+
* Main entry point: parses CLI arguments, executes the matched command, and exits.
|
|
941
|
+
* Handles `--help`, `--version`, usage-on-error, and exit codes.
|
|
942
|
+
*
|
|
943
|
+
* Exit codes: `0` on success / `--help` / `--version`; `1` on parse or execution error.
|
|
944
|
+
*
|
|
945
|
+
* @typeParam Context - Passed unchanged to the command handler; use to inject dependencies.
|
|
946
|
+
*
|
|
947
|
+
* @param cliName - Program name used in usage and `--version` output.
|
|
948
|
+
* @param cliArgs - Raw arguments, typically `process.argv.slice(2)`.
|
|
949
|
+
* @param context - Forwarded to the command handler, injected dependencies.
|
|
950
|
+
* @param command - Root {@link Command}.
|
|
951
|
+
* @param options - Optional runner configuration.
|
|
952
|
+
* @param options.useTtyColors - Color mode: `true` (always), `false` (never),
|
|
953
|
+
* `"mock"` (snapshot-friendly), `undefined` (auto-detect from env).
|
|
954
|
+
* @param options.usageOnHelp - Enables `--help` flag (default `true`).
|
|
955
|
+
* @param options.usageOnError - Prints usage to stderr on parse error (default `true`).
|
|
956
|
+
* @param options.buildVersion - Enables `--version`; prints `<cliName> <buildVersion>`.
|
|
957
|
+
* @param options.onError - Custom handler for errors.
|
|
958
|
+
* @param options.onExit - Overrides `process.exit`; useful for testing.
|
|
959
|
+
*
|
|
960
|
+
* @returns `Promise<never>` — always calls `onExit`.
|
|
1224
961
|
*
|
|
1225
962
|
* @example
|
|
1226
963
|
* ```ts
|
|
@@ -1251,282 +988,231 @@ declare function runAndExit<Context>(cliName: Lowercase<string>, cliArgs: Readon
|
|
|
1251
988
|
}): Promise<never>;
|
|
1252
989
|
|
|
1253
990
|
/**
|
|
1254
|
-
*
|
|
1255
|
-
*
|
|
1256
|
-
* Colors are divided into two groups:
|
|
1257
|
-
* - **dark** variants correspond to standard ANSI colors (codes 30–37 / 40–47).
|
|
1258
|
-
* - **bright** variants correspond to high-intensity ANSI colors (codes 90–97 / 100–107).
|
|
1259
|
-
*
|
|
1260
|
-
* Used by {@link TypoStyle}'s `fgColor` and `bgColor` fields.
|
|
991
|
+
* Color names for terminal styling, used by {@link TypoStyle}.
|
|
992
|
+
* `dark*` = standard ANSI (30–37); `bright*` = high-intensity (90–97).
|
|
1261
993
|
*/
|
|
1262
994
|
type TypoColor = "darkBlack" | "darkRed" | "darkGreen" | "darkYellow" | "darkBlue" | "darkMagenta" | "darkCyan" | "darkWhite" | "brightBlack" | "brightRed" | "brightGreen" | "brightYellow" | "brightBlue" | "brightMagenta" | "brightCyan" | "brightWhite";
|
|
1263
995
|
/**
|
|
1264
|
-
*
|
|
1265
|
-
*
|
|
1266
|
-
*
|
|
1267
|
-
* All fields are optional. When `TypoSupport` is in `"none"` mode, no styling is
|
|
1268
|
-
* applied and the raw text is returned unchanged. In `"tty"` mode the corresponding
|
|
1269
|
-
* ANSI escape codes are emitted. In `"mock"` mode a deterministic textual representation
|
|
1270
|
-
* is produced (useful for snapshot tests).
|
|
996
|
+
* Visual styling applied by a {@link TypoSupport} instance.
|
|
997
|
+
* All fields are optional; ignored entirely in `"none"` mode.
|
|
1271
998
|
*/
|
|
1272
999
|
type TypoStyle = {
|
|
1273
|
-
/**
|
|
1000
|
+
/**
|
|
1001
|
+
* Foreground (text) color.
|
|
1002
|
+
*/
|
|
1274
1003
|
fgColor?: TypoColor;
|
|
1275
|
-
/**
|
|
1004
|
+
/**
|
|
1005
|
+
* Background color.
|
|
1006
|
+
*/
|
|
1276
1007
|
bgColor?: TypoColor;
|
|
1277
|
-
/**
|
|
1008
|
+
/**
|
|
1009
|
+
* Render with reduced intensity.
|
|
1010
|
+
*/
|
|
1278
1011
|
dim?: boolean;
|
|
1279
|
-
/**
|
|
1012
|
+
/**
|
|
1013
|
+
* Render in bold.
|
|
1014
|
+
*/
|
|
1280
1015
|
bold?: boolean;
|
|
1281
|
-
/**
|
|
1016
|
+
/**
|
|
1017
|
+
* Render in italic.
|
|
1018
|
+
*/
|
|
1282
1019
|
italic?: boolean;
|
|
1283
|
-
/**
|
|
1020
|
+
/**
|
|
1021
|
+
* Render with an underline.
|
|
1022
|
+
*/
|
|
1284
1023
|
underline?: boolean;
|
|
1285
|
-
/**
|
|
1024
|
+
/**
|
|
1025
|
+
* Render with a strikethrough.
|
|
1026
|
+
*/
|
|
1286
1027
|
strikethrough?: boolean;
|
|
1287
1028
|
};
|
|
1288
1029
|
/**
|
|
1289
|
-
* Pre-defined
|
|
1290
|
-
* `"Positionals:"`, `"Options:"`).
|
|
1291
|
-
* Rendered in bold dark-green.
|
|
1030
|
+
* Pre-defined style for section titles (e.g. `"Positionals:"`). Bold dark-green.
|
|
1292
1031
|
*/
|
|
1293
1032
|
declare const typoStyleTitle: TypoStyle;
|
|
1294
|
-
/**
|
|
1033
|
+
/**
|
|
1034
|
+
* Pre-defined style for logic/type identifiers in error messages. Bold dark-magenta.
|
|
1035
|
+
*/
|
|
1295
1036
|
declare const typoStyleLogic: TypoStyle;
|
|
1296
|
-
/**
|
|
1037
|
+
/**
|
|
1038
|
+
* Pre-defined style for quoted user-supplied values in error messages. Bold dark-yellow.
|
|
1039
|
+
*/
|
|
1297
1040
|
declare const typoStyleQuote: TypoStyle;
|
|
1298
|
-
/**
|
|
1041
|
+
/**
|
|
1042
|
+
* Pre-defined style for failure/error labels (e.g. `"Error:"`). Bold dark-red.
|
|
1043
|
+
*/
|
|
1299
1044
|
declare const typoStyleFailure: TypoStyle;
|
|
1300
|
-
/**
|
|
1045
|
+
/**
|
|
1046
|
+
* Pre-defined style for CLI flag/option/command names. Bold dark-cyan.
|
|
1047
|
+
*/
|
|
1301
1048
|
declare const typoStyleConstants: TypoStyle;
|
|
1302
|
-
/**
|
|
1049
|
+
/**
|
|
1050
|
+
* Pre-defined style for positional placeholders and user-input labels. Bold dark-blue.
|
|
1051
|
+
*/
|
|
1303
1052
|
declare const typoStyleUserInput: TypoStyle;
|
|
1304
|
-
/**
|
|
1053
|
+
/**
|
|
1054
|
+
* Pre-defined style for strong regular text (e.g. command descriptions). Bold.
|
|
1055
|
+
*/
|
|
1305
1056
|
declare const typoStyleRegularStrong: TypoStyle;
|
|
1306
|
-
/**
|
|
1057
|
+
/**
|
|
1058
|
+
* Pre-defined style for subtle supplementary text (e.g. hints). Italic and dim.
|
|
1059
|
+
*/
|
|
1307
1060
|
declare const typoStyleRegularWeaker: TypoStyle;
|
|
1308
1061
|
/**
|
|
1309
|
-
* An immutable styled string segment
|
|
1310
|
-
* {@link
|
|
1311
|
-
*
|
|
1312
|
-
* Multiple `TypoString`s are composed into a {@link TypoText} for multi-part messages.
|
|
1313
|
-
* Rendering is deferred until {@link TypoString.computeStyledString} is called with a
|
|
1314
|
-
* {@link TypoSupport} instance.
|
|
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}.
|
|
1315
1064
|
*/
|
|
1316
1065
|
declare class TypoString {
|
|
1317
1066
|
#private;
|
|
1318
1067
|
/**
|
|
1319
|
-
* @param value -
|
|
1320
|
-
* @param typoStyle -
|
|
1068
|
+
* @param value - Raw text content.
|
|
1069
|
+
* @param typoStyle - Style to apply when rendering. Defaults to `{}` (no style).
|
|
1321
1070
|
*/
|
|
1322
1071
|
constructor(value: string, typoStyle?: TypoStyle);
|
|
1323
|
-
/**
|
|
1072
|
+
/**
|
|
1073
|
+
* Returns the unstyled raw text content.
|
|
1074
|
+
*/
|
|
1324
1075
|
getRawString(): string;
|
|
1325
1076
|
/**
|
|
1326
|
-
* Returns the text
|
|
1077
|
+
* Returns the text styled by `typoSupport`.
|
|
1327
1078
|
*
|
|
1328
|
-
* @param typoSupport -
|
|
1079
|
+
* @param typoSupport - Rendering mode.
|
|
1329
1080
|
*/
|
|
1330
1081
|
computeStyledString(typoSupport: TypoSupport): string;
|
|
1331
1082
|
}
|
|
1332
1083
|
/**
|
|
1333
|
-
* A mutable sequence of {@link TypoString} segments
|
|
1334
|
-
* multi-part message.
|
|
1335
|
-
*
|
|
1336
|
-
* `TypoText` is used throughout the library to build error messages and usage output
|
|
1337
|
-
* that carry styling information without being coupled to a specific output mode.
|
|
1084
|
+
* A mutable sequence of {@link TypoString} segments forming a styled multi-part message.
|
|
1338
1085
|
* Rendering is deferred to {@link TypoText.computeStyledString}.
|
|
1339
1086
|
*/
|
|
1340
1087
|
declare class TypoText {
|
|
1341
1088
|
#private;
|
|
1342
1089
|
/**
|
|
1343
|
-
*
|
|
1344
|
-
* `TypoText` (flattened by value), a `TypoString`, or a plain `string` (wrapped in an
|
|
1345
|
-
* unstyled `TypoString`).
|
|
1346
|
-
*
|
|
1347
|
-
* @param typoParts - Initial parts to append. Can be any mix of `TypoText`,
|
|
1348
|
-
* `TypoString`, and `string`.
|
|
1090
|
+
* @param typoParts - Initial segments; `TypoText` is flattened, `string` is wrapped unstyled.
|
|
1349
1091
|
*/
|
|
1350
1092
|
constructor(...typoParts: Array<TypoText | TypoString | string>);
|
|
1351
1093
|
/**
|
|
1352
|
-
* Appends a
|
|
1094
|
+
* Appends a {@link TypoString} segment.
|
|
1353
1095
|
*
|
|
1354
|
-
* @param typoString -
|
|
1096
|
+
* @param typoString - Segment to append.
|
|
1355
1097
|
*/
|
|
1356
|
-
pushString(typoString: TypoString): void;
|
|
1098
|
+
pushString(typoString: TypoString | string): void;
|
|
1357
1099
|
/**
|
|
1358
|
-
* Appends all segments from another {@link TypoText}
|
|
1359
|
-
* (shallow copy of segments).
|
|
1100
|
+
* Appends all segments from another {@link TypoText} (shallow copy).
|
|
1360
1101
|
*
|
|
1361
|
-
* @param typoText -
|
|
1102
|
+
* @param typoText - Source text.
|
|
1362
1103
|
*/
|
|
1363
|
-
pushText(typoText: TypoText): void;
|
|
1104
|
+
pushText(typoText: TypoText | string): void;
|
|
1364
1105
|
/**
|
|
1365
|
-
* Renders all segments into a single string
|
|
1106
|
+
* Renders all segments into a single styled string.
|
|
1366
1107
|
*
|
|
1367
|
-
* @param typoSupport -
|
|
1368
|
-
* @returns
|
|
1108
|
+
* @param typoSupport - Rendering mode.
|
|
1109
|
+
* @returns Concatenated styled string.
|
|
1369
1110
|
*/
|
|
1370
1111
|
computeStyledString(typoSupport: TypoSupport): string;
|
|
1371
1112
|
/**
|
|
1372
1113
|
* Returns the concatenation of all segments' raw (unstyled) text.
|
|
1373
|
-
* Equivalent to calling {@link TypoText.computeStyledString} with
|
|
1374
|
-
* {@link TypoSupport.none}.
|
|
1375
1114
|
*/
|
|
1376
1115
|
computeRawString(): string;
|
|
1377
1116
|
/**
|
|
1378
|
-
* Returns the total character
|
|
1379
|
-
* Used by {@link TypoGrid} to compute column widths for alignment.
|
|
1117
|
+
* Returns the total character count of the raw (unstyled) text.
|
|
1380
1118
|
*/
|
|
1381
1119
|
computeRawLength(): number;
|
|
1382
1120
|
}
|
|
1383
1121
|
/**
|
|
1384
|
-
* A grid of {@link TypoText} cells
|
|
1385
|
-
*
|
|
1386
|
-
*
|
|
1387
|
-
* called, each column is padded to the width of its widest cell (measured in raw
|
|
1388
|
-
* characters). The last column in each row is **not** padded.
|
|
1389
|
-
*
|
|
1390
|
-
* Used internally by {@link usageToStyledLines} to render the `Positionals:`,
|
|
1391
|
-
* `Subcommands:`, and `Options:` sections with neat alignment.
|
|
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:`.
|
|
1392
1125
|
*/
|
|
1393
1126
|
declare class TypoGrid {
|
|
1394
1127
|
#private;
|
|
1395
1128
|
constructor();
|
|
1396
1129
|
/**
|
|
1397
|
-
* Appends a row
|
|
1130
|
+
* Appends a row. All rows should have the same cell count for alignment to be meaningful.
|
|
1398
1131
|
*
|
|
1399
|
-
* @param cells -
|
|
1400
|
-
* should have the same number of cells for alignment to be meaningful.
|
|
1132
|
+
* @param cells - Ordered {@link TypoText} cells.
|
|
1401
1133
|
*/
|
|
1402
1134
|
pushRow(cells: Array<TypoText>): void;
|
|
1403
1135
|
/**
|
|
1404
|
-
* Renders the grid
|
|
1405
|
-
*
|
|
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.
|
|
1406
1138
|
*
|
|
1407
|
-
* @param typoSupport -
|
|
1408
|
-
* @returns
|
|
1409
|
-
* one row. Join the inner arrays with `""` to get a single line string.
|
|
1139
|
+
* @param typoSupport - Rendering mode.
|
|
1140
|
+
* @returns 2-D array of styled strings.
|
|
1410
1141
|
*/
|
|
1411
1142
|
computeStyledGrid(typoSupport: TypoSupport): Array<Array<string>>;
|
|
1412
1143
|
}
|
|
1413
1144
|
/**
|
|
1414
|
-
*
|
|
1415
|
-
* the
|
|
1416
|
-
*
|
|
1417
|
-
* `TypoError` is used throughout `cli-kiss` to report parsing failures (unknown option,
|
|
1418
|
-
* type decoding error, missing required argument, etc.). Its styled representation is
|
|
1419
|
-
* rendered by {@link TypoSupport.computeStyledErrorMessage} when outputting errors to
|
|
1420
|
-
* the terminal.
|
|
1421
|
-
*
|
|
1422
|
-
* Errors can be chained: if `source` is a `TypoError`, its styled text is appended
|
|
1423
|
-
* after `": "` to form the full message context chain.
|
|
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 `": "`.
|
|
1424
1148
|
*/
|
|
1425
1149
|
declare class TypoError extends Error {
|
|
1426
1150
|
#private;
|
|
1427
1151
|
/**
|
|
1428
|
-
* @param currentTypoText -
|
|
1429
|
-
* @param source -
|
|
1430
|
-
* appended (chained context). If it is a plain `Error`, its `.message` is appended
|
|
1431
|
-
* as a plain string. Any other value is stringified with `String()`.
|
|
1152
|
+
* @param currentTypoText - Styled message for this error.
|
|
1153
|
+
* @param source - Optional cause; `TypoError` chains styled text, `Error` appends `.message`, else `String()`.
|
|
1432
1154
|
*/
|
|
1433
1155
|
constructor(currentTypoText: TypoText, source?: unknown);
|
|
1434
1156
|
/**
|
|
1435
|
-
* Renders
|
|
1157
|
+
* Renders the styled error message (without a `"Error:"` prefix).
|
|
1436
1158
|
*
|
|
1437
|
-
* @param typoSupport -
|
|
1438
|
-
* @returns
|
|
1159
|
+
* @param typoSupport - Rendering mode.
|
|
1160
|
+
* @returns Styled error string.
|
|
1439
1161
|
*/
|
|
1440
1162
|
computeStyledString(typoSupport: TypoSupport): string;
|
|
1441
1163
|
/**
|
|
1442
|
-
*
|
|
1443
|
-
*
|
|
1444
|
-
* error chained as the source.
|
|
1445
|
-
*
|
|
1446
|
-
* This is a convenience helper for adding contextual information to errors that arise
|
|
1447
|
-
* deep in a call chain (e.g. "at 0: Number: Unable to parse: ...").
|
|
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: ..."`).
|
|
1448
1166
|
*
|
|
1449
|
-
* @typeParam Value -
|
|
1450
|
-
* @param thrower -
|
|
1451
|
-
*
|
|
1452
|
-
* @
|
|
1453
|
-
*
|
|
1454
|
-
* @returns The value returned by `thrower`.
|
|
1455
|
-
* @throws `TypoError` wrapping the original error with the provided context prepended.
|
|
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.
|
|
1456
1172
|
*/
|
|
1457
1173
|
static tryWithContext<Value>(thrower: () => Value, context: () => TypoText): Value;
|
|
1458
1174
|
}
|
|
1459
1175
|
/**
|
|
1460
|
-
* Controls
|
|
1461
|
-
* {@link
|
|
1462
|
-
*
|
|
1463
|
-
* Instances are created via the static factory methods:
|
|
1464
|
-
* - {@link TypoSupport.none} — strips all styling (plain text).
|
|
1465
|
-
* - {@link TypoSupport.tty} — applies ANSI escape codes for color terminals.
|
|
1466
|
-
* - {@link TypoSupport.mock} — applies a deterministic textual representation useful
|
|
1467
|
-
* for snapshot tests.
|
|
1468
|
-
* - {@link TypoSupport.inferFromProcess} — auto-detects based on `process.stdout.isTTY`
|
|
1469
|
-
* and the `FORCE_COLOR` / `NO_COLOR` environment variables.
|
|
1470
|
-
*
|
|
1471
|
-
* `TypoSupport` is consumed by {@link runAndExit} (via the `useTtyColors` option)
|
|
1472
|
-
* and can also be used directly when building custom usage renderers with {@link usageToStyledLines}.
|
|
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}.
|
|
1473
1179
|
*/
|
|
1474
1180
|
declare class TypoSupport {
|
|
1475
1181
|
#private;
|
|
1476
1182
|
private constructor();
|
|
1477
1183
|
/**
|
|
1478
|
-
* Returns a `TypoSupport` that strips all styling
|
|
1479
|
-
* as-is (plain text, no ANSI codes).
|
|
1184
|
+
* Returns a `TypoSupport` that strips all styling (plain text, no ANSI codes).
|
|
1480
1185
|
*/
|
|
1481
1186
|
static none(): TypoSupport;
|
|
1482
1187
|
/**
|
|
1483
|
-
* Returns a `TypoSupport` that applies ANSI escape codes.
|
|
1484
|
-
* Use this when writing to a color-capable terminal (`stdout.isTTY === true`).
|
|
1188
|
+
* Returns a `TypoSupport` that applies ANSI escape codes (for color-capable terminals).
|
|
1485
1189
|
*/
|
|
1486
1190
|
static tty(): TypoSupport;
|
|
1487
1191
|
/**
|
|
1488
|
-
* Returns a `TypoSupport`
|
|
1489
|
-
*
|
|
1490
|
-
*
|
|
1491
|
-
* `{text}@color`, `{text}+` (bold), `{text}-` (dim), `{text}*` (italic),
|
|
1492
|
-
* `{text}_` (underline), `{text}~` (strikethrough). Useful for snapshot testing.
|
|
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).
|
|
1493
1195
|
*/
|
|
1494
1196
|
static mock(): TypoSupport;
|
|
1495
1197
|
/**
|
|
1496
|
-
*
|
|
1497
|
-
*
|
|
1498
|
-
*
|
|
1499
|
-
* 2. `FORCE_COLOR` env var set (any truthy value) → {@link TypoSupport.tty}.
|
|
1500
|
-
* 3. `process.stdout.isTTY === true` → {@link TypoSupport.tty}.
|
|
1501
|
-
* 4. Otherwise → {@link TypoSupport.none}.
|
|
1502
|
-
*
|
|
1503
|
-
* Falls back to {@link TypoSupport.none} if `process` is not available (e.g. in a
|
|
1504
|
-
* non-Node environment).
|
|
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.
|
|
1505
1201
|
*/
|
|
1506
1202
|
static inferFromProcess(): TypoSupport;
|
|
1507
1203
|
/**
|
|
1508
|
-
* Applies
|
|
1204
|
+
* Applies `typoStyle` to `value` according to the current mode.
|
|
1509
1205
|
*
|
|
1510
|
-
*
|
|
1511
|
-
*
|
|
1512
|
-
*
|
|
1513
|
-
*
|
|
1514
|
-
* @param value - The raw text to style.
|
|
1515
|
-
* @param typoStyle - The style to apply.
|
|
1516
|
-
* @returns The styled string.
|
|
1206
|
+
* @param value - Raw text.
|
|
1207
|
+
* @param typoStyle - Style to apply.
|
|
1208
|
+
* @returns Styled string.
|
|
1517
1209
|
*/
|
|
1518
1210
|
computeStyledString(value: string, typoStyle: TypoStyle): string;
|
|
1519
1211
|
/**
|
|
1520
|
-
* Formats
|
|
1521
|
-
*
|
|
1522
|
-
* - If `error` is a {@link TypoError}, its styled text is used for the message part.
|
|
1523
|
-
* - If `error` is a plain `Error`, its `.message` property is used.
|
|
1524
|
-
* - Otherwise `String(error)` is used.
|
|
1212
|
+
* Formats any thrown value as `"Error: <message>"` with {@link typoStyleFailure} on the prefix.
|
|
1525
1213
|
*
|
|
1526
|
-
*
|
|
1527
|
-
*
|
|
1528
|
-
* @param error - The error to format (any value thrown by a handler).
|
|
1529
|
-
* @returns A styled error string ready to print to stderr.
|
|
1214
|
+
* @param error - Any thrown value.
|
|
1215
|
+
* @returns Styled error string.
|
|
1530
1216
|
*/
|
|
1531
1217
|
computeStyledErrorMessage(error: unknown): string;
|
|
1532
1218
|
}
|
|
@@ -1537,7 +1223,7 @@ declare class TypoSupport {
|
|
|
1537
1223
|
*
|
|
1538
1224
|
* The output format is:
|
|
1539
1225
|
* ```
|
|
1540
|
-
* Usage: <cliName> [
|
|
1226
|
+
* Usage: <cliName> [segments...]
|
|
1541
1227
|
*
|
|
1542
1228
|
* <description> (<hint>)
|
|
1543
1229
|
* <detail lines...>
|
|
@@ -1551,23 +1237,26 @@ declare class TypoSupport {
|
|
|
1551
1237
|
* Options:
|
|
1552
1238
|
* -s, --long <LABEL> <description> (<hint>)
|
|
1553
1239
|
*
|
|
1240
|
+
* Examples:
|
|
1241
|
+
* <description>
|
|
1242
|
+
* <command line>
|
|
1243
|
+
*
|
|
1554
1244
|
* ```
|
|
1555
1245
|
* Sections that have no entries are omitted. The trailing empty line is always included.
|
|
1556
1246
|
*
|
|
1557
1247
|
* Column alignment within each section is handled by {@link TypoGrid}: the widest entry
|
|
1558
1248
|
* in each column sets the width for the entire section.
|
|
1559
1249
|
*
|
|
1560
|
-
* @param params.cliName -
|
|
1561
|
-
* @param params.commandUsage -
|
|
1562
|
-
* @param params.typoSupport -
|
|
1563
|
-
* @returns
|
|
1564
|
-
* empty string for the blank line at the end).
|
|
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.
|
|
1565
1254
|
*
|
|
1566
1255
|
* @example
|
|
1567
1256
|
* ```ts
|
|
1568
1257
|
* const lines = usageToStyledLines({
|
|
1569
1258
|
* cliName: "my-cli",
|
|
1570
|
-
* commandUsage:
|
|
1259
|
+
* commandUsage: commandDecoder.generateUsage(),
|
|
1571
1260
|
* typoSupport: TypoSupport.tty(),
|
|
1572
1261
|
* });
|
|
1573
1262
|
* process.stdout.write(lines.join("\n"));
|
|
@@ -1579,4 +1268,4 @@ declare function usageToStyledLines(params: {
|
|
|
1579
1268
|
typoSupport: TypoSupport;
|
|
1580
1269
|
}): string[];
|
|
1581
1270
|
|
|
1582
|
-
export { type Command, type
|
|
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 };
|