@optique/core 0.2.0-dev.32 → 0.3.0-dev.34

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/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
@@ -434,6 +434,166 @@ declare function or<TA, TB, TC, TD, TStateA, TStateB, TStateC, TStateD>(a: Parse
434
434
  * in order, returning the result of the first successful parser.
435
435
  */
436
436
  declare function or<TA, TB, TC, TD, TE, 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, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>]>;
437
+ /**
438
+ * Creates a parser that combines six mutually exclusive parsers into one.
439
+ * The resulting parser will try each of the provided parsers in order,
440
+ * and return the result of the first successful parser.
441
+ * @template TA The type of the value returned by the first parser.
442
+ * @template TB The type of the value returned by the second parser.
443
+ * @template TC The type of the value returned by the third parser.
444
+ * @template TD The type of the value returned by the fourth parser.
445
+ * @template TE The type of the value returned by the fifth parser.
446
+ * @template TF The type of the value returned by the sixth parser.
447
+ * @template TStateA The type of the state used by the first parser.
448
+ * @template TStateB The type of the state used by the second parser.
449
+ * @template TStateC The type of the state used by the third parser.
450
+ * @template TStateD The type of the state used by the fourth parser.
451
+ * @template TStateE The type of the state used by the fifth parser.
452
+ * @template TStateF The type of the state used by the sixth parser.
453
+ * @param a The first {@link Parser} to try.
454
+ * @param b The second {@link Parser} to try.
455
+ * @param c The third {@link Parser} to try.
456
+ * @param d The fourth {@link Parser} to try.
457
+ * @param e The fifth {@link Parser} to try.
458
+ * @param f The sixth {@link Parser} to try.
459
+ * @return A {@link Parser} that tries to parse using the provided parsers
460
+ * in order, returning the result of the first successful parser.
461
+ */
462
+ declare function or<TA, TB, TC, TD, TE, TF, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>): Parser<TA | TB | TC | TD | TE | TF, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>]>;
463
+ /**
464
+ * Creates a parser that combines seven mutually exclusive parsers into one.
465
+ * The resulting parser will try each of the provided parsers in order,
466
+ * and return the result of the first successful parser.
467
+ * @template TA The type of the value returned by the first parser.
468
+ * @template TB The type of the value returned by the second parser.
469
+ * @template TC The type of the value returned by the third parser.
470
+ * @template TD The type of the value returned by the fourth parser.
471
+ * @template TE The type of the value returned by the fifth parser.
472
+ * @template TF The type of the value returned by the sixth parser.
473
+ * @template TG The type of the value returned by the seventh parser.
474
+ * @template TStateA The type of the state used by the first parser.
475
+ * @template TStateB The type of the state used by the second parser.
476
+ * @template TStateC The type of the state used by the third parser.
477
+ * @template TStateD The type of the state used by the fourth parser.
478
+ * @template TStateE The type of the state used by the fifth parser.
479
+ * @template TStateF The type of the state used by the sixth parser.
480
+ * @template TStateG The type of the state used by the seventh parser.
481
+ * @param a The first {@link Parser} to try.
482
+ * @param b The second {@link Parser} to try.
483
+ * @param c The third {@link Parser} to try.
484
+ * @param d The fourth {@link Parser} to try.
485
+ * @param e The fifth {@link Parser} to try.
486
+ * @param f The sixth {@link Parser} to try.
487
+ * @param g The seventh {@link Parser} to try.
488
+ * @return A {@link Parser} that tries to parse using the provided parsers
489
+ * in order, returning the result of the first successful parser.
490
+ */
491
+ declare function or<TA, TB, TC, TD, TE, TF, TG, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF, TStateG>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>, g: Parser<TG, TStateG>): Parser<TA | TB | TC | TD | TE | TF | TG, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>] | [6, ParserResult<TStateG>]>;
492
+ /**
493
+ * Creates a parser that combines eight mutually exclusive parsers into one.
494
+ * The resulting parser will try each of the provided parsers in order,
495
+ * and return the result of the first successful parser.
496
+ * @template TA The type of the value returned by the first parser.
497
+ * @template TB The type of the value returned by the second parser.
498
+ * @template TC The type of the value returned by the third parser.
499
+ * @template TD The type of the value returned by the fourth parser.
500
+ * @template TE The type of the value returned by the fifth parser.
501
+ * @template TF The type of the value returned by the sixth parser.
502
+ * @template TG The type of the value returned by the seventh parser.
503
+ * @template TH The type of the value returned by the eighth parser.
504
+ * @template TStateA The type of the state used by the first parser.
505
+ * @template TStateB The type of the state used by the second parser.
506
+ * @template TStateC The type of the state used by the third parser.
507
+ * @template TStateD The type of the state used by the fourth parser.
508
+ * @template TStateE The type of the state used by the fifth parser.
509
+ * @template TStateF The type of the state used by the sixth parser.
510
+ * @template TStateG The type of the state used by the seventh parser.
511
+ * @template TStateH The type of the state used by the eighth parser.
512
+ * @param a The first {@link Parser} to try.
513
+ * @param b The second {@link Parser} to try.
514
+ * @param c The third {@link Parser} to try.
515
+ * @param d The fourth {@link Parser} to try.
516
+ * @param e The fifth {@link Parser} to try.
517
+ * @param f The sixth {@link Parser} to try.
518
+ * @param g The seventh {@link Parser} to try.
519
+ * @param h The eighth {@link Parser} to try.
520
+ * @return A {@link Parser} that tries to parse using the provided parsers
521
+ * in order, returning the result of the first successful parser.
522
+ */
523
+ declare function or<TA, TB, TC, TD, TE, TF, TG, TH, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF, TStateG, TStateH>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>, g: Parser<TG, TStateG>, h: Parser<TH, TStateH>): Parser<TA | TB | TC | TD | TE | TF | TG | TH, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>] | [6, ParserResult<TStateG>] | [7, ParserResult<TStateH>]>;
524
+ /**
525
+ * Creates a parser that combines nine mutually exclusive parsers into one.
526
+ * The resulting parser will try each of the provided parsers in order,
527
+ * and return the result of the first successful parser.
528
+ * @template TA The type of the value returned by the first parser.
529
+ * @template TB The type of the value returned by the second parser.
530
+ * @template TC The type of the value returned by the third parser.
531
+ * @template TD The type of the value returned by the fourth parser.
532
+ * @template TE The type of the value returned by the fifth parser.
533
+ * @template TF The type of the value returned by the sixth parser.
534
+ * @template TG The type of the value returned by the seventh parser.
535
+ * @template TH The type of the value returned by the eighth parser.
536
+ * @template TI The type of the value returned by the ninth parser.
537
+ * @template TStateA The type of the state used by the first parser.
538
+ * @template TStateB The type of the state used by the second parser.
539
+ * @template TStateC The type of the state used by the third parser.
540
+ * @template TStateD The type of the state used by the fourth parser.
541
+ * @template TStateE The type of the state used by the fifth parser.
542
+ * @template TStateF The type of the state used by the sixth parser.
543
+ * @template TStateG The type of the state used by the seventh parser.
544
+ * @template TStateH The type of the state used by the eighth parser.
545
+ * @template TStateI The type of the state used by the ninth parser.
546
+ * @param a The first {@link Parser} to try.
547
+ * @param b The second {@link Parser} to try.
548
+ * @param c The third {@link Parser} to try.
549
+ * @param d The fourth {@link Parser} to try.
550
+ * @param e The fifth {@link Parser} to try.
551
+ * @param f The sixth {@link Parser} to try.
552
+ * @param g The seventh {@link Parser} to try.
553
+ * @param h The eighth {@link Parser} to try.
554
+ * @param i The ninth {@link Parser} to try.
555
+ * @return A {@link Parser} that tries to parse using the provided parsers
556
+ * in order, returning the result of the first successful parser.
557
+ */
558
+ declare function or<TA, TB, TC, TD, TE, TF, TG, TH, TI, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF, TStateG, TStateH, TStateI>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>, g: Parser<TG, TStateG>, h: Parser<TH, TStateH>, i: Parser<TI, TStateI>): Parser<TA | TB | TC | TD | TE | TF | TG | TH | TI, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>] | [6, ParserResult<TStateG>] | [7, ParserResult<TStateH>] | [8, ParserResult<TStateI>]>;
559
+ /**
560
+ * Creates a parser that combines ten mutually exclusive parsers into one.
561
+ * The resulting parser will try each of the provided parsers in order,
562
+ * and return the result of the first successful parser.
563
+ * @template TA The type of the value returned by the first parser.
564
+ * @template TB The type of the value returned by the second parser.
565
+ * @template TC The type of the value returned by the third parser.
566
+ * @template TD The type of the value returned by the fourth parser.
567
+ * @template TE The type of the value returned by the fifth parser.
568
+ * @template TF The type of the value returned by the sixth parser.
569
+ * @template TG The type of the value returned by the seventh parser.
570
+ * @template TH The type of the value returned by the eighth parser.
571
+ * @template TI The type of the value returned by the ninth parser.
572
+ * @template TJ The type of the value returned by the tenth parser.
573
+ * @template TStateA The type of the state used by the first parser.
574
+ * @template TStateB The type of the state used by the second parser.
575
+ * @template TStateC The type of the state used by the third parser.
576
+ * @template TStateD The type of the state used by the fourth parser.
577
+ * @template TStateE The type of the state used by the fifth parser.
578
+ * @template TStateF The type of the state used by the sixth parser.
579
+ * @template TStateG The type of the state used by the seventh parser.
580
+ * @template TStateH The type of the state used by the eighth parser.
581
+ * @template TStateI The type of the state used by the ninth parser.
582
+ * @template TStateJ The type of the state used by the tenth parser.
583
+ * @param a The first {@link Parser} to try.
584
+ * @param b The second {@link Parser} to try.
585
+ * @param c The third {@link Parser} to try.
586
+ * @param d The fourth {@link Parser} to try.
587
+ * @param e The fifth {@link Parser} to try.
588
+ * @param f The sixth {@link Parser} to try.
589
+ * @param g The seventh {@link Parser} to try.
590
+ * @param h The eighth {@link Parser} to try.
591
+ * @param i The ninth {@link Parser} to try.
592
+ * @param j The tenth {@link Parser} to try.
593
+ * @return A {@link Parser} that tries to parse using the provided parsers
594
+ * in order, returning the result of the first successful parser.
595
+ */
596
+ declare function or<TA, TB, TC, TD, TE, TF, TG, TH, TI, TJ, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF, TStateG, TStateH, TStateI, TStateJ>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>, g: Parser<TG, TStateG>, h: Parser<TH, TStateH>, i: Parser<TI, TStateI>, j: Parser<TJ, TStateJ>): Parser<TA | TB | TC | TD | TE | TF | TG | TH | TI | TJ, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>] | [6, ParserResult<TStateG>] | [7, ParserResult<TStateH>] | [8, ParserResult<TStateI>] | [9, ParserResult<TStateJ>]>;
437
597
  /**
438
598
  * Merges multiple {@link object} parsers into a single {@link object} parser.
439
599
  * It is useful for combining multiple {@link object} parsers so that
@@ -502,6 +662,107 @@ declare function merge<TA extends Parser<Record<string | symbol, unknown>, Recor
502
662
  * of the two parsers into a single object.
503
663
  */
