@optique/core 0.9.0-dev.201 → 0.9.0-dev.202

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/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): Iterable<Suggestion>;
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.
260
326
  */
261
- declare function parse<T>(parser: Parser<T, unknown>, args: readonly string[]): Result<T>;
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
+ */
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,58 @@ 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 = suggest(parser, ["--"]);
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 = suggest(parser, ["-v", "--format="]);
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
289
441
  */
290
- declare function suggest<T>(parser: Parser<T, unknown>, args: readonly [string, ...readonly string[]]): readonly Suggestion[];
442
+ declare function suggest<M extends Mode, T>(parser: Parser<M, T, unknown>, args: readonly [string, ...readonly string[]]): ModeValue<M, readonly Suggestion[]>;
291
443
  /**
292
444
  * Generates a documentation page for a parser based on its current state after
293
445
  * attempting to parse the provided arguments. This function is useful for
@@ -321,6 +473,6 @@ declare function suggest<T>(parser: Parser<T, unknown>, args: readonly [string,
321
473
  * const contextDoc = getDocPage(parser, ["-v"]);
322
474
  * ```
323
475
  */
324
- declare function getDocPage(parser: Parser<unknown, unknown>, args?: readonly string[]): DocPage | undefined;
476
+ declare function getDocPage(parser: Parser<"sync", unknown, unknown>, args?: readonly string[]): DocPage | undefined;
325
477
  //#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 };
478
+ 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, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, parseAsync, parseSync, passThrough, suggest, suggestAsync, suggestSync, tuple, withDefault };
package/dist/parser.d.ts 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): Iterable<Suggestion>;
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.
260
326
  */
261
- declare function parse<T>(parser: Parser<T, unknown>, args: readonly string[]): Result<T>;
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
+ */
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,58 @@ 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 = suggest(parser, ["--"]);
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 = suggest(parser, ["-v", "--format="]);
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
289
441
  */
290
- declare function suggest<T>(parser: Parser<T, unknown>, args: readonly [string, ...readonly string[]]): readonly Suggestion[];
442
+ declare function suggest<M extends Mode, T>(parser: Parser<M, T, unknown>, args: readonly [string, ...readonly string[]]): ModeValue<M, readonly Suggestion[]>;
291
443
  /**
292
444
  * Generates a documentation page for a parser based on its current state after
293
445
  * attempting to parse the provided arguments. This function is useful for
@@ -321,6 +473,6 @@ declare function suggest<T>(parser: Parser<T, unknown>, args: readonly [string,
321
473
  * const contextDoc = getDocPage(parser, ["-v"]);
322
474
  * ```
323
475
  */
324
- declare function getDocPage(parser: Parser<unknown, unknown>, args?: readonly string[]): DocPage | undefined;
476
+ declare function getDocPage(parser: Parser<"sync", unknown, unknown>, args?: readonly string[]): DocPage | undefined;
325
477
  //#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 };
478
+ 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, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, parseAsync, parseSync, passThrough, suggest, suggestAsync, suggestSync, tuple, withDefault };