@optique/core 1.0.0-dev.1244 → 1.0.0-dev.1252

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.
@@ -8,6 +8,21 @@ const require_usage_internals = require('./usage-internals.cjs');
8
8
 
9
9
  //#region src/constructs.ts
10
10
  const inheritParentAnnotationsKey = Symbol.for("@optique/core/inheritParentAnnotations");
11
+ /**
12
+ * Returns the field state with parent annotations inherited, respecting
13
+ * the parser's {@link inheritParentAnnotationsKey} flag. This is the
14
+ * same logic as {@link createFieldStateGetter} inside `object()` but
15
+ * without the per-state cache, for use in module-level helpers like
16
+ * {@link pendingDependencyDefaults}.
17
+ * @internal
18
+ */
19
+ function getAnnotatedFieldState(parentState, field, parser) {
20
+ const sourceState = parentState != null && typeof parentState === "object" && field in parentState ? parentState[field] : parser.initialState;
21
+ if (sourceState == null || typeof sourceState !== "object") return sourceState;
22
+ const annotations = require_annotations.getAnnotations(parentState);
23
+ if (annotations === void 0 || require_annotations.getAnnotations(sourceState) === annotations) return sourceState;
24
+ return Reflect.get(parser, inheritParentAnnotationsKey) === true ? require_annotations.injectAnnotations(sourceState, annotations) : require_annotations.inheritAnnotations(parentState, sourceState);
25
+ }
11
26
  function createUnexpectedInputErrorWithScopedSuggestions(baseError, invalidInput, parsers, customFormatter) {
12
27
  const options = /* @__PURE__ */ new Set();
13
28
  const commands = /* @__PURE__ */ new Set();
@@ -693,7 +708,13 @@ function registerCompletedDependency(completed, registry) {
693
708
  function* pendingDependencyDefaults(context, parserPairs) {
694
709
  for (const [field, fieldParser] of parserPairs) {
695
710
  const fieldState = context.state != null && typeof context.state === "object" && field in context.state ? context.state[field] : void 0;
696
- if (fieldState != null) continue;
711
+ if (fieldState != null) {
712
+ if (!Array.isArray(fieldState) && !require_dependency.isDependencySourceState(fieldState) && (require_dependency.isWrappedDependencySource(fieldParser) || require_dependency.isPendingDependencySourceState(fieldParser.initialState))) yield {
713
+ parser: fieldParser,
714
+ state: getAnnotatedFieldState(context.state, field, fieldParser)
715
+ };
716
+ continue;
717
+ }
697
718
  if (require_dependency.isPendingDependencySourceState(fieldParser.initialState)) yield {
698
719
  parser: fieldParser,
699
720
  state: fieldParser.initialState
@@ -714,7 +735,31 @@ function* pendingDependencyDefaults(context, parserPairs) {
714
735
  * @internal
715
736
  */
716
737
  function completeDependencySourceDefaults(context, parserPairs, registry) {
717
- for (const { parser, state } of pendingDependencyDefaults(context, parserPairs)) registerCompletedDependency(parser.complete(state), registry);
738
+ for (const { parser, state } of pendingDependencyDefaults(context, parserPairs)) {
739
+ const completed = parser.complete(state);
740
+ const depState = wrapAsDependencySourceState(completed, parser);
741
+ if (depState) registerCompletedDependency(depState, registry);
742
+ }
743
+ }
744
+ /**
745
+ * Wraps a completed result as a {@link DependencySourceState} if the parser
746
+ * contains a dependency source and the result is a successful plain Result
747
+ * with a defined value. Returns the existing state if the result is already
748
+ * a DependencySourceState, or `undefined` if no wrapping applies.
749
+ *
750
+ * This helper is shared by both `object()` Phase 1 and the suggest-time
751
+ * pre-completion paths to keep the dep-ID selection and `value !== undefined`
752
+ * rule in one place.
753
+ * @internal
754
+ */
755
+ function wrapAsDependencySourceState(completed, parser) {
756
+ if (require_dependency.isDependencySourceState(completed)) return completed;
757
+ const hasDep = require_dependency.isWrappedDependencySource(parser) || require_dependency.isPendingDependencySourceState(parser.initialState);
758
+ if (hasDep && typeof completed === "object" && completed !== null && "success" in completed && completed.success && "value" in completed && completed.value !== void 0) {
759
+ const depId = require_dependency.isWrappedDependencySource(parser) ? parser[require_dependency.wrappedDependencySourceMarker][require_dependency.dependencyId] : parser.initialState[require_dependency.dependencyId];
760
+ return require_dependency.createDependencySourceState(completed, depId);
761
+ }
762
+ return void 0;
718
763
  }
719
764
  /**
720
765
  * Async version of {@link completeDependencySourceDefaults} that awaits
@@ -725,7 +770,11 @@ function completeDependencySourceDefaults(context, parserPairs, registry) {
725
770
  * @internal
726
771
  */
727
772
  async function completeDependencySourceDefaultsAsync(context, parserPairs, registry) {
728
- for (const { parser, state } of pendingDependencyDefaults(context, parserPairs)) registerCompletedDependency(await parser.complete(state), registry);
773
+ for (const { parser, state } of pendingDependencyDefaults(context, parserPairs)) {
774
+ const completed = await parser.complete(state);
775
+ const depState = wrapAsDependencySourceState(completed, parser);
776
+ if (depState) registerCompletedDependency(depState, registry);
777
+ }
729
778
  }
730
779
  /**
731
780
  * Recursively collects dependency values from DependencySourceState objects
@@ -1085,6 +1134,14 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1085
1134
  preCompletedState[fieldKey] = completed;
1086
1135
  preCompletedKeys.add(fieldKey);
1087
1136
  } else preCompletedState[fieldKey] = fieldState;
1137
+ } else if (fieldState != null && !Array.isArray(fieldState) && !require_dependency.isDependencySourceState(fieldState) && (require_dependency.isWrappedDependencySource(fieldParser) || require_dependency.isPendingDependencySourceState(fieldParser.initialState))) {
1138
+ const annotatedFieldState = getFieldState(field, fieldParser);
1139
+ const completed = fieldParser.complete(annotatedFieldState);
1140
+ const depState = wrapAsDependencySourceState(completed, fieldParser);
1141
+ if (depState) {
1142
+ preCompletedState[fieldKey] = depState;
1143
+ preCompletedKeys.add(fieldKey);
1144
+ } else preCompletedState[fieldKey] = annotatedFieldState;
1088
1145
  } else preCompletedState[fieldKey] = getFieldState(field, fieldParser);
1089
1146
  }
1090
1147
  const resolvedState = resolveDeferredParseStates(preCompletedState);
@@ -1138,6 +1195,14 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1138
1195
  preCompletedState[fieldKey] = completed;
1139
1196
  preCompletedKeys.add(fieldKey);
1140
1197
  } else preCompletedState[fieldKey] = fieldState;
1198
+ } else if (fieldState != null && !Array.isArray(fieldState) && !require_dependency.isDependencySourceState(fieldState) && (require_dependency.isWrappedDependencySource(fieldParser) || require_dependency.isPendingDependencySourceState(fieldParser.initialState))) {
1199
+ const annotatedFieldState = getFieldState(field, fieldParser);
1200
+ const completed = await fieldParser.complete(annotatedFieldState);
1201
+ const depState = wrapAsDependencySourceState(completed, fieldParser);
1202
+ if (depState) {
1203
+ preCompletedState[fieldKey] = depState;
1204
+ preCompletedKeys.add(fieldKey);
1205
+ } else preCompletedState[fieldKey] = annotatedFieldState;
1141
1206
  } else preCompletedState[fieldKey] = getFieldState(field, fieldParser);
1142
1207
  }
1143
1208
  const resolvedState = await resolveDeferredParseStatesAsync(preCompletedState);
@@ -1,6 +1,6 @@
1
1
  import { getAnnotations, inheritAnnotations, injectAnnotations } from "./annotations.js";
2
2
  import { message, optionName, text, values } from "./message.js";
3
- import { DependencyRegistry, dependencyId, isDeferredParseState, isDependencySourceState, isPendingDependencySourceState, isWrappedDependencySource, parseWithDependency, wrappedDependencySourceMarker } from "./dependency.js";
3
+ import { DependencyRegistry, createDependencySourceState, dependencyId, isDeferredParseState, isDependencySourceState, isPendingDependencySourceState, isWrappedDependencySource, parseWithDependency, wrappedDependencySourceMarker } from "./dependency.js";
4
4
  import { dispatchByMode, dispatchIterableByMode } from "./mode-dispatch.js";
5
5
  import { extractArgumentMetavars, extractCommandNames, extractOptionNames, isDocHidden, mergeHidden } from "./usage.js";
6
6
  import { DEFAULT_FIND_SIMILAR_OPTIONS, createErrorWithSuggestions, createSuggestionMessage, deduplicateSuggestions, findSimilar } from "./suggestion.js";
@@ -8,6 +8,21 @@ import { collectLeadingCandidates } from "./usage-internals.js";
8
8
 
9
9
  //#region src/constructs.ts
10
10
  const inheritParentAnnotationsKey = Symbol.for("@optique/core/inheritParentAnnotations");
11
+ /**
12
+ * Returns the field state with parent annotations inherited, respecting
13
+ * the parser's {@link inheritParentAnnotationsKey} flag. This is the
14
+ * same logic as {@link createFieldStateGetter} inside `object()` but
15
+ * without the per-state cache, for use in module-level helpers like
16
+ * {@link pendingDependencyDefaults}.
17
+ * @internal
18
+ */
19
+ function getAnnotatedFieldState(parentState, field, parser) {
20
+ const sourceState = parentState != null && typeof parentState === "object" && field in parentState ? parentState[field] : parser.initialState;
21
+ if (sourceState == null || typeof sourceState !== "object") return sourceState;
22
+ const annotations = getAnnotations(parentState);
23
+ if (annotations === void 0 || getAnnotations(sourceState) === annotations) return sourceState;
24
+ return Reflect.get(parser, inheritParentAnnotationsKey) === true ? injectAnnotations(sourceState, annotations) : inheritAnnotations(parentState, sourceState);
25
+ }
11
26
  function createUnexpectedInputErrorWithScopedSuggestions(baseError, invalidInput, parsers, customFormatter) {
12
27
  const options = /* @__PURE__ */ new Set();
13
28
  const commands = /* @__PURE__ */ new Set();
@@ -693,7 +708,13 @@ function registerCompletedDependency(completed, registry) {
693
708
  function* pendingDependencyDefaults(context, parserPairs) {
694
709
  for (const [field, fieldParser] of parserPairs) {
695
710
  const fieldState = context.state != null && typeof context.state === "object" && field in context.state ? context.state[field] : void 0;
696
- if (fieldState != null) continue;
711
+ if (fieldState != null) {
712
+ if (!Array.isArray(fieldState) && !isDependencySourceState(fieldState) && (isWrappedDependencySource(fieldParser) || isPendingDependencySourceState(fieldParser.initialState))) yield {
713
+ parser: fieldParser,
714
+ state: getAnnotatedFieldState(context.state, field, fieldParser)
715
+ };
716
+ continue;
717
+ }
697
718
  if (isPendingDependencySourceState(fieldParser.initialState)) yield {
698
719
  parser: fieldParser,
699
720
  state: fieldParser.initialState
@@ -714,7 +735,31 @@ function* pendingDependencyDefaults(context, parserPairs) {
714
735
  * @internal
715
736
  */
716
737
  function completeDependencySourceDefaults(context, parserPairs, registry) {
717
- for (const { parser, state } of pendingDependencyDefaults(context, parserPairs)) registerCompletedDependency(parser.complete(state), registry);
738
+ for (const { parser, state } of pendingDependencyDefaults(context, parserPairs)) {
739
+ const completed = parser.complete(state);
740
+ const depState = wrapAsDependencySourceState(completed, parser);
741
+ if (depState) registerCompletedDependency(depState, registry);
742
+ }
743
+ }
744
+ /**
745
+ * Wraps a completed result as a {@link DependencySourceState} if the parser
746
+ * contains a dependency source and the result is a successful plain Result
747
+ * with a defined value. Returns the existing state if the result is already
748
+ * a DependencySourceState, or `undefined` if no wrapping applies.
749
+ *
750
+ * This helper is shared by both `object()` Phase 1 and the suggest-time
751
+ * pre-completion paths to keep the dep-ID selection and `value !== undefined`
752
+ * rule in one place.
753
+ * @internal
754
+ */
755
+ function wrapAsDependencySourceState(completed, parser) {
756
+ if (isDependencySourceState(completed)) return completed;
757
+ const hasDep = isWrappedDependencySource(parser) || isPendingDependencySourceState(parser.initialState);
758
+ if (hasDep && typeof completed === "object" && completed !== null && "success" in completed && completed.success && "value" in completed && completed.value !== void 0) {
759
+ const depId = isWrappedDependencySource(parser) ? parser[wrappedDependencySourceMarker][dependencyId] : parser.initialState[dependencyId];
760
+ return createDependencySourceState(completed, depId);
761
+ }
762
+ return void 0;
718
763
  }
719
764
  /**
720
765
  * Async version of {@link completeDependencySourceDefaults} that awaits
@@ -725,7 +770,11 @@ function completeDependencySourceDefaults(context, parserPairs, registry) {
725
770
  * @internal
726
771
  */
727
772
  async function completeDependencySourceDefaultsAsync(context, parserPairs, registry) {
728
- for (const { parser, state } of pendingDependencyDefaults(context, parserPairs)) registerCompletedDependency(await parser.complete(state), registry);
773
+ for (const { parser, state } of pendingDependencyDefaults(context, parserPairs)) {
774
+ const completed = await parser.complete(state);
775
+ const depState = wrapAsDependencySourceState(completed, parser);
776
+ if (depState) registerCompletedDependency(depState, registry);
777
+ }
729
778
  }
730
779
  /**
731
780
  * Recursively collects dependency values from DependencySourceState objects
@@ -1085,6 +1134,14 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1085
1134
  preCompletedState[fieldKey] = completed;
1086
1135
  preCompletedKeys.add(fieldKey);
1087
1136
  } else preCompletedState[fieldKey] = fieldState;
1137
+ } else if (fieldState != null && !Array.isArray(fieldState) && !isDependencySourceState(fieldState) && (isWrappedDependencySource(fieldParser) || isPendingDependencySourceState(fieldParser.initialState))) {
1138
+ const annotatedFieldState = getFieldState(field, fieldParser);
1139
+ const completed = fieldParser.complete(annotatedFieldState);
1140
+ const depState = wrapAsDependencySourceState(completed, fieldParser);
1141
+ if (depState) {
1142
+ preCompletedState[fieldKey] = depState;
1143
+ preCompletedKeys.add(fieldKey);
1144
+ } else preCompletedState[fieldKey] = annotatedFieldState;
1088
1145
  } else preCompletedState[fieldKey] = getFieldState(field, fieldParser);
1089
1146
  }
1090
1147
  const resolvedState = resolveDeferredParseStates(preCompletedState);
@@ -1138,6 +1195,14 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1138
1195
  preCompletedState[fieldKey] = completed;
1139
1196
  preCompletedKeys.add(fieldKey);
1140
1197
  } else preCompletedState[fieldKey] = fieldState;
1198
+ } else if (fieldState != null && !Array.isArray(fieldState) && !isDependencySourceState(fieldState) && (isWrappedDependencySource(fieldParser) || isPendingDependencySourceState(fieldParser.initialState))) {
1199
+ const annotatedFieldState = getFieldState(field, fieldParser);
1200
+ const completed = await fieldParser.complete(annotatedFieldState);
1201
+ const depState = wrapAsDependencySourceState(completed, fieldParser);
1202
+ if (depState) {
1203
+ preCompletedState[fieldKey] = depState;
1204
+ preCompletedKeys.add(fieldKey);
1205
+ } else preCompletedState[fieldKey] = annotatedFieldState;
1141
1206
  } else preCompletedState[fieldKey] = getFieldState(field, fieldParser);
1142
1207
  }
1143
1208
  const resolvedState = await resolveDeferredParseStatesAsync(preCompletedState);
@@ -2647,8 +2647,30 @@ function cidr(options) {
2647
2647
  const maxPrefix = options?.maxPrefix;
2648
2648
  const errors = options?.errors;
2649
2649
  const metavar = options?.metavar ?? "CIDR";
2650
- const ipv4Parser = version === 4 || version === "both" ? ipv4(options?.ipv4) : null;
2651
- const ipv6Parser = version === 6 || version === "both" ? ipv6(options?.ipv6) : null;
2650
+ const genericIpSentinel = [];
2651
+ const ipv4Parser = version === 4 || version === "both" ? ipv4({
2652
+ ...options?.ipv4,
2653
+ errors: {
2654
+ invalidIpv4: genericIpSentinel,
2655
+ privateNotAllowed: errors?.privateNotAllowed,
2656
+ loopbackNotAllowed: errors?.loopbackNotAllowed,
2657
+ linkLocalNotAllowed: errors?.linkLocalNotAllowed,
2658
+ multicastNotAllowed: errors?.multicastNotAllowed,
2659
+ broadcastNotAllowed: errors?.broadcastNotAllowed,
2660
+ zeroNotAllowed: errors?.zeroNotAllowed
2661
+ }
2662
+ }) : null;
2663
+ const ipv6Parser = version === 6 || version === "both" ? ipv6({
2664
+ ...options?.ipv6,
2665
+ errors: {
2666
+ invalidIpv6: genericIpSentinel,
2667
+ loopbackNotAllowed: errors?.loopbackNotAllowed,
2668
+ linkLocalNotAllowed: errors?.linkLocalNotAllowed,
2669
+ multicastNotAllowed: errors?.multicastNotAllowed,
2670
+ zeroNotAllowed: errors?.zeroNotAllowed,
2671
+ uniqueLocalNotAllowed: errors?.uniqueLocalNotAllowed
2672
+ }
2673
+ }) : null;
2652
2674
  return {
2653
2675
  $mode: "sync",
2654
2676
  metavar,
@@ -2709,6 +2731,8 @@ function cidr(options) {
2709
2731
  }
2710
2732
  let ipVersion = null;
2711
2733
  let normalizedIp = null;
2734
+ let ipv4Error = null;
2735
+ let ipv6Error = null;
2712
2736
  if (ipv4Parser !== null) {
2713
2737
  const result = ipv4Parser.parse(ipPart);
2714
2738
  if (result.success) {
@@ -2747,7 +2771,7 @@ function cidr(options) {
2747
2771
  error: msg
2748
2772
  };
2749
2773
  }
2750
- }
2774
+ } else ipv4Error = result;
2751
2775
  }
2752
2776
  if (ipVersion === null && ipv6Parser !== null) {
2753
2777
  const result = ipv6Parser.parse(ipPart);
@@ -2787,9 +2811,120 @@ function cidr(options) {
2787
2811
  error: msg
2788
2812
  };
2789
2813
  }
2790
- }
2814
+ } else ipv6Error = result;
2791
2815
  }
2792
2816
  if (ipVersion === null || normalizedIp === null) {
2817
+ const candidates = [[
2818
+ ipv4Error,
2819
+ 4,
2820
+ 32
2821
+ ], [
2822
+ ipv6Error,
2823
+ 6,
2824
+ 128
2825
+ ]];
2826
+ for (const [err, ver, maxPfx] of candidates) if (err !== null && !err.success && err.error !== genericIpSentinel) {
2827
+ if (prefix > maxPfx) {
2828
+ const errorMsg$1 = errors?.invalidPrefix;
2829
+ if (typeof errorMsg$1 === "function") return {
2830
+ success: false,
2831
+ error: errorMsg$1(prefix, ver)
2832
+ };
2833
+ const msg$1 = errorMsg$1 ?? [
2834
+ {
2835
+ type: "text",
2836
+ text: "Expected a prefix length between 0 and "
2837
+ },
2838
+ {
2839
+ type: "text",
2840
+ text: maxPfx.toString()
2841
+ },
2842
+ {
2843
+ type: "text",
2844
+ text: ` for IPv${ver}, but got `
2845
+ },
2846
+ {
2847
+ type: "text",
2848
+ text: prefix.toString()
2849
+ },
2850
+ {
2851
+ type: "text",
2852
+ text: "."
2853
+ }
2854
+ ];
2855
+ return {
2856
+ success: false,
2857
+ error: msg$1
2858
+ };
2859
+ }
2860
+ if (minPrefix !== void 0 && prefix < minPrefix) {
2861
+ const errorMsg$1 = errors?.prefixBelowMinimum;
2862
+ if (typeof errorMsg$1 === "function") return {
2863
+ success: false,
2864
+ error: errorMsg$1(prefix, minPrefix)
2865
+ };
2866
+ const msg$1 = errorMsg$1 ?? [
2867
+ {
2868
+ type: "text",
2869
+ text: "Expected a prefix length greater than or equal to "
2870
+ },
2871
+ {
2872
+ type: "text",
2873
+ text: minPrefix.toString()
2874
+ },
2875
+ {
2876
+ type: "text",
2877
+ text: ", but got "
2878
+ },
2879
+ {
2880
+ type: "text",
2881
+ text: prefix.toString()
2882
+ },
2883
+ {
2884
+ type: "text",
2885
+ text: "."
2886
+ }
2887
+ ];
2888
+ return {
2889
+ success: false,
2890
+ error: msg$1
2891
+ };
2892
+ }
2893
+ if (maxPrefix !== void 0 && prefix > maxPrefix) {
2894
+ const errorMsg$1 = errors?.prefixAboveMaximum;
2895
+ if (typeof errorMsg$1 === "function") return {
2896
+ success: false,
2897
+ error: errorMsg$1(prefix, maxPrefix)
2898
+ };
2899
+ const msg$1 = errorMsg$1 ?? [
2900
+ {
2901
+ type: "text",
2902
+ text: "Expected a prefix length less than or equal to "
2903
+ },
2904
+ {
2905
+ type: "text",
2906
+ text: maxPrefix.toString()
2907
+ },
2908
+ {
2909
+ type: "text",
2910
+ text: ", but got "
2911
+ },
2912
+ {
2913
+ type: "text",
2914
+ text: prefix.toString()
2915
+ },
2916
+ {
2917
+ type: "text",
2918
+ text: "."
2919
+ }
2920
+ ];
2921
+ return {
2922
+ success: false,
2923
+ error: msg$1
2924
+ };
2925
+ }
2926
+ return err;
2927
+ }
2793
2928
  const errorMsg = errors?.invalidCidr;
2794
2929
  if (typeof errorMsg === "function") return {
2795
2930
  success: false,
@@ -1922,6 +1922,50 @@ interface CidrOptions {
1922
1922
  * Can be a static message or a function that receives the prefix and maximum.
1923
1923
  */
1924
1924
  prefixAboveMaximum?: Message | ((prefix: number, max: number) => Message);
1925
+ /**
1926
+ * Custom error message when a private IPv4 address is used but disallowed.
1927
+ * Can be a static message or a function that receives the IP.
1928
+ * @since 1.0.0
1929
+ */
1930
+ privateNotAllowed?: Message | ((ip: string) => Message);
1931
+ /**
1932
+ * Custom error message when a loopback address is used but disallowed.
1933
+ * Can be a static message or a function that receives the IP.
1934
+ * @since 1.0.0
1935
+ */
1936
+ loopbackNotAllowed?: Message | ((ip: string) => Message);
1937
+ /**
1938
+ * Custom error message when a link-local address is used but disallowed.
1939
+ * Can be a static message or a function that receives the IP.
1940
+ * @since 1.0.0
1941
+ */
1942
+ linkLocalNotAllowed?: Message | ((ip: string) => Message);
1943
+ /**
1944
+ * Custom error message when a multicast address is used but disallowed.
1945
+ * Can be a static message or a function that receives the IP.
1946
+ * @since 1.0.0
1947
+ */
1948
+ multicastNotAllowed?: Message | ((ip: string) => Message);
1949
+ /**
1950
+ * Custom error message when the broadcast address is used but disallowed
1951
+ * (IPv4 only).
1952
+ * Can be a static message or a function that receives the IP.
1953
+ * @since 1.0.0
1954
+ */
1955
+ broadcastNotAllowed?: Message | ((ip: string) => Message);
1956
+ /**
1957
+ * Custom error message when the zero address is used but disallowed.
1958
+ * Can be a static message or a function that receives the IP.
1959
+ * @since 1.0.0
1960
+ */
1961
+ zeroNotAllowed?: Message | ((ip: string) => Message);
1962
+ /**
1963
+ * Custom error message when a unique local address is used but disallowed
1964
+ * (IPv6 only).
1965
+ * Can be a static message or a function that receives the IP.
1966
+ * @since 1.0.0
1967
+ */
1968
+ uniqueLocalNotAllowed?: Message | ((ip: string) => Message);
1925
1969
  };
1926
1970
  }
1927
1971
  /**
@@ -1922,6 +1922,50 @@ interface CidrOptions {
1922
1922
  * Can be a static message or a function that receives the prefix and maximum.
1923
1923
  */
1924
1924
  prefixAboveMaximum?: Message | ((prefix: number, max: number) => Message);
1925
+ /**
1926
+ * Custom error message when a private IPv4 address is used but disallowed.
1927
+ * Can be a static message or a function that receives the IP.
1928
+ * @since 1.0.0
1929
+ */
1930
+ privateNotAllowed?: Message | ((ip: string) => Message);
1931
+ /**
1932
+ * Custom error message when a loopback address is used but disallowed.
1933
+ * Can be a static message or a function that receives the IP.
1934
+ * @since 1.0.0
1935
+ */
1936
+ loopbackNotAllowed?: Message | ((ip: string) => Message);
1937
+ /**
1938
+ * Custom error message when a link-local address is used but disallowed.
1939
+ * Can be a static message or a function that receives the IP.
1940
+ * @since 1.0.0
1941
+ */
1942
+ linkLocalNotAllowed?: Message | ((ip: string) => Message);
1943
+ /**
1944
+ * Custom error message when a multicast address is used but disallowed.
1945
+ * Can be a static message or a function that receives the IP.
1946
+ * @since 1.0.0
1947
+ */
1948
+ multicastNotAllowed?: Message | ((ip: string) => Message);
1949
+ /**
1950
+ * Custom error message when the broadcast address is used but disallowed
1951
+ * (IPv4 only).
1952
+ * Can be a static message or a function that receives the IP.
1953
+ * @since 1.0.0
1954
+ */
1955
+ broadcastNotAllowed?: Message | ((ip: string) => Message);
1956
+ /**
1957
+ * Custom error message when the zero address is used but disallowed.
1958
+ * Can be a static message or a function that receives the IP.
1959
+ * @since 1.0.0
1960
+ */
1961
+ zeroNotAllowed?: Message | ((ip: string) => Message);
1962
+ /**
1963
+ * Custom error message when a unique local address is used but disallowed
1964
+ * (IPv6 only).
1965
+ * Can be a static message or a function that receives the IP.
1966
+ * @since 1.0.0
1967
+ */
1968
+ uniqueLocalNotAllowed?: Message | ((ip: string) => Message);
1925
1969
  };
1926
1970
  }
1927
1971
  /**
@@ -2647,8 +2647,30 @@ function cidr(options) {
2647
2647
  const maxPrefix = options?.maxPrefix;
2648
2648
  const errors = options?.errors;
2649
2649
  const metavar = options?.metavar ?? "CIDR";
2650
- const ipv4Parser = version === 4 || version === "both" ? ipv4(options?.ipv4) : null;
2651
- const ipv6Parser = version === 6 || version === "both" ? ipv6(options?.ipv6) : null;
2650
+ const genericIpSentinel = [];
2651
+ const ipv4Parser = version === 4 || version === "both" ? ipv4({
2652
+ ...options?.ipv4,
2653
+ errors: {
2654
+ invalidIpv4: genericIpSentinel,
2655
+ privateNotAllowed: errors?.privateNotAllowed,
2656
+ loopbackNotAllowed: errors?.loopbackNotAllowed,
2657
+ linkLocalNotAllowed: errors?.linkLocalNotAllowed,
2658
+ multicastNotAllowed: errors?.multicastNotAllowed,
2659
+ broadcastNotAllowed: errors?.broadcastNotAllowed,
2660
+ zeroNotAllowed: errors?.zeroNotAllowed
2661
+ }
2662
+ }) : null;
2663
+ const ipv6Parser = version === 6 || version === "both" ? ipv6({
2664
+ ...options?.ipv6,
2665
+ errors: {
2666
+ invalidIpv6: genericIpSentinel,
2667
+ loopbackNotAllowed: errors?.loopbackNotAllowed,
2668
+ linkLocalNotAllowed: errors?.linkLocalNotAllowed,
2669
+ multicastNotAllowed: errors?.multicastNotAllowed,
2670
+ zeroNotAllowed: errors?.zeroNotAllowed,
2671
+ uniqueLocalNotAllowed: errors?.uniqueLocalNotAllowed
2672
+ }
2673
+ }) : null;
2652
2674
  return {
2653
2675
  $mode: "sync",
2654
2676
  metavar,
@@ -2709,6 +2731,8 @@ function cidr(options) {
2709
2731
  }
2710
2732
  let ipVersion = null;
2711
2733
  let normalizedIp = null;
2734
+ let ipv4Error = null;
2735
+ let ipv6Error = null;
2712
2736
  if (ipv4Parser !== null) {
2713
2737
  const result = ipv4Parser.parse(ipPart);
2714
2738
  if (result.success) {
@@ -2747,7 +2771,7 @@ function cidr(options) {
2747
2771
  error: msg
2748
2772
  };
2749
2773
  }
2750
- }
2774
+ } else ipv4Error = result;
2751
2775
  }
2752
2776
  if (ipVersion === null && ipv6Parser !== null) {
2753
2777
  const result = ipv6Parser.parse(ipPart);
@@ -2787,9 +2811,120 @@ function cidr(options) {
2787
2811
  error: msg
2788
2812
  };
2789
2813
  }
2790
- }
2814
+ } else ipv6Error = result;
2791
2815
  }
2792
2816
  if (ipVersion === null || normalizedIp === null) {
2817
+ const candidates = [[
2818
+ ipv4Error,
2819
+ 4,
2820
+ 32
2821
+ ], [
2822
+ ipv6Error,
2823
+ 6,
2824
+ 128
2825
+ ]];
2826
+ for (const [err, ver, maxPfx] of candidates) if (err !== null && !err.success && err.error !== genericIpSentinel) {
2827
+ if (prefix > maxPfx) {
2828
+ const errorMsg$1 = errors?.invalidPrefix;
2829
+ if (typeof errorMsg$1 === "function") return {
2830
+ success: false,
2831
+ error: errorMsg$1(prefix, ver)
2832
+ };
2833
+ const msg$1 = errorMsg$1 ?? [
2834
+ {
2835
+ type: "text",
2836
+ text: "Expected a prefix length between 0 and "
2837
+ },
2838
+ {
2839
+ type: "text",
2840
+ text: maxPfx.toString()
2841
+ },
2842
+ {
2843
+ type: "text",
2844
+ text: ` for IPv${ver}, but got `
2845
+ },
2846
+ {
2847
+ type: "text",
2848
+ text: prefix.toString()
2849
+ },
2850
+ {
2851
+ type: "text",
2852
+ text: "."
2853
+ }
2854
+ ];
2855
+ return {
2856
+ success: false,
2857
+ error: msg$1
2858
+ };
2859
+ }
2860
+ if (minPrefix !== void 0 && prefix < minPrefix) {
2861
+ const errorMsg$1 = errors?.prefixBelowMinimum;
2862
+ if (typeof errorMsg$1 === "function") return {
2863
+ success: false,
2864
+ error: errorMsg$1(prefix, minPrefix)
2865
+ };
2866
+ const msg$1 = errorMsg$1 ?? [
2867
+ {
2868
+ type: "text",
2869
+ text: "Expected a prefix length greater than or equal to "
2870
+ },
2871
+ {
2872
+ type: "text",
2873
+ text: minPrefix.toString()
2874
+ },
2875
+ {
2876
+ type: "text",
2877
+ text: ", but got "
2878
+ },
2879
+ {
2880
+ type: "text",
2881
+ text: prefix.toString()
2882
+ },
2883
+ {
2884
+ type: "text",
2885
+ text: "."
2886
+ }
2887
+ ];
2888
+ return {
2889
+ success: false,
2890
+ error: msg$1
2891
+ };
2892
+ }
2893
+ if (maxPrefix !== void 0 && prefix > maxPrefix) {
2894
+ const errorMsg$1 = errors?.prefixAboveMaximum;
2895
+ if (typeof errorMsg$1 === "function") return {
2896
+ success: false,
2897
+ error: errorMsg$1(prefix, maxPrefix)
2898
+ };
2899
+ const msg$1 = errorMsg$1 ?? [
2900
+ {
2901
+ type: "text",
2902
+ text: "Expected a prefix length less than or equal to "
2903
+ },
2904
+ {
2905
+ type: "text",
2906
+ text: maxPrefix.toString()
2907
+ },
2908
+ {
2909
+ type: "text",
2910
+ text: ", but got "
2911
+ },
2912
+ {
2913
+ type: "text",
2914
+ text: prefix.toString()
2915
+ },
2916
+ {
2917
+ type: "text",
2918
+ text: "."
2919
+ }
2920
+ ];
2921
+ return {
2922
+ success: false,
2923
+ error: msg$1
2924
+ };
2925
+ }
2926
+ return err;
2927
+ }
2793
2928
  const errorMsg = errors?.invalidCidr;
2794
2929
  if (typeof errorMsg === "function") return {
2795
2930
  success: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.0-dev.1244+338f4cf1",
3
+ "version": "1.0.0-dev.1252+eebac476",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",