504
664
  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 }>;
665
+ /**
666
+ * Concatenates two {@link tuple} parsers into a single parser that produces
667
+ * a flattened tuple containing the values from both parsers in order.
668
+ *
669
+ * This is similar to {@link merge} for object parsers, but operates on tuple
670
+ * parsers and preserves the sequential, positional nature of tuples by
671
+ * flattening the results into a single tuple array.
672
+ *
673
+ * @example
674
+ * ```typescript
675
+ * const basicTuple = tuple([
676
+ * option("-v", "--verbose"),
677
+ * option("-p", "--port", integer()),
678
+ * ]);
679
+ *
680
+ * const serverTuple = tuple([
681
+ * option("-h", "--host", string()),
682
+ * option("-d", "--debug"),
683
+ * ]);
684
+ *
685
+ * const combined = concat(basicTuple, serverTuple);
686
+ * // Type: Parser<[boolean, number, string, boolean], [BasicState, ServerState]>
687
+ *
688
+ * const result = parse(combined, ["-v", "-p", "8080", "-h", "localhost", "-d"]);
689
+ * // result.value: [true, 8080, "localhost", true]
690
+ * ```
691
+ *
692
+ * @template TA The value type of the first tuple parser.
693
+ * @template TB The value type of the second tuple parser.
694
+ * @template TStateA The state type of the first tuple parser.
695
+ * @template TStateB The state type of the second tuple parser.
696
+ * @param a The first {@link tuple} parser to concatenate.
697
+ * @param b The second {@link tuple} parser to concatenate.
698
+ * @return A new {@link tuple} parser that combines the values of both parsers
699
+ * into a single flattened tuple.
700
+ * @since 0.2.0
701
+ */
702
+ 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]>;
703
+ /**
704
+ * Concatenates three {@link tuple} parsers into a single parser that produces
705
+ * a flattened tuple containing the values from all parsers in order.
706
+ *
707
+ * @template TA The value type of the first tuple parser.
708
+ * @template TB The value type of the second tuple parser.
709
+ * @template TC The value type of the third tuple parser.
710
+ * @template TStateA The state type of the first tuple parser.
711
+ * @template TStateB The state type of the second tuple parser.
712
+ * @template TStateC The state type of the third tuple parser.
713
+ * @param a The first {@link tuple} parser to concatenate.
714
+ * @param b The second {@link tuple} parser to concatenate.
715
+ * @param c The third {@link tuple} parser to concatenate.
716
+ * @return A new {@link tuple} parser that combines the values of all parsers
717
+ * into a single flattened tuple.
718
+ * @since 0.2.0
719
+ */
720
+ 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]>;
721
+ /**
722
+ * Concatenates four {@link tuple} parsers into a single parser that produces
723
+ * a flattened tuple containing the values from all parsers in order.
724
+ *
725
+ * @template TA The value type of the first tuple parser.
726
+ * @template TB The value type of the second tuple parser.
727
+ * @template TC The value type of the third tuple parser.
728
+ * @template TD The value type of the fourth tuple parser.
729
+ * @template TStateA The state type of the first tuple parser.
730
+ * @template TStateB The state type of the second tuple parser.
731
+ * @template TStateC The state type of the third tuple parser.
732
+ * @template TStateD The state type of the fourth tuple parser.
733
+ * @param a The first {@link tuple} parser to concatenate.
734
+ * @param b The second {@link tuple} parser to concatenate.
735
+ * @param c The third {@link tuple} parser to concatenate.
736
+ * @param d The fourth {@link tuple} parser to concatenate.
737
+ * @return A new {@link tuple} parser that combines the values of all parsers
738
+ * into a single flattened tuple.
739
+ * @since 0.2.0
740
+ */
741
+ 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]>;
742
+ /**
743
+ * Concatenates five {@link tuple} parsers into a single parser that produces
744
+ * a flattened tuple containing the values from all parsers in order.
745
+ *
746
+ * @template TA The value type of the first tuple parser.
747
+ * @template TB The value type of the second tuple parser.
748
+ * @template TC The value type of the third tuple parser.
749
+ * @template TD The value type of the fourth tuple parser.
750
+ * @template TE The value type of the fifth tuple parser.
751
+ * @template TStateA The state type of the first tuple parser.
752
+ * @template TStateB The state type of the second tuple parser.
753
+ * @template TStateC The state type of the third tuple parser.
754
+ * @template TStateD The state type of the fourth tuple parser.
755
+ * @template TStateE The state type of the fifth tuple parser.
756
+ * @param a The first {@link tuple} parser to concatenate.
757
+ * @param b The second {@link tuple} parser to concatenate.
758
+ * @param c The third {@link tuple} parser to concatenate.
759
+ * @param d The fourth {@link tuple} parser to concatenate.
760
+ * @param e The fifth {@link tuple} parser to concatenate.
761
+ * @return A new {@link tuple} parser that combines the values of all parsers
762
+ * into a single flattened tuple.
763
+ * @since 0.2.0
764
+ */
765
+ 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
766
  /**
506
767
  * Options for the {@link command} parser.
507
768
  */
