@optique/core 0.10.0-dev.291 → 0.10.0-dev.293
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 +71 -2
- package/dist/constructs.js +71 -2
- package/dist/dependency.cjs +528 -0
- package/dist/dependency.d.cts +617 -0
- package/dist/dependency.d.ts +617 -0
- package/dist/dependency.js +510 -0
- package/dist/facade.cjs +1 -1
- package/dist/facade.js +1 -1
- package/dist/index.cjs +20 -1
- package/dist/index.d.cts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +3 -2
- package/dist/parser.d.cts +3 -3
- package/dist/parser.d.ts +3 -3
- package/dist/primitives.cjs +60 -12
- package/dist/primitives.d.cts +8 -2
- package/dist/primitives.d.ts +8 -2
- package/dist/primitives.js +60 -12
- package/package.json +9 -1
package/dist/parser.d.cts
CHANGED
|
@@ -2,9 +2,9 @@ import { Message } from "./message.cjs";
|
|
|
2
2
|
import { Usage } from "./usage.cjs";
|
|
3
3
|
import { DocFragments, DocPage } from "./doc.cjs";
|
|
4
4
|
import { ValueParserResult } from "./valueparser.cjs";
|
|
5
|
-
import { MultipleErrorOptions, MultipleOptions, WithDefaultError, WithDefaultOptions, map, multiple, optional, withDefault } from "./modifiers.cjs";
|
|
6
|
-
import { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, FlagErrorOptions, FlagOptions, OptionErrorOptions, OptionOptions, PassThroughFormat, PassThroughOptions, argument, command, constant, flag, option, passThrough } from "./primitives.cjs";
|
|
7
5
|
import { ConditionalErrorOptions, ConditionalOptions, DuplicateOptionError, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OrErrorOptions, OrOptions, TupleOptions, concat, conditional, group, longestMatch, merge, object, or, tuple } from "./constructs.cjs";
|
|
6
|
+
import { MultipleErrorOptions, MultipleOptions, WithDefaultError, WithDefaultOptions, map, multiple, optional, withDefault } from "./modifiers.cjs";
|
|
7
|
+
import { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, FlagErrorOptions, FlagOptions, OptionErrorOptions, OptionOptions, OptionState, PassThroughFormat, PassThroughOptions, argument, command, constant, flag, option, passThrough } from "./primitives.cjs";
|
|
8
8
|
|
|
9
9
|
//#region src/parser.d.ts
|
|
10
10
|
|
|
@@ -507,4 +507,4 @@ declare function getDocPage(parser: Parser<"sync", unknown, unknown>, args?: rea
|
|
|
507
507
|
declare function getDocPage(parser: Parser<"async", unknown, unknown>, args?: readonly string[]): Promise<DocPage | undefined>;
|
|
508
508
|
declare function getDocPage<M extends Mode>(parser: Parser<M, unknown, unknown>, args?: readonly string[]): ModeValue<M, DocPage | undefined>;
|
|
509
509
|
//#endregion
|
|
510
|
-
export { ArgumentErrorOptions, ArgumentOptions, CombineModes, CommandErrorOptions, CommandOptions, ConditionalErrorOptions, ConditionalOptions, DocState, DuplicateOptionError, FlagErrorOptions, FlagOptions, InferMode, InferValue, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionOptions, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, PassThroughFormat, PassThroughOptions, Result, Suggestion, TupleOptions, WithDefaultError, WithDefaultOptions, argument, command, concat, conditional, constant, flag, getDocPage, getDocPageAsync, getDocPageSync, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, parseAsync, parseSync, passThrough, suggest, suggestAsync, suggestSync, tuple, withDefault };
|
|
510
|
+
export { ArgumentErrorOptions, ArgumentOptions, CombineModes, CommandErrorOptions, CommandOptions, ConditionalErrorOptions, ConditionalOptions, DocState, DuplicateOptionError, FlagErrorOptions, FlagOptions, InferMode, InferValue, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionOptions, OptionState, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, PassThroughFormat, PassThroughOptions, Result, Suggestion, TupleOptions, WithDefaultError, WithDefaultOptions, argument, command, concat, conditional, constant, flag, getDocPage, getDocPageAsync, getDocPageSync, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, parseAsync, parseSync, passThrough, suggest, suggestAsync, suggestSync, tuple, withDefault };
|
package/dist/parser.d.ts
CHANGED
|
@@ -2,9 +2,9 @@ import { Message } from "./message.js";
|
|
|
2
2
|
import { Usage } from "./usage.js";
|
|
3
3
|
import { DocFragments, DocPage } from "./doc.js";
|
|
4
4
|
import { ValueParserResult } from "./valueparser.js";
|
|
5
|
-
import { MultipleErrorOptions, MultipleOptions, WithDefaultError, WithDefaultOptions, map, multiple, optional, withDefault } from "./modifiers.js";
|
|
6
|
-
import { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, FlagErrorOptions, FlagOptions, OptionErrorOptions, OptionOptions, PassThroughFormat, PassThroughOptions, argument, command, constant, flag, option, passThrough } from "./primitives.js";
|
|
7
5
|
import { ConditionalErrorOptions, ConditionalOptions, DuplicateOptionError, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OrErrorOptions, OrOptions, TupleOptions, concat, conditional, group, longestMatch, merge, object, or, tuple } from "./constructs.js";
|
|
6
|
+
import { MultipleErrorOptions, MultipleOptions, WithDefaultError, WithDefaultOptions, map, multiple, optional, withDefault } from "./modifiers.js";
|
|
7
|
+
import { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, FlagErrorOptions, FlagOptions, OptionErrorOptions, OptionOptions, OptionState, PassThroughFormat, PassThroughOptions, argument, command, constant, flag, option, passThrough } from "./primitives.js";
|
|
8
8
|
|
|
9
9
|
//#region src/parser.d.ts
|
|
10
10
|
|
|
@@ -507,4 +507,4 @@ declare function getDocPage(parser: Parser<"sync", unknown, unknown>, args?: rea
|
|
|
507
507
|
declare function getDocPage(parser: Parser<"async", unknown, unknown>, args?: readonly string[]): Promise<DocPage | undefined>;
|
|
508
508
|
declare function getDocPage<M extends Mode>(parser: Parser<M, unknown, unknown>, args?: readonly string[]): ModeValue<M, DocPage | undefined>;
|
|
509
509
|
//#endregion
|
|
510
|
-
export { ArgumentErrorOptions, ArgumentOptions, CombineModes, CommandErrorOptions, CommandOptions, ConditionalErrorOptions, ConditionalOptions, DocState, DuplicateOptionError, FlagErrorOptions, FlagOptions, InferMode, InferValue, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionOptions, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, PassThroughFormat, PassThroughOptions, Result, Suggestion, TupleOptions, WithDefaultError, WithDefaultOptions, argument, command, concat, conditional, constant, flag, getDocPage, getDocPageAsync, getDocPageSync, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, parseAsync, parseSync, passThrough, suggest, suggestAsync, suggestSync, tuple, withDefault };
|
|
510
|
+
export { ArgumentErrorOptions, ArgumentOptions, CombineModes, CommandErrorOptions, CommandOptions, ConditionalErrorOptions, ConditionalOptions, DocState, DuplicateOptionError, FlagErrorOptions, FlagOptions, InferMode, InferValue, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionOptions, OptionState, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, PassThroughFormat, PassThroughOptions, Result, Suggestion, TupleOptions, WithDefaultError, WithDefaultOptions, argument, command, concat, conditional, constant, flag, getDocPage, getDocPageAsync, getDocPageSync, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, parseAsync, parseSync, passThrough, suggest, suggestAsync, suggestSync, tuple, withDefault };
|
package/dist/primitives.cjs
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
const require_message = require('./message.cjs');
|
|
2
|
+
const require_dependency = require('./dependency.cjs');
|
|
2
3
|
const require_usage = require('./usage.cjs');
|
|
3
4
|
const require_suggestion = require('./suggestion.cjs');
|
|
4
5
|
const require_valueparser = require('./valueparser.cjs');
|
|
5
6
|
|
|
6
7
|
//#region src/primitives.ts
|
|
7
8
|
/**
|
|
9
|
+
* Helper function to create the appropriate state for an option value.
|
|
10
|
+
* - If the value parser is a DerivedValueParser, wraps the result in a DeferredParseState
|
|
11
|
+
* to allow later resolution with actual dependency values.
|
|
12
|
+
* - If the value parser is a DependencySource, wraps the result in a DependencySourceState
|
|
13
|
+
* so that it can be matched with DeferredParseState during resolution.
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
function createOptionParseState(rawInput, valueParser, parseResult) {
|
|
17
|
+
if (require_dependency.isDerivedValueParser(valueParser)) return require_dependency.createDeferredParseState(rawInput, valueParser, parseResult);
|
|
18
|
+
if (require_dependency.isDependencySource(valueParser)) return require_dependency.createDependencySourceState(parseResult, valueParser[require_dependency.DependencyId]);
|
|
19
|
+
return parseResult;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
8
22
|
* Creates a parser that always succeeds without consuming any input and
|
|
9
23
|
* produces a constant value of the type {@link T}.
|
|
10
24
|
* @template T The type of the constant value produced by the parser.
|
|
@@ -237,12 +251,13 @@ function option(...args) {
|
|
|
237
251
|
consumed: 1,
|
|
238
252
|
error: require_message.message`Option ${require_message.optionName(context.buffer[0])} requires a value, but got no value.`
|
|
239
253
|
};
|
|
240
|
-
const
|
|
254
|
+
const rawInput = context.buffer[1];
|
|
255
|
+
const parseResultOrPromise = valueParser.parse(rawInput);
|
|
241
256
|
if (isAsync) return parseResultOrPromise.then((parseResult) => ({
|
|
242
257
|
success: true,
|
|
243
258
|
next: {
|
|
244
259
|
...context,
|
|
245
|
-
state: parseResult,
|
|
260
|
+
state: createOptionParseState(rawInput, valueParser, parseResult),
|
|
246
261
|
buffer: context.buffer.slice(2)
|
|
247
262
|
},
|
|
248
263
|
consumed: context.buffer.slice(0, 2)
|
|
@@ -251,7 +266,7 @@ function option(...args) {
|
|
|
251
266
|
success: true,
|
|
252
267
|
next: {
|
|
253
268
|
...context,
|
|
254
|
-
state: parseResultOrPromise,
|
|
269
|
+
state: createOptionParseState(rawInput, valueParser, parseResultOrPromise),
|
|
255
270
|
buffer: context.buffer.slice(2)
|
|
256
271
|
},
|
|
257
272
|
consumed: context.buffer.slice(0, 2)
|
|
@@ -265,18 +280,18 @@ function option(...args) {
|
|
|
265
280
|
consumed: 1,
|
|
266
281
|
error: options.errors?.duplicate ? typeof options.errors.duplicate === "function" ? options.errors.duplicate(prefix) : options.errors.duplicate : require_message.message`${require_message.optionName(prefix)} cannot be used multiple times.`
|
|
267
282
|
};
|
|
268
|
-
const
|
|
283
|
+
const rawInput = context.buffer[0].slice(prefix.length);
|
|
269
284
|
if (valueParser == null) return {
|
|
270
285
|
success: false,
|
|
271
286
|
consumed: 1,
|
|
272
|
-
error: options.errors?.unexpectedValue ? typeof options.errors.unexpectedValue === "function" ? options.errors.unexpectedValue(
|
|
287
|
+
error: options.errors?.unexpectedValue ? typeof options.errors.unexpectedValue === "function" ? options.errors.unexpectedValue(rawInput) : options.errors.unexpectedValue : require_message.message`Option ${require_message.optionName(prefix)} is a Boolean flag, but got a value: ${rawInput}.`
|
|
273
288
|
};
|
|
274
|
-
const parseResultOrPromise = valueParser.parse(
|
|
289
|
+
const parseResultOrPromise = valueParser.parse(rawInput);
|
|
275
290
|
if (isAsync) return parseResultOrPromise.then((parseResult) => ({
|
|
276
291
|
success: true,
|
|
277
292
|
next: {
|
|
278
293
|
...context,
|
|
279
|
-
state: parseResult,
|
|
294
|
+
state: createOptionParseState(rawInput, valueParser, parseResult),
|
|
280
295
|
buffer: context.buffer.slice(1)
|
|
281
296
|
},
|
|
282
297
|
consumed: context.buffer.slice(0, 1)
|
|
@@ -285,7 +300,7 @@ function option(...args) {
|
|
|
285
300
|
success: true,
|
|
286
301
|
next: {
|
|
287
302
|
...context,
|
|
288
|
-
state: parseResultOrPromise,
|
|
303
|
+
state: createOptionParseState(rawInput, valueParser, parseResultOrPromise),
|
|
289
304
|
buffer: context.buffer.slice(1)
|
|
290
305
|
},
|
|
291
306
|
consumed: context.buffer.slice(0, 1)
|
|
@@ -341,6 +356,22 @@ function option(...args) {
|
|
|
341
356
|
success: false,
|
|
342
357
|
error: options.errors?.missing ? typeof options.errors.missing === "function" ? options.errors.missing(optionNames$1) : options.errors.missing : require_message.message`Missing option ${require_message.optionNames(optionNames$1)}.`
|
|
343
358
|
};
|
|
359
|
+
if (require_dependency.isDeferredParseState(state)) {
|
|
360
|
+
const preliminaryResult = state.preliminaryResult;
|
|
361
|
+
if (preliminaryResult.success) return preliminaryResult;
|
|
362
|
+
return {
|
|
363
|
+
success: false,
|
|
364
|
+
error: options.errors?.invalidValue ? typeof options.errors.invalidValue === "function" ? options.errors.invalidValue(preliminaryResult.error) : options.errors.invalidValue : require_message.message`${require_message.optionNames(optionNames$1)}: ${preliminaryResult.error}`
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
if (require_dependency.isDependencySourceState(state)) {
|
|
368
|
+
const result$1 = state.result;
|
|
369
|
+
if (result$1.success) return result$1;
|
|
370
|
+
return {
|
|
371
|
+
success: false,
|
|
372
|
+
error: options.errors?.invalidValue ? typeof options.errors.invalidValue === "function" ? options.errors.invalidValue(result$1.error) : options.errors.invalidValue : require_message.message`${require_message.optionNames(optionNames$1)}: ${result$1.error}`
|
|
373
|
+
};
|
|
374
|
+
}
|
|
344
375
|
if (state.success) return state;
|
|
345
376
|
return {
|
|
346
377
|
success: false,
|
|
@@ -623,13 +654,14 @@ function argument(valueParser, options = {}) {
|
|
|
623
654
|
consumed: i,
|
|
624
655
|
error: options.errors?.multiple ? typeof options.errors.multiple === "function" ? options.errors.multiple(valueParser.metavar) : options.errors.multiple : require_message.message`The argument ${require_message.metavar(valueParser.metavar)} cannot be used multiple times.`
|
|
625
656
|
};
|
|
626
|
-
const
|
|
657
|
+
const rawInput = context.buffer[i];
|
|
658
|
+
const parseResultOrPromise = valueParser.parse(rawInput);
|
|
627
659
|
if (isAsync) return parseResultOrPromise.then((parseResult) => ({
|
|
628
660
|
success: true,
|
|
629
661
|
next: {
|
|
630
662
|
...context,
|
|
631
663
|
buffer: context.buffer.slice(i + 1),
|
|
632
|
-
state: parseResult,
|
|
664
|
+
state: createOptionParseState(rawInput, valueParser, parseResult),
|
|
633
665
|
optionsTerminated
|
|
634
666
|
},
|
|
635
667
|
consumed: context.buffer.slice(0, i + 1)
|
|
@@ -639,7 +671,7 @@ function argument(valueParser, options = {}) {
|
|
|
639
671
|
next: {
|
|
640
672
|
...context,
|
|
641
673
|
buffer: context.buffer.slice(i + 1),
|
|
642
|
-
state: parseResultOrPromise,
|
|
674
|
+
state: createOptionParseState(rawInput, valueParser, parseResultOrPromise),
|
|
643
675
|
optionsTerminated
|
|
644
676
|
},
|
|
645
677
|
consumed: context.buffer.slice(0, i + 1)
|
|
@@ -650,7 +682,23 @@ function argument(valueParser, options = {}) {
|
|
|
650
682
|
success: false,
|
|
651
683
|
error: options.errors?.endOfInput ?? require_message.message`Expected a ${require_message.metavar(valueParser.metavar)}, but too few arguments.`
|
|
652
684
|
};
|
|
653
|
-
|
|
685
|
+
if (require_dependency.isDeferredParseState(state)) {
|
|
686
|
+
const preliminaryResult = state.preliminaryResult;
|
|
687
|
+
if (preliminaryResult.success) return preliminaryResult;
|
|
688
|
+
return {
|
|
689
|
+
success: false,
|
|
690
|
+
error: options.errors?.invalidValue ? typeof options.errors.invalidValue === "function" ? options.errors.invalidValue(preliminaryResult.error) : options.errors.invalidValue : require_message.message`${require_message.metavar(valueParser.metavar)}: ${preliminaryResult.error}`
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
if (require_dependency.isDependencySourceState(state)) {
|
|
694
|
+
const result$1 = state.result;
|
|
695
|
+
if (result$1.success) return result$1;
|
|
696
|
+
return {
|
|
697
|
+
success: false,
|
|
698
|
+
error: options.errors?.invalidValue ? typeof options.errors.invalidValue === "function" ? options.errors.invalidValue(result$1.error) : options.errors.invalidValue : require_message.message`${require_message.metavar(valueParser.metavar)}: ${result$1.error}`
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
if (state.success) return state;
|
|
654
702
|
return {
|
|
655
703
|
success: false,
|
|
656
704
|
error: options.errors?.invalidValue ? typeof options.errors.invalidValue === "function" ? options.errors.invalidValue(state.error) : options.errors.invalidValue : require_message.message`${require_message.metavar(valueParser.metavar)}: ${state.error}`
|
package/dist/primitives.d.cts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { Message } from "./message.cjs";
|
|
2
2
|
import { OptionName } from "./usage.cjs";
|
|
3
3
|
import { ValueParser, ValueParserResult } from "./valueparser.cjs";
|
|
4
|
+
import { DeferredParseState } from "./dependency.cjs";
|
|
4
5
|
import { Mode, Parser } from "./parser.cjs";
|
|
5
6
|
|
|
6
7
|
//#region src/primitives.d.ts
|
|
7
|
-
|
|
8
|
+
/**
|
|
9
|
+
* State type for options that may use deferred parsing (DerivedValueParser).
|
|
10
|
+
* This extends the normal ValueParserResult to also support DeferredParseState.
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
type OptionState<T> = ValueParserResult<T> | DeferredParseState<T> | undefined;
|
|
8
14
|
/**
|
|
9
15
|
* Creates a parser that always succeeds without consuming any input and
|
|
10
16
|
* produces a constant value of the type {@link T}.
|
|
@@ -423,4 +429,4 @@ interface PassThroughOptions {
|
|
|
423
429
|
*/
|
|
424
430
|
declare function passThrough(options?: PassThroughOptions): Parser<"sync", readonly string[], readonly string[]>;
|
|
425
431
|
//#endregion
|
|
426
|
-
export { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, FlagErrorOptions, FlagOptions, OptionErrorOptions, OptionOptions, PassThroughFormat, PassThroughOptions, argument, command, constant, flag, option, passThrough };
|
|
432
|
+
export { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, FlagErrorOptions, FlagOptions, OptionErrorOptions, OptionOptions, OptionState, PassThroughFormat, PassThroughOptions, argument, command, constant, flag, option, passThrough };
|
package/dist/primitives.d.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { Message } from "./message.js";
|
|
2
2
|
import { OptionName } from "./usage.js";
|
|
3
3
|
import { ValueParser, ValueParserResult } from "./valueparser.js";
|
|
4
|
+
import { DeferredParseState } from "./dependency.js";
|
|
4
5
|
import { Mode, Parser } from "./parser.js";
|
|
5
6
|
|
|
6
7
|
//#region src/primitives.d.ts
|
|
7
|
-
|
|
8
|
+
/**
|
|
9
|
+
* State type for options that may use deferred parsing (DerivedValueParser).
|
|
10
|
+
* This extends the normal ValueParserResult to also support DeferredParseState.
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
type OptionState<T> = ValueParserResult<T> | DeferredParseState<T> | undefined;
|
|
8
14
|
/**
|
|
9
15
|
* Creates a parser that always succeeds without consuming any input and
|
|
10
16
|
* produces a constant value of the type {@link T}.
|
|
@@ -423,4 +429,4 @@ interface PassThroughOptions {
|
|
|
423
429
|
*/
|
|
424
430
|
declare function passThrough(options?: PassThroughOptions): Parser<"sync", readonly string[], readonly string[]>;
|
|
425
431
|
//#endregion
|
|
426
|
-
export { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, FlagErrorOptions, FlagOptions, OptionErrorOptions, OptionOptions, PassThroughFormat, PassThroughOptions, argument, command, constant, flag, option, passThrough };
|
|
432
|
+
export { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, FlagErrorOptions, FlagOptions, OptionErrorOptions, OptionOptions, OptionState, PassThroughFormat, PassThroughOptions, argument, command, constant, flag, option, passThrough };
|
package/dist/primitives.js
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
import { message, metavar, optionName, optionNames } from "./message.js";
|
|
2
|
+
import { DependencyId, createDeferredParseState, createDependencySourceState, isDeferredParseState, isDependencySource, isDependencySourceState, isDerivedValueParser } from "./dependency.js";
|
|
2
3
|
import { extractCommandNames, extractOptionNames } from "./usage.js";
|
|
3
4
|
import { DEFAULT_FIND_SIMILAR_OPTIONS, createErrorWithSuggestions, findSimilar } from "./suggestion.js";
|
|
4
5
|
import { isValueParser } from "./valueparser.js";
|
|
5
6
|
|
|
6
7
|
//#region src/primitives.ts
|
|
7
8
|
/**
|
|
9
|
+
* Helper function to create the appropriate state for an option value.
|
|
10
|
+
* - If the value parser is a DerivedValueParser, wraps the result in a DeferredParseState
|
|
11
|
+
* to allow later resolution with actual dependency values.
|
|
12
|
+
* - If the value parser is a DependencySource, wraps the result in a DependencySourceState
|
|
13
|
+
* so that it can be matched with DeferredParseState during resolution.
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
function createOptionParseState(rawInput, valueParser, parseResult) {
|
|
17
|
+
if (isDerivedValueParser(valueParser)) return createDeferredParseState(rawInput, valueParser, parseResult);
|
|
18
|
+
if (isDependencySource(valueParser)) return createDependencySourceState(parseResult, valueParser[DependencyId]);
|
|
19
|
+
return parseResult;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
8
22
|
* Creates a parser that always succeeds without consuming any input and
|
|
9
23
|
* produces a constant value of the type {@link T}.
|
|
10
24
|
* @template T The type of the constant value produced by the parser.
|
|
@@ -237,12 +251,13 @@ function option(...args) {
|
|
|
237
251
|
consumed: 1,
|
|
238
252
|
error: message`Option ${optionName(context.buffer[0])} requires a value, but got no value.`
|
|
239
253
|
};
|
|
240
|
-
const
|
|
254
|
+
const rawInput = context.buffer[1];
|
|
255
|
+
const parseResultOrPromise = valueParser.parse(rawInput);
|
|
241
256
|
if (isAsync) return parseResultOrPromise.then((parseResult) => ({
|
|
242
257
|
success: true,
|
|
243
258
|
next: {
|
|
244
259
|
...context,
|
|
245
|
-
state: parseResult,
|
|
260
|
+
state: createOptionParseState(rawInput, valueParser, parseResult),
|
|
246
261
|
buffer: context.buffer.slice(2)
|
|
247
262
|
},
|
|
248
263
|
consumed: context.buffer.slice(0, 2)
|
|
@@ -251,7 +266,7 @@ function option(...args) {
|
|
|
251
266
|
success: true,
|
|
252
267
|
next: {
|
|
253
268
|
...context,
|
|
254
|
-
state: parseResultOrPromise,
|
|
269
|
+
state: createOptionParseState(rawInput, valueParser, parseResultOrPromise),
|
|
255
270
|
buffer: context.buffer.slice(2)
|
|
256
271
|
},
|
|
257
272
|
consumed: context.buffer.slice(0, 2)
|
|
@@ -265,18 +280,18 @@ function option(...args) {
|
|
|
265
280
|
consumed: 1,
|
|
266
281
|
error: options.errors?.duplicate ? typeof options.errors.duplicate === "function" ? options.errors.duplicate(prefix) : options.errors.duplicate : message`${optionName(prefix)} cannot be used multiple times.`
|
|
267
282
|
};
|
|
268
|
-
const
|
|
283
|
+
const rawInput = context.buffer[0].slice(prefix.length);
|
|
269
284
|
if (valueParser == null) return {
|
|
270
285
|
success: false,
|
|
271
286
|
consumed: 1,
|
|
272
|
-
error: options.errors?.unexpectedValue ? typeof options.errors.unexpectedValue === "function" ? options.errors.unexpectedValue(
|
|
287
|
+
error: options.errors?.unexpectedValue ? typeof options.errors.unexpectedValue === "function" ? options.errors.unexpectedValue(rawInput) : options.errors.unexpectedValue : message`Option ${optionName(prefix)} is a Boolean flag, but got a value: ${rawInput}.`
|
|
273
288
|
};
|
|
274
|
-
const parseResultOrPromise = valueParser.parse(
|
|
289
|
+
const parseResultOrPromise = valueParser.parse(rawInput);
|
|
275
290
|
if (isAsync) return parseResultOrPromise.then((parseResult) => ({
|
|
276
291
|
success: true,
|
|
277
292
|
next: {
|
|
278
293
|
...context,
|
|
279
|
-
state: parseResult,
|
|
294
|
+
state: createOptionParseState(rawInput, valueParser, parseResult),
|
|
280
295
|
buffer: context.buffer.slice(1)
|
|
281
296
|
},
|
|
282
297
|
consumed: context.buffer.slice(0, 1)
|
|
@@ -285,7 +300,7 @@ function option(...args) {
|
|
|
285
300
|
success: true,
|
|
286
301
|
next: {
|
|
287
302
|
...context,
|
|
288
|
-
state: parseResultOrPromise,
|
|
303
|
+
state: createOptionParseState(rawInput, valueParser, parseResultOrPromise),
|
|
289
304
|
buffer: context.buffer.slice(1)
|
|
290
305
|
},
|
|
291
306
|
consumed: context.buffer.slice(0, 1)
|
|
@@ -341,6 +356,22 @@ function option(...args) {
|
|
|
341
356
|
success: false,
|
|
342
357
|
error: options.errors?.missing ? typeof options.errors.missing === "function" ? options.errors.missing(optionNames$1) : options.errors.missing : message`Missing option ${optionNames(optionNames$1)}.`
|
|
343
358
|
};
|
|
359
|
+
if (isDeferredParseState(state)) {
|
|
360
|
+
const preliminaryResult = state.preliminaryResult;
|
|
361
|
+
if (preliminaryResult.success) return preliminaryResult;
|
|
362
|
+
return {
|
|
363
|
+
success: false,
|
|
364
|
+
error: options.errors?.invalidValue ? typeof options.errors.invalidValue === "function" ? options.errors.invalidValue(preliminaryResult.error) : options.errors.invalidValue : message`${optionNames(optionNames$1)}: ${preliminaryResult.error}`
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
if (isDependencySourceState(state)) {
|
|
368
|
+
const result$1 = state.result;
|
|
369
|
+
if (result$1.success) return result$1;
|
|
370
|
+
return {
|
|
371
|
+
success: false,
|
|
372
|
+
error: options.errors?.invalidValue ? typeof options.errors.invalidValue === "function" ? options.errors.invalidValue(result$1.error) : options.errors.invalidValue : message`${optionNames(optionNames$1)}: ${result$1.error}`
|
|
373
|
+
};
|
|
374
|
+
}
|
|
344
375
|
if (state.success) return state;
|
|
345
376
|
return {
|
|
346
377
|
success: false,
|
|
@@ -623,13 +654,14 @@ function argument(valueParser, options = {}) {
|
|
|
623
654
|
consumed: i,
|
|
624
655
|
error: options.errors?.multiple ? typeof options.errors.multiple === "function" ? options.errors.multiple(valueParser.metavar) : options.errors.multiple : message`The argument ${metavar(valueParser.metavar)} cannot be used multiple times.`
|
|
625
656
|
};
|
|
626
|
-
const
|
|
657
|
+
const rawInput = context.buffer[i];
|
|
658
|
+
const parseResultOrPromise = valueParser.parse(rawInput);
|
|
627
659
|
if (isAsync) return parseResultOrPromise.then((parseResult) => ({
|
|
628
660
|
success: true,
|
|
629
661
|
next: {
|
|
630
662
|
...context,
|
|
631
663
|
buffer: context.buffer.slice(i + 1),
|
|
632
|
-
state: parseResult,
|
|
664
|
+
state: createOptionParseState(rawInput, valueParser, parseResult),
|
|
633
665
|
optionsTerminated
|
|
634
666
|
},
|
|
635
667
|
consumed: context.buffer.slice(0, i + 1)
|
|
@@ -639,7 +671,7 @@ function argument(valueParser, options = {}) {
|
|
|
639
671
|
next: {
|
|
640
672
|
...context,
|
|
641
673
|
buffer: context.buffer.slice(i + 1),
|
|
642
|
-
state: parseResultOrPromise,
|
|
674
|
+
state: createOptionParseState(rawInput, valueParser, parseResultOrPromise),
|
|
643
675
|
optionsTerminated
|
|
644
676
|
},
|
|
645
677
|
consumed: context.buffer.slice(0, i + 1)
|
|
@@ -650,7 +682,23 @@ function argument(valueParser, options = {}) {
|
|
|
650
682
|
success: false,
|
|
651
683
|
error: options.errors?.endOfInput ?? message`Expected a ${metavar(valueParser.metavar)}, but too few arguments.`
|
|
652
684
|
};
|
|
653
|
-
|
|
685
|
+
if (isDeferredParseState(state)) {
|
|
686
|
+
const preliminaryResult = state.preliminaryResult;
|
|
687
|
+
if (preliminaryResult.success) return preliminaryResult;
|
|
688
|
+
return {
|
|
689
|
+
success: false,
|
|
690
|
+
error: options.errors?.invalidValue ? typeof options.errors.invalidValue === "function" ? options.errors.invalidValue(preliminaryResult.error) : options.errors.invalidValue : message`${metavar(valueParser.metavar)}: ${preliminaryResult.error}`
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
if (isDependencySourceState(state)) {
|
|
694
|
+
const result$1 = state.result;
|
|
695
|
+
if (result$1.success) return result$1;
|
|
696
|
+
return {
|
|
697
|
+
success: false,
|
|
698
|
+
error: options.errors?.invalidValue ? typeof options.errors.invalidValue === "function" ? options.errors.invalidValue(result$1.error) : options.errors.invalidValue : message`${metavar(valueParser.metavar)}: ${result$1.error}`
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
if (state.success) return state;
|
|
654
702
|
return {
|
|
655
703
|
success: false,
|
|
656
704
|
error: options.errors?.invalidValue ? typeof options.errors.invalidValue === "function" ? options.errors.invalidValue(state.error) : options.errors.invalidValue : message`${metavar(valueParser.metavar)}: ${state.error}`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@optique/core",
|
|
3
|
-
"version": "0.10.0-dev.
|
|
3
|
+
"version": "0.10.0-dev.293+9411c366",
|
|
4
4
|
"description": "Type-safe combinatorial command-line interface parser",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"CLI",
|
|
@@ -67,6 +67,14 @@
|
|
|
67
67
|
"import": "./dist/constructs.js",
|
|
68
68
|
"require": "./dist/constructs.cjs"
|
|
69
69
|
},
|
|
70
|
+
"./dependency": {
|
|
71
|
+
"types": {
|
|
72
|
+
"import": "./dist/dependency.d.ts",
|
|
73
|
+
"require": "./dist/dependency.d.cts"
|
|
74
|
+
},
|
|
75
|
+
"import": "./dist/dependency.js",
|
|
76
|
+
"require": "./dist/dependency.cjs"
|
|
77
|
+
},
|
|
70
78
|
"./doc": {
|
|
71
79
|
"types": {
|
|
72
80
|
"import": "./dist/doc.d.ts",
|