cli-kiss 0.2.2 → 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 +711 -1046
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/docs/.vitepress/config.mts +1 -0
- package/docs/.vitepress/theme/index.ts +4 -0
- package/docs/.vitepress/theme/style.css +4 -0
- package/docs/guide/01_getting_started.md +4 -4
- package/docs/guide/02_commands.md +72 -41
- package/docs/guide/03_options.md +13 -14
- package/docs/guide/04_positionals.md +6 -8
- package/docs/guide/05_types.md +9 -11
- package/docs/guide/06_run.md +4 -5
- package/docs/index.md +8 -3
- package/docs/public/hero.png +0 -0
- package/package.json +1 -1
- package/src/lib/Command.ts +151 -275
- package/src/lib/Operation.ts +57 -95
- package/src/lib/Option.ts +194 -181
- package/src/lib/Positional.ts +54 -112
- package/src/lib/Reader.ts +155 -156
- package/src/lib/Run.ts +64 -69
- package/src/lib/Type.ts +89 -145
- package/src/lib/Typo.ts +131 -195
- package/src/lib/Usage.ts +203 -69
- 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 +146 -91
- package/tests/unit.command.usage.ts +235 -114
- package/tests/unit.runner.cycle.ts +50 -20
- package/tests/unit.runner.errors.ts +19 -3
package/src/lib/Positional.ts
CHANGED
|
@@ -7,85 +7,52 @@ import {
|
|
|
7
7
|
typoStyleUserInput,
|
|
8
8
|
TypoText,
|
|
9
9
|
} from "./Typo";
|
|
10
|
+
import { UsagePositional } from "./Usage";
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
13
|
+
* A positional argument. Created with {@link positionalRequired}, {@link positionalOptional},
|
|
14
|
+
* or {@link positionalVariadics}.
|
|
14
15
|
*
|
|
15
|
-
*
|
|
16
|
-
* {@link positionalVariadics} and are passed via the `positionals` array of
|
|
17
|
-
* {@link operation}, where they are consumed in declaration order.
|
|
18
|
-
*
|
|
19
|
-
* @typeParam Value - The TypeScript type of the parsed positional value.
|
|
16
|
+
* @typeParam Value - Decoded value type.
|
|
20
17
|
*/
|
|
21
18
|
export type Positional<Value> = {
|
|
22
|
-
/** Returns human-readable metadata used to render the `Positionals:` section of help. */
|
|
23
|
-
generateUsage(): PositionalUsage;
|
|
24
19
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* The parser is created during {@link Operation.createFactory} and may throw a
|
|
28
|
-
* {@link TypoError} if the positional is missing (when required) or if decoding fails.
|
|
29
|
-
* @param readerPositionals - The source of positional arguments to be consumed.
|
|
20
|
+
* Returns metadata for the `Positionals:` section.
|
|
30
21
|
*/
|
|
31
|
-
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Retrieves the parsed value for a positional argument after parsing is complete.
|
|
36
|
-
*
|
|
37
|
-
* Returned by {@link Positional.createParser} and called by {@link OperationFactory.createInstance}.
|
|
38
|
-
*
|
|
39
|
-
* @typeParam Value - The TypeScript type of the parsed value.
|
|
40
|
-
*/
|
|
41
|
-
export type PositionalParser<Value> = {
|
|
22
|
+
generateUsage(): UsagePositional;
|
|
42
23
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* @throws {@link TypoError} if the positional was missing (when required) or if decoding failed.
|
|
24
|
+
* Consumes the next positional token from `readerPositionals`.
|
|
25
|
+
* Returns a decoder that produces the final value.
|
|
46
26
|
*/
|
|
47
|
-
|
|
27
|
+
consumeAndMakeDecoder(
|
|
28
|
+
readerPositionals: ReaderPositionals,
|
|
29
|
+
): PositionalDecoder<Value>;
|
|
48
30
|
};
|
|
49
31
|
|
|
50
32
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
33
|
+
* Produced by {@link Positional.consumeAndMakeDecoder}.
|
|
34
|
+
*
|
|
35
|
+
* @typeParam Value - Decoded value type.
|
|
53
36
|
*/
|
|
54
|
-
export type
|
|
55
|
-
/** Short description of what the positional represents. */
|
|
56
|
-
description: string | undefined;
|
|
37
|
+
export type PositionalDecoder<Value> = {
|
|
57
38
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
|
|
61
|
-
hint: string | undefined;
|
|
62
|
-
/**
|
|
63
|
-
* The placeholder label shown in the usage line and the `Positionals:` section.
|
|
64
|
-
* Required positionals use angle-bracket notation (e.g. `"<NAME>"`); optional ones
|
|
65
|
-
* use square-bracket notation (e.g. `"[FILE]"`); variadic ones append `...`
|
|
66
|
-
* (e.g. `"[ITEM]..."`).
|
|
39
|
+
* Returns the decoded positional value.
|
|
40
|
+
*
|
|
41
|
+
* @throws {@link TypoError} if decoding failed.
|
|
67
42
|
*/
|
|
68
|
-
|
|
43
|
+
decodeValue(): Value;
|
|
69
44
|
};
|
|
70
45
|
|
|
71
46
|
/**
|
|
72
|
-
* Creates a required positional
|
|
73
|
-
*
|
|
74
|
-
* The parser consumes the next available positional token and decodes it with
|
|
75
|
-
* `definition.type`. If no token is available, a {@link TypoError} is thrown immediately
|
|
76
|
-
* during parsing (i.e. inside {@link Operation.createFactory}).
|
|
77
|
-
*
|
|
78
|
-
* The label displayed in the usage line defaults to the uppercased `type.content`
|
|
79
|
-
* wrapped in angle brackets (e.g. `<STRING>`). Supply `label` to override.
|
|
47
|
+
* Creates a required positional — missing token throws {@link TypoError}.
|
|
48
|
+
* Label defaults to uppercased `type.content` in angle brackets (e.g. `<STRING>`).
|
|
80
49
|
*
|
|
81
|
-
* @typeParam Value -
|
|
50
|
+
* @typeParam Value - Type produced by the decoder.
|
|
82
51
|
*
|
|
83
|
-
* @param definition -
|
|
84
|
-
* @param definition.
|
|
85
|
-
* @param definition.
|
|
86
|
-
* @param definition.
|
|
87
|
-
* Defaults to the uppercased `type.content`.
|
|
88
|
-
* @param definition.type - The {@link Type} used to decode the raw string token.
|
|
52
|
+
* @param definition.description - Help text.
|
|
53
|
+
* @param definition.hint - Short note shown in parentheses.
|
|
54
|
+
* @param definition.label - Label without brackets; defaults to uppercased `type.content`.
|
|
55
|
+
* @param definition.type - Decoder for the raw token.
|
|
89
56
|
* @returns A {@link Positional}`<Value>`.
|
|
90
57
|
*
|
|
91
58
|
* @example
|
|
@@ -113,7 +80,7 @@ export function positionalRequired<Value>(definition: {
|
|
|
113
80
|
label: label as Uppercase<string>,
|
|
114
81
|
};
|
|
115
82
|
},
|
|
116
|
-
|
|
83
|
+
consumeAndMakeDecoder(readerPositionals: ReaderPositionals) {
|
|
117
84
|
const positional = readerPositionals.consumePositional();
|
|
118
85
|
if (positional === undefined) {
|
|
119
86
|
throw new TypoError(
|
|
@@ -124,7 +91,7 @@ export function positionalRequired<Value>(definition: {
|
|
|
124
91
|
);
|
|
125
92
|
}
|
|
126
93
|
return {
|
|
127
|
-
|
|
94
|
+
decodeValue() {
|
|
128
95
|
return decodeValue(label, definition.type, positional);
|
|
129
96
|
},
|
|
130
97
|
};
|
|
@@ -133,26 +100,16 @@ export function positionalRequired<Value>(definition: {
|
|
|
133
100
|
}
|
|
134
101
|
|
|
135
102
|
/**
|
|
136
|
-
* Creates an optional positional
|
|
137
|
-
*
|
|
103
|
+
* Creates an optional positional — absent token falls back to `default()`.
|
|
104
|
+
* Label defaults to uppercased `type.content` in square brackets (e.g. `[STRING]`).
|
|
138
105
|
*
|
|
139
|
-
*
|
|
140
|
-
* `definition.default()` is called to supply the fallback value. If the default factory
|
|
141
|
-
* throws, a {@link TypoError} is produced.
|
|
106
|
+
* @typeParam Value - Type produced by the decoder (or the default).
|
|
142
107
|
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
* @
|
|
147
|
-
*
|
|
148
|
-
* @param definition - Configuration for the positional.
|
|
149
|
-
* @param definition.description - Human-readable description for the help output.
|
|
150
|
-
* @param definition.hint - Optional supplementary note shown in parentheses.
|
|
151
|
-
* @param definition.label - Custom label shown in the usage line (without square brackets).
|
|
152
|
-
* Defaults to the uppercased `type.content`.
|
|
153
|
-
* @param definition.type - The {@link Type} used to decode the raw string token.
|
|
154
|
-
* @param definition.default - Factory called when the positional is absent to supply the
|
|
155
|
-
* default value. Throw from this factory to make omission an error.
|
|
108
|
+
* @param definition.description - Help text.
|
|
109
|
+
* @param definition.hint - Short note shown in parentheses.
|
|
110
|
+
* @param definition.label - Label without brackets; defaults to uppercased `type.content`.
|
|
111
|
+
* @param definition.type - Decoder for the raw token.
|
|
112
|
+
* @param definition.default - Value when absent. Throw to make it required.
|
|
156
113
|
* @returns A {@link Positional}`<Value>`.
|
|
157
114
|
*
|
|
158
115
|
* @example
|
|
@@ -183,10 +140,10 @@ export function positionalOptional<Value>(definition: {
|
|
|
183
140
|
label: label as Uppercase<string>,
|
|
184
141
|
};
|
|
185
142
|
},
|
|
186
|
-
|
|
143
|
+
consumeAndMakeDecoder(readerPositionals: ReaderPositionals) {
|
|
187
144
|
const positional = readerPositionals.consumePositional();
|
|
188
145
|
return {
|
|
189
|
-
|
|
146
|
+
decodeValue() {
|
|
190
147
|
if (positional === undefined) {
|
|
191
148
|
try {
|
|
192
149
|
return definition.default();
|
|
@@ -208,31 +165,16 @@ export function positionalOptional<Value>(definition: {
|
|
|
208
165
|
}
|
|
209
166
|
|
|
210
167
|
/**
|
|
211
|
-
* Creates a variadic positional
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
* The parser greedily consumes tokens until either there are no more tokens or it
|
|
215
|
-
* encounters the optional `endDelimiter` sentinel string, which is consumed but not
|
|
216
|
-
* included in the result. Each token is decoded independently with `definition.type`.
|
|
217
|
-
*
|
|
218
|
-
* If absent entirely, the result is an empty array `[]`.
|
|
219
|
-
*
|
|
220
|
-
* The label displayed in the usage line defaults to the uppercased `type.content`
|
|
221
|
-
* wrapped in square brackets followed by `...` (e.g. `[STRING]...`). When an
|
|
222
|
-
* `endDelimiter` is configured, the delimiter is also shown (e.g. `[STRING]...["--"]`).
|
|
223
|
-
* Supply `label` to override the base label.
|
|
168
|
+
* Creates a variadic positional that collects zero or more remaining tokens into an array.
|
|
169
|
+
* Stops at `endDelimiter` (consumed, not included). Label: `[TYPE]...` notation.
|
|
224
170
|
*
|
|
225
|
-
* @typeParam Value -
|
|
171
|
+
* @typeParam Value - Type produced by the decoder for each token.
|
|
226
172
|
*
|
|
227
|
-
* @param definition -
|
|
228
|
-
* @param definition.
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
* @param definition.
|
|
232
|
-
* @param definition.hint - Optional supplementary note shown in parentheses.
|
|
233
|
-
* @param definition.label - Custom label shown in the usage line (without brackets).
|
|
234
|
-
* Defaults to the uppercased `type.content`.
|
|
235
|
-
* @param definition.type - The {@link Type} used to decode each raw string token.
|
|
173
|
+
* @param definition.endDelimiter - Sentinel token that stops collection (consumed, not included).
|
|
174
|
+
* @param definition.description - Help text.
|
|
175
|
+
* @param definition.hint - Short note shown in parentheses.
|
|
176
|
+
* @param definition.label - Label without brackets; defaults to uppercased `type.content`.
|
|
177
|
+
* @param definition.type - Decoder applied to each token.
|
|
236
178
|
* @returns A {@link Positional}`<Array<Value>>`.
|
|
237
179
|
*
|
|
238
180
|
* @example
|
|
@@ -261,11 +203,11 @@ export function positionalVariadics<Value>(definition: {
|
|
|
261
203
|
hint: definition.hint,
|
|
262
204
|
label: (`${label}...` +
|
|
263
205
|
(definition.endDelimiter
|
|
264
|
-
? `["${definition.endDelimiter}"]`
|
|
206
|
+
? ` ["${definition.endDelimiter}"]`
|
|
265
207
|
: "")) as Uppercase<string>,
|
|
266
208
|
};
|
|
267
209
|
},
|
|
268
|
-
|
|
210
|
+
consumeAndMakeDecoder(readerPositionals: ReaderPositionals) {
|
|
269
211
|
const positionals = new Array<string>();
|
|
270
212
|
while (true) {
|
|
271
213
|
const positional = readerPositionals.consumePositional();
|
|
@@ -278,10 +220,10 @@ export function positionalVariadics<Value>(definition: {
|
|
|
278
220
|
positionals.push(positional);
|
|
279
221
|
}
|
|
280
222
|
return {
|
|
281
|
-
|
|
282
|
-
return positionals.map((positional) =>
|
|
283
|
-
|
|
284
|
-
|
|
223
|
+
decodeValue() {
|
|
224
|
+
return positionals.map((positional) =>
|
|
225
|
+
decodeValue(label, definition.type, positional),
|
|
226
|
+
);
|
|
285
227
|
},
|
|
286
228
|
};
|
|
287
229
|
},
|
|
@@ -291,10 +233,10 @@ export function positionalVariadics<Value>(definition: {
|
|
|
291
233
|
function decodeValue<Value>(
|
|
292
234
|
label: string,
|
|
293
235
|
type: Type<Value>,
|
|
294
|
-
|
|
236
|
+
input: string,
|
|
295
237
|
): Value {
|
|
296
238
|
return TypoError.tryWithContext(
|
|
297
|
-
() => type.decoder(
|
|
239
|
+
() => type.decoder(input),
|
|
298
240
|
() =>
|
|
299
241
|
new TypoText(
|
|
300
242
|
new TypoString(label, typoStyleUserInput),
|