@@ -611,4 +872,4 @@ declare function parse<T>(parser: Parser<T, unknown>, args: readonly string[]):
611
872
  */
612
873
  declare function getDocPage(parser: Parser<unknown, unknown>, args?: readonly string[]): DocPage | undefined;
613
874
  //#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 };
875
+ 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
@@ -434,6 +434,166 @@ declare function or<TA, TB, TC, TD, TStateA, TStateB, TStateC, TStateD>(a: Parse
434
434
  * in order, returning the result of the first successful parser.
435
435
  */
436
436
  declare function or<TA, TB, TC, TD, TE, 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, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>]>;
437
+ /**
438
+ * Creates a parser that combines six mutually exclusive parsers into one.
439
+ * The resulting parser will try each of the provided parsers in order,
440
+ * and return the result of the first successful parser.
441
+ * @template TA The type of the value returned by the first parser.
442
+ * @template TB The type of the value returned by the second parser.
443
+ * @template TC The type of the value returned by the third parser.
444
+ * @template TD The type of the value returned by the fourth parser.
445
+ * @template TE The type of the value returned by the fifth parser.
446
+ * @template TF The type of the value returned by the sixth parser.
447
+ * @template TStateA The type of the state used by the first parser.
448
+ * @template TStateB The type of the state used by the second parser.
449
+ * @template TStateC The type of the state used by the third parser.
450
+ * @template TStateD The type of the state used by the fourth parser.
451
+ * @template TStateE The type of the state used by the fifth parser.
452
+ * @template TStateF The type of the state used by the sixth parser.
453
+ * @param a The first {@link Parser} to try.
454
+ * @param b The second {@link Parser} to try.
455
+ * @param c The third {@link Parser} to try.
456
+ * @param d The fourth {@link Parser} to try.
457
+ * @param e The fifth {@link Parser} to try.
458
+ * @param f The sixth {@link Parser} to try.
459
+ * @return A {@link Parser} that tries to parse using the provided parsers
460
+ * in order, returning the result of the first successful parser.
461
+ */
462
+ declare function or<TA, TB, TC, TD, TE, TF, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>): Parser<TA | TB | TC | TD | TE | TF, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>]>;
463
+ /**
464
+ * Creates a parser that combines seven mutually exclusive parsers into one.
465
+ * The resulting parser will try each of the provided parsers in order,
466
+ * and return the result of the first successful parser.
467
+ * @template TA The type of the value returned by the first parser.
468
+ * @template TB The type of the value returned by the second parser.
469
+ * @template TC The type of the value returned by the third parser.
470
+ * @template TD The type of the value returned by the fourth parser.
471
+ * @template TE The type of the value returned by the fifth parser.
472
+ * @template TF The type of the value returned by the sixth parser.
473
+ * @template TG The type of the value returned by the seventh parser.
474
+ * @template TStateA The type of the state used by the first parser.
475
+ * @template TStateB The type of the state used by the second parser.
476
+ * @template TStateC The type of the state used by the third parser.
477
+ * @template TStateD The type of the state used by the fourth parser.
478
+ * @template TStateE The type of the state used by the fifth parser.
479
+ * @template TStateF The type of the state used by the sixth parser.
480
+ * @template TStateG The type of the state used by the seventh parser.
481
+ * @param a The first {@link Parser} to try.
482
+ * @param b The second {@link Parser} to try.
483
+ * @param c The third {@link Parser} to try.
484
+ * @param d The fourth {@link Parser} to try.
485
+ * @param e The fifth {@link Parser} to try.
486
+ * @param f The sixth {@link Parser} to try.
487
+ * @param g The seventh {@link Parser} to try.
488
+ * @return A {@link Parser} that tries to parse using the provided parsers
489
+ * in order, returning the result of the first successful parser.
490
+ */
491
+ declare function or<TA, TB, TC, TD, TE, TF, TG, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF, TStateG>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>, g: Parser<TG, TStateG>): Parser<TA | TB | TC | TD | TE | TF | TG, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>] | [6, ParserResult<TStateG>]>;
492
+ /**
493
+ * Creates a parser that combines eight mutually exclusive parsers into one.
494
+ * The resulting parser will try each of the provided parsers in order,
495
+ * and return the result of the first successful parser.
496
+ * @template TA The type of the value returned by the first parser.
497
+ * @template TB The type of the value returned by the second parser.
498
+ * @template TC The type of the value returned by the third parser.
499
+ * @template TD The type of the value returned by the fourth parser.
500
+ * @template TE The type of the value returned by the fifth parser.
501
+ * @template TF The type of the value returned by the sixth parser.
502
+ * @template TG The type of the value returned by the seventh parser.
503
+ * @template TH The type of the value returned by the eighth parser.
504
+ * @template TStateA The type of the state used by the first parser.
505
+ * @template TStateB The type of the state used by the second parser.
506
+ * @template TStateC The type of the state used by the third parser.
507
+ * @template TStateD The type of the state used by the fourth parser.
508
+ * @template TStateE The type of the state used by the fifth parser.
509
+ * @template TStateF The type of the state used by the sixth parser.
510
+ * @template TStateG The type of the state used by the seventh parser.
511
+ * @template TStateH The type of the state used by the eighth parser.
512
+ * @param a The first {@link Parser} to try.
513
+ * @param b The second {@link Parser} to try.
514
+ * @param c The third {@link Parser} to try.
515
+ * @param d The fourth {@link Parser} to try.
516
+ * @param e The fifth {@link Parser} to try.
517
+ * @param f The sixth {@link Parser} to try.
518
+ * @param g The seventh {@link Parser} to try.
519
+ * @param h The eighth {@link Parser} to try.
520
+ * @return A {@link Parser} that tries to parse using the provided parsers
521
+ * in order, returning the result of the first successful parser.
522
+ */
523
+ declare function or<TA, TB, TC, TD, TE, TF, TG, TH, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF, TStateG, TStateH>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>, g: Parser<TG, TStateG>, h: Parser<TH, TStateH>): Parser<TA | TB | TC | TD | TE | TF | TG | TH, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>] | [6, ParserResult<TStateG>] | [7, ParserResult<TStateH>]>;
524
+ /**
525
+ * Creates a parser that combines nine mutually exclusive parsers into one.
526
+ * The resulting parser will try each of the provided parsers in order,
527
+ * and return the result of the first successful parser.
528
+ * @template TA The type of the value returned by the first parser.
529
+ * @template TB The type of the value returned by the second parser.
530
+ * @template TC The type of the value returned by the third parser.
531
+ * @template TD The type of the value returned by the fourth parser.
532
+ * @template TE The type of the value returned by the fifth parser.
533
+ * @template TF The type of the value returned by the sixth parser.
534
+ * @template TG The type of the value returned by the seventh parser.
535
+ * @template TH The type of the value returned by the eighth parser.
536
+ * @template TI The type of the value returned by the ninth parser.
537
+ * @template TStateA The type of the state used by the first parser.
538
+ * @template TStateB The type of the state used by the second parser.
539
+ * @template TStateC The type of the state used by the third parser.
540
+ * @template TStateD The type of the state used by the fourth parser.
541
+ * @template TStateE The type of the state used by the fifth parser.
542
+ * @template TStateF The type of the state used by the sixth parser.
543
+ * @template TStateG The type of the state used by the seventh parser.
544
+ * @template TStateH The type of the state used by the eighth parser.
545
+ * @template TStateI The type of the state used by the ninth parser.
546
+ * @param a The first {@link Parser} to try.
547
+ * @param b The second {@link Parser} to try.
548
+ * @param c The third {@link Parser} to try.
549
+ * @param d The fourth {@link Parser} to try.
550
+ * @param e The fifth {@link Parser} to try.
551
+ * @param f The sixth {@link Parser} to try.
552
+ * @param g The seventh {@link Parser} to try.
553
+ * @param h The eighth {@link Parser} to try.
554
+ * @param i The ninth {@link Parser} to try.
555
+ * @return A {@link Parser} that tries to parse using the provided parsers
556
+ * in order, returning the result of the first successful parser.
557
+ */
558
+ declare function or<TA, TB, TC, TD, TE, TF, TG, TH, TI, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF, TStateG, TStateH, TStateI>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>, g: Parser<TG, TStateG>, h: Parser<TH, TStateH>, i: Parser<TI, TStateI>): Parser<TA | TB | TC | TD | TE | TF | TG | TH | TI, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>] | [6, ParserResult<TStateG>] | [7, ParserResult<TStateH>] | [8, ParserResult<TStateI>]>;
559
+ /**
560
+ * Creates a parser that combines ten mutually exclusive parsers into one.
561
+ * The resulting parser will try each of the provided parsers in order,
562
+ * and return the result of the first successful parser.
563
+ * @template TA The type of the value returned by the first parser.
564
+ * @template TB The type of the value returned by the second parser.
565
+ * @template TC The type of the value returned by the third parser.
566
+ * @template TD The type of the value returned by the fourth parser.
567
+ * @template TE The type of the value returned by the fifth parser.
568
+ * @template TF The type of the value returned by the sixth parser.
569
+ * @template TG The type of the value returned by the seventh parser.
570
+ * @template TH The type of the value returned by the eighth parser.
571
+ * @template TI The type of the value returned by the ninth parser.
572
+ * @template TJ The type of the value returned by the tenth parser.
573
+ * @template TStateA The type of the state used by the first parser.
574
+ * @template TStateB The type of the state used by the second parser.
575
+ * @template TStateC The type of the state used by the third parser.
576
+ * @template TStateD The type of the state used by the fourth parser.
577
+ * @template TStateE The type of the state used by the fifth parser.
578
+ * @template TStateF The type of the state used by the sixth parser.
579
+ * @template TStateG The type of the state used by the seventh parser.
580
+ * @template TStateH The type of the state used by the eighth parser.
581
+ * @template TStateI The type of the state used by the ninth parser.
582
+ * @template TStateJ The type of the state used by the tenth parser.
583
+ * @param a The first {@link Parser} to try.
584
+ * @param b The second {@link Parser} to try.
585
+ * @param c The third {@link Parser} to try.
586
+ * @param d The fourth {@link Parser} to try.
587
+ * @param e The fifth {@link Parser} to try.
588
+ * @param f The sixth {@link Parser} to try.
589
+ * @param g The seventh {@link Parser} to try.
590
+ * @param h The eighth {@link Parser} to try.
591
+ * @param i The ninth {@link Parser} to try.
592
+ * @param j The tenth {@link Parser} to try.
593
+ * @return A {@link Parser} that tries to parse using the provided parsers
594
+ * in order, returning the result of the first successful parser.
595
+ */
596
+ declare function or<TA, TB, TC, TD, TE, TF, TG, TH, TI, TJ, TStateA, TStateB, TStateC, TStateD, TStateE, TStateF, TStateG, TStateH, TStateI, TStateJ>(a: Parser<TA, TStateA>, b: Parser<TB, TStateB>, c: Parser<TC, TStateC>, d: Parser<TD, TStateD>, e: Parser<TE, TStateE>, f: Parser<TF, TStateF>, g: Parser<TG, TStateG>, h: Parser<TH, TStateH>, i: Parser<TI, TStateI>, j: Parser<TJ, TStateJ>): Parser<TA | TB | TC | TD | TE | TF | TG | TH | TI | TJ, undefined | [0, ParserResult<TStateA>] | [1, ParserResult<TStateB>] | [2, ParserResult<TStateC>] | [3, ParserResult<TStateD>] | [4, ParserResult<TStateE>] | [5, ParserResult<TStateF>] | [6, ParserResult<TStateG>] | [7, ParserResult<TStateH>] | [8, ParserResult<TStateI>] | [9, ParserResult<TStateJ>]>;
437
597
  /**
438
598
  * Merges multiple {@link object} parsers into a single {@link object} parser.
439
599
  * It is useful for combining multiple {@link object} parsers so that
@@ -502,6 +662,107 @@ declare function merge<TA extends Parser<Record<string | symbol, unknown>, Recor
502
662
  * of the two parsers into a single object.
503
663
  */
