@optique/core 1.0.0-dev.908 → 1.0.0
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/annotation-state.cjs +425 -0
- package/dist/annotation-state.d.cts +24 -0
- package/dist/annotation-state.d.ts +24 -0
- package/dist/annotation-state.js +414 -0
- package/dist/annotations.cjs +2 -248
- package/dist/annotations.d.cts +2 -137
- package/dist/annotations.d.ts +2 -137
- package/dist/annotations.js +2 -238
- package/dist/completion.cjs +611 -100
- package/dist/completion.d.cts +1 -1
- package/dist/completion.d.ts +1 -1
- package/dist/completion.js +611 -100
- package/dist/constructs.cjs +3338 -827
- package/dist/constructs.d.cts +48 -7
- package/dist/constructs.d.ts +48 -7
- package/dist/constructs.js +3338 -827
- package/dist/context.cjs +0 -23
- package/dist/context.d.cts +119 -53
- package/dist/context.d.ts +119 -53
- package/dist/context.js +0 -22
- package/dist/dependency-metadata.cjs +139 -0
- package/dist/dependency-metadata.d.cts +112 -0
- package/dist/dependency-metadata.d.ts +112 -0
- package/dist/dependency-metadata.js +138 -0
- package/dist/dependency-runtime.cjs +698 -0
- package/dist/dependency-runtime.d.cts +149 -0
- package/dist/dependency-runtime.d.ts +149 -0
- package/dist/dependency-runtime.js +687 -0
- package/dist/dependency.cjs +7 -928
- package/dist/dependency.d.cts +2 -794
- package/dist/dependency.d.ts +2 -794
- package/dist/dependency.js +2 -899
- package/dist/displaywidth.cjs +44 -0
- package/dist/displaywidth.js +43 -0
- package/dist/doc.cjs +285 -23
- package/dist/doc.d.cts +57 -2
- package/dist/doc.d.ts +57 -2
- package/dist/doc.js +283 -25
- package/dist/execution-context.cjs +56 -0
- package/dist/execution-context.js +53 -0
- package/dist/extension.cjs +87 -0
- package/dist/extension.d.cts +97 -0
- package/dist/extension.d.ts +97 -0
- package/dist/extension.js +76 -0
- package/dist/facade.cjs +718 -523
- package/dist/facade.d.cts +87 -18
- package/dist/facade.d.ts +87 -18
- package/dist/facade.js +718 -523
- package/dist/index.cjs +14 -29
- package/dist/index.d.cts +10 -10
- package/dist/index.d.ts +10 -10
- package/dist/index.js +7 -7
- package/dist/input-trace.cjs +56 -0
- package/dist/input-trace.d.cts +77 -0
- package/dist/input-trace.d.ts +77 -0
- package/dist/input-trace.js +55 -0
- package/dist/internal/annotations.cjs +316 -0
- package/dist/internal/annotations.d.cts +140 -0
- package/dist/internal/annotations.d.ts +140 -0
- package/dist/internal/annotations.js +306 -0
- package/dist/internal/dependency.cjs +984 -0
- package/dist/internal/dependency.d.cts +539 -0
- package/dist/internal/dependency.d.ts +539 -0
- package/dist/internal/dependency.js +964 -0
- package/dist/{mode-dispatch.cjs → internal/mode-dispatch.cjs} +1 -3
- package/dist/{mode-dispatch.d.cts → internal/mode-dispatch.d.cts} +3 -7
- package/dist/{mode-dispatch.d.ts → internal/mode-dispatch.d.ts} +3 -7
- package/dist/{mode-dispatch.js → internal/mode-dispatch.js} +1 -3
- package/dist/internal/parser.cjs +728 -0
- package/dist/internal/parser.d.cts +947 -0
- package/dist/internal/parser.d.ts +947 -0
- package/dist/internal/parser.js +711 -0
- package/dist/message.cjs +84 -26
- package/dist/message.d.cts +49 -9
- package/dist/message.d.ts +49 -9
- package/dist/message.js +84 -27
- package/dist/modifiers.cjs +1023 -240
- package/dist/modifiers.d.cts +42 -1
- package/dist/modifiers.d.ts +42 -1
- package/dist/modifiers.js +1023 -240
- package/dist/parser.cjs +11 -463
- package/dist/parser.d.cts +3 -537
- package/dist/parser.d.ts +3 -537
- package/dist/parser.js +2 -433
- package/dist/phase2-seed.cjs +59 -0
- package/dist/phase2-seed.js +56 -0
- package/dist/primitives.cjs +557 -208
- package/dist/primitives.d.cts +10 -14
- package/dist/primitives.d.ts +10 -14
- package/dist/primitives.js +557 -208
- package/dist/program.cjs +5 -1
- package/dist/program.d.cts +5 -3
- package/dist/program.d.ts +5 -3
- package/dist/program.js +6 -1
- package/dist/suggestion.cjs +22 -8
- package/dist/suggestion.js +22 -8
- package/dist/usage-internals.cjs +3 -2
- package/dist/usage-internals.js +4 -2
- package/dist/usage.cjs +195 -40
- package/dist/usage.d.cts +92 -11
- package/dist/usage.d.ts +92 -11
- package/dist/usage.js +194 -41
- package/dist/validate.cjs +170 -0
- package/dist/validate.js +164 -0
- package/dist/valueparser.cjs +1278 -191
- package/dist/valueparser.d.cts +330 -20
- package/dist/valueparser.d.ts +330 -20
- package/dist/valueparser.js +1277 -192
- package/package.json +9 -9
package/dist/facade.d.cts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Message } from "./message.cjs";
|
|
2
2
|
import { HiddenVisibility, OptionName } from "./usage.cjs";
|
|
3
3
|
import { DocSection, ShowChoicesOptions, ShowDefaultOptions } from "./doc.cjs";
|
|
4
|
-
import { InferMode, InferValue, Mode, ModeValue, Parser } from "./parser.cjs";
|
|
4
|
+
import { InferMode, InferValue, Mode, ModeValue, Parser } from "./internal/parser.cjs";
|
|
5
5
|
import { ShellCompletion } from "./completion.cjs";
|
|
6
|
-
import { ParserValuePlaceholder, SourceContext } from "./context.cjs";
|
|
6
|
+
import { ParserValuePlaceholder, SourceContext, SourceContextRequest } from "./context.cjs";
|
|
7
7
|
import { Program } from "./program.cjs";
|
|
8
8
|
|
|
9
9
|
//#region src/facade.d.ts
|
|
@@ -272,6 +272,13 @@ interface RunOptions<THelp, TError> {
|
|
|
272
272
|
* @param options Configuration options for output formatting and callbacks.
|
|
273
273
|
* @returns The parsed result value, or the return value of `onHelp`/`onError`
|
|
274
274
|
* callbacks.
|
|
275
|
+
* @throws {TypeError} If `programName` (or `program.metadata.name`) is not
|
|
276
|
+
* a string, is empty, is whitespace-only, or contains control
|
|
277
|
+
* characters. Also thrown if `options.version.value` is not a
|
|
278
|
+
* non-empty string without ASCII control characters, or if any
|
|
279
|
+
* meta command/option name is empty, whitespace-only, contains
|
|
280
|
+
* whitespace or control characters, or (for option names) lacks a
|
|
281
|
+
* valid prefix (`--`, `-`, `/`, or `+`).
|
|
275
282
|
* @throws {RunParserError} When parsing fails and no `onError` callback is
|
|
276
283
|
* provided.
|
|
277
284
|
* @since 0.10.0 Added support for {@link Program} objects.
|
|
@@ -296,6 +303,8 @@ declare function runParser<TParser extends Parser<Mode, unknown, unknown>, THelp
|
|
|
296
303
|
* @param args The command-line arguments to parse.
|
|
297
304
|
* @param options Configuration options for customizing behavior.
|
|
298
305
|
* @returns The parsed result if successful.
|
|
306
|
+
* @throws {TypeError} If an async parser is passed at runtime. Use
|
|
307
|
+
* {@link runParser} or {@link runParserAsync} for async parsers.
|
|
299
308
|
* @since 0.9.0
|
|
300
309
|
*/
|
|
301
310
|
declare function runParserSync<TParser extends Parser<"sync", unknown, unknown>, THelp = void, TError = never>(parser: TParser, programName: string, args: readonly string[], options?: RunOptions<THelp, TError>): InferValue<TParser>;
|
|
@@ -367,24 +376,58 @@ interface RunWithOptions<THelp, TError> extends RunOptions<THelp, TError> {
|
|
|
367
376
|
* `process.argv.slice(2)` on Node.js/Bun or `Deno.args` on Deno.
|
|
368
377
|
*/
|
|
369
378
|
readonly args?: readonly string[];
|
|
379
|
+
/**
|
|
380
|
+
* Options to forward to source contexts. When contexts declare
|
|
381
|
+
* required options (via `$requiredOptions`), pass them here to
|
|
382
|
+
* avoid name collisions with runner-level options such as `args`,
|
|
383
|
+
* `help`, or `colors`.
|
|
384
|
+
*
|
|
385
|
+
* @since 1.0.0
|
|
386
|
+
*/
|
|
387
|
+
readonly contextOptions?: Record<string, unknown>;
|
|
370
388
|
}
|
|
389
|
+
/**
|
|
390
|
+
* When contexts require options, demands a `contextOptions` property
|
|
391
|
+
* typed to those requirements. When no context needs options
|
|
392
|
+
* (`void`, `unknown`, or `{}`), resolves to `unknown` (intersection no-op).
|
|
393
|
+
* When all context option keys are optional, `contextOptions` itself becomes
|
|
394
|
+
* optional so callers are not forced to pass an empty wrapper.
|
|
395
|
+
*
|
|
396
|
+
* @template TContexts The tuple/array type of source contexts.
|
|
397
|
+
* @template TValue The parser value type for placeholder substitution.
|
|
398
|
+
* @since 1.0.0
|
|
399
|
+
*/
|
|
400
|
+
type ContextOptionsParam<TContexts extends readonly SourceContext<unknown>[], TValue> = keyof ExtractRequiredOptions<TContexts, TValue> extends never ? unknown : Record<string, never> extends ExtractRequiredOptions<TContexts, TValue> ? {
|
|
401
|
+
readonly contextOptions?: ExtractRequiredOptions<TContexts, TValue>;
|
|
402
|
+
} : {
|
|
403
|
+
readonly contextOptions: ExtractRequiredOptions<TContexts, TValue>;
|
|
404
|
+
};
|
|
371
405
|
/**
|
|
372
406
|
* Runs a parser with multiple source contexts.
|
|
373
407
|
*
|
|
374
|
-
* This function automatically handles
|
|
375
|
-
* priority. Earlier contexts in the array override later ones.
|
|
408
|
+
* This function automatically handles single-pass and two-pass contexts with
|
|
409
|
+
* proper priority. Earlier contexts in the array override later ones.
|
|
376
410
|
*
|
|
377
411
|
* The function uses a smart two-phase approach:
|
|
378
412
|
*
|
|
379
|
-
* 1. *Phase 1*: Collect annotations from all contexts
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
*
|
|
384
|
-
*
|
|
413
|
+
* 1. *Phase 1*: Collect annotations from all contexts.
|
|
414
|
+
* 2. *First parse*: Parse with Phase 1 annotations. If that pass finishes
|
|
415
|
+
* successfully, its value becomes the phase-two input. If the parser
|
|
416
|
+
* reaches a usable intermediate state but still does not complete
|
|
417
|
+
* successfully, the runner extracts a best-effort seed from that state
|
|
418
|
+
* instead.
|
|
419
|
+
* 3. *Phase 2*: Call `getAnnotations({ phase: "phase2", parsed })` on all
|
|
420
|
+
* two-pass contexts with the first pass value. Deferred or otherwise
|
|
421
|
+
* unresolved fields in `parsed` may be `undefined`. Each two-pass
|
|
422
|
+
* context's phase-two return
|
|
423
|
+
* value replaces its own phase-one contribution for the final parse, so
|
|
424
|
+
* returning `{}` clears any annotations that context provided during
|
|
425
|
+
* phase 1. Single-pass contexts reuse their phase-one snapshot.
|
|
426
|
+
* 4. *Second parse*: Parse again with the merged phase-two annotations.
|
|
385
427
|
*
|
|
386
|
-
* If all contexts are
|
|
387
|
-
*
|
|
428
|
+
* If all contexts are single-pass, the second parse is skipped for
|
|
429
|
+
* optimization. Phase 2 is also skipped when the first pass does not yield
|
|
430
|
+
* any usable seed at all.
|
|
388
431
|
*
|
|
389
432
|
* @template TParser The parser type.
|
|
390
433
|
* @template THelp Return type when help is shown.
|
|
@@ -394,6 +437,13 @@ interface RunWithOptions<THelp, TError> extends RunOptions<THelp, TError> {
|
|
|
394
437
|
* @param contexts Source contexts to use (priority: earlier overrides later).
|
|
395
438
|
* @param options Run options including args, help, version, etc.
|
|
396
439
|
* @returns Promise that resolves to the parsed result.
|
|
440
|
+
* @throws {TypeError} If two or more contexts share the same
|
|
441
|
+
* {@link SourceContext.id}.
|
|
442
|
+
* @throws {TypeError} If any context omits `phase` or declares an invalid
|
|
443
|
+
* phase value.
|
|
444
|
+
* @throws {SuppressedError} If the runner throws and a context's disposal
|
|
445
|
+
* also throws. The original error is available via `.suppressed` and the
|
|
446
|
+
* disposal error via `.error`.
|
|
397
447
|
* @since 0.10.0
|
|
398
448
|
*
|
|
399
449
|
* @example
|
|
@@ -403,6 +453,7 @@ interface RunWithOptions<THelp, TError> extends RunOptions<THelp, TError> {
|
|
|
403
453
|
*
|
|
404
454
|
* const envContext: SourceContext = {
|
|
405
455
|
* id: Symbol.for("@myapp/env"),
|
|
456
|
+
* phase: "single-pass",
|
|
406
457
|
* getAnnotations() {
|
|
407
458
|
* return { [Symbol.for("@myapp/env")]: process.env };
|
|
408
459
|
* }
|
|
@@ -416,12 +467,16 @@ interface RunWithOptions<THelp, TError> extends RunOptions<THelp, TError> {
|
|
|
416
467
|
* );
|
|
417
468
|
* ```
|
|
418
469
|
*/
|
|
419
|
-
declare function runWith<TParser extends Parser<Mode, unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> &
|
|
470
|
+
declare function runWith<TParser extends Parser<Mode, unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> & ContextOptionsParam<TContexts, InferValue<TParser>>): Promise<InferValue<TParser>>;
|
|
420
471
|
/**
|
|
421
472
|
* Runs a synchronous parser with multiple source contexts.
|
|
422
473
|
*
|
|
423
474
|
* This is the sync-only variant of {@link runWith}. All contexts must return
|
|
424
|
-
* annotations synchronously (not Promises).
|
|
475
|
+
* annotations synchronously (not Promises). It uses the same two-phase
|
|
476
|
+
* best-effort seed extraction as {@link runWith} when two-pass contexts are
|
|
477
|
+
* present. In two-phase runs, each two-pass context's phase-two return value
|
|
478
|
+
* replaces that context's phase-one contribution for the final parse, so
|
|
479
|
+
* returning `{}` clears any annotations that context provided during phase 1.
|
|
425
480
|
*
|
|
426
481
|
* @template TParser The sync parser type.
|
|
427
482
|
* @template THelp Return type when help is shown.
|
|
@@ -431,11 +486,20 @@ declare function runWith<TParser extends Parser<Mode, unknown, unknown>, TContex
|
|
|
431
486
|
* @param contexts Source contexts to use (priority: earlier overrides later).
|
|
432
487
|
* @param options Run options including args, help, version, etc.
|
|
433
488
|
* @returns The parsed result.
|
|
434
|
-
* @throws
|
|
489
|
+
* @throws {TypeError} If an async parser is passed at runtime. Use
|
|
490
|
+
* {@link runWith} or {@link runWithAsync} for async parsers.
|
|
491
|
+
* @throws {TypeError} If two or more contexts share the same
|
|
492
|
+
* {@link SourceContext.id}.
|
|
493
|
+
* @throws {TypeError} If any context omits `phase` or declares an invalid
|
|
494
|
+
* phase value.
|
|
495
|
+
* @throws {TypeError} If any context returns a Promise or if a context's
|
|
435
496
|
* `[Symbol.asyncDispose]` returns a Promise.
|
|
497
|
+
* @throws {SuppressedError} If the runner throws and a context's disposal
|
|
498
|
+
* also throws. The original error is available via `.suppressed` and the
|
|
499
|
+
* disposal error via `.error`.
|
|
436
500
|
* @since 0.10.0
|
|
437
501
|
*/
|
|
438
|
-
declare function runWithSync<TParser extends Parser<"sync", unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> &
|
|
502
|
+
declare function runWithSync<TParser extends Parser<"sync", unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> & ContextOptionsParam<TContexts, InferValue<TParser>>): InferValue<TParser>;
|
|
439
503
|
/**
|
|
440
504
|
* Runs any parser asynchronously with multiple source contexts.
|
|
441
505
|
*
|
|
@@ -450,8 +514,13 @@ declare function runWithSync<TParser extends Parser<"sync", unknown, unknown>, T
|
|
|
450
514
|
* @param contexts Source contexts to use (priority: earlier overrides later).
|
|
451
515
|
* @param options Run options including args, help, version, etc.
|
|
452
516
|
* @returns Promise that resolves to the parsed result.
|
|
517
|
+
* @throws {TypeError} If two or more contexts share the same
|
|
518
|
+
* {@link SourceContext.id}.
|
|
519
|
+
* @throws {SuppressedError} If the runner throws and a context's disposal
|
|
520
|
+
* also throws. The original error is available via `.suppressed` and the
|
|
521
|
+
* disposal error via `.error`.
|
|
453
522
|
* @since 0.10.0
|
|
454
523
|
*/
|
|
455
|
-
declare function runWithAsync<TParser extends Parser<Mode, unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> &
|
|
524
|
+
declare function runWithAsync<TParser extends Parser<Mode, unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> & ContextOptionsParam<TContexts, InferValue<TParser>>): Promise<InferValue<TParser>>;
|
|
456
525
|
//#endregion
|
|
457
|
-
export { CommandSubConfig, ExtractRequiredOptions, OptionSubConfig, type ParserValuePlaceholder, RunOptions, RunParserError, RunWithOptions, type SourceContext, SubstituteParserValue, runParser, runParserAsync, runParserSync, runWith, runWithAsync, runWithSync };
|
|
526
|
+
export { CommandSubConfig, ContextOptionsParam, ExtractRequiredOptions, OptionSubConfig, type ParserValuePlaceholder, RunOptions, RunParserError, RunWithOptions, type SourceContext, type SourceContextRequest, SubstituteParserValue, runParser, runParserAsync, runParserSync, runWith, runWithAsync, runWithSync };
|
package/dist/facade.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Message } from "./message.js";
|
|
2
2
|
import { HiddenVisibility, OptionName } from "./usage.js";
|
|
3
3
|
import { DocSection, ShowChoicesOptions, ShowDefaultOptions } from "./doc.js";
|
|
4
|
-
import { InferMode, InferValue, Mode, ModeValue, Parser } from "./parser.js";
|
|
4
|
+
import { InferMode, InferValue, Mode, ModeValue, Parser } from "./internal/parser.js";
|
|
5
5
|
import { ShellCompletion } from "./completion.js";
|
|
6
|
-
import { ParserValuePlaceholder, SourceContext } from "./context.js";
|
|
6
|
+
import { ParserValuePlaceholder, SourceContext, SourceContextRequest } from "./context.js";
|
|
7
7
|
import { Program } from "./program.js";
|
|
8
8
|
|
|
9
9
|
//#region src/facade.d.ts
|
|
@@ -272,6 +272,13 @@ interface RunOptions<THelp, TError> {
|
|
|
272
272
|
* @param options Configuration options for output formatting and callbacks.
|
|
273
273
|
* @returns The parsed result value, or the return value of `onHelp`/`onError`
|
|
274
274
|
* callbacks.
|
|
275
|
+
* @throws {TypeError} If `programName` (or `program.metadata.name`) is not
|
|
276
|
+
* a string, is empty, is whitespace-only, or contains control
|
|
277
|
+
* characters. Also thrown if `options.version.value` is not a
|
|
278
|
+
* non-empty string without ASCII control characters, or if any
|
|
279
|
+
* meta command/option name is empty, whitespace-only, contains
|
|
280
|
+
* whitespace or control characters, or (for option names) lacks a
|
|
281
|
+
* valid prefix (`--`, `-`, `/`, or `+`).
|
|
275
282
|
* @throws {RunParserError} When parsing fails and no `onError` callback is
|
|
276
283
|
* provided.
|
|
277
284
|
* @since 0.10.0 Added support for {@link Program} objects.
|
|
@@ -296,6 +303,8 @@ declare function runParser<TParser extends Parser<Mode, unknown, unknown>, THelp
|
|
|
296
303
|
* @param args The command-line arguments to parse.
|
|
297
304
|
* @param options Configuration options for customizing behavior.
|
|
298
305
|
* @returns The parsed result if successful.
|
|
306
|
+
* @throws {TypeError} If an async parser is passed at runtime. Use
|
|
307
|
+
* {@link runParser} or {@link runParserAsync} for async parsers.
|
|
299
308
|
* @since 0.9.0
|
|
300
309
|
*/
|
|
301
310
|
declare function runParserSync<TParser extends Parser<"sync", unknown, unknown>, THelp = void, TError = never>(parser: TParser, programName: string, args: readonly string[], options?: RunOptions<THelp, TError>): InferValue<TParser>;
|
|
@@ -367,24 +376,58 @@ interface RunWithOptions<THelp, TError> extends RunOptions<THelp, TError> {
|
|
|
367
376
|
* `process.argv.slice(2)` on Node.js/Bun or `Deno.args` on Deno.
|
|
368
377
|
*/
|
|
369
378
|
readonly args?: readonly string[];
|
|
379
|
+
/**
|
|
380
|
+
* Options to forward to source contexts. When contexts declare
|
|
381
|
+
* required options (via `$requiredOptions`), pass them here to
|
|
382
|
+
* avoid name collisions with runner-level options such as `args`,
|
|
383
|
+
* `help`, or `colors`.
|
|
384
|
+
*
|
|
385
|
+
* @since 1.0.0
|
|
386
|
+
*/
|
|
387
|
+
readonly contextOptions?: Record<string, unknown>;
|
|
370
388
|
}
|
|
389
|
+
/**
|
|
390
|
+
* When contexts require options, demands a `contextOptions` property
|
|
391
|
+
* typed to those requirements. When no context needs options
|
|
392
|
+
* (`void`, `unknown`, or `{}`), resolves to `unknown` (intersection no-op).
|
|
393
|
+
* When all context option keys are optional, `contextOptions` itself becomes
|
|
394
|
+
* optional so callers are not forced to pass an empty wrapper.
|
|
395
|
+
*
|
|
396
|
+
* @template TContexts The tuple/array type of source contexts.
|
|
397
|
+
* @template TValue The parser value type for placeholder substitution.
|
|
398
|
+
* @since 1.0.0
|
|
399
|
+
*/
|
|
400
|
+
type ContextOptionsParam<TContexts extends readonly SourceContext<unknown>[], TValue> = keyof ExtractRequiredOptions<TContexts, TValue> extends never ? unknown : Record<string, never> extends ExtractRequiredOptions<TContexts, TValue> ? {
|
|
401
|
+
readonly contextOptions?: ExtractRequiredOptions<TContexts, TValue>;
|
|
402
|
+
} : {
|
|
403
|
+
readonly contextOptions: ExtractRequiredOptions<TContexts, TValue>;
|
|
404
|
+
};
|
|
371
405
|
/**
|
|
372
406
|
* Runs a parser with multiple source contexts.
|
|
373
407
|
*
|
|
374
|
-
* This function automatically handles
|
|
375
|
-
* priority. Earlier contexts in the array override later ones.
|
|
408
|
+
* This function automatically handles single-pass and two-pass contexts with
|
|
409
|
+
* proper priority. Earlier contexts in the array override later ones.
|
|
376
410
|
*
|
|
377
411
|
* The function uses a smart two-phase approach:
|
|
378
412
|
*
|
|
379
|
-
* 1. *Phase 1*: Collect annotations from all contexts
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
*
|
|
384
|
-
*
|
|
413
|
+
* 1. *Phase 1*: Collect annotations from all contexts.
|
|
414
|
+
* 2. *First parse*: Parse with Phase 1 annotations. If that pass finishes
|
|
415
|
+
* successfully, its value becomes the phase-two input. If the parser
|
|
416
|
+
* reaches a usable intermediate state but still does not complete
|
|
417
|
+
* successfully, the runner extracts a best-effort seed from that state
|
|
418
|
+
* instead.
|
|
419
|
+
* 3. *Phase 2*: Call `getAnnotations({ phase: "phase2", parsed })` on all
|
|
420
|
+
* two-pass contexts with the first pass value. Deferred or otherwise
|
|
421
|
+
* unresolved fields in `parsed` may be `undefined`. Each two-pass
|
|
422
|
+
* context's phase-two return
|
|
423
|
+
* value replaces its own phase-one contribution for the final parse, so
|
|
424
|
+
* returning `{}` clears any annotations that context provided during
|
|
425
|
+
* phase 1. Single-pass contexts reuse their phase-one snapshot.
|
|
426
|
+
* 4. *Second parse*: Parse again with the merged phase-two annotations.
|
|
385
427
|
*
|
|
386
|
-
* If all contexts are
|
|
387
|
-
*
|
|
428
|
+
* If all contexts are single-pass, the second parse is skipped for
|
|
429
|
+
* optimization. Phase 2 is also skipped when the first pass does not yield
|
|
430
|
+
* any usable seed at all.
|
|
388
431
|
*
|
|
389
432
|
* @template TParser The parser type.
|
|
390
433
|
* @template THelp Return type when help is shown.
|
|
@@ -394,6 +437,13 @@ interface RunWithOptions<THelp, TError> extends RunOptions<THelp, TError> {
|
|
|
394
437
|
* @param contexts Source contexts to use (priority: earlier overrides later).
|
|
395
438
|
* @param options Run options including args, help, version, etc.
|
|
396
439
|
* @returns Promise that resolves to the parsed result.
|
|
440
|
+
* @throws {TypeError} If two or more contexts share the same
|
|
441
|
+
* {@link SourceContext.id}.
|
|
442
|
+
* @throws {TypeError} If any context omits `phase` or declares an invalid
|
|
443
|
+
* phase value.
|
|
444
|
+
* @throws {SuppressedError} If the runner throws and a context's disposal
|
|
445
|
+
* also throws. The original error is available via `.suppressed` and the
|
|
446
|
+
* disposal error via `.error`.
|
|
397
447
|
* @since 0.10.0
|
|
398
448
|
*
|
|
399
449
|
* @example
|
|
@@ -403,6 +453,7 @@ interface RunWithOptions<THelp, TError> extends RunOptions<THelp, TError> {
|
|
|
403
453
|
*
|
|
404
454
|
* const envContext: SourceContext = {
|
|
405
455
|
* id: Symbol.for("@myapp/env"),
|
|
456
|
+
* phase: "single-pass",
|
|
406
457
|
* getAnnotations() {
|
|
407
458
|
* return { [Symbol.for("@myapp/env")]: process.env };
|
|
408
459
|
* }
|
|
@@ -416,12 +467,16 @@ interface RunWithOptions<THelp, TError> extends RunOptions<THelp, TError> {
|
|
|
416
467
|
* );
|
|
417
468
|
* ```
|
|
418
469
|
*/
|
|
419
|
-
declare function runWith<TParser extends Parser<Mode, unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> &
|
|
470
|
+
declare function runWith<TParser extends Parser<Mode, unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> & ContextOptionsParam<TContexts, InferValue<TParser>>): Promise<InferValue<TParser>>;
|
|
420
471
|
/**
|
|
421
472
|
* Runs a synchronous parser with multiple source contexts.
|
|
422
473
|
*
|
|
423
474
|
* This is the sync-only variant of {@link runWith}. All contexts must return
|
|
424
|
-
* annotations synchronously (not Promises).
|
|
475
|
+
* annotations synchronously (not Promises). It uses the same two-phase
|
|
476
|
+
* best-effort seed extraction as {@link runWith} when two-pass contexts are
|
|
477
|
+
* present. In two-phase runs, each two-pass context's phase-two return value
|
|
478
|
+
* replaces that context's phase-one contribution for the final parse, so
|
|
479
|
+
* returning `{}` clears any annotations that context provided during phase 1.
|
|
425
480
|
*
|
|
426
481
|
* @template TParser The sync parser type.
|
|
427
482
|
* @template THelp Return type when help is shown.
|
|
@@ -431,11 +486,20 @@ declare function runWith<TParser extends Parser<Mode, unknown, unknown>, TContex
|
|
|
431
486
|
* @param contexts Source contexts to use (priority: earlier overrides later).
|
|
432
487
|
* @param options Run options including args, help, version, etc.
|
|
433
488
|
* @returns The parsed result.
|
|
434
|
-
* @throws
|
|
489
|
+
* @throws {TypeError} If an async parser is passed at runtime. Use
|
|
490
|
+
* {@link runWith} or {@link runWithAsync} for async parsers.
|
|
491
|
+
* @throws {TypeError} If two or more contexts share the same
|
|
492
|
+
* {@link SourceContext.id}.
|
|
493
|
+
* @throws {TypeError} If any context omits `phase` or declares an invalid
|
|
494
|
+
* phase value.
|
|
495
|
+
* @throws {TypeError} If any context returns a Promise or if a context's
|
|
435
496
|
* `[Symbol.asyncDispose]` returns a Promise.
|
|
497
|
+
* @throws {SuppressedError} If the runner throws and a context's disposal
|
|
498
|
+
* also throws. The original error is available via `.suppressed` and the
|
|
499
|
+
* disposal error via `.error`.
|
|
436
500
|
* @since 0.10.0
|
|
437
501
|
*/
|
|
438
|
-
declare function runWithSync<TParser extends Parser<"sync", unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> &
|
|
502
|
+
declare function runWithSync<TParser extends Parser<"sync", unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> & ContextOptionsParam<TContexts, InferValue<TParser>>): InferValue<TParser>;
|
|
439
503
|
/**
|
|
440
504
|
* Runs any parser asynchronously with multiple source contexts.
|
|
441
505
|
*
|
|
@@ -450,8 +514,13 @@ declare function runWithSync<TParser extends Parser<"sync", unknown, unknown>, T
|
|
|
450
514
|
* @param contexts Source contexts to use (priority: earlier overrides later).
|
|
451
515
|
* @param options Run options including args, help, version, etc.
|
|
452
516
|
* @returns Promise that resolves to the parsed result.
|
|
517
|
+
* @throws {TypeError} If two or more contexts share the same
|
|
518
|
+
* {@link SourceContext.id}.
|
|
519
|
+
* @throws {SuppressedError} If the runner throws and a context's disposal
|
|
520
|
+
* also throws. The original error is available via `.suppressed` and the
|
|
521
|
+
* disposal error via `.error`.
|
|
453
522
|
* @since 0.10.0
|
|
454
523
|
*/
|
|
455
|
-
declare function runWithAsync<TParser extends Parser<Mode, unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> &
|
|
524
|
+
declare function runWithAsync<TParser extends Parser<Mode, unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> & ContextOptionsParam<TContexts, InferValue<TParser>>): Promise<InferValue<TParser>>;
|
|
456
525
|
//#endregion
|
|
457
|
-
export { CommandSubConfig, ExtractRequiredOptions, OptionSubConfig, type ParserValuePlaceholder, RunOptions, RunParserError, RunWithOptions, type SourceContext, SubstituteParserValue, runParser, runParserAsync, runParserSync, runWith, runWithAsync, runWithSync };
|
|
526
|
+
export { CommandSubConfig, ContextOptionsParam, ExtractRequiredOptions, OptionSubConfig, type ParserValuePlaceholder, RunOptions, RunParserError, RunWithOptions, type SourceContext, type SourceContextRequest, SubstituteParserValue, runParser, runParserAsync, runParserSync, runWith, runWithAsync, runWithSync };
|