@optique/core 1.0.0-dev.1242 → 1.0.0-dev.1251

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.
@@ -460,6 +460,19 @@ function float(options = {}) {
460
460
  };
461
461
  }
462
462
  /**
463
+ * The set of URL schemes that are considered "special" by the WHATWG URL
464
+ * Standard. These schemes always use the `://` authority syntax.
465
+ * Non-special schemes use only `:` (e.g., `mailto:`, `urn:`).
466
+ */
467
+ const SPECIAL_URL_SCHEMES = new Set([
468
+ "ftp",
469
+ "file",
470
+ "http",
471
+ "https",
472
+ "ws",
473
+ "wss"
474
+ ]);
475
+ /**
463
476
  * Creates a {@link ValueParser} for URL values.
464
477
  *
465
478
  * This parser validates that the input is a well-formed URL and optionally
@@ -515,12 +528,15 @@ function url(options = {}) {
515
528
  return value.href;
516
529
  },
517
530
  *suggest(prefix) {
518
- if (allowedProtocols && prefix.length > 0 && !prefix.includes("://")) for (const protocol of allowedProtocols) {
531
+ if (allowedProtocols && prefix.length > 0 && !prefix.includes(":")) for (const protocol of allowedProtocols) {
519
532
  const cleanProtocol = protocol.replace(/:+$/, "");
520
- if (cleanProtocol.startsWith(prefix.toLowerCase())) yield {
521
- kind: "literal",
522
- text: `${cleanProtocol}://`
523
- };
533
+ if (cleanProtocol.startsWith(prefix.toLowerCase())) {
534
+ const suffix = SPECIAL_URL_SCHEMES.has(cleanProtocol) ? "://" : ":";
535
+ yield {
536
+ kind: "literal",
537
+ text: `${cleanProtocol}${suffix}`
538
+ };
539
+ }
524
540
  }
525
541
  }
526
542
  };
@@ -2631,8 +2647,30 @@ function cidr(options) {
2631
2647
  const maxPrefix = options?.maxPrefix;
2632
2648
  const errors = options?.errors;
2633
2649
  const metavar = options?.metavar ?? "CIDR";
2634
- const ipv4Parser = version === 4 || version === "both" ? ipv4(options?.ipv4) : null;
2635
- 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;
2636
2674
  return {
2637
2675
  $mode: "sync",
2638
2676
  metavar,
@@ -2693,6 +2731,8 @@ function cidr(options) {
2693
2731
  }
2694
2732
  let ipVersion = null;
2695
2733
  let normalizedIp = null;
2734
+ let ipv4Error = null;
2735
+ let ipv6Error = null;
2696
2736
  if (ipv4Parser !== null) {
2697
2737
  const result = ipv4Parser.parse(ipPart);
2698
2738
  if (result.success) {
@@ -2731,7 +2771,7 @@ function cidr(options) {
2731
2771
  error: msg
2732
2772
  };
2733
2773
  }
2734
- }
2774
+ } else ipv4Error = result;
2735
2775
  }
2736
2776
  if (ipVersion === null && ipv6Parser !== null) {
2737
2777
  const result = ipv6Parser.parse(ipPart);
@@ -2771,9 +2811,120 @@ function cidr(options) {
2771
2811
  error: msg
2772
2812
  };
2773
2813
  }
2774
- }
2814
+ } else ipv6Error = result;
2775
2815
  }
2776
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
+ }
2777
2928
  const errorMsg = errors?.invalidCidr;
2778
2929
  if (typeof errorMsg === "function") return {
2779
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
  /**
@@ -460,6 +460,19 @@ function float(options = {}) {
460
460
  };
461
461
  }
462
462
  /**
463
+ * The set of URL schemes that are considered "special" by the WHATWG URL
464
+ * Standard. These schemes always use the `://` authority syntax.
465
+ * Non-special schemes use only `:` (e.g., `mailto:`, `urn:`).
466
+ */
467
+ const SPECIAL_URL_SCHEMES = new Set([
468
+ "ftp",
469
+ "file",
470
+ "http",
471
+ "https",
472
+ "ws",
473
+ "wss"
474
+ ]);
475
+ /**
463
476
  * Creates a {@link ValueParser} for URL values.
464
477
  *
465
478
  * This parser validates that the input is a well-formed URL and optionally
@@ -515,12 +528,15 @@ function url(options = {}) {
515
528
  return value.href;
516
529
  },
517
530
  *suggest(prefix) {
518
- if (allowedProtocols && prefix.length > 0 && !prefix.includes("://")) for (const protocol of allowedProtocols) {
531
+ if (allowedProtocols && prefix.length > 0 && !prefix.includes(":")) for (const protocol of allowedProtocols) {
519
532
  const cleanProtocol = protocol.replace(/:+$/, "");
520
- if (cleanProtocol.startsWith(prefix.toLowerCase())) yield {
521
- kind: "literal",
522
- text: `${cleanProtocol}://`
523
- };
533
+ if (cleanProtocol.startsWith(prefix.toLowerCase())) {
534
+ const suffix = SPECIAL_URL_SCHEMES.has(cleanProtocol) ? "://" : ":";
535
+ yield {
536
+ kind: "literal",
537
+ text: `${cleanProtocol}${suffix}`
538
+ };
539
+ }
524
540
  }
525
541
  }
526
542
  };
@@ -2631,8 +2647,30 @@ function cidr(options) {
2631
2647
  const maxPrefix = options?.maxPrefix;
2632
2648
  const errors = options?.errors;
2633
2649
  const metavar = options?.metavar ?? "CIDR";
2634
- const ipv4Parser = version === 4 || version === "both" ? ipv4(options?.ipv4) : null;
2635
- 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;
2636
2674
  return {
2637
2675
  $mode: "sync",
2638
2676
  metavar,
@@ -2693,6 +2731,8 @@ function cidr(options) {
2693
2731
  }
2694
2732
  let ipVersion = null;
2695
2733
  let normalizedIp = null;
2734
+ let ipv4Error = null;
2735
+ let ipv6Error = null;
2696
2736
  if (ipv4Parser !== null) {
2697
2737
  const result = ipv4Parser.parse(ipPart);
2698
2738
  if (result.success) {
@@ -2731,7 +2771,7 @@ function cidr(options) {
2731
2771
  error: msg
2732
2772
  };
2733
2773
  }
2734
- }
2774
+ } else ipv4Error = result;
2735
2775
  }
2736
2776
  if (ipVersion === null && ipv6Parser !== null) {
2737
2777
  const result = ipv6Parser.parse(ipPart);
@@ -2771,9 +2811,120 @@ function cidr(options) {
2771
2811
  error: msg
2772
2812
  };
2773
2813
  }
2774
- }
2814
+ } else ipv6Error = result;
2775
2815
  }
2776
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
+ }
2777
2928
  const errorMsg = errors?.invalidCidr;
2778
2929
  if (typeof errorMsg === "function") return {
2779
2930
  success: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.0-dev.1242+dd1a7a84",
3
+ "version": "1.0.0-dev.1251+2501c981",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",