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.
@@ -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
- * Describes a single positional argument a bare (non-option) token on the command
13
- * line together with its parsing and usage-generation logic.
13
+ * A positional argument. Created with {@link positionalRequired}, {@link positionalOptional},
14
+ * or {@link positionalVariadics}.
14
15
  *
15
- * Positionals are created with {@link positionalRequired}, {@link positionalOptional}, or
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
- * Consumes the positional from `readerPositionals` and then returns a parser that produces the final decoded value.
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
- createParser(readerPositionals: ReaderPositionals): PositionalParser<Value>;
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
- * Returns the fully decoded and validated value for the positional
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
- parseValue(): Value;
27
+ consumeAndMakeDecoder(
28
+ readerPositionals: ReaderPositionals,
29
+ ): PositionalDecoder<Value>;
48
30
  };
49
31
 
50
32
  /**
51
- * Human-readable metadata for a single positional argument, used to render the
52
- * `Positionals:` section of the help output produced by {@link usageToStyledLines}.
33
+ * Produced by {@link Positional.consumeAndMakeDecoder}.
34
+ *
35
+ * @typeParam Value - Decoded value type.
53
36
  */
54
- export type PositionalUsage = {
55
- /** Short description of what the positional represents. */
56
- description: string | undefined;
37
+ export type PositionalDecoder<Value> = {
57
38
  /**
58
- * Optional supplementary note shown in parentheses next to the description.
59
- * Suitable for short caveats such as `"defaults to 'world'"`.
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
- label: Uppercase<string>;
43
+ decodeValue(): Value;
69
44
  };
70
45
 
71
46
  /**
72
- * Creates a required positional argument one that must be present on the command line.
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 - The TypeScript type produced by the type decoder.
50
+ * @typeParam Value - Type produced by the decoder.
82
51
  *
83
- * @param definition - Configuration for the positional.
84
- * @param definition.description - Human-readable description for the help output.
85
- * @param definition.hint - Optional supplementary note shown in parentheses.
86
- * @param definition.label - Custom label shown in the usage line (without angle brackets).
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
- createParser(readerPositionals: ReaderPositionals) {
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
- parseValue() {
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 argument one that may or may not appear on the
137
- * command line.
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
- * The parser consumes the next available positional token. If no token is available,
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
- * The label displayed in the usage line defaults to the uppercased `type.content`
144
- * wrapped in square brackets (e.g. `[STRING]`). Supply `label` to override.
145
- *
146
- * @typeParam Value - The TypeScript type produced by the type decoder (or the default).
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
- createParser(readerPositionals: ReaderPositionals) {
143
+ consumeAndMakeDecoder(readerPositionals: ReaderPositionals) {
187
144
  const positional = readerPositionals.consumePositional();
188
145
  return {
189
- parseValue() {
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 argument — one that collects zero or more remaining
212
- * positional tokens into an array.
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 - The TypeScript type produced by the type decoder for each token.
171
+ * @typeParam Value - Type produced by the decoder for each token.
226
172
  *
227
- * @param definition - Configuration for the variadic positional.
228
- * @param definition.endDelimiter - Optional sentinel string that signals the end of
229
- * the variadic sequence (e.g. `"--"`). When encountered it is consumed but not
230
- * included in the result array.
231
- * @param definition.description - Human-readable description for the help output.
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
- createParser(readerPositionals: ReaderPositionals) {
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
- parseValue() {
282
- return positionals.map((positional) => {
283
- return decodeValue(label, definition.type, positional);
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
- value: string,
236
+ input: string,
295
237
  ): Value {
296
238
  return TypoError.tryWithContext(
297
- () => type.decoder(value),
239
+ () => type.decoder(input),
298
240
  () =>
299
241
  new TypoText(
300
242
  new TypoString(label, typoStyleUserInput),