504
664
  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 }>;
665
+ /**
666
+ * Concatenates two {@link tuple} parsers into a single parser that produces
667
+ * a flattened tuple containing the values from both parsers in order.
668
+ *
669
+ * This is similar to {@link merge} for object parsers, but operates on tuple
670
+ * parsers and preserves the sequential, positional nature of tuples by
671
+ * flattening the results into a single tuple array.
672
+ *
673
+ * @example
674
+ * ```typescript
675
+ * const basicTuple = tuple([
676
+ * option("-v", "--verbose"),
677
+ * option("-p", "--port", integer()),
678
+ * ]);
679
+ *
680
+ * const serverTuple = tuple([
681
+ * option("-h", "--host", string()),
682
+ * option("-d", "--debug"),
683
+ * ]);
684
+ *
685
+ * const combined = concat(basicTuple, serverTuple);
686
+ * // Type: Parser<[boolean, number, string, boolean], [BasicState, ServerState]>
687
+ *
688
+ * const result = parse(combined, ["-v", "-p", "8080", "-h", "localhost", "-d"]);
689
+ * // result.value: [true, 8080, "localhost", true]
690
+ * ```
691
+ *
692
+ * @template TA The value type of the first tuple parser.
693
+ * @template TB The value type of the second tuple parser.
694
+ * @template TStateA The state type of the first tuple parser.
695
+ * @template TStateB The state type of the second tuple parser.
696
+ * @param a The first {@link tuple} parser to concatenate.
697
+ * @param b The second {@link tuple} parser to concatenate.
698
+ * @return A new {@link tuple} parser that combines the values of both parsers
699
+ * into a single flattened tuple.
700
+ * @since 0.2.0
701
+ */
702
+ 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]>;
703
+ /**
704
+ * Concatenates three {@link tuple} parsers into a single parser that produces
705
+ * a flattened tuple containing the values from all parsers in order.
706
+ *
707
+ * @template TA The value type of the first tuple parser.
708
+ * @template TB The value type of the second tuple parser.
709
+ * @template TC The value type of the third tuple parser.
710
+ * @template TStateA The state type of the first tuple parser.
711
+ * @template TStateB The state type of the second tuple parser.
712
+ * @template TStateC The state type of the third tuple parser.
713
+ * @param a The first {@link tuple} parser to concatenate.
714
+ * @param b The second {@link tuple} parser to concatenate.
715
+ * @param c The third {@link tuple} parser to concatenate.
716
+ * @return A new {@link tuple} parser that combines the values of all parsers
717
+ * into a single flattened tuple.
718
+ * @since 0.2.0
719
+ */
720
+ 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]>;
721
+ /**
722
+ * Concatenates four {@link tuple} parsers into a single parser that produces
723
+ * a flattened tuple containing the values from all parsers in order.
724
+ *
725
+ * @template TA The value type of the first tuple parser.
726
+ * @template TB The value type of the second tuple parser.
727
+ * @template TC The value type of the third tuple parser.
728
+ * @template TD The value type of the fourth tuple parser.
729
+ * @template TStateA The state type of the first tuple parser.
730
+ * @template TStateB The state type of the second tuple parser.
731
+ * @template TStateC The state type of the third tuple parser.
732
+ * @template TStateD The state type of the fourth tuple parser.
733
+ * @param a The first {@link tuple} parser to concatenate.
734
+ * @param b The second {@link tuple} parser to concatenate.
735
+ * @param c The third {@link tuple} parser to concatenate.
736
+ * @param d The fourth {@link tuple} parser to concatenate.
737
+ * @return A new {@link tuple} parser that combines the values of all parsers
738
+ * into a single flattened tuple.
739
+ * @since 0.2.0
740
+ */
741
+ 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]>;
742
+ /**
743
+ * Concatenates five {@link tuple} parsers into a single parser that produces
744
+ * a flattened tuple containing the values from all parsers in order.
745
+ *
746
+ * @template TA The value type of the first tuple parser.
747
+ * @template TB The value type of the second tuple parser.
748
+ * @template TC The value type of the third tuple parser.
749
+ * @template TD The value type of the fourth tuple parser.
750
+ * @template TE The value type of the fifth tuple parser.
751
+ * @template TStateA The state type of the first tuple parser.
752
+ * @template TStateB The state type of the second tuple parser.
753
+ * @template TStateC The state type of the third tuple parser.
754
+ * @template TStateD The state type of the fourth tuple parser.
755
+ * @template TStateE The state type of the fifth tuple parser.
756
+ * @param a The first {@link tuple} parser to concatenate.
757
+ * @param b The second {@link tuple} parser to concatenate.
758
+ * @param c The third {@link tuple} parser to concatenate.
759
+ * @param d The fourth {@link tuple} parser to concatenate.
760
+ * @param e The fifth {@link tuple} parser to concatenate.
761
+ * @return A new {@link tuple} parser that combines the values of all parsers
762
+ * into a single flattened tuple.
763
+ * @since 0.2.0
764
+ */
765
+ 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
766
  /**
506
767
  * Options for the {@link command} parser.
507
768
  */
@@ -611,4 +872,4 @@ declare function parse<T>(parser: Parser<T, unknown>, args: readonly string[]):
611
872
  */
612
873
  declare function getDocPage(parser: Parser<unknown, unknown>, args?: readonly string[]): DocPage | undefined;
613
874
  //#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 };
875
+ 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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "0.2.0-dev.32+58f16652",
3
+ "version": "0.3.0-dev.34+d4931ae5",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",