@optique/core 0.2.0-dev.32 → 0.2.1
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/LICENSE +20 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/parser.cjs +109 -0
- package/dist/parser.d.cts +102 -1
- package/dist/parser.d.ts +102 -1
- package/dist/parser.js +109 -1
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2025 Hong Minhee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/dist/index.cjs
CHANGED
|
@@ -9,6 +9,7 @@ exports.RunError = require_facade.RunError;
|
|
|
9
9
|
exports.argument = require_parser.argument;
|
|
10
10
|
exports.choice = require_valueparser.choice;
|
|
11
11
|
exports.command = require_parser.command;
|
|
12
|
+
exports.concat = require_parser.concat;
|
|
12
13
|
exports.constant = require_parser.constant;
|
|
13
14
|
exports.float = require_valueparser.float;
|
|
14
15
|
exports.formatDocPage = require_doc.formatDocPage;
|
package/dist/index.d.cts
CHANGED
|
@@ -2,6 +2,6 @@ import { Message, MessageFormatOptions, MessageTerm, formatMessage, message, met
|
|
|
2
2
|
import { OptionName, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, formatUsage, formatUsageTerm, normalizeUsage } from "./usage.cjs";
|
|
3
3
|
import { DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, formatDocPage } from "./doc.cjs";
|
|
4
4
|
import { ChoiceOptions, FloatOptions, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, StringOptions, UrlOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.cjs";
|
|
5
|
-
import { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.cjs";
|
|
5
|
+
import { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, concat, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.cjs";
|
|
6
6
|
import { RunError, RunOptions, run } from "./facade.cjs";
|
|
7
|
-
export { ArgumentOptions, ChoiceOptions, CommandOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, FloatOptions, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, Message, MessageFormatOptions, MessageTerm, MultipleOptions, OptionName, OptionOptions, Parser, ParserContext, ParserResult, Result, RunError, RunOptions, StringOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, argument, choice, command, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, map, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
|
7
|
+
export { ArgumentOptions, ChoiceOptions, CommandOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, FloatOptions, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, Message, MessageFormatOptions, MessageTerm, MultipleOptions, OptionName, OptionOptions, Parser, ParserContext, ParserResult, Result, RunError, RunOptions, StringOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, argument, choice, command, concat, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, map, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,6 @@ import { Message, MessageFormatOptions, MessageTerm, formatMessage, message, met
|
|
|
2
2
|
import { OptionName, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, formatUsage, formatUsageTerm, normalizeUsage } from "./usage.js";
|
|
3
3
|
import { DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, formatDocPage } from "./doc.js";
|
|
4
4
|
import { ChoiceOptions, FloatOptions, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, StringOptions, UrlOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.js";
|
|
5
|
-
import { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.js";
|
|
5
|
+
import { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, concat, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.js";
|
|
6
6
|
import { RunError, RunOptions, run } from "./facade.js";
|
|
7
|
-
export { ArgumentOptions, ChoiceOptions, CommandOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, FloatOptions, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, Message, MessageFormatOptions, MessageTerm, MultipleOptions, OptionName, OptionOptions, Parser, ParserContext, ParserResult, Result, RunError, RunOptions, StringOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, argument, choice, command, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, map, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
|
7
|
+
export { ArgumentOptions, ChoiceOptions, CommandOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, FloatOptions, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, Message, MessageFormatOptions, MessageTerm, MultipleOptions, OptionName, OptionOptions, Parser, ParserContext, ParserResult, Result, RunError, RunOptions, StringOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, argument, choice, command, concat, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, map, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { formatMessage, message, metavar, optionName, optionNames, text, value,
|
|
|
2
2
|
import { formatUsage, formatUsageTerm, normalizeUsage } from "./usage.js";
|
|
3
3
|
import { formatDocPage } from "./doc.js";
|
|
4
4
|
import { choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.js";
|
|
5
|
-
import { argument, command, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.js";
|
|
5
|
+
import { argument, command, concat, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.js";
|
|
6
6
|
import { RunError, run } from "./facade.js";
|
|
7
7
|
|
|
8
|
-
export { RunError, argument, choice, command, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, map, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
|
8
|
+
export { RunError, argument, choice, command, concat, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, map, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
package/dist/parser.cjs
CHANGED
|
@@ -897,6 +897,109 @@ function merge(...parsers) {
|
|
|
897
897
|
}
|
|
898
898
|
};
|
|
899
899
|
}
|
|
900
|
+
function concat(...parsers) {
|
|
901
|
+
const initialState = parsers.map((parser) => parser.initialState);
|
|
902
|
+
return {
|
|
903
|
+
$valueType: [],
|
|
904
|
+
$stateType: [],
|
|
905
|
+
priority: parsers.length > 0 ? Math.max(...parsers.map((p) => p.priority)) : 0,
|
|
906
|
+
usage: parsers.flatMap((p) => p.usage),
|
|
907
|
+
initialState,
|
|
908
|
+
parse(context) {
|
|
909
|
+
let currentContext = context;
|
|
910
|
+
const allConsumed = [];
|
|
911
|
+
const matchedParsers = /* @__PURE__ */ new Set();
|
|
912
|
+
while (matchedParsers.size < parsers.length) {
|
|
913
|
+
let foundMatch = false;
|
|
914
|
+
let error = {
|
|
915
|
+
consumed: 0,
|
|
916
|
+
error: require_message.message`No remaining parsers could match the input.`
|
|
917
|
+
};
|
|
918
|
+
const remainingParsers = parsers.map((parser, index) => [parser, index]).filter(([_, index]) => !matchedParsers.has(index)).sort(([parserA], [parserB]) => parserB.priority - parserA.priority);
|
|
919
|
+
for (const [parser, index] of remainingParsers) {
|
|
920
|
+
const result = parser.parse({
|
|
921
|
+
...currentContext,
|
|
922
|
+
state: currentContext.state[index]
|
|
923
|
+
});
|
|
924
|
+
if (result.success && result.consumed.length > 0) {
|
|
925
|
+
currentContext = {
|
|
926
|
+
...currentContext,
|
|
927
|
+
buffer: result.next.buffer,
|
|
928
|
+
optionsTerminated: result.next.optionsTerminated,
|
|
929
|
+
state: currentContext.state.map((s, idx) => idx === index ? result.next.state : s)
|
|
930
|
+
};
|
|
931
|
+
allConsumed.push(...result.consumed);
|
|
932
|
+
matchedParsers.add(index);
|
|
933
|
+
foundMatch = true;
|
|
934
|
+
break;
|
|
935
|
+
} else if (!result.success && error.consumed < result.consumed) error = result;
|
|
936
|
+
}
|
|
937
|
+
if (!foundMatch) for (const [parser, index] of remainingParsers) {
|
|
938
|
+
const result = parser.parse({
|
|
939
|
+
...currentContext,
|
|
940
|
+
state: currentContext.state[index]
|
|
941
|
+
});
|
|
942
|
+
if (result.success && result.consumed.length < 1) {
|
|
943
|
+
currentContext = {
|
|
944
|
+
...currentContext,
|
|
945
|
+
state: currentContext.state.map((s, idx) => idx === index ? result.next.state : s)
|
|
946
|
+
};
|
|
947
|
+
matchedParsers.add(index);
|
|
948
|
+
foundMatch = true;
|
|
949
|
+
break;
|
|
950
|
+
} else if (!result.success && result.consumed < 1) {
|
|
951
|
+
matchedParsers.add(index);
|
|
952
|
+
foundMatch = true;
|
|
953
|
+
break;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
if (!foundMatch) return {
|
|
957
|
+
...error,
|
|
958
|
+
success: false
|
|
959
|
+
};
|
|
960
|
+
}
|
|
961
|
+
return {
|
|
962
|
+
success: true,
|
|
963
|
+
next: currentContext,
|
|
964
|
+
consumed: allConsumed
|
|
965
|
+
};
|
|
966
|
+
},
|
|
967
|
+
complete(state) {
|
|
968
|
+
const results = [];
|
|
969
|
+
for (let i = 0; i < parsers.length; i++) {
|
|
970
|
+
const parser = parsers[i];
|
|
971
|
+
const parserState = state[i];
|
|
972
|
+
const result = parser.complete(parserState);
|
|
973
|
+
if (!result.success) return result;
|
|
974
|
+
if (Array.isArray(result.value)) results.push(...result.value);
|
|
975
|
+
else results.push(result.value);
|
|
976
|
+
}
|
|
977
|
+
return {
|
|
978
|
+
success: true,
|
|
979
|
+
value: results
|
|
980
|
+
};
|
|
981
|
+
},
|
|
982
|
+
getDocFragments(state, _defaultValue) {
|
|
983
|
+
const fragments = parsers.flatMap((p, index) => p.getDocFragments(state[index], void 0).fragments);
|
|
984
|
+
const entries = fragments.filter((f) => f.type === "entry");
|
|
985
|
+
const sections = [];
|
|
986
|
+
for (const fragment of fragments) {
|
|
987
|
+
if (fragment.type !== "section") continue;
|
|
988
|
+
if (fragment.title == null) entries.push(...fragment.entries);
|
|
989
|
+
else sections.push(fragment);
|
|
990
|
+
}
|
|
991
|
+
const result = [...sections.map((s) => ({
|
|
992
|
+
...s,
|
|
993
|
+
type: "section"
|
|
994
|
+
}))];
|
|
995
|
+
if (entries.length > 0) result.push({
|
|
996
|
+
type: "section",
|
|
997
|
+
entries
|
|
998
|
+
});
|
|
999
|
+
return { fragments: result };
|
|
1000
|
+
}
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
900
1003
|
/**
|
|
901
1004
|
* Creates a parser that matches a specific subcommand name and then applies
|
|
902
1005
|
* an inner parser to the remaining arguments.
|
|
@@ -1032,7 +1135,12 @@ function parse(parser, args) {
|
|
|
1032
1135
|
success: false,
|
|
1033
1136
|
error: result.error
|
|
1034
1137
|
};
|
|
1138
|
+
const previousBuffer = context.buffer;
|
|
1035
1139
|
context = result.next;
|
|
1140
|
+
if (context.buffer.length > 0 && context.buffer.length === previousBuffer.length && context.buffer[0] === previousBuffer[0]) return {
|
|
1141
|
+
success: false,
|
|
1142
|
+
error: require_message.message`Unexpected option or argument: ${context.buffer[0]}.`
|
|
1143
|
+
};
|
|
1036
1144
|
} while (context.buffer.length > 0);
|
|
1037
1145
|
const endResult = parser.complete(context.state);
|
|
1038
1146
|
return endResult.success ? {
|
|
@@ -1121,6 +1229,7 @@ function getDocPage(parser, args = []) {
|
|
|
1121
1229
|
//#endregion
|
|
1122
1230
|
exports.argument = argument;
|
|
1123
1231
|
exports.command = command;
|
|
1232
|
+
exports.concat = concat;
|
|
1124
1233
|
exports.constant = constant;
|
|
1125
1234
|
exports.getDocPage = getDocPage;
|
|
1126
1235
|
exports.map = map;
|
package/dist/parser.d.cts
CHANGED
|
@@ -502,6 +502,107 @@ declare function merge<TA extends Parser<Record<string | symbol, unknown>, Recor
|
|
|
502
502
|
* of the two parsers into a single object.
|
|
503
503
|
*/
|
|
504
504
|
declare function merge<TA extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TB extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TC extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TD extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TE extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>>(a: TA, b: TB, c: TC, d: TD, e: TE): Parser<{ readonly [K in keyof TA["$valueType"][number]]: TA["$valueType"][number][K] extends (infer U) ? U : never } & { readonly [K in keyof TB["$valueType"][number]]: TB["$valueType"][number][K] extends (infer U2) ? U2 : never } & { readonly [K in keyof TC["$valueType"][number]]: TC["$valueType"][number][K] extends (infer U3) ? U3 : never } & { readonly [K in keyof TD["$valueType"][number]]: TD["$valueType"][number][K] extends (infer U4) ? U4 : never } & { readonly [K in keyof TE["$valueType"][number]]: TE["$valueType"][number][K] extends (infer U5) ? U5 : never }, { readonly [K in keyof TA]: unknown } & { readonly [K in keyof TB]: unknown } & { readonly [K in keyof TC]: unknown } & { readonly [K in keyof TD]: unknown } & { readonly [K in keyof TE]: unknown }>;
|
|
505
|
+
/**
|
|
506
|
+
* Concatenates two {@link tuple} parsers into a single parser that produces
|
|
507
|
+
* a flattened tuple containing the values from both parsers in order.
|
|
508
|
+
*
|
|
509
|
+
* This is similar to {@link merge} for object parsers, but operates on tuple
|
|
510
|
+
* parsers and preserves the sequential, positional nature of tuples by
|
|
511
|
+
* flattening the results into a single tuple array.
|
|
512
|
+
*
|
|
513
|
+
* @example
|
|
514
|
+
* ```typescript
|
|
515
|
+
* const basicTuple = tuple([
|
|
516
|
+
* option("-v", "--verbose"),
|
|
517
|
+
* option("-p", "--port", integer()),
|
|
518
|
+
* ]);
|
|
519
|
+
*
|
|
520
|
+
* const serverTuple = tuple([
|
|
521
|
+
* option("-h", "--host", string()),
|
|
522
|
+
* option("-d", "--debug"),
|
|
523
|
+
* ]);
|
|
524
|
+
*
|
|
525
|
+
* const combined = concat(basicTuple, serverTuple);
|
|
526
|
+
* // Type: Parser<[boolean, number, string, boolean], [BasicState, ServerState]>
|
|
527
|
+
*
|
|
528
|
+
* const result = parse(combined, ["-v", "-p", "8080", "-h", "localhost", "-d"]);
|
|
529
|
+
* // result.value: [true, 8080, "localhost", true]
|
|
530
|
+
* ```
|
|
531
|
+
*
|
|
532
|
+
* @template TA The value type of the first tuple parser.
|
|
533
|
+
* @template TB The value type of the second tuple parser.
|
|
534
|
+
* @template TStateA The state type of the first tuple parser.
|
|
535
|
+
* @template TStateB The state type of the second tuple parser.
|
|
536
|
+
* @param a The first {@link tuple} parser to concatenate.
|
|
537
|
+
* @param b The second {@link tuple} parser to concatenate.
|
|
538
|
+
* @return A new {@link tuple} parser that combines the values of both parsers
|
|
539
|
+
* into a single flattened tuple.
|
|
540
|
+
* @since 0.2.0
|
|
541
|
+
*/
|
|
542
|
+
declare function concat<TA extends readonly unknown[], TB extends readonly unknown[], TStateA, TStateB>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>): Parser<[...TA, ...TB], [TStateA, TStateB]>;
|
|
543
|
+
/**
|
|
544
|
+
* Concatenates three {@link tuple} parsers into a single parser that produces
|
|
545
|
+
* a flattened tuple containing the values from all parsers in order.
|
|
546
|
+
*
|
|
547
|
+
* @template TA The value type of the first tuple parser.
|
|
548
|
+
* @template TB The value type of the second tuple parser.
|
|
549
|
+
* @template TC The value type of the third tuple parser.
|
|
550
|
+
* @template TStateA The state type of the first tuple parser.
|
|
551
|
+
* @template TStateB The state type of the second tuple parser.
|
|
552
|
+
* @template TStateC The state type of the third tuple parser.
|
|
553
|
+
* @param a The first {@link tuple} parser to concatenate.
|
|
554
|
+
* @param b The second {@link tuple} parser to concatenate.
|
|
555
|
+
* @param c The third {@link tuple} parser to concatenate.
|
|
556
|
+
* @return A new {@link tuple} parser that combines the values of all parsers
|
|
557
|
+
* into a single flattened tuple.
|
|
558
|
+
* @since 0.2.0
|
|
559
|
+
*/
|
|
560
|
+
declare function concat<TA extends readonly unknown[], TB extends readonly unknown[], TC extends readonly unknown[], TStateA, TStateB, TStateC>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>): Parser<[...TA, ...TB, ...TC], [TStateA, TStateB, TStateC]>;
|
|
561
|
+
/**
|
|
562
|
+
* Concatenates four {@link tuple} parsers into a single parser that produces
|
|
563
|
+
* a flattened tuple containing the values from all parsers in order.
|
|
564
|
+
*
|
|
565
|
+
* @template TA The value type of the first tuple parser.
|
|
566
|
+
* @template TB The value type of the second tuple parser.
|
|
567
|
+
* @template TC The value type of the third tuple parser.
|
|
568
|
+
* @template TD The value type of the fourth tuple parser.
|
|
569
|
+
* @template TStateA The state type of the first tuple parser.
|
|
570
|
+
* @template TStateB The state type of the second tuple parser.
|
|
571
|
+
* @template TStateC The state type of the third tuple parser.
|
|
572
|
+
* @template TStateD The state type of the fourth tuple parser.
|
|
573
|
+
* @param a The first {@link tuple} parser to concatenate.
|
|
574
|
+
* @param b The second {@link tuple} parser to concatenate.
|
|
575
|
+
* @param c The third {@link tuple} parser to concatenate.
|
|
576
|
+
* @param d The fourth {@link tuple} parser to concatenate.
|
|
577
|
+
* @return A new {@link tuple} parser that combines the values of all parsers
|
|
578
|
+
* into a single flattened tuple.
|
|
579
|
+
* @since 0.2.0
|
|
580
|
+
*/
|
|
581
|
+
declare function concat<TA extends readonly unknown[], TB extends readonly unknown[], TC extends readonly unknown[], TD extends readonly unknown[], TStateA, TStateB, TStateC, TStateD>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>): Parser<[...TA, ...TB, ...TC, ...TD], [TStateA, TStateB, TStateC, TStateD]>;
|
|
582
|
+
/**
|
|
583
|
+
* Concatenates five {@link tuple} parsers into a single parser that produces
|
|
584
|
+
* a flattened tuple containing the values from all parsers in order.
|
|
585
|
+
*
|
|
586
|
+
* @template TA The value type of the first tuple parser.
|
|
587
|
+
* @template TB The value type of the second tuple parser.
|
|
588
|
+
* @template TC The value type of the third tuple parser.
|
|
589
|
+
* @template TD The value type of the fourth tuple parser.
|
|
590
|
+
* @template TE The value type of the fifth tuple parser.
|
|
591
|
+
* @template TStateA The state type of the first tuple parser.
|
|
592
|
+
* @template TStateB The state type of the second tuple parser.
|
|
593
|
+
* @template TStateC The state type of the third tuple parser.
|
|
594
|
+
* @template TStateD The state type of the fourth tuple parser.
|
|
595
|
+
* @template TStateE The state type of the fifth tuple parser.
|
|
596
|
+
* @param a The first {@link tuple} parser to concatenate.
|
|
597
|
+
* @param b The second {@link tuple} parser to concatenate.
|
|
598
|
+
* @param c The third {@link tuple} parser to concatenate.
|
|
599
|
+
* @param d The fourth {@link tuple} parser to concatenate.
|
|
600
|
+
* @param e The fifth {@link tuple} parser to concatenate.
|
|
601
|
+
* @return A new {@link tuple} parser that combines the values of all parsers
|
|
602
|
+
* into a single flattened tuple.
|
|
603
|
+
* @since 0.2.0
|
|
604
|
+
*/
|
|
605
|
+
declare function concat<TA extends readonly unknown[], TB extends readonly unknown[], TC extends readonly unknown[], TD extends readonly unknown[], TE extends readonly unknown[], TStateA, TStateB, TStateC, TStateD, TStateE>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>): Parser<[...TA, ...TB, ...TC, ...TD, ...TE], [TStateA, TStateB, TStateC, TStateD, TStateE]>;
|
|
505
606
|
/**
|
|
506
607
|
* Options for the {@link command} parser.
|
|
507
608
|
*/
|
|
@@ -611,4 +712,4 @@ declare function parse<T>(parser: Parser<T, unknown>, args: readonly string[]):
|
|
|
611
712
|
*/
|
|
612
713
|
declare function getDocPage(parser: Parser<unknown, unknown>, args?: readonly string[]): DocPage | undefined;
|
|
613
714
|
//#endregion
|
|
614
|
-
export { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
|
|
715
|
+
export { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, concat, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
|
package/dist/parser.d.ts
CHANGED
|
@@ -502,6 +502,107 @@ declare function merge<TA extends Parser<Record<string | symbol, unknown>, Recor
|
|
|
502
502
|
* of the two parsers into a single object.
|
|
503
503
|
*/
|
|
504
504
|
declare function merge<TA extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TB extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TC extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TD extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>, TE extends Parser<Record<string | symbol, unknown>, Record<string | symbol, unknown>>>(a: TA, b: TB, c: TC, d: TD, e: TE): Parser<{ readonly [K in keyof TA["$valueType"][number]]: TA["$valueType"][number][K] extends (infer U) ? U : never } & { readonly [K in keyof TB["$valueType"][number]]: TB["$valueType"][number][K] extends (infer U2) ? U2 : never } & { readonly [K in keyof TC["$valueType"][number]]: TC["$valueType"][number][K] extends (infer U3) ? U3 : never } & { readonly [K in keyof TD["$valueType"][number]]: TD["$valueType"][number][K] extends (infer U4) ? U4 : never } & { readonly [K in keyof TE["$valueType"][number]]: TE["$valueType"][number][K] extends (infer U5) ? U5 : never }, { readonly [K in keyof TA]: unknown } & { readonly [K in keyof TB]: unknown } & { readonly [K in keyof TC]: unknown } & { readonly [K in keyof TD]: unknown } & { readonly [K in keyof TE]: unknown }>;
|
|
505
|
+
/**
|
|
506
|
+
* Concatenates two {@link tuple} parsers into a single parser that produces
|
|
507
|
+
* a flattened tuple containing the values from both parsers in order.
|
|
508
|
+
*
|
|
509
|
+
* This is similar to {@link merge} for object parsers, but operates on tuple
|
|
510
|
+
* parsers and preserves the sequential, positional nature of tuples by
|
|
511
|
+
* flattening the results into a single tuple array.
|
|
512
|
+
*
|
|
513
|
+
* @example
|
|
514
|
+
* ```typescript
|
|
515
|
+
* const basicTuple = tuple([
|
|
516
|
+
* option("-v", "--verbose"),
|
|
517
|
+
* option("-p", "--port", integer()),
|
|
518
|
+
* ]);
|
|
519
|
+
*
|
|
520
|
+
* const serverTuple = tuple([
|
|
521
|
+
* option("-h", "--host", string()),
|
|
522
|
+
* option("-d", "--debug"),
|
|
523
|
+
* ]);
|
|
524
|
+
*
|
|
525
|
+
* const combined = concat(basicTuple, serverTuple);
|
|
526
|
+
* // Type: Parser<[boolean, number, string, boolean], [BasicState, ServerState]>
|
|
527
|
+
*
|
|
528
|
+
* const result = parse(combined, ["-v", "-p", "8080", "-h", "localhost", "-d"]);
|
|
529
|
+
* // result.value: [true, 8080, "localhost", true]
|
|
530
|
+
* ```
|
|
531
|
+
*
|
|
532
|
+
* @template TA The value type of the first tuple parser.
|
|
533
|
+
* @template TB The value type of the second tuple parser.
|
|
534
|
+
* @template TStateA The state type of the first tuple parser.
|
|
535
|
+
* @template TStateB The state type of the second tuple parser.
|
|
536
|
+
* @param a The first {@link tuple} parser to concatenate.
|
|
537
|
+
* @param b The second {@link tuple} parser to concatenate.
|
|
538
|
+
* @return A new {@link tuple} parser that combines the values of both parsers
|
|
539
|
+
* into a single flattened tuple.
|
|
540
|
+
* @since 0.2.0
|
|
541
|
+
*/
|
|
542
|
+
declare function concat<TA extends readonly unknown[], TB extends readonly unknown[], TStateA, TStateB>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>): Parser<[...TA, ...TB], [TStateA, TStateB]>;
|
|
543
|
+
/**
|
|
544
|
+
* Concatenates three {@link tuple} parsers into a single parser that produces
|
|
545
|
+
* a flattened tuple containing the values from all parsers in order.
|
|
546
|
+
*
|
|
547
|
+
* @template TA The value type of the first tuple parser.
|
|
548
|
+
* @template TB The value type of the second tuple parser.
|
|
549
|
+
* @template TC The value type of the third tuple parser.
|
|
550
|
+
* @template TStateA The state type of the first tuple parser.
|
|
551
|
+
* @template TStateB The state type of the second tuple parser.
|
|
552
|
+
* @template TStateC The state type of the third tuple parser.
|
|
553
|
+
* @param a The first {@link tuple} parser to concatenate.
|
|
554
|
+
* @param b The second {@link tuple} parser to concatenate.
|
|
555
|
+
* @param c The third {@link tuple} parser to concatenate.
|
|
556
|
+
* @return A new {@link tuple} parser that combines the values of all parsers
|
|
557
|
+
* into a single flattened tuple.
|
|
558
|
+
* @since 0.2.0
|
|
559
|
+
*/
|
|
560
|
+
declare function concat<TA extends readonly unknown[], TB extends readonly unknown[], TC extends readonly unknown[], TStateA, TStateB, TStateC>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>): Parser<[...TA, ...TB, ...TC], [TStateA, TStateB, TStateC]>;
|
|
561
|
+
/**
|
|
562
|
+
* Concatenates four {@link tuple} parsers into a single parser that produces
|
|
563
|
+
* a flattened tuple containing the values from all parsers in order.
|
|
564
|
+
*
|
|
565
|
+
* @template TA The value type of the first tuple parser.
|
|
566
|
+
* @template TB The value type of the second tuple parser.
|
|
567
|
+
* @template TC The value type of the third tuple parser.
|
|
568
|
+
* @template TD The value type of the fourth tuple parser.
|
|
569
|
+
* @template TStateA The state type of the first tuple parser.
|
|
570
|
+
* @template TStateB The state type of the second tuple parser.
|
|
571
|
+
* @template TStateC The state type of the third tuple parser.
|
|
572
|
+
* @template TStateD The state type of the fourth tuple parser.
|
|
573
|
+
* @param a The first {@link tuple} parser to concatenate.
|
|
574
|
+
* @param b The second {@link tuple} parser to concatenate.
|
|
575
|
+
* @param c The third {@link tuple} parser to concatenate.
|
|
576
|
+
* @param d The fourth {@link tuple} parser to concatenate.
|
|
577
|
+
* @return A new {@link tuple} parser that combines the values of all parsers
|
|
578
|
+
* into a single flattened tuple.
|
|
579
|
+
* @since 0.2.0
|
|
580
|
+
*/
|
|
581
|
+
declare function concat<TA extends readonly unknown[], TB extends readonly unknown[], TC extends readonly unknown[], TD extends readonly unknown[], TStateA, TStateB, TStateC, TStateD>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>): Parser<[...TA, ...TB, ...TC, ...TD], [TStateA, TStateB, TStateC, TStateD]>;
|
|
582
|
+
/**
|
|
583
|
+
* Concatenates five {@link tuple} parsers into a single parser that produces
|
|
584
|
+
* a flattened tuple containing the values from all parsers in order.
|
|
585
|
+
*
|
|
586
|
+
* @template TA The value type of the first tuple parser.
|
|
587
|
+
* @template TB The value type of the second tuple parser.
|
|
588
|
+
* @template TC The value type of the third tuple parser.
|
|
589
|
+
* @template TD The value type of the fourth tuple parser.
|
|
590
|
+
* @template TE The value type of the fifth tuple parser.
|
|
591
|
+
* @template TStateA The state type of the first tuple parser.
|
|
592
|
+
* @template TStateB The state type of the second tuple parser.
|
|
593
|
+
* @template TStateC The state type of the third tuple parser.
|
|
594
|
+
* @template TStateD The state type of the fourth tuple parser.
|
|
595
|
+
* @template TStateE The state type of the fifth tuple parser.
|
|
596
|
+
* @param a The first {@link tuple} parser to concatenate.
|
|
597
|
+
* @param b The second {@link tuple} parser to concatenate.
|
|
598
|
+
* @param c The third {@link tuple} parser to concatenate.
|
|
599
|
+
* @param d The fourth {@link tuple} parser to concatenate.
|
|
600
|
+
* @param e The fifth {@link tuple} parser to concatenate.
|
|
601
|
+
* @return A new {@link tuple} parser that combines the values of all parsers
|
|
602
|
+
* into a single flattened tuple.
|
|
603
|
+
* @since 0.2.0
|
|
604
|
+
*/
|
|
605
|
+
declare function concat<TA extends readonly unknown[], TB extends readonly unknown[], TC extends readonly unknown[], TD extends readonly unknown[], TE extends readonly unknown[], TStateA, TStateB, TStateC, TStateD, TStateE>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>): Parser<[...TA, ...TB, ...TC, ...TD, ...TE], [TStateA, TStateB, TStateC, TStateD, TStateE]>;
|
|
505
606
|
/**
|
|
506
607
|
* Options for the {@link command} parser.
|
|
507
608
|
*/
|
|
@@ -611,4 +712,4 @@ declare function parse<T>(parser: Parser<T, unknown>, args: readonly string[]):
|
|
|
611
712
|
*/
|
|
612
713
|
declare function getDocPage(parser: Parser<unknown, unknown>, args?: readonly string[]): DocPage | undefined;
|
|
613
714
|
//#endregion
|
|
614
|
-
export { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
|
|
715
|
+
export { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, concat, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
|
package/dist/parser.js
CHANGED
|
@@ -897,6 +897,109 @@ function merge(...parsers) {
|
|
|
897
897
|
}
|
|
898
898
|
};
|
|
899
899
|
}
|
|
900
|
+
function concat(...parsers) {
|
|
901
|
+
const initialState = parsers.map((parser) => parser.initialState);
|
|
902
|
+
return {
|
|
903
|
+
$valueType: [],
|
|
904
|
+
$stateType: [],
|
|
905
|
+
priority: parsers.length > 0 ? Math.max(...parsers.map((p) => p.priority)) : 0,
|
|
906
|
+
usage: parsers.flatMap((p) => p.usage),
|
|
907
|
+
initialState,
|
|
908
|
+
parse(context) {
|
|
909
|
+
let currentContext = context;
|
|
910
|
+
const allConsumed = [];
|
|
911
|
+
const matchedParsers = /* @__PURE__ */ new Set();
|
|
912
|
+
while (matchedParsers.size < parsers.length) {
|
|
913
|
+
let foundMatch = false;
|
|
914
|
+
let error = {
|
|
915
|
+
consumed: 0,
|
|
916
|
+
error: message`No remaining parsers could match the input.`
|
|
917
|
+
};
|
|
918
|
+
const remainingParsers = parsers.map((parser, index) => [parser, index]).filter(([_, index]) => !matchedParsers.has(index)).sort(([parserA], [parserB]) => parserB.priority - parserA.priority);
|
|
919
|
+
for (const [parser, index] of remainingParsers) {
|
|
920
|
+
const result = parser.parse({
|
|
921
|
+
...currentContext,
|
|
922
|
+
state: currentContext.state[index]
|
|
923
|
+
});
|
|
924
|
+
if (result.success && result.consumed.length > 0) {
|
|
925
|
+
currentContext = {
|
|
926
|
+
...currentContext,
|
|
927
|
+
buffer: result.next.buffer,
|
|
928
|
+
optionsTerminated: result.next.optionsTerminated,
|
|
929
|
+
state: currentContext.state.map((s, idx) => idx === index ? result.next.state : s)
|
|
930
|
+
};
|
|
931
|
+
allConsumed.push(...result.consumed);
|
|
932
|
+
matchedParsers.add(index);
|
|
933
|
+
foundMatch = true;
|
|
934
|
+
break;
|
|
935
|
+
} else if (!result.success && error.consumed < result.consumed) error = result;
|
|
936
|
+
}
|
|
937
|
+
if (!foundMatch) for (const [parser, index] of remainingParsers) {
|
|
938
|
+
const result = parser.parse({
|
|
939
|
+
...currentContext,
|
|
940
|
+
state: currentContext.state[index]
|
|
941
|
+
});
|
|
942
|
+
if (result.success && result.consumed.length < 1) {
|
|
943
|
+
currentContext = {
|
|
944
|
+
...currentContext,
|
|
945
|
+
state: currentContext.state.map((s, idx) => idx === index ? result.next.state : s)
|
|
946
|
+
};
|
|
947
|
+
matchedParsers.add(index);
|
|
948
|
+
foundMatch = true;
|
|
949
|
+
break;
|
|
950
|
+
} else if (!result.success && result.consumed < 1) {
|
|
951
|
+
matchedParsers.add(index);
|
|
952
|
+
foundMatch = true;
|
|
953
|
+
break;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
if (!foundMatch) return {
|
|
957
|
+
...error,
|
|
958
|
+
success: false
|
|
959
|
+
};
|
|
960
|
+
}
|
|
961
|
+
return {
|
|
962
|
+
success: true,
|
|
963
|
+
next: currentContext,
|
|
964
|
+
consumed: allConsumed
|
|
965
|
+
};
|
|
966
|
+
},
|
|
967
|
+
complete(state) {
|
|
968
|
+
const results = [];
|
|
969
|
+
for (let i = 0; i < parsers.length; i++) {
|
|
970
|
+
const parser = parsers[i];
|
|
971
|
+
const parserState = state[i];
|
|
972
|
+
const result = parser.complete(parserState);
|
|
973
|
+
if (!result.success) return result;
|
|
974
|
+
if (Array.isArray(result.value)) results.push(...result.value);
|
|
975
|
+
else results.push(result.value);
|
|
976
|
+
}
|
|
977
|
+
return {
|
|
978
|
+
success: true,
|
|
979
|
+
value: results
|
|
980
|
+
};
|
|
981
|
+
},
|
|
982
|
+
getDocFragments(state, _defaultValue) {
|
|
983
|
+
const fragments = parsers.flatMap((p, index) => p.getDocFragments(state[index], void 0).fragments);
|
|
984
|
+
const entries = fragments.filter((f) => f.type === "entry");
|
|
985
|
+
const sections = [];
|
|
986
|
+
for (const fragment of fragments) {
|
|
987
|
+
if (fragment.type !== "section") continue;
|
|
988
|
+
if (fragment.title == null) entries.push(...fragment.entries);
|
|
989
|
+
else sections.push(fragment);
|
|
990
|
+
}
|
|
991
|
+
const result = [...sections.map((s) => ({
|
|
992
|
+
...s,
|
|
993
|
+
type: "section"
|
|
994
|
+
}))];
|
|
995
|
+
if (entries.length > 0) result.push({
|
|
996
|
+
type: "section",
|
|
997
|
+
entries
|
|
998
|
+
});
|
|
999
|
+
return { fragments: result };
|
|
1000
|
+
}
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
900
1003
|
/**
|
|
901
1004
|
* Creates a parser that matches a specific subcommand name and then applies
|
|
902
1005
|
* an inner parser to the remaining arguments.
|
|
@@ -1032,7 +1135,12 @@ function parse(parser, args) {
|
|
|
1032
1135
|
success: false,
|
|
1033
1136
|
error: result.error
|
|
1034
1137
|
};
|
|
1138
|
+
const previousBuffer = context.buffer;
|
|
1035
1139
|
context = result.next;
|
|
1140
|
+
if (context.buffer.length > 0 && context.buffer.length === previousBuffer.length && context.buffer[0] === previousBuffer[0]) return {
|
|
1141
|
+
success: false,
|
|
1142
|
+
error: message`Unexpected option or argument: ${context.buffer[0]}.`
|
|
1143
|
+
};
|
|
1036
1144
|
} while (context.buffer.length > 0);
|
|
1037
1145
|
const endResult = parser.complete(context.state);
|
|
1038
1146
|
return endResult.success ? {
|
|
@@ -1119,4 +1227,4 @@ function getDocPage(parser, args = []) {
|
|
|
1119
1227
|
}
|
|
1120
1228
|
|
|
1121
1229
|
//#endregion
|
|
1122
|
-
export { argument, command, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
|
|
1230
|
+
export { argument, command, concat, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
|