@optique/core 0.9.0-dev.211 → 0.9.0-dev.215
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/constructs.cjs +1332 -602
- package/dist/constructs.d.cts +182 -62
- package/dist/constructs.d.ts +182 -62
- package/dist/constructs.js +1332 -602
- package/dist/facade.cjs +188 -144
- package/dist/facade.d.cts +41 -3
- package/dist/facade.d.ts +41 -3
- package/dist/facade.js +187 -145
- package/dist/index.cjs +8 -0
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/modifiers.cjs +228 -79
- package/dist/modifiers.d.cts +11 -6
- package/dist/modifiers.d.ts +11 -6
- package/dist/modifiers.js +228 -79
- package/dist/parser.cjs +217 -33
- package/dist/parser.d.cts +202 -18
- package/dist/parser.d.ts +202 -18
- package/dist/parser.js +212 -34
- package/dist/primitives.cjs +242 -97
- package/dist/primitives.d.cts +14 -10
- package/dist/primitives.d.ts +14 -10
- package/dist/primitives.js +242 -97
- package/dist/valueparser.cjs +10 -1
- package/dist/valueparser.d.cts +29 -16
- package/dist/valueparser.d.ts +29 -16
- package/dist/valueparser.js +10 -1
- package/package.json +1 -1
package/dist/parser.cjs
CHANGED
|
@@ -9,17 +9,23 @@ const require_primitives = require('./primitives.cjs');
|
|
|
9
9
|
* Parses an array of command-line arguments using the provided combined parser.
|
|
10
10
|
* This function processes the input arguments, applying the parser to each
|
|
11
11
|
* argument until all arguments are consumed or an error occurs.
|
|
12
|
+
*
|
|
13
|
+
* This function only accepts synchronous parsers. For asynchronous parsers,
|
|
14
|
+
* use {@link parseAsync}.
|
|
15
|
+
*
|
|
12
16
|
* @template T The type of the value produced by the parser.
|
|
13
17
|
* @param parser The combined {@link Parser} to use for parsing the input
|
|
14
|
-
* arguments.
|
|
18
|
+
* arguments. Must be a synchronous parser.
|
|
15
19
|
* @param args The array of command-line arguments to parse. Usually this is
|
|
16
20
|
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
17
21
|
* @returns A {@link Result} object indicating whether the parsing was
|
|
18
22
|
* successful or not. If successful, it contains the parsed value of
|
|
19
23
|
* type `T`. If not, it contains an error message describing the
|
|
20
24
|
* failure.
|
|
25
|
+
* @since 0.9.0 Renamed from the original `parse` function which now delegates
|
|
26
|
+
* to this for sync parsers.
|
|
21
27
|
*/
|
|
22
|
-
function
|
|
28
|
+
function parseSync(parser, args) {
|
|
23
29
|
let context = {
|
|
24
30
|
buffer: args,
|
|
25
31
|
optionsTerminated: false,
|
|
@@ -49,11 +55,86 @@ function parse(parser, args) {
|
|
|
49
55
|
};
|
|
50
56
|
}
|
|
51
57
|
/**
|
|
58
|
+
* Parses an array of command-line arguments using the provided combined parser.
|
|
59
|
+
* This function processes the input arguments, applying the parser to each
|
|
60
|
+
* argument until all arguments are consumed or an error occurs.
|
|
61
|
+
*
|
|
62
|
+
* This function accepts any parser (sync or async) and always returns a Promise.
|
|
63
|
+
* For synchronous parsing with sync parsers, use {@link parseSync} instead.
|
|
64
|
+
*
|
|
65
|
+
* @template T The type of the value produced by the parser.
|
|
66
|
+
* @param parser The combined {@link Parser} to use for parsing the input
|
|
67
|
+
* arguments.
|
|
68
|
+
* @param args The array of command-line arguments to parse. Usually this is
|
|
69
|
+
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
70
|
+
* @returns A Promise that resolves to a {@link Result} object indicating
|
|
71
|
+
* whether the parsing was successful or not.
|
|
72
|
+
* @since 0.9.0
|
|
73
|
+
*/
|
|
74
|
+
async function parseAsync(parser, args) {
|
|
75
|
+
let context = {
|
|
76
|
+
buffer: args,
|
|
77
|
+
optionsTerminated: false,
|
|
78
|
+
state: parser.initialState,
|
|
79
|
+
usage: parser.usage
|
|
80
|
+
};
|
|
81
|
+
do {
|
|
82
|
+
const result = await parser.parse(context);
|
|
83
|
+
if (!result.success) return {
|
|
84
|
+
success: false,
|
|
85
|
+
error: result.error
|
|
86
|
+
};
|
|
87
|
+
const previousBuffer = context.buffer;
|
|
88
|
+
context = result.next;
|
|
89
|
+
if (context.buffer.length > 0 && context.buffer.length === previousBuffer.length && context.buffer.every((item, i) => item === previousBuffer[i])) return {
|
|
90
|
+
success: false,
|
|
91
|
+
error: require_message.message`Unexpected option or argument: ${context.buffer[0]}.`
|
|
92
|
+
};
|
|
93
|
+
} while (context.buffer.length > 0);
|
|
94
|
+
const endResult = await parser.complete(context.state);
|
|
95
|
+
return endResult.success ? {
|
|
96
|
+
success: true,
|
|
97
|
+
value: endResult.value
|
|
98
|
+
} : {
|
|
99
|
+
success: false,
|
|
100
|
+
error: endResult.error
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Parses an array of command-line arguments using the provided combined parser.
|
|
105
|
+
* This function processes the input arguments, applying the parser to each
|
|
106
|
+
* argument until all arguments are consumed or an error occurs.
|
|
107
|
+
*
|
|
108
|
+
* The return type depends on the parser's mode:
|
|
109
|
+
* - Sync parsers return `Result<T>` directly.
|
|
110
|
+
* - Async parsers return `Promise<Result<T>>`.
|
|
111
|
+
*
|
|
112
|
+
* For explicit control, use {@link parseSync} or {@link parseAsync}.
|
|
113
|
+
*
|
|
114
|
+
* @template M The execution mode of the parser.
|
|
115
|
+
* @template T The type of the value produced by the parser.
|
|
116
|
+
* @param parser The combined {@link Parser} to use for parsing the input
|
|
117
|
+
* arguments.
|
|
118
|
+
* @param args The array of command-line arguments to parse. Usually this is
|
|
119
|
+
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
120
|
+
* @returns A {@link Result} object (for sync) or Promise thereof (for async)
|
|
121
|
+
* indicating whether the parsing was successful or not.
|
|
122
|
+
*/
|
|
123
|
+
function parse(parser, args) {
|
|
124
|
+
if (parser.$mode === "async") return parseAsync(parser, args);
|
|
125
|
+
return parseSync(parser, args);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
52
128
|
* Generates command-line suggestions based on current parsing state.
|
|
53
129
|
* This function processes the input arguments up to the last argument,
|
|
54
130
|
* then calls the parser's suggest method with the remaining prefix.
|
|
131
|
+
*
|
|
132
|
+
* This function only accepts synchronous parsers. For asynchronous parsers,
|
|
133
|
+
* use {@link suggestAsync}.
|
|
134
|
+
*
|
|
55
135
|
* @template T The type of the value produced by the parser.
|
|
56
136
|
* @param parser The {@link Parser} to use for generating suggestions.
|
|
137
|
+
* Must be a synchronous parser.
|
|
57
138
|
* @param args The array of command-line arguments including the partial
|
|
58
139
|
* argument to complete. The last element is treated as
|
|
59
140
|
* the prefix for suggestions.
|
|
@@ -67,16 +148,17 @@ function parse(parser, args) {
|
|
|
67
148
|
* });
|
|
68
149
|
*
|
|
69
150
|
* // Get suggestions for options starting with "--"
|
|
70
|
-
* const suggestions =
|
|
151
|
+
* const suggestions = suggestSync(parser, ["--"]);
|
|
71
152
|
* // Returns: [{ text: "--verbose" }, { text: "--format" }]
|
|
72
153
|
*
|
|
73
154
|
* // Get suggestions after parsing some arguments
|
|
74
|
-
* const suggestions2 =
|
|
155
|
+
* const suggestions2 = suggestSync(parser, ["-v", "--format="]);
|
|
75
156
|
* // Returns: [{ text: "--format=json" }, { text: "--format=yaml" }]
|
|
76
157
|
* ```
|
|
77
158
|
* @since 0.6.0
|
|
159
|
+
* @since 0.9.0 Renamed from the original `suggest` function.
|
|
78
160
|
*/
|
|
79
|
-
function
|
|
161
|
+
function suggestSync(parser, args) {
|
|
80
162
|
const allButLast = args.slice(0, -1);
|
|
81
163
|
const prefix = args[args.length - 1];
|
|
82
164
|
let context = {
|
|
@@ -95,6 +177,73 @@ function suggest(parser, args) {
|
|
|
95
177
|
return Array.from(parser.suggest(context, prefix));
|
|
96
178
|
}
|
|
97
179
|
/**
|
|
180
|
+
* Generates command-line suggestions based on current parsing state.
|
|
181
|
+
* This function processes the input arguments up to the last argument,
|
|
182
|
+
* then calls the parser's suggest method with the remaining prefix.
|
|
183
|
+
*
|
|
184
|
+
* This function accepts any parser (sync or async) and always returns a Promise.
|
|
185
|
+
* For synchronous suggestion generation with sync parsers, use
|
|
186
|
+
* {@link suggestSync} instead.
|
|
187
|
+
*
|
|
188
|
+
* @template T The type of the value produced by the parser.
|
|
189
|
+
* @param parser The {@link Parser} to use for generating suggestions.
|
|
190
|
+
* @param args The array of command-line arguments including the partial
|
|
191
|
+
* argument to complete. The last element is treated as
|
|
192
|
+
* the prefix for suggestions.
|
|
193
|
+
* @returns A Promise that resolves to an array of {@link Suggestion} objects
|
|
194
|
+
* containing completion candidates.
|
|
195
|
+
* @since 0.9.0
|
|
196
|
+
*/
|
|
197
|
+
async function suggestAsync(parser, args) {
|
|
198
|
+
const allButLast = args.slice(0, -1);
|
|
199
|
+
const prefix = args[args.length - 1];
|
|
200
|
+
let context = {
|
|
201
|
+
buffer: allButLast,
|
|
202
|
+
optionsTerminated: false,
|
|
203
|
+
state: parser.initialState,
|
|
204
|
+
usage: parser.usage
|
|
205
|
+
};
|
|
206
|
+
while (context.buffer.length > 0) {
|
|
207
|
+
const result = await parser.parse(context);
|
|
208
|
+
if (!result.success) {
|
|
209
|
+
const suggestions$1 = [];
|
|
210
|
+
for await (const suggestion of parser.suggest(context, prefix)) suggestions$1.push(suggestion);
|
|
211
|
+
return suggestions$1;
|
|
212
|
+
}
|
|
213
|
+
const previousBuffer = context.buffer;
|
|
214
|
+
context = result.next;
|
|
215
|
+
if (context.buffer.length > 0 && context.buffer.length === previousBuffer.length && context.buffer.every((item, i) => item === previousBuffer[i])) return [];
|
|
216
|
+
}
|
|
217
|
+
const suggestions = [];
|
|
218
|
+
for await (const suggestion of parser.suggest(context, prefix)) suggestions.push(suggestion);
|
|
219
|
+
return suggestions;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Generates command-line suggestions based on current parsing state.
|
|
223
|
+
* This function processes the input arguments up to the last argument,
|
|
224
|
+
* then calls the parser's suggest method with the remaining prefix.
|
|
225
|
+
*
|
|
226
|
+
* The return type depends on the parser's mode:
|
|
227
|
+
* - Sync parsers return `readonly Suggestion[]` directly.
|
|
228
|
+
* - Async parsers return `Promise<readonly Suggestion[]>`.
|
|
229
|
+
*
|
|
230
|
+
* For explicit control, use {@link suggestSync} or {@link suggestAsync}.
|
|
231
|
+
*
|
|
232
|
+
* @template M The execution mode of the parser.
|
|
233
|
+
* @template T The type of the value produced by the parser.
|
|
234
|
+
* @param parser The {@link Parser} to use for generating suggestions.
|
|
235
|
+
* @param args The array of command-line arguments including the partial
|
|
236
|
+
* argument to complete. The last element is treated as
|
|
237
|
+
* the prefix for suggestions.
|
|
238
|
+
* @returns An array of {@link Suggestion} objects (for sync) or Promise thereof
|
|
239
|
+
* (for async) containing completion candidates.
|
|
240
|
+
* @since 0.6.0
|
|
241
|
+
*/
|
|
242
|
+
function suggest(parser, args) {
|
|
243
|
+
if (parser.$mode === "async") return suggestAsync(parser, args);
|
|
244
|
+
return suggestSync(parser, args);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
98
247
|
* Recursively searches for a command within nested exclusive usage terms.
|
|
99
248
|
* When the command is found, returns the expanded usage terms for that command.
|
|
100
249
|
*
|
|
@@ -115,39 +264,44 @@ function findCommandInExclusive(term, commandName) {
|
|
|
115
264
|
return null;
|
|
116
265
|
}
|
|
117
266
|
/**
|
|
118
|
-
* Generates a documentation page for a parser
|
|
119
|
-
* attempting to parse the provided arguments. This function is useful for
|
|
120
|
-
* creating help documentation that reflects the current parsing context.
|
|
121
|
-
*
|
|
122
|
-
* The function works by:
|
|
123
|
-
* 1. Attempting to parse the provided arguments to determine the current state
|
|
124
|
-
* 2. Generating documentation fragments from the parser's current state
|
|
125
|
-
* 3. Organizing fragments into entries and sections
|
|
126
|
-
* 4. Resolving command usage terms based on parsed arguments
|
|
127
|
-
*
|
|
128
|
-
* @param parser The parser to generate documentation for
|
|
129
|
-
* @param args Optional array of command-line arguments that have been parsed
|
|
130
|
-
* so far. Defaults to an empty array. This is used to determine
|
|
131
|
-
* the current parsing context and generate contextual documentation.
|
|
132
|
-
* @returns A {@link DocPage} containing usage information, sections, and
|
|
133
|
-
* optional description, or `undefined` if no documentation can be
|
|
134
|
-
* generated.
|
|
267
|
+
* Generates a documentation page for a synchronous parser.
|
|
135
268
|
*
|
|
136
|
-
* @
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* verbose: option("-v", "--verbose"),
|
|
140
|
-
* port: option("-p", "--port", integer())
|
|
141
|
-
* });
|
|
269
|
+
* This is the sync-specific version of {@link getDocPage}. It only accepts
|
|
270
|
+
* sync parsers and returns the documentation page directly (not wrapped
|
|
271
|
+
* in a Promise).
|
|
142
272
|
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
273
|
+
* @param parser The sync parser to generate documentation for.
|
|
274
|
+
* @param args Optional array of command-line arguments for context.
|
|
275
|
+
* @returns A {@link DocPage} or `undefined`.
|
|
276
|
+
* @since 0.9.0
|
|
277
|
+
*/
|
|
278
|
+
function getDocPageSync(parser, args = []) {
|
|
279
|
+
return getDocPageSyncImpl(parser, args);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Generates a documentation page for any parser, returning a Promise.
|
|
145
283
|
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
284
|
+
* This function accepts parsers of any mode (sync or async) and always
|
|
285
|
+
* returns a Promise. Use this when working with parsers that may contain
|
|
286
|
+
* async value parsers.
|
|
287
|
+
*
|
|
288
|
+
* @param parser The parser to generate documentation for.
|
|
289
|
+
* @param args Optional array of command-line arguments for context.
|
|
290
|
+
* @returns A Promise of {@link DocPage} or `undefined`.
|
|
291
|
+
* @since 0.9.0
|
|
149
292
|
*/
|
|
293
|
+
function getDocPageAsync(parser, args = []) {
|
|
294
|
+
if (parser.$mode === "sync") return Promise.resolve(getDocPageSyncImpl(parser, args));
|
|
295
|
+
return getDocPageAsyncImpl(parser, args);
|
|
296
|
+
}
|
|
150
297
|
function getDocPage(parser, args = []) {
|
|
298
|
+
if (parser.$mode === "sync") return getDocPageSyncImpl(parser, args);
|
|
299
|
+
return getDocPageAsyncImpl(parser, args);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Internal sync implementation of getDocPage.
|
|
303
|
+
*/
|
|
304
|
+
function getDocPageSyncImpl(parser, args) {
|
|
151
305
|
let context = {
|
|
152
306
|
buffer: args,
|
|
153
307
|
optionsTerminated: false,
|
|
@@ -159,6 +313,30 @@ function getDocPage(parser, args = []) {
|
|
|
159
313
|
if (!result.success) break;
|
|
160
314
|
context = result.next;
|
|
161
315
|
} while (context.buffer.length > 0);
|
|
316
|
+
return buildDocPage(parser, context, args);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Internal async implementation of getDocPage.
|
|
320
|
+
*/
|
|
321
|
+
async function getDocPageAsyncImpl(parser, args) {
|
|
322
|
+
let context = {
|
|
323
|
+
buffer: args,
|
|
324
|
+
optionsTerminated: false,
|
|
325
|
+
state: parser.initialState,
|
|
326
|
+
usage: parser.usage
|
|
327
|
+
};
|
|
328
|
+
do {
|
|
329
|
+
const result = await parser.parse(context);
|
|
330
|
+
if (!result.success) break;
|
|
331
|
+
context = result.next;
|
|
332
|
+
} while (context.buffer.length > 0);
|
|
333
|
+
return buildDocPage(parser, context, args);
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Builds a DocPage from the parser and context.
|
|
337
|
+
* Shared by both sync and async implementations.
|
|
338
|
+
*/
|
|
339
|
+
function buildDocPage(parser, context, args) {
|
|
162
340
|
const { description, fragments, footer } = parser.getDocFragments({
|
|
163
341
|
kind: "available",
|
|
164
342
|
state: context.state
|
|
@@ -202,6 +380,8 @@ exports.conditional = require_constructs.conditional;
|
|
|
202
380
|
exports.constant = require_primitives.constant;
|
|
203
381
|
exports.flag = require_primitives.flag;
|
|
204
382
|
exports.getDocPage = getDocPage;
|
|
383
|
+
exports.getDocPageAsync = getDocPageAsync;
|
|
384
|
+
exports.getDocPageSync = getDocPageSync;
|
|
205
385
|
exports.group = require_constructs.group;
|
|
206
386
|
exports.longestMatch = require_constructs.longestMatch;
|
|
207
387
|
exports.map = require_modifiers.map;
|
|
@@ -212,7 +392,11 @@ exports.option = require_primitives.option;
|
|
|
212
392
|
exports.optional = require_modifiers.optional;
|
|
213
393
|
exports.or = require_constructs.or;
|
|
214
394
|
exports.parse = parse;
|
|
395
|
+
exports.parseAsync = parseAsync;
|
|
396
|
+
exports.parseSync = parseSync;
|
|
215
397
|
exports.passThrough = require_primitives.passThrough;
|
|
216
398
|
exports.suggest = suggest;
|
|
399
|
+
exports.suggestAsync = suggestAsync;
|
|
400
|
+
exports.suggestSync = suggestSync;
|
|
217
401
|
exports.tuple = require_constructs.tuple;
|
|
218
402
|
exports.withDefault = require_modifiers.withDefault;
|
package/dist/parser.d.cts
CHANGED
|
@@ -8,6 +8,46 @@ import { ConditionalErrorOptions, ConditionalOptions, DuplicateOptionError, Long
|
|
|
8
8
|
|
|
9
9
|
//#region src/parser.d.ts
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Represents the execution mode for parsers.
|
|
13
|
+
*
|
|
14
|
+
* - `"sync"`: Synchronous execution where methods return values directly.
|
|
15
|
+
* - `"async"`: Asynchronous execution where methods return Promises or
|
|
16
|
+
* AsyncIterables.
|
|
17
|
+
*
|
|
18
|
+
* @since 0.9.0
|
|
19
|
+
*/
|
|
20
|
+
type Mode = "sync" | "async";
|
|
21
|
+
/**
|
|
22
|
+
* Wraps a value type based on the execution mode.
|
|
23
|
+
*
|
|
24
|
+
* - In sync mode: Returns `T` directly.
|
|
25
|
+
* - In async mode: Returns `Promise<T>`.
|
|
26
|
+
*
|
|
27
|
+
* @template M The execution mode.
|
|
28
|
+
* @template T The value type to wrap.
|
|
29
|
+
* @since 0.9.0
|
|
30
|
+
*/
|
|
31
|
+
type ModeValue<M extends Mode, T> = M extends "async" ? Promise<T> : T;
|
|
32
|
+
/**
|
|
33
|
+
* Wraps an iterable type based on the execution mode.
|
|
34
|
+
*
|
|
35
|
+
* - In sync mode: Returns `Iterable<T>`.
|
|
36
|
+
* - In async mode: Returns `AsyncIterable<T>`.
|
|
37
|
+
*
|
|
38
|
+
* @template M The execution mode.
|
|
39
|
+
* @template T The element type.
|
|
40
|
+
* @since 0.9.0
|
|
41
|
+
*/
|
|
42
|
+
type ModeIterable<M extends Mode, T> = M extends "async" ? AsyncIterable<T> : Iterable<T>;
|
|
43
|
+
/**
|
|
44
|
+
* Combines multiple modes into a single mode.
|
|
45
|
+
* If any mode is `"async"`, the result is `"async"`; otherwise `"sync"`.
|
|
46
|
+
*
|
|
47
|
+
* @template T A tuple of Mode types.
|
|
48
|
+
* @since 0.9.0
|
|
49
|
+
*/
|
|
50
|
+
type CombineModes<T extends readonly Mode[]> = "async" extends T[number] ? "async" : "sync";
|
|
11
51
|
/**
|
|
12
52
|
* Represents the state passed to getDocFragments.
|
|
13
53
|
* Can be either the actual parser state or an explicit indicator
|
|
@@ -23,10 +63,12 @@ type DocState<TState> = {
|
|
|
23
63
|
};
|
|
24
64
|
/**
|
|
25
65
|
* Parser interface for command-line argument parsing.
|
|
66
|
+
* @template M The execution mode of the parser (`"sync"` or `"async"`).
|
|
26
67
|
* @template TValue The type of the value returned by the parser.
|
|
27
68
|
* @template TState The type of the state used during parsing.
|
|
69
|
+
* @since 0.9.0 Added the `M` type parameter for sync/async mode support.
|
|
28
70
|
*/
|
|
29
|
-
interface Parser<TValue, TState> {
|
|
71
|
+
interface Parser<M extends Mode = "sync", TValue = unknown, TState = unknown> {
|
|
30
72
|
/**
|
|
31
73
|
* A type tag for the result value of this parser, used for type inference.
|
|
32
74
|
* Usually this is an empty array at runtime, but it does not matter
|
|
@@ -41,6 +83,15 @@ interface Parser<TValue, TState> {
|
|
|
41
83
|
* @internal
|
|
42
84
|
*/
|
|
43
85
|
readonly $stateType: readonly TState[];
|
|
86
|
+
/**
|
|
87
|
+
* The execution mode of this parser.
|
|
88
|
+
*
|
|
89
|
+
* - `"sync"`: All methods return values directly.
|
|
90
|
+
* - `"async"`: Methods return Promises or AsyncIterables.
|
|
91
|
+
*
|
|
92
|
+
* @since 0.9.0
|
|
93
|
+
*/
|
|
94
|
+
readonly $mode: M;
|
|
44
95
|
/**
|
|
45
96
|
* The priority of this parser, which determines the order in which
|
|
46
97
|
* parsers are applied when multiple parsers are available. The greater
|
|
@@ -63,8 +114,9 @@ interface Parser<TValue, TState> {
|
|
|
63
114
|
* @param context The context of the parser, which includes the input buffer
|
|
64
115
|
* and the current state.
|
|
65
116
|
* @returns A result object indicating success or failure.
|
|
117
|
+
* In async mode, returns a Promise that resolves to the result.
|
|
66
118
|
*/
|
|
67
|
-
parse(context: ParserContext<TState>): ParserResult<TState
|
|
119
|
+
parse(context: ParserContext<TState>): ModeValue<M, ParserResult<TState>>;
|
|
68
120
|
/**
|
|
69
121
|
* Transforms a {@link TState} into a {@link TValue}, if applicable.
|
|
70
122
|
* If the transformation is not applicable, it should return
|
|
@@ -76,8 +128,9 @@ interface Parser<TValue, TState> {
|
|
|
76
128
|
* the transformation. If successful, it should contain
|
|
77
129
|
* the parsed value of type {@link TValue}. If not applicable,
|
|
78
130
|
* it should return an error message.
|
|
131
|
+
* In async mode, returns a Promise that resolves to the result.
|
|
79
132
|
*/
|
|
80
|
-
complete(state: TState): ValueParserResult<TValue
|
|
133
|
+
complete(state: TState): ModeValue<M, ValueParserResult<TValue>>;
|
|
81
134
|
/**
|
|
82
135
|
* Generates next-step suggestions based on the current context
|
|
83
136
|
* and an optional prefix. This can be used to provide shell completion
|
|
@@ -88,9 +141,10 @@ interface Parser<TValue, TState> {
|
|
|
88
141
|
* Can be an empty string if no prefix is provided.
|
|
89
142
|
* @returns An iterable of {@link Suggestion} objects, each containing
|
|
90
143
|
* a suggestion text and an optional description.
|
|
144
|
+
* In async mode, returns an AsyncIterable.
|
|
91
145
|
* @since 0.6.0
|
|
92
146
|
*/
|
|
93
|
-
suggest(context: ParserContext<TState>, prefix: string):
|
|
147
|
+
suggest(context: ParserContext<TState>, prefix: string): ModeIterable<M, Suggestion>;
|
|
94
148
|
/**
|
|
95
149
|
* Generates a documentation fragment for this parser, which can be used
|
|
96
150
|
* to describe the parser's usage, description, and default value.
|
|
@@ -217,7 +271,13 @@ type ParserResult<TState> = {
|
|
|
217
271
|
* Infers the result value type of a {@link Parser}.
|
|
218
272
|
* @template T The {@link Parser} to infer the result value type from.
|
|
219
273
|
*/
|
|
220
|
-
type InferValue<T extends Parser<unknown, unknown>> = T["$valueType"][number];
|
|
274
|
+
type InferValue<T extends Parser<Mode, unknown, unknown>> = T["$valueType"][number];
|
|
275
|
+
/**
|
|
276
|
+
* Infers the execution mode of a {@link Parser}.
|
|
277
|
+
* @template T The {@link Parser} to infer the execution mode from.
|
|
278
|
+
* @since 0.9.0
|
|
279
|
+
*/
|
|
280
|
+
type InferMode<T extends Parser<Mode, unknown, unknown>> = T["$mode"];
|
|
221
281
|
/**
|
|
222
282
|
* The result type of a whole parser operation, which can either be a successful
|
|
223
283
|
* result with a value of type `T`, or a failure with an error message.
|
|
@@ -248,23 +308,73 @@ type Result<T> = {
|
|
|
248
308
|
* Parses an array of command-line arguments using the provided combined parser.
|
|
249
309
|
* This function processes the input arguments, applying the parser to each
|
|
250
310
|
* argument until all arguments are consumed or an error occurs.
|
|
311
|
+
*
|
|
312
|
+
* This function only accepts synchronous parsers. For asynchronous parsers,
|
|
313
|
+
* use {@link parseAsync}.
|
|
314
|
+
*
|
|
251
315
|
* @template T The type of the value produced by the parser.
|
|
252
316
|
* @param parser The combined {@link Parser} to use for parsing the input
|
|
253
|
-
* arguments.
|
|
317
|
+
* arguments. Must be a synchronous parser.
|
|
254
318
|
* @param args The array of command-line arguments to parse. Usually this is
|
|
255
319
|
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
256
320
|
* @returns A {@link Result} object indicating whether the parsing was
|
|
257
321
|
* successful or not. If successful, it contains the parsed value of
|
|
258
322
|
* type `T`. If not, it contains an error message describing the
|
|
259
323
|
* failure.
|
|
324
|
+
* @since 0.9.0 Renamed from the original `parse` function which now delegates
|
|
325
|
+
* to this for sync parsers.
|
|
326
|
+
*/
|
|
327
|
+
declare function parseSync<T>(parser: Parser<"sync", T, unknown>, args: readonly string[]): Result<T>;
|
|
328
|
+
/**
|
|
329
|
+
* Parses an array of command-line arguments using the provided combined parser.
|
|
330
|
+
* This function processes the input arguments, applying the parser to each
|
|
331
|
+
* argument until all arguments are consumed or an error occurs.
|
|
332
|
+
*
|
|
333
|
+
* This function accepts any parser (sync or async) and always returns a Promise.
|
|
334
|
+
* For synchronous parsing with sync parsers, use {@link parseSync} instead.
|
|
335
|
+
*
|
|
336
|
+
* @template T The type of the value produced by the parser.
|
|
337
|
+
* @param parser The combined {@link Parser} to use for parsing the input
|
|
338
|
+
* arguments.
|
|
339
|
+
* @param args The array of command-line arguments to parse. Usually this is
|
|
340
|
+
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
341
|
+
* @returns A Promise that resolves to a {@link Result} object indicating
|
|
342
|
+
* whether the parsing was successful or not.
|
|
343
|
+
* @since 0.9.0
|
|
344
|
+
*/
|
|
345
|
+
declare function parseAsync<T>(parser: Parser<Mode, T, unknown>, args: readonly string[]): Promise<Result<T>>;
|
|
346
|
+
/**
|
|
347
|
+
* Parses an array of command-line arguments using the provided combined parser.
|
|
348
|
+
* This function processes the input arguments, applying the parser to each
|
|
349
|
+
* argument until all arguments are consumed or an error occurs.
|
|
350
|
+
*
|
|
351
|
+
* The return type depends on the parser's mode:
|
|
352
|
+
* - Sync parsers return `Result<T>` directly.
|
|
353
|
+
* - Async parsers return `Promise<Result<T>>`.
|
|
354
|
+
*
|
|
355
|
+
* For explicit control, use {@link parseSync} or {@link parseAsync}.
|
|
356
|
+
*
|
|
357
|
+
* @template M The execution mode of the parser.
|
|
358
|
+
* @template T The type of the value produced by the parser.
|
|
359
|
+
* @param parser The combined {@link Parser} to use for parsing the input
|
|
360
|
+
* arguments.
|
|
361
|
+
* @param args The array of command-line arguments to parse. Usually this is
|
|
362
|
+
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
363
|
+
* @returns A {@link Result} object (for sync) or Promise thereof (for async)
|
|
364
|
+
* indicating whether the parsing was successful or not.
|
|
260
365
|
*/
|
|
261
|
-
declare function parse<T>(parser: Parser<T, unknown>, args: readonly string[]): Result<T
|
|
366
|
+
declare function parse<M extends Mode, T>(parser: Parser<M, T, unknown>, args: readonly string[]): ModeValue<M, Result<T>>;
|
|
262
367
|
/**
|
|
263
368
|
* Generates command-line suggestions based on current parsing state.
|
|
264
369
|
* This function processes the input arguments up to the last argument,
|
|
265
370
|
* then calls the parser's suggest method with the remaining prefix.
|
|
371
|
+
*
|
|
372
|
+
* This function only accepts synchronous parsers. For asynchronous parsers,
|
|
373
|
+
* use {@link suggestAsync}.
|
|
374
|
+
*
|
|
266
375
|
* @template T The type of the value produced by the parser.
|
|
267
376
|
* @param parser The {@link Parser} to use for generating suggestions.
|
|
377
|
+
* Must be a synchronous parser.
|
|
268
378
|
* @param args The array of command-line arguments including the partial
|
|
269
379
|
* argument to complete. The last element is treated as
|
|
270
380
|
* the prefix for suggestions.
|
|
@@ -278,16 +388,84 @@ declare function parse<T>(parser: Parser<T, unknown>, args: readonly string[]):
|
|
|
278
388
|
* });
|
|
279
389
|
*
|
|
280
390
|
* // Get suggestions for options starting with "--"
|
|
281
|
-
* const suggestions =
|
|
391
|
+
* const suggestions = suggestSync(parser, ["--"]);
|
|
282
392
|
* // Returns: [{ text: "--verbose" }, { text: "--format" }]
|
|
283
393
|
*
|
|
284
394
|
* // Get suggestions after parsing some arguments
|
|
285
|
-
* const suggestions2 =
|
|
395
|
+
* const suggestions2 = suggestSync(parser, ["-v", "--format="]);
|
|
286
396
|
* // Returns: [{ text: "--format=json" }, { text: "--format=yaml" }]
|
|
287
397
|
* ```
|
|
288
398
|
* @since 0.6.0
|
|
399
|
+
* @since 0.9.0 Renamed from the original `suggest` function.
|
|
400
|
+
*/
|
|
401
|
+
declare function suggestSync<T>(parser: Parser<"sync", T, unknown>, args: readonly [string, ...readonly string[]]): readonly Suggestion[];
|
|
402
|
+
/**
|
|
403
|
+
* Generates command-line suggestions based on current parsing state.
|
|
404
|
+
* This function processes the input arguments up to the last argument,
|
|
405
|
+
* then calls the parser's suggest method with the remaining prefix.
|
|
406
|
+
*
|
|
407
|
+
* This function accepts any parser (sync or async) and always returns a Promise.
|
|
408
|
+
* For synchronous suggestion generation with sync parsers, use
|
|
409
|
+
* {@link suggestSync} instead.
|
|
410
|
+
*
|
|
411
|
+
* @template T The type of the value produced by the parser.
|
|
412
|
+
* @param parser The {@link Parser} to use for generating suggestions.
|
|
413
|
+
* @param args The array of command-line arguments including the partial
|
|
414
|
+
* argument to complete. The last element is treated as
|
|
415
|
+
* the prefix for suggestions.
|
|
416
|
+
* @returns A Promise that resolves to an array of {@link Suggestion} objects
|
|
417
|
+
* containing completion candidates.
|
|
418
|
+
* @since 0.9.0
|
|
419
|
+
*/
|
|
420
|
+
declare function suggestAsync<T>(parser: Parser<Mode, T, unknown>, args: readonly [string, ...readonly string[]]): Promise<readonly Suggestion[]>;
|
|
421
|
+
/**
|
|
422
|
+
* Generates command-line suggestions based on current parsing state.
|
|
423
|
+
* This function processes the input arguments up to the last argument,
|
|
424
|
+
* then calls the parser's suggest method with the remaining prefix.
|
|
425
|
+
*
|
|
426
|
+
* The return type depends on the parser's mode:
|
|
427
|
+
* - Sync parsers return `readonly Suggestion[]` directly.
|
|
428
|
+
* - Async parsers return `Promise<readonly Suggestion[]>`.
|
|
429
|
+
*
|
|
430
|
+
* For explicit control, use {@link suggestSync} or {@link suggestAsync}.
|
|
431
|
+
*
|
|
432
|
+
* @template M The execution mode of the parser.
|
|
433
|
+
* @template T The type of the value produced by the parser.
|
|
434
|
+
* @param parser The {@link Parser} to use for generating suggestions.
|
|
435
|
+
* @param args The array of command-line arguments including the partial
|
|
436
|
+
* argument to complete. The last element is treated as
|
|
437
|
+
* the prefix for suggestions.
|
|
438
|
+
* @returns An array of {@link Suggestion} objects (for sync) or Promise thereof
|
|
439
|
+
* (for async) containing completion candidates.
|
|
440
|
+
* @since 0.6.0
|
|
441
|
+
*/
|
|
442
|
+
declare function suggest<M extends Mode, T>(parser: Parser<M, T, unknown>, args: readonly [string, ...readonly string[]]): ModeValue<M, readonly Suggestion[]>;
|
|
443
|
+
/**
|
|
444
|
+
* Generates a documentation page for a synchronous parser.
|
|
445
|
+
*
|
|
446
|
+
* This is the sync-specific version of {@link getDocPage}. It only accepts
|
|
447
|
+
* sync parsers and returns the documentation page directly (not wrapped
|
|
448
|
+
* in a Promise).
|
|
449
|
+
*
|
|
450
|
+
* @param parser The sync parser to generate documentation for.
|
|
451
|
+
* @param args Optional array of command-line arguments for context.
|
|
452
|
+
* @returns A {@link DocPage} or `undefined`.
|
|
453
|
+
* @since 0.9.0
|
|
289
454
|
*/
|
|
290
|
-
declare function
|
|
455
|
+
declare function getDocPageSync(parser: Parser<"sync", unknown, unknown>, args?: readonly string[]): DocPage | undefined;
|
|
456
|
+
/**
|
|
457
|
+
* Generates a documentation page for any parser, returning a Promise.
|
|
458
|
+
*
|
|
459
|
+
* This function accepts parsers of any mode (sync or async) and always
|
|
460
|
+
* returns a Promise. Use this when working with parsers that may contain
|
|
461
|
+
* async value parsers.
|
|
462
|
+
*
|
|
463
|
+
* @param parser The parser to generate documentation for.
|
|
464
|
+
* @param args Optional array of command-line arguments for context.
|
|
465
|
+
* @returns A Promise of {@link DocPage} or `undefined`.
|
|
466
|
+
* @since 0.9.0
|
|
467
|
+
*/
|
|
468
|
+
declare function getDocPageAsync(parser: Parser<Mode, unknown, unknown>, args?: readonly string[]): Promise<DocPage | undefined>;
|
|
291
469
|
/**
|
|
292
470
|
* Generates a documentation page for a parser based on its current state after
|
|
293
471
|
* attempting to parse the provided arguments. This function is useful for
|
|
@@ -299,13 +477,16 @@ declare function suggest<T>(parser: Parser<T, unknown>, args: readonly [string,
|
|
|
299
477
|
* 3. Organizing fragments into entries and sections
|
|
300
478
|
* 4. Resolving command usage terms based on parsed arguments
|
|
301
479
|
*
|
|
480
|
+
* For sync parsers, returns the documentation page directly.
|
|
481
|
+
* For async parsers, returns a Promise of the documentation page.
|
|
482
|
+
*
|
|
302
483
|
* @param parser The parser to generate documentation for
|
|
303
484
|
* @param args Optional array of command-line arguments that have been parsed
|
|
304
485
|
* so far. Defaults to an empty array. This is used to determine
|
|
305
486
|
* the current parsing context and generate contextual documentation.
|
|
306
|
-
* @returns
|
|
307
|
-
*
|
|
308
|
-
* generated.
|
|
487
|
+
* @returns For sync parsers, returns a {@link DocPage} directly.
|
|
488
|
+
* For async parsers, returns a Promise of {@link DocPage}.
|
|
489
|
+
* Returns `undefined` if no documentation can be generated.
|
|
309
490
|
*
|
|
310
491
|
* @example
|
|
311
492
|
* ```typescript
|
|
@@ -314,13 +495,16 @@ declare function suggest<T>(parser: Parser<T, unknown>, args: readonly [string,
|
|
|
314
495
|
* port: option("-p", "--port", integer())
|
|
315
496
|
* });
|
|
316
497
|
*
|
|
317
|
-
* // Get documentation for
|
|
498
|
+
* // Get documentation for sync parser
|
|
318
499
|
* const rootDoc = getDocPage(parser);
|
|
319
500
|
*
|
|
320
|
-
* // Get documentation
|
|
321
|
-
* const
|
|
501
|
+
* // Get documentation for async parser
|
|
502
|
+
* const asyncDoc = await getDocPage(asyncParser);
|
|
322
503
|
* ```
|
|
504
|
+
* @since 0.9.0 Updated to support async parsers.
|
|
323
505
|
*/
|
|
324
|
-
declare function getDocPage(parser: Parser<unknown, unknown>, args?: readonly string[]): DocPage | undefined;
|
|
506
|
+
declare function getDocPage(parser: Parser<"sync", unknown, unknown>, args?: readonly string[]): DocPage | undefined;
|
|
507
|
+
declare function getDocPage(parser: Parser<"async", unknown, unknown>, args?: readonly string[]): Promise<DocPage | undefined>;
|
|
508
|
+
declare function getDocPage<M extends Mode>(parser: Parser<M, unknown, unknown>, args?: readonly string[]): ModeValue<M, DocPage | undefined>;
|
|
325
509
|
//#endregion
|
|
326
|
-
export { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, ConditionalErrorOptions, ConditionalOptions, DocState, DuplicateOptionError, FlagErrorOptions, FlagOptions, InferValue, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, MultipleErrorOptions, MultipleOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionOptions, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, PassThroughFormat, PassThroughOptions, Result, Suggestion, TupleOptions, WithDefaultError, WithDefaultOptions, argument, command, concat, conditional, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, passThrough, suggest, tuple, withDefault };
|
|
510
|
+
export { ArgumentErrorOptions, ArgumentOptions, CombineModes, CommandErrorOptions, CommandOptions, ConditionalErrorOptions, ConditionalOptions, DocState, DuplicateOptionError, FlagErrorOptions, FlagOptions, InferMode, InferValue, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionOptions, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, PassThroughFormat, PassThroughOptions, Result, Suggestion, TupleOptions, WithDefaultError, WithDefaultOptions, argument, command, concat, conditional, constant, flag, getDocPage, getDocPageAsync, getDocPageSync, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, parseAsync, parseSync, passThrough, suggest, suggestAsync, suggestSync, tuple, withDefault };
|