@optique/core 0.1.0-dev.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +475 -0
- package/dist/doc.cjs +104 -0
- package/dist/doc.d.cts +129 -0
- package/dist/doc.d.ts +129 -0
- package/dist/doc.js +104 -0
- package/dist/facade.cjs +98 -0
- package/dist/facade.d.cts +113 -0
- package/dist/facade.d.ts +113 -0
- package/dist/facade.js +97 -0
- package/dist/index.cjs +42 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/message.cjs +202 -0
- package/dist/message.d.cts +199 -0
- package/dist/message.d.ts +199 -0
- package/dist/message.js +194 -0
- package/dist/parser.cjs +1065 -0
- package/dist/parser.d.cts +580 -0
- package/dist/parser.d.ts +580 -0
- package/dist/parser.js +1053 -0
- package/dist/usage.cjs +242 -0
- package/dist/usage.d.cts +217 -0
- package/dist/usage.d.ts +217 -0
- package/dist/usage.js +239 -0
- package/dist/valueparser.cjs +332 -0
- package/dist/valueparser.d.cts +332 -0
- package/dist/valueparser.d.ts +332 -0
- package/dist/valueparser.js +325 -0
- package/package.json +117 -0
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
import { Message } from "./message.cjs";
|
|
2
|
+
import { OptionName, Usage } from "./usage.cjs";
|
|
3
|
+
import { DocFragments, DocPage } from "./doc.cjs";
|
|
4
|
+
import { ValueParser, ValueParserResult } from "./valueparser.cjs";
|
|
5
|
+
|
|
6
|
+
//#region src/parser.d.ts
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Parser interface for command-line argument parsing.
|
|
10
|
+
* @template TValue The type of the value returned by the parser.
|
|
11
|
+
* @template TState The type of the state used during parsing.
|
|
12
|
+
*/
|
|
13
|
+
interface Parser<TValue, TState> {
|
|
14
|
+
/**
|
|
15
|
+
* A type tag for the result value of this parser, used for type inference.
|
|
16
|
+
* Usually this is an empty array at runtime, but it does not matter
|
|
17
|
+
* what it contains.
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
readonly $valueType: readonly TValue[];
|
|
21
|
+
/**
|
|
22
|
+
* A type tag for the state of this parser, used for type inference.
|
|
23
|
+
* Usually this is an empty array at runtime, but it does not matter
|
|
24
|
+
* what it contains.
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
readonly $stateType: readonly TState[];
|
|
28
|
+
/**
|
|
29
|
+
* The priority of this parser, which determines the order in which
|
|
30
|
+
* parsers are applied when multiple parsers are available. The greater
|
|
31
|
+
* the number, the higher the priority.
|
|
32
|
+
*/
|
|
33
|
+
readonly priority: number;
|
|
34
|
+
/**
|
|
35
|
+
* The usage information for this parser, which describes how
|
|
36
|
+
* to use it in command-line interfaces.
|
|
37
|
+
*/
|
|
38
|
+
readonly usage: Usage;
|
|
39
|
+
/**
|
|
40
|
+
* The initial state for this parser. This is used to initialize the
|
|
41
|
+
* state when parsing starts.
|
|
42
|
+
*/
|
|
43
|
+
readonly initialState: TState;
|
|
44
|
+
/**
|
|
45
|
+
* Parses the input context and returns a result indicating
|
|
46
|
+
* whether the parsing was successful or not.
|
|
47
|
+
* @param context The context of the parser, which includes the input buffer
|
|
48
|
+
* and the current state.
|
|
49
|
+
* @returns A result object indicating success or failure.
|
|
50
|
+
*/
|
|
51
|
+
parse(context: ParserContext<TState>): ParserResult<TState>;
|
|
52
|
+
/**
|
|
53
|
+
* Transforms a {@link TState} into a {@link TValue}, if applicable.
|
|
54
|
+
* If the transformation is not applicable, it should return
|
|
55
|
+
* a `ValueParserResult` with `success: false` and an appropriate error
|
|
56
|
+
* message.
|
|
57
|
+
* @param state The current state of the parser, which may contain accumulated
|
|
58
|
+
* data or context needed to produce the final value.
|
|
59
|
+
* @returns A result object indicating success or failure of
|
|
60
|
+
* the transformation. If successful, it should contain
|
|
61
|
+
* the parsed value of type {@link TValue}. If not applicable,
|
|
62
|
+
* it should return an error message.
|
|
63
|
+
*/
|
|
64
|
+
complete(state: TState): ValueParserResult<TValue>;
|
|
65
|
+
/**
|
|
66
|
+
* Generates a documentation fragment for this parser, which can be used
|
|
67
|
+
* to describe the parser's usage, description, and default value.
|
|
68
|
+
* @param state The current state of the parser, which may contain
|
|
69
|
+
* accumulated data or context needed to produce
|
|
70
|
+
* the documentation.
|
|
71
|
+
* @param defaultValue An optional default value that can be used
|
|
72
|
+
* to provide a default value in the documentation.
|
|
73
|
+
* @returns {@link DocFragments} object containing documentation
|
|
74
|
+
* fragments for this parser.
|
|
75
|
+
*/
|
|
76
|
+
getDocFragments(state: TState, defaultValue?: TValue): DocFragments;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The context of the parser, which includes the input buffer and the state.
|
|
80
|
+
* @template TState The type of the state used during parsing.
|
|
81
|
+
*/
|
|
82
|
+
interface ParserContext<TState> {
|
|
83
|
+
/**
|
|
84
|
+
* The array of input strings that the parser is currently processing.
|
|
85
|
+
*/
|
|
86
|
+
readonly buffer: readonly string[];
|
|
87
|
+
/**
|
|
88
|
+
* The current state of the parser, which is used to track
|
|
89
|
+
* the progress of parsing and any accumulated data.
|
|
90
|
+
*/
|
|
91
|
+
readonly state: TState;
|
|
92
|
+
/**
|
|
93
|
+
* A flag indicating whether no more options should be parsed and instead
|
|
94
|
+
* the remaining input should be treated as positional arguments.
|
|
95
|
+
* This is typically set when the parser encounters a `--` in the input,
|
|
96
|
+
* which is a common convention in command-line interfaces to indicate
|
|
97
|
+
* that no further options should be processed.
|
|
98
|
+
*/
|
|
99
|
+
readonly optionsTerminated: boolean;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* A discriminated union type representing the result of a parser operation.
|
|
103
|
+
* It can either indicate a successful parse with the next state and context,
|
|
104
|
+
* or a failure with an error message.
|
|
105
|
+
* @template TState The type of the state after parsing. It should match with
|
|
106
|
+
* the `TState` type of the {@link Parser} interface.
|
|
107
|
+
*/
|
|
108
|
+
type ParserResult<TState> = {
|
|
109
|
+
/**
|
|
110
|
+
* Indicates that the parsing operation was successful.
|
|
111
|
+
*/
|
|
112
|
+
readonly success: true;
|
|
113
|
+
/**
|
|
114
|
+
* The next context after parsing, which includes the updated input buffer.
|
|
115
|
+
*/
|
|
116
|
+
readonly next: ParserContext<TState>;
|
|
117
|
+
/**
|
|
118
|
+
* The input elements consumed by the parser during this operation.
|
|
119
|
+
*/
|
|
120
|
+
readonly consumed: readonly string[];
|
|
121
|
+
} | {
|
|
122
|
+
/**
|
|
123
|
+
* Indicates that the parsing operation failed.
|
|
124
|
+
*/
|
|
125
|
+
readonly success: false;
|
|
126
|
+
/**
|
|
127
|
+
* The number of the consumed input elements.
|
|
128
|
+
*/
|
|
129
|
+
readonly consumed: number;
|
|
130
|
+
/**
|
|
131
|
+
* The error message describing why the parsing failed.
|
|
132
|
+
*/
|
|
133
|
+
readonly error: Message;
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* Creates a parser that always succeeds without consuming any input and
|
|
137
|
+
* produces a constant value of the type {@link T}.
|
|
138
|
+
* @template T The type of the constant value produced by the parser.
|
|
139
|
+
*/
|
|
140
|
+
declare function constant<const T>(value: T): Parser<T, T>;
|
|
141
|
+
/**
|
|
142
|
+
* Options for the {@link option} parser.
|
|
143
|
+
*/
|
|
144
|
+
interface OptionOptions {
|
|
145
|
+
/**
|
|
146
|
+
* The description of the option, which can be used for help messages.
|
|
147
|
+
*/
|
|
148
|
+
readonly description?: Message;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Creates a parser for various styles of command-line options that take an
|
|
152
|
+
* argument value, such as `--option=value`, `-o value`, or `/option:value`.
|
|
153
|
+
* @template T The type of value this parser produces.
|
|
154
|
+
* @param args The {@link OptionName}s to parse, followed by
|
|
155
|
+
* a {@link ValueParser} that defines how to parse the value of
|
|
156
|
+
* the option. If no value parser is provided, the option is
|
|
157
|
+
* treated as a boolean flag.
|
|
158
|
+
* @returns A {@link Parser} that can parse the specified options and their
|
|
159
|
+
* values.
|
|
160
|
+
*/
|
|
161
|
+
declare function option<T>(...args: readonly [...readonly OptionName[], ValueParser<T>]): Parser<T, ValueParserResult<T> | undefined>;
|
|
162
|
+
/**
|
|
163
|
+
* Creates a parser for various styles of command-line options that take an
|
|
164
|
+
* argument value, such as `--option=value`, `-o value`, or `/option:value`.
|
|
165
|
+
* @template T The type of value this parser produces.
|
|
166
|
+
* @param args The {@link OptionName}s to parse, followed by
|
|
167
|
+
* a {@link ValueParser} that defines how to parse the value of
|
|
168
|
+
* the option, and an optional {@link OptionOptions} object
|
|
169
|
+
* that allows you to specify a description or other metadata.
|
|
170
|
+
* @returns A {@link Parser} that can parse the specified options and their
|
|
171
|
+
* values.
|
|
172
|
+
*/
|
|
173
|
+
declare function option<T>(...args: readonly [...readonly OptionName[], ValueParser<T>, OptionOptions]): Parser<T, ValueParserResult<T> | undefined>;
|
|
174
|
+
/**
|
|
175
|
+
* Creates a parser for various styles of command-line options that do not
|
|
176
|
+
* take an argument value, such as `--option`, `-o`, or `/option`.
|
|
177
|
+
* @param optionNames The {@link OptionName}s to parse.
|
|
178
|
+
* @return A {@link Parser} that can parse the specified options as Boolean
|
|
179
|
+
* flags, producing `true` if the option is present.
|
|
180
|
+
*/
|
|
181
|
+
declare function option(...optionNames: readonly OptionName[]): Parser<boolean, ValueParserResult<boolean> | undefined>;
|
|
182
|
+
/**
|
|
183
|
+
* Creates a parser for various styles of command-line options that take an
|
|
184
|
+
* argument value, such as `--option=value`, `-o value`, or `/option:value`.
|
|
185
|
+
* @param args The {@link OptionName}s to parse, followed by
|
|
186
|
+
* an optional {@link OptionOptions} object that allows you to
|
|
187
|
+
* specify a description or other metadata.
|
|
188
|
+
* @returns A {@link Parser} that can parse the specified options and their
|
|
189
|
+
* values.
|
|
190
|
+
*/
|
|
191
|
+
declare function option(...args: readonly [...readonly OptionName[], OptionOptions]): Parser<boolean, ValueParserResult<boolean> | undefined>;
|
|
192
|
+
/**
|
|
193
|
+
* Options for the {@link argument} parser.
|
|
194
|
+
*/
|
|
195
|
+
interface ArgumentOptions {
|
|
196
|
+
/**
|
|
197
|
+
* The description of the argument, which can be used for help messages.
|
|
198
|
+
*/
|
|
199
|
+
readonly description?: Message;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Creates a parser that expects a single argument value.
|
|
203
|
+
* This parser is typically used for positional arguments
|
|
204
|
+
* that are not options or flags.
|
|
205
|
+
* @template T The type of the value produced by the parser.
|
|
206
|
+
* @param valueParser The {@link ValueParser} that defines how to parse
|
|
207
|
+
* the argument value.
|
|
208
|
+
* @param options Optional configuration for the argument parser,
|
|
209
|
+
* allowing you to specify a description or other metadata.
|
|
210
|
+
* @returns A {@link Parser} that expects a single argument value and produces
|
|
211
|
+
* the parsed value of type {@link T}.
|
|
212
|
+
*/
|
|
213
|
+
declare function argument<T>(valueParser: ValueParser<T>, options?: ArgumentOptions): Parser<T, ValueParserResult<T> | undefined>;
|
|
214
|
+
/**
|
|
215
|
+
* Creates a parser that makes another parser optional, allowing it to succeed
|
|
216
|
+
* without consuming input if the wrapped parser fails to match.
|
|
217
|
+
* If the wrapped parser succeeds, this returns its value.
|
|
218
|
+
* If the wrapped parser fails, this returns `undefined` without consuming input.
|
|
219
|
+
* @template TValue The type of the value returned by the wrapped parser.
|
|
220
|
+
* @template TState The type of the state used by the wrapped parser.
|
|
221
|
+
* @param parser The {@link Parser} to make optional.
|
|
222
|
+
* @returns A {@link Parser} that produces either the result of the wrapped parser
|
|
223
|
+
* or `undefined` if the wrapped parser fails to match.
|
|
224
|
+
*/
|
|
225
|
+
declare function optional<TValue, TState>(parser: Parser<TValue, TState>): Parser<TValue | undefined, [TState] | undefined>;
|
|
226
|
+
/**
|
|
227
|
+
* Creates a parser that makes another parser use a default value when it fails
|
|
228
|
+
* to match or consume input. This is similar to {@link optional}, but instead
|
|
229
|
+
* of returning `undefined` when the wrapped parser doesn't match, it returns
|
|
230
|
+
* a specified default value.
|
|
231
|
+
* @template TValue The type of the value returned by the wrapped parser.
|
|
232
|
+
* @template TState The type of the state used by the wrapped parser.
|
|
233
|
+
* @param parser The {@link Parser} to wrap with default behavior.
|
|
234
|
+
* @param defaultValue The default value to return when the wrapped parser
|
|
235
|
+
* doesn't match or consume input. Can be a value of type
|
|
236
|
+
* {@link TValue} or a function that returns such a value.
|
|
237
|
+
* @returns A {@link Parser} that produces either the result of the wrapped parser
|
|
238
|
+
* or the default value if the wrapped parser fails to match.
|
|
239
|
+
*/
|
|
240
|
+
declare function withDefault<TValue, TState>(parser: Parser<TValue, TState>, defaultValue: TValue | (() => TValue)): Parser<TValue, [TState] | undefined>;
|
|
241
|
+
/**
|
|
242
|
+
* Options for the {@link multiple} parser.
|
|
243
|
+
*/
|
|
244
|
+
interface MultipleOptions {
|
|
245
|
+
/**
|
|
246
|
+
* The minimum number of occurrences required for the parser to succeed.
|
|
247
|
+
* If the number of occurrences is less than this value,
|
|
248
|
+
* the parser will fail with an error.
|
|
249
|
+
* @default `0`
|
|
250
|
+
*/
|
|
251
|
+
readonly min?: number;
|
|
252
|
+
/**
|
|
253
|
+
* The maximum number of occurrences allowed for the parser.
|
|
254
|
+
* If the number of occurrences exceeds this value,
|
|
255
|
+
* the parser will fail with an error.
|
|
256
|
+
* @default `Infinity`
|
|
257
|
+
*/
|
|
258
|
+
readonly max?: number;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Creates a parser that allows multiple occurrences of a given parser.
|
|
262
|
+
* This parser can be used to parse multiple values of the same type,
|
|
263
|
+
* such as multiple command-line arguments or options.
|
|
264
|
+
* @template TValue The type of the value that the parser produces.
|
|
265
|
+
* @template TState The type of the state used by the parser.
|
|
266
|
+
* @param parser The {@link Parser} to apply multiple times.
|
|
267
|
+
* @param options Optional configuration for the parser,
|
|
268
|
+
* allowing you to specify the minimum and maximum number of
|
|
269
|
+
* occurrences allowed.
|
|
270
|
+
* @returns A {@link Parser} that produces an array of values
|
|
271
|
+
* of type {@link TValue} and an array of states
|
|
272
|
+
* of type {@link TState}.
|
|
273
|
+
*/
|
|
274
|
+
declare function multiple<TValue, TState>(parser: Parser<TValue, TState>, options?: MultipleOptions): Parser<readonly TValue[], readonly TState[]>;
|
|
275
|
+
/**
|
|
276
|
+
* Creates a parser that combines multiple parsers into a single object parser.
|
|
277
|
+
* Each parser in the object is applied to parse different parts of the input,
|
|
278
|
+
* and the results are combined into an object with the same structure.
|
|
279
|
+
* @template T A record type where each value is a {@link Parser}.
|
|
280
|
+
* @param parsers An object containing named parsers that will be combined
|
|
281
|
+
* into a single object parser.
|
|
282
|
+
* @returns A {@link Parser} that produces an object with the same keys as
|
|
283
|
+
* the input, where each value is the result of the corresponding
|
|
284
|
+
* parser.
|
|
285
|
+
*/
|
|
286
|
+
declare function object<T extends {
|
|
287
|
+
readonly [key: string | symbol]: Parser<unknown, unknown>;
|
|
288
|
+
}>(parsers: T): Parser<{ readonly [K in keyof T]: T[K]["$valueType"][number] extends (infer U) ? U : never }, { readonly [K in keyof T]: T[K]["$stateType"][number] extends (infer U2) ? U2 : never }>;
|
|
289
|
+
/**
|
|
290
|
+
* Creates a labeled parser that combines multiple parsers into a single
|
|
291
|
+
* object parser with an associated label for documentation or error reporting.
|
|
292
|
+
* @template T A record type where each value is a {@link Parser}.
|
|
293
|
+
* @param label A descriptive label for this parser group, used for
|
|
294
|
+
* documentation and error messages.
|
|
295
|
+
* @param parsers An object containing named parsers that will be combined
|
|
296
|
+
* into a single object parser.
|
|
297
|
+
* @returns A {@link Parser} that produces an object with the same keys as
|
|
298
|
+
* the input, where each value is the result of the corresponding
|
|
299
|
+
* parser.
|
|
300
|
+
*/
|
|
301
|
+
declare function object<T extends {
|
|
302
|
+
readonly [key: string | symbol]: Parser<unknown, unknown>;
|
|
303
|
+
}>(label: string, parsers: T): Parser<{ readonly [K in keyof T]: T[K]["$valueType"][number] extends (infer U) ? U : never }, { readonly [K in keyof T]: T[K]["$stateType"][number] extends (infer U2) ? U2 : never }>;
|
|
304
|
+
/**
|
|
305
|
+
* Creates a parser that combines multiple parsers into a sequential tuple parser.
|
|
306
|
+
* The parsers are applied in the order they appear in the array, and all must
|
|
307
|
+
* succeed for the tuple parser to succeed.
|
|
308
|
+
* @template T A readonly array type where each element is a {@link Parser}.
|
|
309
|
+
* @param parsers An array of parsers that will be applied sequentially
|
|
310
|
+
* to create a tuple of their results.
|
|
311
|
+
* @returns A {@link Parser} that produces a readonly tuple with the same length
|
|
312
|
+
* as the input array, where each element is the result of the
|
|
313
|
+
* corresponding parser.
|
|
314
|
+
*/
|
|
315
|
+
declare function tuple<T extends readonly Parser<unknown, unknown>[]>(parsers: T): Parser<{ readonly [K in keyof T]: T[K]["$valueType"][number] extends (infer U) ? U : never }, { readonly [K in keyof T]: T[K]["$stateType"][number] extends (infer U2) ? U2 : never }>;
|
|
316
|
+
/**
|
|
317
|
+
* Creates a labeled parser that combines multiple parsers into a sequential
|
|
318
|
+
* tuple parser with an associated label for documentation or error reporting.
|
|
319
|
+
* @template T A readonly array type where each element is a {@link Parser}.
|
|
320
|
+
* @param label A descriptive label for this parser group, used for
|
|
321
|
+
* documentation and error messages.
|
|
322
|
+
* @param parsers An array of parsers that will be applied sequentially
|
|
323
|
+
* to create a tuple of their results.
|
|
324
|
+
* @returns A {@link Parser} that produces a readonly tuple with the same length
|
|
325
|
+
* as the input array, where each element is the result of the
|
|
326
|
+
* corresponding parser.
|
|
327
|
+
*/
|
|
328
|
+
declare function tuple<T extends readonly Parser<unknown, unknown>[]>(label: string, parsers: T): Parser<{ readonly [K in keyof T]: T[K]["$valueType"][number] extends (infer U) ? U : never }, { readonly [K in keyof T]: T[K]["$stateType"][number] extends (infer U2) ? U2 : never }>;
|
|
329
|
+
/**
|
|
330
|
+
* Creates a parser that combines two mutually exclusive parsers into one.
|
|
331
|
+
* The resulting parser will try each of the provided parsers in order,
|
|
332
|
+
* and return the result of the first successful parser.
|
|
333
|
+
* @template TA The type of the value returned by the first parser.
|
|
334
|
+
* @template TB The type of the value returned by the second parser.
|
|
335
|
+
* @template TStateA The type of the state used by the first parser.
|
|
336
|
+
* @template TStateB The type of the state used by the second parser.
|
|
337
|
+
* @param a The first {@link Parser} to try.
|
|
338
|
+
* @param b The second {@link Parser} to try.
|
|
339
|
+
* @returns A {@link Parser} that tries to parse using the provided parsers
|
|
340
|
+
* in order, returning the result of the first successful parser.
|
|
341
|
+
*/
|
|
342
|
+
declare function or<TA, TB, TStateA, TStateB>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>): Parser<TA | TB, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>]>;
|
|
343
|
+
/**
|
|
344
|
+
* Creates a parser that combines three mutually exclusive parsers into one.
|
|
345
|
+
* The resulting parser will try each of the provided parsers in order,
|
|
346
|
+
* and return the result of the first successful parser.
|
|
347
|
+
* @template TA The type of the value returned by the first parser.
|
|
348
|
+
* @template TB The type of the value returned by the second parser.
|
|
349
|
+
* @template TC The type of the value returned by the third parser.
|
|
350
|
+
* @template TStateA The type of the state used by the first parser.
|
|
351
|
+
* @template TStateB The type of the state used by the second parser.
|
|
352
|
+
* @template TStateC The type of the state used by the third parser.
|
|
353
|
+
* @param a The first {@link Parser} to try.
|
|
354
|
+
* @param b The second {@link Parser} to try.
|
|
355
|
+
* @param c The third {@link Parser} to try.
|
|
356
|
+
* @return A {@link Parser} that tries to parse using the provided parsers
|
|
357
|
+
* in order, returning the result of the first successful parser.
|
|
358
|
+
*/
|
|
359
|
+
declare function or<TA, TB, TC, TStateA, TStateB, TStateC>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>): Parser<TA | TB | TC, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>]>;
|
|
360
|
+
/**
|
|
361
|
+
* Creates a parser that combines four mutually exclusive parsers into one.
|
|
362
|
+
* The resulting parser will try each of the provided parsers in order,
|
|
363
|
+
* and return the result of the first successful parser.
|
|
364
|
+
* @template TA The type of the value returned by the first parser.
|
|
365
|
+
* @template TB The type of the value returned by the second parser.
|
|
366
|
+
* @template TC The type of the value returned by the third parser.
|
|
367
|
+
* @template TD The type of the value returned by the fourth parser.
|
|
368
|
+
* @template TStateA The type of the state used by the first parser.
|
|
369
|
+
* @template TStateB The type of the state used by the second parser.
|
|
370
|
+
* @template TStateC The type of the state used by the third parser.
|
|
371
|
+
* @template TStateD The type of the state used by the fourth parser.
|
|
372
|
+
* @param a The first {@link Parser} to try.
|
|
373
|
+
* @param b The second {@link Parser} to try.
|
|
374
|
+
* @param c The third {@link Parser} to try.
|
|
375
|
+
* @param d The fourth {@link Parser} to try.
|
|
376
|
+
* @return A {@link Parser} that tries to parse using the provided parsers
|
|
377
|
+
* in order, returning the result of the first successful parser.
|
|
378
|
+
*/
|
|
379
|
+
declare function or<TA, TB, TC, TD, TStateA, TStateB, TStateC, TStateD>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>): Parser<TA | TB | TC | TD, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>]>;
|
|
380
|
+
/**
|
|
381
|
+
* Creates a parser that combines five mutually exclusive parsers into one.
|
|
382
|
+
* The resulting parser will try each of the provided parsers in order,
|
|
383
|
+
* and return the result of the first successful parser.
|
|
384
|
+
* @template TA The type of the value returned by the first parser.
|
|
385
|
+
* @template TB The type of the value returned by the second parser.
|
|
386
|
+
* @template TC The type of the value returned by the third parser.
|
|
387
|
+
* @template TD The type of the value returned by the fourth parser.
|
|
388
|
+
* @template TE The type of the value returned by the fifth parser.
|
|
389
|
+
* @template TStateA The type of the state used by the first parser.
|
|
390
|
+
* @template TStateB The type of the state used by the second parser.
|
|
391
|
+
* @template TStateC The type of the state used by the third parser.
|
|
392
|
+
* @template TStateD The type of the state used by the fourth parser.
|
|
393
|
+
* @template TStateE The type of the state used by the fifth parser.
|
|
394
|
+
* @param a The first {@link Parser} to try.
|
|
395
|
+
* @param b The second {@link Parser} to try.
|
|
396
|
+
* @param c The third {@link Parser} to try.
|
|
397
|
+
* @param d The fourth {@link Parser} to try.
|
|
398
|
+
* @param e The fifth {@link Parser} to try.
|
|
399
|
+
* @return A {@link Parser} that tries to parse using the provided parsers
|
|
400
|
+
* in order, returning the result of the first successful parser.
|
|
401
|
+
*/
|
|
402
|
+
declare function or<TA, TB, TC, TD, TE, TStateA, TStateB, TStateC, TStateD, TStateE>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>): Parser<TA | TB | TC | TD | TE, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>]>;
|
|
403
|
+
/**
|
|
404
|
+
* Merges multiple {@link object} parsers into a single {@link object} parser.
|
|
405
|
+
* It is useful for combining multiple {@link object} parsers so that
|
|
406
|
+
* the unified parser produces a single object containing all the values
|
|
407
|
+
* from the individual parsers while separating the fields into multiple
|
|
408
|
+
* groups.
|
|
409
|
+
* @template TA The type of the first parser.
|
|
410
|
+
* @template TB The type of the second parser.
|
|
411
|
+
* @param a The first {@link object} parser to merge.
|
|
412
|
+
* @param b The second {@link object} parser to merge.
|
|
413
|
+
* @return A new {@link object} parser that combines the values and states
|
|
414
|
+
* of the two parsers into a single object.
|
|
415
|
+
*/
|
|
416
|
+
declare function merge<TA extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TB extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>>(a: TA, b: TB): Parser<{ readonly [K in keyof TA["$valueType"][number]]: TA["$valueType"][number][K] extends (infer U) ? U : never } & { readonly [K in keyof TB["$valueType"][number]]: TB["$valueType"][number][K] extends (infer U2) ? U2 : never }, { readonly [K in keyof TA]: unknown } & { readonly [K in keyof TB]: unknown }>;
|
|
417
|
+
/**
|
|
418
|
+
* Merges multiple {@link object} parsers into a single {@link object} parser.
|
|
419
|
+
* It is useful for combining multiple {@link object} parsers so that
|
|
420
|
+
* the unified parser produces a single object containing all the values
|
|
421
|
+
* from the individual parsers while separating the fields into multiple
|
|
422
|
+
* groups.
|
|
423
|
+
* @template TA The type of the first parser.
|
|
424
|
+
* @template TB The type of the second parser.
|
|
425
|
+
* @template TC The type of the third parser.
|
|
426
|
+
* @param a The first {@link object} parser to merge.
|
|
427
|
+
* @param b The second {@link object} parser to merge.
|
|
428
|
+
* @param c The third {@link object} parser to merge.
|
|
429
|
+
* @return A new {@link object} parser that combines the values and states
|
|
430
|
+
* of the two parsers into a single object.
|
|
431
|
+
*/
|
|
432
|
+
declare function merge<TA extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TB extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TC extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>>(a: TA, b: TB, c: TC): Parser<{ readonly [K in keyof TA["$valueType"][number]]: TA["$valueType"][number][K] extends (infer U) ? U : never } & { readonly [K in keyof TB["$valueType"][number]]: TB["$valueType"][number][K] extends (infer U2) ? U2 : never } & { readonly [K in keyof TC["$valueType"][number]]: TC["$valueType"][number][K] extends (infer U3) ? U3 : never }, { readonly [K in keyof TA]: unknown } & { readonly [K in keyof TB]: unknown } & { readonly [K in keyof TC]: unknown }>;
|
|
433
|
+
/**
|
|
434
|
+
* Merges multiple {@link object} parsers into a single {@link object} parser.
|
|
435
|
+
* It is useful for combining multiple {@link object} parsers so that
|
|
436
|
+
* the unified parser produces a single object containing all the values
|
|
437
|
+
* from the individual parsers while separating the fields into multiple
|
|
438
|
+
* groups.
|
|
439
|
+
* @template TA The type of the first parser.
|
|
440
|
+
* @template TB The type of the second parser.
|
|
441
|
+
* @template TC The type of the third parser.
|
|
442
|
+
* @template TD The type of the fourth parser.
|
|
443
|
+
* @param a The first {@link object} parser to merge.
|
|
444
|
+
* @param b The second {@link object} parser to merge.
|
|
445
|
+
* @param c The third {@link object} parser to merge.
|
|
446
|
+
* @param d The fourth {@link object} parser to merge.
|
|
447
|
+
* @return A new {@link object} parser that combines the values and states
|
|
448
|
+
* of the two parsers into a single object.
|
|
449
|
+
*/
|
|
450
|
+
declare function merge<TA extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TB extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TC extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TD extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>>(a: TA, b: TB, c: TC, d: TD): Parser<{ readonly [K in keyof TA["$valueType"][number]]: TA["$valueType"][number][K] extends (infer U) ? U : never } & { readonly [K in keyof TB["$valueType"][number]]: TB["$valueType"][number][K] extends (infer U2) ? U2 : never } & { readonly [K in keyof TC["$valueType"][number]]: TC["$valueType"][number][K] extends (infer U3) ? U3 : never } & { readonly [K in keyof TD["$valueType"][number]]: TD["$valueType"][number][K] extends (infer U4) ? U4 : never }, { readonly [K in keyof TA]: unknown } & { readonly [K in keyof TB]: unknown } & { readonly [K in keyof TC]: unknown } & { readonly [K in keyof TD]: unknown }>;
|
|
451
|
+
/**
|
|
452
|
+
* Merges multiple {@link object} parsers into a single {@link object} parser.
|
|
453
|
+
* It is useful for combining multiple {@link object} parsers so that
|
|
454
|
+
* the unified parser produces a single object containing all the values
|
|
455
|
+
* from the individual parsers while separating the fields into multiple
|
|
456
|
+
* groups.
|
|
457
|
+
* @template TA The type of the first parser.
|
|
458
|
+
* @template TB The type of the second parser.
|
|
459
|
+
* @template TC The type of the third parser.
|
|
460
|
+
* @template TD The type of the fourth parser.
|
|
461
|
+
* @template TE The type of the fifth parser.
|
|
462
|
+
* @param a The first {@link object} parser to merge.
|
|
463
|
+
* @param b The second {@link object} parser to merge.
|
|
464
|
+
* @param c The third {@link object} parser to merge.
|
|
465
|
+
* @param d The fourth {@link object} parser to merge.
|
|
466
|
+
* @param e The fifth {@link object} parser to merge.
|
|
467
|
+
* @return A new {@link object} parser that combines the values and states
|
|
468
|
+
* of the two parsers into a single object.
|
|
469
|
+
*/
|
|
470
|
+
declare function merge<TA extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TB extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TC extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TD extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TE extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>>(a: TA, b: TB, c: TC, d: TD, e: TE): Parser<{ readonly [K in keyof TA["$valueType"][number]]: TA["$valueType"][number][K] extends (infer U) ? U : never } & { readonly [K in keyof TB["$valueType"][number]]: TB["$valueType"][number][K] extends (infer U2) ? U2 : never } & { readonly [K in keyof TC["$valueType"][number]]: TC["$valueType"][number][K] extends (infer U3) ? U3 : never } & { readonly [K in keyof TD["$valueType"][number]]: TD["$valueType"][number][K] extends (infer U4) ? U4 : never } & { readonly [K in keyof TE["$valueType"][number]]: TE["$valueType"][number][K] extends (infer U5) ? U5 : never }, { readonly [K in keyof TA]: unknown } & { readonly [K in keyof TB]: unknown } & { readonly [K in keyof TC]: unknown } & { readonly [K in keyof TD]: unknown } & { readonly [K in keyof TE]: unknown }>;
|
|
471
|
+
/**
|
|
472
|
+
* Options for the {@link command} parser.
|
|
473
|
+
*/
|
|
474
|
+
interface CommandOptions {
|
|
475
|
+
/**
|
|
476
|
+
* A description of the command, used for documentation.
|
|
477
|
+
*/
|
|
478
|
+
readonly description?: Message;
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* The state type for the {@link command} parser.
|
|
482
|
+
* @template TState The type of the inner parser's state.
|
|
483
|
+
*/
|
|
484
|
+
type CommandState<TState> = undefined | ["matched", string] | ["parsing", TState];
|
|
485
|
+
/**
|
|
486
|
+
* Creates a parser that matches a specific subcommand name and then applies
|
|
487
|
+
* an inner parser to the remaining arguments.
|
|
488
|
+
* This is useful for building CLI tools with subcommands like git, npm, etc.
|
|
489
|
+
* @template T The type of the value returned by the inner parser.
|
|
490
|
+
* @template TState The type of the state used by the inner parser.
|
|
491
|
+
* @param name The subcommand name to match (e.g., `"show"`, `"edit"`).
|
|
492
|
+
* @param parser The {@link Parser} to apply after the command is matched.
|
|
493
|
+
* @param options Optional configuration for the command parser, such as
|
|
494
|
+
* a description for documentation.
|
|
495
|
+
* @returns A {@link Parser} that matches the command name and delegates
|
|
496
|
+
* to the inner parser for the remaining arguments.
|
|
497
|
+
*/
|
|
498
|
+
declare function command<T, TState>(name: string, parser: Parser<T, TState>, options?: CommandOptions): Parser<T, CommandState<TState>>;
|
|
499
|
+
/**
|
|
500
|
+
* Infers the result value type of a {@link Parser}.
|
|
501
|
+
* @template T The {@link Parser} to infer the result value type from.
|
|
502
|
+
*/
|
|
503
|
+
type InferValue<T extends Parser<unknown, unknown>> = T["$valueType"][number];
|
|
504
|
+
/**
|
|
505
|
+
* The result type of a whole parser operation, which can either be a successful
|
|
506
|
+
* result with a value of type `T`, or a failure with an error message.
|
|
507
|
+
* @template T The type of the value produced by the parser.
|
|
508
|
+
*/
|
|
509
|
+
type Result<T> = {
|
|
510
|
+
/**
|
|
511
|
+
* Indicates that the parsing operation was successful.
|
|
512
|
+
*/
|
|
513
|
+
success: true;
|
|
514
|
+
/**
|
|
515
|
+
* The successfully parsed value of type {@link T}.
|
|
516
|
+
* This is the final result of the parsing operation after all parsers
|
|
517
|
+
* have been applied and completed.
|
|
518
|
+
*/
|
|
519
|
+
value: T;
|
|
520
|
+
} | {
|
|
521
|
+
/**
|
|
522
|
+
* Indicates that the parsing operation failed.
|
|
523
|
+
*/
|
|
524
|
+
success: false;
|
|
525
|
+
/**
|
|
526
|
+
* The error message describing why the parsing failed.
|
|
527
|
+
*/
|
|
528
|
+
error: Message;
|
|
529
|
+
};
|
|
530
|
+
/**
|
|
531
|
+
* Parses an array of command-line arguments using the provided combined parser.
|
|
532
|
+
* This function processes the input arguments, applying the parser to each
|
|
533
|
+
* argument until all arguments are consumed or an error occurs.
|
|
534
|
+
* @template T The type of the value produced by the parser.
|
|
535
|
+
* @param parser The combined {@link Parser} to use for parsing the input
|
|
536
|
+
* arguments.
|
|
537
|
+
* @param args The array of command-line arguments to parse. Usually this is
|
|
538
|
+
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
539
|
+
* @returns A {@link Result} object indicating whether the parsing was
|
|
540
|
+
* successful or not. If successful, it contains the parsed value of
|
|
541
|
+
* type `T`. If not, it contains an error message describing the
|
|
542
|
+
* failure.
|
|
543
|
+
*/
|
|
544
|
+
declare function parse<T>(parser: Parser<T, unknown>, args: readonly string[]): Result<T>;
|
|
545
|
+
/**
|
|
546
|
+
* Generates a documentation page for a parser based on its current state after
|
|
547
|
+
* attempting to parse the provided arguments. This function is useful for
|
|
548
|
+
* creating help documentation that reflects the current parsing context.
|
|
549
|
+
*
|
|
550
|
+
* The function works by:
|
|
551
|
+
* 1. Attempting to parse the provided arguments to determine the current state
|
|
552
|
+
* 2. Generating documentation fragments from the parser's current state
|
|
553
|
+
* 3. Organizing fragments into entries and sections
|
|
554
|
+
* 4. Resolving command usage terms based on parsed arguments
|
|
555
|
+
*
|
|
556
|
+
* @param parser The parser to generate documentation for
|
|
557
|
+
* @param args Optional array of command-line arguments that have been parsed
|
|
558
|
+
* so far. Defaults to an empty array. This is used to determine
|
|
559
|
+
* the current parsing context and generate contextual documentation.
|
|
560
|
+
* @returns A {@link DocPage} containing usage information, sections, and
|
|
561
|
+
* optional description, or `undefined` if no documentation can be
|
|
562
|
+
* generated.
|
|
563
|
+
*
|
|
564
|
+
* @example
|
|
565
|
+
* ```typescript
|
|
566
|
+
* const parser = object({
|
|
567
|
+
* verbose: option("-v", "--verbose"),
|
|
568
|
+
* port: option("-p", "--port", integer())
|
|
569
|
+
* });
|
|
570
|
+
*
|
|
571
|
+
* // Get documentation for the root parser
|
|
572
|
+
* const rootDoc = getDocPage(parser);
|
|
573
|
+
*
|
|
574
|
+
* // Get documentation after parsing some arguments
|
|
575
|
+
* const contextDoc = getDocPage(parser, ["-v"]);
|
|
576
|
+
* ```
|
|
577
|
+
*/
|
|
578
|
+
declare function getDocPage(parser: Parser<unknown, unknown>, args?: readonly string[]): DocPage | undefined;
|
|
579
|
+
//#endregion
|
|
580
|
+
export { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
|