@optique/core 0.9.0-dev.217 → 0.9.0-dev.224
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 +602 -1332
- package/dist/constructs.d.cts +62 -182
- package/dist/constructs.d.ts +62 -182
- package/dist/constructs.js +602 -1332
- package/dist/facade.cjs +144 -188
- package/dist/facade.d.cts +3 -41
- package/dist/facade.d.ts +3 -41
- package/dist/facade.js +145 -187
- package/dist/index.cjs +1 -8
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/message.cjs +50 -0
- package/dist/message.d.cts +66 -1
- package/dist/message.d.ts +66 -1
- package/dist/message.js +50 -1
- package/dist/modifiers.cjs +79 -228
- package/dist/modifiers.d.cts +6 -11
- package/dist/modifiers.d.ts +6 -11
- package/dist/modifiers.js +79 -228
- package/dist/parser.cjs +33 -217
- package/dist/parser.d.cts +18 -202
- package/dist/parser.d.ts +18 -202
- package/dist/parser.js +34 -212
- package/dist/primitives.cjs +97 -242
- package/dist/primitives.d.cts +10 -14
- package/dist/primitives.d.ts +10 -14
- package/dist/primitives.js +97 -242
- package/dist/valueparser.cjs +3 -16
- package/dist/valueparser.d.cts +16 -29
- package/dist/valueparser.d.ts +16 -29
- package/dist/valueparser.js +4 -17
- package/package.json +1 -1
package/dist/parser.cjs
CHANGED
|
@@ -9,23 +9,17 @@ 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
|
-
*
|
|
16
12
|
* @template T The type of the value produced by the parser.
|
|
17
13
|
* @param parser The combined {@link Parser} to use for parsing the input
|
|
18
|
-
* arguments.
|
|
14
|
+
* arguments.
|
|
19
15
|
* @param args The array of command-line arguments to parse. Usually this is
|
|
20
16
|
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
21
17
|
* @returns A {@link Result} object indicating whether the parsing was
|
|
22
18
|
* successful or not. If successful, it contains the parsed value of
|
|
23
19
|
* type `T`. If not, it contains an error message describing the
|
|
24
20
|
* failure.
|
|
25
|
-
* @since 0.9.0 Renamed from the original `parse` function which now delegates
|
|
26
|
-
* to this for sync parsers.
|
|
27
21
|
*/
|
|
28
|
-
function
|
|
22
|
+
function parse(parser, args) {
|
|
29
23
|
let context = {
|
|
30
24
|
buffer: args,
|
|
31
25
|
optionsTerminated: false,
|
|
@@ -55,86 +49,11 @@ function parseSync(parser, args) {
|
|
|
55
49
|
};
|
|
56
50
|
}
|
|
57
51
|
/**
|
|
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
|
-
/**
|
|
128
52
|
* Generates command-line suggestions based on current parsing state.
|
|
129
53
|
* This function processes the input arguments up to the last argument,
|
|
130
54
|
* 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
|
-
*
|
|
135
55
|
* @template T The type of the value produced by the parser.
|
|
136
56
|
* @param parser The {@link Parser} to use for generating suggestions.
|
|
137
|
-
* Must be a synchronous parser.
|
|
138
57
|
* @param args The array of command-line arguments including the partial
|
|
139
58
|
* argument to complete. The last element is treated as
|
|
140
59
|
* the prefix for suggestions.
|
|
@@ -148,17 +67,16 @@ function parse(parser, args) {
|
|
|
148
67
|
* });
|
|
149
68
|
*
|
|
150
69
|
* // Get suggestions for options starting with "--"
|
|
151
|
-
* const suggestions =
|
|
70
|
+
* const suggestions = suggest(parser, ["--"]);
|
|
152
71
|
* // Returns: [{ text: "--verbose" }, { text: "--format" }]
|
|
153
72
|
*
|
|
154
73
|
* // Get suggestions after parsing some arguments
|
|
155
|
-
* const suggestions2 =
|
|
74
|
+
* const suggestions2 = suggest(parser, ["-v", "--format="]);
|
|
156
75
|
* // Returns: [{ text: "--format=json" }, { text: "--format=yaml" }]
|
|
157
76
|
* ```
|
|
158
77
|
* @since 0.6.0
|
|
159
|
-
* @since 0.9.0 Renamed from the original `suggest` function.
|
|
160
78
|
*/
|
|
161
|
-
function
|
|
79
|
+
function suggest(parser, args) {
|
|
162
80
|
const allButLast = args.slice(0, -1);
|
|
163
81
|
const prefix = args[args.length - 1];
|
|
164
82
|
let context = {
|
|
@@ -177,73 +95,6 @@ function suggestSync(parser, args) {
|
|
|
177
95
|
return Array.from(parser.suggest(context, prefix));
|
|
178
96
|
}
|
|
179
97
|
/**
|
|
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
|
-
/**
|
|
247
98
|
* Recursively searches for a command within nested exclusive usage terms.
|
|
248
99
|
* When the command is found, returns the expanded usage terms for that command.
|
|
249
100
|
*
|
|
@@ -264,44 +115,39 @@ function findCommandInExclusive(term, commandName) {
|
|
|
264
115
|
return null;
|
|
265
116
|
}
|
|
266
117
|
/**
|
|
267
|
-
* Generates a documentation page for a
|
|
118
|
+
* Generates a documentation page for a parser based on its current state after
|
|
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.
|
|
268
135
|
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
*
|
|
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.
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const parser = object({
|
|
139
|
+
* verbose: option("-v", "--verbose"),
|
|
140
|
+
* port: option("-p", "--port", integer())
|
|
141
|
+
* });
|
|
283
142
|
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
* async value parsers.
|
|
143
|
+
* // Get documentation for the root parser
|
|
144
|
+
* const rootDoc = getDocPage(parser);
|
|
287
145
|
*
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
* @since 0.9.0
|
|
146
|
+
* // Get documentation after parsing some arguments
|
|
147
|
+
* const contextDoc = getDocPage(parser, ["-v"]);
|
|
148
|
+
* ```
|
|
292
149
|
*/
|
|
293
|
-
function getDocPageAsync(parser, args = []) {
|
|
294
|
-
if (parser.$mode === "sync") return Promise.resolve(getDocPageSyncImpl(parser, args));
|
|
295
|
-
return getDocPageAsyncImpl(parser, args);
|
|
296
|
-
}
|
|
297
150
|
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) {
|
|
305
151
|
let context = {
|
|
306
152
|
buffer: args,
|
|
307
153
|
optionsTerminated: false,
|
|
@@ -313,30 +159,6 @@ function getDocPageSyncImpl(parser, args) {
|
|
|
313
159
|
if (!result.success) break;
|
|
314
160
|
context = result.next;
|
|
315
161
|
} 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) {
|
|
340
162
|
const { description, fragments, footer } = parser.getDocFragments({
|
|
341
163
|
kind: "available",
|
|
342
164
|
state: context.state
|
|
@@ -380,8 +202,6 @@ exports.conditional = require_constructs.conditional;
|
|
|
380
202
|
exports.constant = require_primitives.constant;
|
|
381
203
|
exports.flag = require_primitives.flag;
|
|
382
204
|
exports.getDocPage = getDocPage;
|
|
383
|
-
exports.getDocPageAsync = getDocPageAsync;
|
|
384
|
-
exports.getDocPageSync = getDocPageSync;
|
|
385
205
|
exports.group = require_constructs.group;
|
|
386
206
|
exports.longestMatch = require_constructs.longestMatch;
|
|
387
207
|
exports.map = require_modifiers.map;
|
|
@@ -392,11 +212,7 @@ exports.option = require_primitives.option;
|
|
|
392
212
|
exports.optional = require_modifiers.optional;
|
|
393
213
|
exports.or = require_constructs.or;
|
|
394
214
|
exports.parse = parse;
|
|
395
|
-
exports.parseAsync = parseAsync;
|
|
396
|
-
exports.parseSync = parseSync;
|
|
397
215
|
exports.passThrough = require_primitives.passThrough;
|
|
398
216
|
exports.suggest = suggest;
|
|
399
|
-
exports.suggestAsync = suggestAsync;
|
|
400
|
-
exports.suggestSync = suggestSync;
|
|
401
217
|
exports.tuple = require_constructs.tuple;
|
|
402
218
|
exports.withDefault = require_modifiers.withDefault;
|
package/dist/parser.d.cts
CHANGED
|
@@ -8,46 +8,6 @@ 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";
|
|
51
11
|
/**
|
|
52
12
|
* Represents the state passed to getDocFragments.
|
|
53
13
|
* Can be either the actual parser state or an explicit indicator
|
|
@@ -63,12 +23,10 @@ type DocState<TState> = {
|
|
|
63
23
|
};
|
|
64
24
|
/**
|
|
65
25
|
* Parser interface for command-line argument parsing.
|
|
66
|
-
* @template M The execution mode of the parser (`"sync"` or `"async"`).
|
|
67
26
|
* @template TValue The type of the value returned by the parser.
|
|
68
27
|
* @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.
|
|
70
28
|
*/
|
|
71
|
-
interface Parser<
|
|
29
|
+
interface Parser<TValue, TState> {
|
|
72
30
|
/**
|
|
73
31
|
* A type tag for the result value of this parser, used for type inference.
|
|
74
32
|
* Usually this is an empty array at runtime, but it does not matter
|
|
@@ -83,15 +41,6 @@ interface Parser<M extends Mode = "sync", TValue = unknown, TState = unknown> {
|
|
|
83
41
|
* @internal
|
|
84
42
|
*/
|
|
85
43
|
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;
|
|
95
44
|
/**
|
|
96
45
|
* The priority of this parser, which determines the order in which
|
|
97
46
|
* parsers are applied when multiple parsers are available. The greater
|
|
@@ -114,9 +63,8 @@ interface Parser<M extends Mode = "sync", TValue = unknown, TState = unknown> {
|
|
|
114
63
|
* @param context The context of the parser, which includes the input buffer
|
|
115
64
|
* and the current state.
|
|
116
65
|
* @returns A result object indicating success or failure.
|
|
117
|
-
* In async mode, returns a Promise that resolves to the result.
|
|
118
66
|
*/
|
|
119
|
-
parse(context: ParserContext<TState>):
|
|
67
|
+
parse(context: ParserContext<TState>): ParserResult<TState>;
|
|
120
68
|
/**
|
|
121
69
|
* Transforms a {@link TState} into a {@link TValue}, if applicable.
|
|
122
70
|
* If the transformation is not applicable, it should return
|
|
@@ -128,9 +76,8 @@ interface Parser<M extends Mode = "sync", TValue = unknown, TState = unknown> {
|
|
|
128
76
|
* the transformation. If successful, it should contain
|
|
129
77
|
* the parsed value of type {@link TValue}. If not applicable,
|
|
130
78
|
* it should return an error message.
|
|
131
|
-
* In async mode, returns a Promise that resolves to the result.
|
|
132
79
|
*/
|
|
133
|
-
complete(state: TState):
|
|
80
|
+
complete(state: TState): ValueParserResult<TValue>;
|
|
134
81
|
/**
|
|
135
82
|
* Generates next-step suggestions based on the current context
|
|
136
83
|
* and an optional prefix. This can be used to provide shell completion
|
|
@@ -141,10 +88,9 @@ interface Parser<M extends Mode = "sync", TValue = unknown, TState = unknown> {
|
|
|
141
88
|
* Can be an empty string if no prefix is provided.
|
|
142
89
|
* @returns An iterable of {@link Suggestion} objects, each containing
|
|
143
90
|
* a suggestion text and an optional description.
|
|
144
|
-
* In async mode, returns an AsyncIterable.
|
|
145
91
|
* @since 0.6.0
|
|
146
92
|
*/
|
|
147
|
-
suggest(context: ParserContext<TState>, prefix: string):
|
|
93
|
+
suggest(context: ParserContext<TState>, prefix: string): Iterable<Suggestion>;
|
|
148
94
|
/**
|
|
149
95
|
* Generates a documentation fragment for this parser, which can be used
|
|
150
96
|
* to describe the parser's usage, description, and default value.
|
|
@@ -271,13 +217,7 @@ type ParserResult<TState> = {
|
|
|
271
217
|
* Infers the result value type of a {@link Parser}.
|
|
272
218
|
* @template T The {@link Parser} to infer the result value type from.
|
|
273
219
|
*/
|
|
274
|
-
type InferValue<T extends Parser<
|
|
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"];
|
|
220
|
+
type InferValue<T extends Parser<unknown, unknown>> = T["$valueType"][number];
|
|
281
221
|
/**
|
|
282
222
|
* The result type of a whole parser operation, which can either be a successful
|
|
283
223
|
* result with a value of type `T`, or a failure with an error message.
|
|
@@ -308,73 +248,23 @@ type Result<T> = {
|
|
|
308
248
|
* Parses an array of command-line arguments using the provided combined parser.
|
|
309
249
|
* This function processes the input arguments, applying the parser to each
|
|
310
250
|
* 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
|
-
*
|
|
315
251
|
* @template T The type of the value produced by the parser.
|
|
316
252
|
* @param parser The combined {@link Parser} to use for parsing the input
|
|
317
|
-
* arguments.
|
|
253
|
+
* arguments.
|
|
318
254
|
* @param args The array of command-line arguments to parse. Usually this is
|
|
319
255
|
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
320
256
|
* @returns A {@link Result} object indicating whether the parsing was
|
|
321
257
|
* successful or not. If successful, it contains the parsed value of
|
|
322
258
|
* type `T`. If not, it contains an error message describing the
|
|
323
259
|
* 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.
|
|
365
260
|
*/
|
|
366
|
-
declare function parse<
|
|
261
|
+
declare function parse<T>(parser: Parser<T, unknown>, args: readonly string[]): Result<T>;
|
|
367
262
|
/**
|
|
368
263
|
* Generates command-line suggestions based on current parsing state.
|
|
369
264
|
* This function processes the input arguments up to the last argument,
|
|
370
265
|
* 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
|
-
*
|
|
375
266
|
* @template T The type of the value produced by the parser.
|
|
376
267
|
* @param parser The {@link Parser} to use for generating suggestions.
|
|
377
|
-
* Must be a synchronous parser.
|
|
378
268
|
* @param args The array of command-line arguments including the partial
|
|
379
269
|
* argument to complete. The last element is treated as
|
|
380
270
|
* the prefix for suggestions.
|
|
@@ -388,84 +278,16 @@ declare function parse<M extends Mode, T>(parser: Parser<M, T, unknown>, args: r
|
|
|
388
278
|
* });
|
|
389
279
|
*
|
|
390
280
|
* // Get suggestions for options starting with "--"
|
|
391
|
-
* const suggestions =
|
|
281
|
+
* const suggestions = suggest(parser, ["--"]);
|
|
392
282
|
* // Returns: [{ text: "--verbose" }, { text: "--format" }]
|
|
393
283
|
*
|
|
394
284
|
* // Get suggestions after parsing some arguments
|
|
395
|
-
* const suggestions2 =
|
|
285
|
+
* const suggestions2 = suggest(parser, ["-v", "--format="]);
|
|
396
286
|
* // Returns: [{ text: "--format=json" }, { text: "--format=yaml" }]
|
|
397
287
|
* ```
|
|
398
288
|
* @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
|
|
454
289
|
*/
|
|
455
|
-
declare function
|
|
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>;
|
|
290
|
+
declare function suggest<T>(parser: Parser<T, unknown>, args: readonly [string, ...readonly string[]]): readonly Suggestion[];
|
|
469
291
|
/**
|
|
470
292
|
* Generates a documentation page for a parser based on its current state after
|
|
471
293
|
* attempting to parse the provided arguments. This function is useful for
|
|
@@ -477,16 +299,13 @@ declare function getDocPageAsync(parser: Parser<Mode, unknown, unknown>, args?:
|
|
|
477
299
|
* 3. Organizing fragments into entries and sections
|
|
478
300
|
* 4. Resolving command usage terms based on parsed arguments
|
|
479
301
|
*
|
|
480
|
-
* For sync parsers, returns the documentation page directly.
|
|
481
|
-
* For async parsers, returns a Promise of the documentation page.
|
|
482
|
-
*
|
|
483
302
|
* @param parser The parser to generate documentation for
|
|
484
303
|
* @param args Optional array of command-line arguments that have been parsed
|
|
485
304
|
* so far. Defaults to an empty array. This is used to determine
|
|
486
305
|
* the current parsing context and generate contextual documentation.
|
|
487
|
-
* @returns
|
|
488
|
-
*
|
|
489
|
-
*
|
|
306
|
+
* @returns A {@link DocPage} containing usage information, sections, and
|
|
307
|
+
* optional description, or `undefined` if no documentation can be
|
|
308
|
+
* generated.
|
|
490
309
|
*
|
|
491
310
|
* @example
|
|
492
311
|
* ```typescript
|
|
@@ -495,16 +314,13 @@ declare function getDocPageAsync(parser: Parser<Mode, unknown, unknown>, args?:
|
|
|
495
314
|
* port: option("-p", "--port", integer())
|
|
496
315
|
* });
|
|
497
316
|
*
|
|
498
|
-
* // Get documentation for
|
|
317
|
+
* // Get documentation for the root parser
|
|
499
318
|
* const rootDoc = getDocPage(parser);
|
|
500
319
|
*
|
|
501
|
-
* // Get documentation
|
|
502
|
-
* const
|
|
320
|
+
* // Get documentation after parsing some arguments
|
|
321
|
+
* const contextDoc = getDocPage(parser, ["-v"]);
|
|
503
322
|
* ```
|
|
504
|
-
* @since 0.9.0 Updated to support async parsers.
|
|
505
323
|
*/
|
|
506
|
-
declare function getDocPage(parser: Parser<
|
|
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>;
|
|
324
|
+
declare function getDocPage(parser: Parser<unknown, unknown>, args?: readonly string[]): DocPage | undefined;
|
|
509
325
|
//#endregion
|
|
510
|
-
export { ArgumentErrorOptions, ArgumentOptions,
|
|
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 };
|