@optique/core 1.0.0-dev.1484 → 1.0.0-dev.1492

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.
@@ -1726,73 +1726,182 @@ function socketAddress(options) {
1726
1726
  metavar,
1727
1727
  parse(input) {
1728
1728
  const trimmed = input.trim();
1729
- const separatorIndex = trimmed.lastIndexOf(separator);
1730
- let hostPart;
1731
- let portPart;
1732
- if (separatorIndex === -1) {
1733
- hostPart = trimmed;
1734
- portPart = void 0;
1735
- } else {
1736
- hostPart = trimmed.substring(0, separatorIndex);
1737
- portPart = trimmed.substring(separatorIndex + separator.length);
1738
- }
1739
- const hostResult = parseHost(hostPart);
1740
- if (!hostResult.success) {
1741
- const errorMsg = options?.errors?.invalidFormat;
1742
- if (errorMsg) {
1743
- const msg = typeof errorMsg === "function" ? errorMsg(input) : errorMsg;
1729
+ const canOmitPort = defaultPort !== void 0 && !requirePort;
1730
+ let firstHostError;
1731
+ let validHostNumericPortInvalid = false;
1732
+ let trailingSepHost;
1733
+ let trailingSepHostError;
1734
+ let anySeparatorFound = false;
1735
+ let searchFrom = trimmed.length;
1736
+ while (searchFrom > 0) {
1737
+ const separatorIndex = trimmed.lastIndexOf(separator, searchFrom - 1);
1738
+ if (separatorIndex === -1) break;
1739
+ anySeparatorFound = true;
1740
+ const hostPart = trimmed.substring(0, separatorIndex);
1741
+ const portPart = trimmed.substring(separatorIndex + separator.length);
1742
+ if (portPart === "") {
1743
+ if (trailingSepHost === void 0 && trailingSepHostError === void 0) {
1744
+ const hostResult = parseHost(hostPart);
1745
+ if (hostResult.success) trailingSepHost = hostResult.value;
1746
+ else trailingSepHostError = {
1747
+ hostPart,
1748
+ error: hostResult.error
1749
+ };
1750
+ }
1751
+ } else {
1752
+ const portResult = portParser.parse(portPart);
1753
+ if (portResult.success) {
1754
+ const hostResult = parseHost(hostPart);
1755
+ if (hostResult.success) return {
1756
+ success: true,
1757
+ value: {
1758
+ host: hostResult.value,
1759
+ port: portResult.value
1760
+ }
1761
+ };
1762
+ if (firstHostError === void 0 || (looksLikeIpv4(hostPart) || looksLikeAltIpv4Literal(hostPart)) && !looksLikeIpv4(firstHostError.hostPart) && !looksLikeAltIpv4Literal(firstHostError.hostPart)) firstHostError = {
1763
+ hostPart,
1764
+ error: hostResult.error
1765
+ };
1766
+ } else if (!validHostNumericPortInvalid && hostPart !== "" && /^[0-9]+$/.test(portPart)) if (looksLikeIpv4(hostPart) || looksLikeAltIpv4Literal(hostPart)) {
1767
+ const hostResult = parseHost(hostPart);
1768
+ if (!hostResult.success) {
1769
+ if (firstHostError === void 0 || !looksLikeIpv4(firstHostError.hostPart) && !looksLikeAltIpv4Literal(firstHostError.hostPart)) firstHostError = {
1770
+ hostPart,
1771
+ error: hostResult.error
1772
+ };
1773
+ } else validHostNumericPortInvalid = true;
1774
+ } else validHostNumericPortInvalid = true;
1775
+ else if ((firstHostError === void 0 || !looksLikeIpv4(firstHostError.hostPart) && !looksLikeAltIpv4Literal(firstHostError.hostPart)) && (looksLikeIpv4(hostPart) || looksLikeAltIpv4Literal(hostPart))) {
1776
+ const hostResult = parseHost(hostPart);
1777
+ if (!hostResult.success) firstHostError = {
1778
+ hostPart,
1779
+ error: hostResult.error
1780
+ };
1781
+ }
1782
+ }
1783
+ searchFrom = separatorIndex;
1784
+ }
1785
+ if (validHostNumericPortInvalid) {
1786
+ const errorMsg$1 = options?.errors?.invalidFormat;
1787
+ if (errorMsg$1) {
1788
+ const msg = typeof errorMsg$1 === "function" ? errorMsg$1(input) : errorMsg$1;
1744
1789
  return {
1745
1790
  success: false,
1746
1791
  error: msg
1747
1792
  };
1748
1793
  }
1749
- if (looksLikeIpv4(hostPart) || looksLikeAltIpv4Literal(hostPart)) return {
1794
+ return {
1750
1795
  success: false,
1751
- error: hostResult.error
1796
+ error: require_message.message`Expected a socket address in format host${separator}port, but got ${input}.`
1797
+ };
1798
+ }
1799
+ if (firstHostError !== void 0) {
1800
+ if (looksLikeIpv4(firstHostError.hostPart) || looksLikeAltIpv4Literal(firstHostError.hostPart)) {
1801
+ const errorMsg$1 = options?.errors?.invalidFormat;
1802
+ if (errorMsg$1) {
1803
+ const msg = typeof errorMsg$1 === "function" ? errorMsg$1(input) : errorMsg$1;
1804
+ return {
1805
+ success: false,
1806
+ error: msg
1807
+ };
1808
+ }
1809
+ return {
1810
+ success: false,
1811
+ error: firstHostError.error
1812
+ };
1813
+ }
1814
+ }
1815
+ let hostOnlyResult;
1816
+ if (!requirePort || trailingSepHost !== void 0 || trailingSepHostError !== void 0) {
1817
+ hostOnlyResult = parseHost(trimmed);
1818
+ if (canOmitPort && hostOnlyResult.success) return {
1819
+ success: true,
1820
+ value: {
1821
+ host: hostOnlyResult.value,
1822
+ port: defaultPort
1823
+ }
1824
+ };
1825
+ }
1826
+ if (trailingSepHost !== void 0 && hostOnlyResult !== void 0 && !hostOnlyResult.success) {
1827
+ if (canOmitPort) return {
1828
+ success: true,
1829
+ value: {
1830
+ host: trailingSepHost,
1831
+ port: defaultPort
1832
+ }
1752
1833
  };
1834
+ const errorMsg$1 = options?.errors?.missingPort;
1835
+ const msg = typeof errorMsg$1 === "function" ? errorMsg$1(input) : errorMsg$1 ?? require_message.message`Port number is required but was not specified.`;
1753
1836
  return {
1754
1837
  success: false,
1755
- error: require_message.message`Expected a socket address in format host${separator}port, but got ${input}.`
1838
+ error: msg
1756
1839
  };
1757
1840
  }
1758
- const validatedHost = hostResult.value;
1759
- let validatedPort;
1760
- if (portPart === void 0 || portPart === "") {
1761
- if (requirePort) {
1762
- const errorMsg = options?.errors?.missingPort;
1763
- const msg = typeof errorMsg === "function" ? errorMsg(input) : errorMsg ?? require_message.message`Port number is required but was not specified.`;
1841
+ if (trailingSepHostError !== void 0 && hostOnlyResult !== void 0 && !hostOnlyResult.success) {
1842
+ const errorMsg$1 = options?.errors?.invalidFormat;
1843
+ if (errorMsg$1) {
1844
+ const msg = typeof errorMsg$1 === "function" ? errorMsg$1(input) : errorMsg$1;
1764
1845
  return {
1765
1846
  success: false,
1766
1847
  error: msg
1767
1848
  };
1768
1849
  }
1769
- if (defaultPort !== void 0) validatedPort = defaultPort;
1770
- else {
1771
- const errorMsg = options?.errors?.missingPort;
1772
- const msg = typeof errorMsg === "function" ? errorMsg(input) : errorMsg ?? require_message.message`Port number is required but was not specified.`;
1850
+ if (looksLikeIpv4(trailingSepHostError.hostPart) || looksLikeAltIpv4Literal(trailingSepHostError.hostPart)) return {
1851
+ success: false,
1852
+ error: trailingSepHostError.error
1853
+ };
1854
+ return {
1855
+ success: false,
1856
+ error: require_message.message`Expected a socket address in format host${separator}port, but got ${input}.`
1857
+ };
1858
+ }
1859
+ if (!canOmitPort && !requirePort && hostOnlyResult !== void 0 && hostOnlyResult.success) {
1860
+ const errorMsg$1 = options?.errors?.missingPort;
1861
+ const msg = typeof errorMsg$1 === "function" ? errorMsg$1(input) : errorMsg$1 ?? require_message.message`Port number is required but was not specified.`;
1862
+ return {
1863
+ success: false,
1864
+ error: msg
1865
+ };
1866
+ }
1867
+ if (!canOmitPort && !anySeparatorFound) {
1868
+ hostOnlyResult = hostOnlyResult ?? parseHost(trimmed);
1869
+ if (hostOnlyResult.success) {
1870
+ const errorMsg$1 = options?.errors?.missingPort;
1871
+ const msg = typeof errorMsg$1 === "function" ? errorMsg$1(input) : errorMsg$1 ?? require_message.message`Port number is required but was not specified.`;
1773
1872
  return {
1774
1873
  success: false,
1775
1874
  error: msg
1776
1875
  };
1777
1876
  }
1778
- } else {
1779
- const portResult = portParser.parse(portPart);
1780
- if (!portResult.success) {
1781
- const errorMsg = options?.errors?.invalidFormat;
1782
- const msg = typeof errorMsg === "function" ? errorMsg(input) : errorMsg ?? require_message.message`Expected a socket address in format host${separator}port, but got ${input}.`;
1877
+ }
1878
+ if (firstHostError !== void 0) {
1879
+ const errorMsg$1 = options?.errors?.invalidFormat;
1880
+ if (errorMsg$1) {
1881
+ const msg = typeof errorMsg$1 === "function" ? errorMsg$1(input) : errorMsg$1;
1783
1882
  return {
1784
1883
  success: false,
1785
1884
  error: msg
1786
1885
  };
1787
1886
  }
1788
- validatedPort = portResult.value;
1887
+ }
1888
+ const errorMsg = options?.errors?.invalidFormat;
1889
+ if (errorMsg) {
1890
+ const msg = typeof errorMsg === "function" ? errorMsg(input) : errorMsg;
1891
+ return {
1892
+ success: false,
1893
+ error: msg
1894
+ };
1895
+ }
1896
+ if (hostOnlyResult !== void 0 && !hostOnlyResult.success) {
1897
+ if (looksLikeIpv4(trimmed) || looksLikeAltIpv4Literal(trimmed)) return {
1898
+ success: false,
1899
+ error: hostOnlyResult.error
1900
+ };
1789
1901
  }
1790
1902
  return {
1791
- success: true,
1792
- value: {
1793
- host: validatedHost,
1794
- port: validatedPort
1795
- }
1903
+ success: false,
1904
+ error: require_message.message`Expected a socket address in format host${separator}port, but got ${input}.`
1796
1905
  };
1797
1906
  },
1798
1907
  format(value) {
@@ -1726,73 +1726,182 @@ function socketAddress(options) {
1726
1726
  metavar,
1727
1727
  parse(input) {
1728
1728
  const trimmed = input.trim();
1729
- const separatorIndex = trimmed.lastIndexOf(separator);
1730
- let hostPart;
1731
- let portPart;
1732
- if (separatorIndex === -1) {
1733
- hostPart = trimmed;
1734
- portPart = void 0;
1735
- } else {
1736
- hostPart = trimmed.substring(0, separatorIndex);
1737
- portPart = trimmed.substring(separatorIndex + separator.length);
1738
- }
1739
- const hostResult = parseHost(hostPart);
1740
- if (!hostResult.success) {
1741
- const errorMsg = options?.errors?.invalidFormat;
1742
- if (errorMsg) {
1743
- const msg = typeof errorMsg === "function" ? errorMsg(input) : errorMsg;
1729
+ const canOmitPort = defaultPort !== void 0 && !requirePort;
1730
+ let firstHostError;
1731
+ let validHostNumericPortInvalid = false;
1732
+ let trailingSepHost;
1733
+ let trailingSepHostError;
1734
+ let anySeparatorFound = false;
1735
+ let searchFrom = trimmed.length;
1736
+ while (searchFrom > 0) {
1737
+ const separatorIndex = trimmed.lastIndexOf(separator, searchFrom - 1);
1738
+ if (separatorIndex === -1) break;
1739
+ anySeparatorFound = true;
1740
+ const hostPart = trimmed.substring(0, separatorIndex);
1741
+ const portPart = trimmed.substring(separatorIndex + separator.length);
1742
+ if (portPart === "") {
1743
+ if (trailingSepHost === void 0 && trailingSepHostError === void 0) {
1744
+ const hostResult = parseHost(hostPart);
1745
+ if (hostResult.success) trailingSepHost = hostResult.value;
1746
+ else trailingSepHostError = {
1747
+ hostPart,
1748
+ error: hostResult.error
1749
+ };
1750
+ }
1751
+ } else {
1752
+ const portResult = portParser.parse(portPart);
1753
+ if (portResult.success) {
1754
+ const hostResult = parseHost(hostPart);
1755
+ if (hostResult.success) return {
1756
+ success: true,
1757
+ value: {
1758
+ host: hostResult.value,
1759
+ port: portResult.value
1760
+ }
1761
+ };
1762
+ if (firstHostError === void 0 || (looksLikeIpv4(hostPart) || looksLikeAltIpv4Literal(hostPart)) && !looksLikeIpv4(firstHostError.hostPart) && !looksLikeAltIpv4Literal(firstHostError.hostPart)) firstHostError = {
1763
+ hostPart,
1764
+ error: hostResult.error
1765
+ };
1766
+ } else if (!validHostNumericPortInvalid && hostPart !== "" && /^[0-9]+$/.test(portPart)) if (looksLikeIpv4(hostPart) || looksLikeAltIpv4Literal(hostPart)) {
1767
+ const hostResult = parseHost(hostPart);
1768
+ if (!hostResult.success) {
1769
+ if (firstHostError === void 0 || !looksLikeIpv4(firstHostError.hostPart) && !looksLikeAltIpv4Literal(firstHostError.hostPart)) firstHostError = {
1770
+ hostPart,
1771
+ error: hostResult.error
1772
+ };
1773
+ } else validHostNumericPortInvalid = true;
1774
+ } else validHostNumericPortInvalid = true;
1775
+ else if ((firstHostError === void 0 || !looksLikeIpv4(firstHostError.hostPart) && !looksLikeAltIpv4Literal(firstHostError.hostPart)) && (looksLikeIpv4(hostPart) || looksLikeAltIpv4Literal(hostPart))) {
1776
+ const hostResult = parseHost(hostPart);
1777
+ if (!hostResult.success) firstHostError = {
1778
+ hostPart,
1779
+ error: hostResult.error
1780
+ };
1781
+ }
1782
+ }
1783
+ searchFrom = separatorIndex;
1784
+ }
1785
+ if (validHostNumericPortInvalid) {
1786
+ const errorMsg$1 = options?.errors?.invalidFormat;
1787
+ if (errorMsg$1) {
1788
+ const msg = typeof errorMsg$1 === "function" ? errorMsg$1(input) : errorMsg$1;
1744
1789
  return {
1745
1790
  success: false,
1746
1791
  error: msg
1747
1792
  };
1748
1793
  }
1749
- if (looksLikeIpv4(hostPart) || looksLikeAltIpv4Literal(hostPart)) return {
1794
+ return {
1750
1795
  success: false,
1751
- error: hostResult.error
1796
+ error: message`Expected a socket address in format host${separator}port, but got ${input}.`
1797
+ };
1798
+ }
1799
+ if (firstHostError !== void 0) {
1800
+ if (looksLikeIpv4(firstHostError.hostPart) || looksLikeAltIpv4Literal(firstHostError.hostPart)) {
1801
+ const errorMsg$1 = options?.errors?.invalidFormat;
1802
+ if (errorMsg$1) {
1803
+ const msg = typeof errorMsg$1 === "function" ? errorMsg$1(input) : errorMsg$1;
1804
+ return {
1805
+ success: false,
1806
+ error: msg
1807
+ };
1808
+ }
1809
+ return {
1810
+ success: false,
1811
+ error: firstHostError.error
1812
+ };
1813
+ }
1814
+ }
1815
+ let hostOnlyResult;
1816
+ if (!requirePort || trailingSepHost !== void 0 || trailingSepHostError !== void 0) {
1817
+ hostOnlyResult = parseHost(trimmed);
1818
+ if (canOmitPort && hostOnlyResult.success) return {
1819
+ success: true,
1820
+ value: {
1821
+ host: hostOnlyResult.value,
1822
+ port: defaultPort
1823
+ }
1824
+ };
1825
+ }
1826
+ if (trailingSepHost !== void 0 && hostOnlyResult !== void 0 && !hostOnlyResult.success) {
1827
+ if (canOmitPort) return {
1828
+ success: true,
1829
+ value: {
1830
+ host: trailingSepHost,
1831
+ port: defaultPort
1832
+ }
1752
1833
  };
1834
+ const errorMsg$1 = options?.errors?.missingPort;
1835
+ const msg = typeof errorMsg$1 === "function" ? errorMsg$1(input) : errorMsg$1 ?? message`Port number is required but was not specified.`;
1753
1836
  return {
1754
1837
  success: false,
1755
- error: message`Expected a socket address in format host${separator}port, but got ${input}.`
1838
+ error: msg
1756
1839
  };
1757
1840
  }
1758
- const validatedHost = hostResult.value;
1759
- let validatedPort;
1760
- if (portPart === void 0 || portPart === "") {
1761
- if (requirePort) {
1762
- const errorMsg = options?.errors?.missingPort;
1763
- const msg = typeof errorMsg === "function" ? errorMsg(input) : errorMsg ?? message`Port number is required but was not specified.`;
1841
+ if (trailingSepHostError !== void 0 && hostOnlyResult !== void 0 && !hostOnlyResult.success) {
1842
+ const errorMsg$1 = options?.errors?.invalidFormat;
1843
+ if (errorMsg$1) {
1844
+ const msg = typeof errorMsg$1 === "function" ? errorMsg$1(input) : errorMsg$1;
1764
1845
  return {
1765
1846
  success: false,
1766
1847
  error: msg
1767
1848
  };
1768
1849
  }
1769
- if (defaultPort !== void 0) validatedPort = defaultPort;
1770
- else {
1771
- const errorMsg = options?.errors?.missingPort;
1772
- const msg = typeof errorMsg === "function" ? errorMsg(input) : errorMsg ?? message`Port number is required but was not specified.`;
1850
+ if (looksLikeIpv4(trailingSepHostError.hostPart) || looksLikeAltIpv4Literal(trailingSepHostError.hostPart)) return {
1851
+ success: false,
1852
+ error: trailingSepHostError.error
1853
+ };
1854
+ return {
1855
+ success: false,
1856
+ error: message`Expected a socket address in format host${separator}port, but got ${input}.`
1857
+ };
1858
+ }
1859
+ if (!canOmitPort && !requirePort && hostOnlyResult !== void 0 && hostOnlyResult.success) {
1860
+ const errorMsg$1 = options?.errors?.missingPort;
1861
+ const msg = typeof errorMsg$1 === "function" ? errorMsg$1(input) : errorMsg$1 ?? message`Port number is required but was not specified.`;
1862
+ return {
1863
+ success: false,
1864
+ error: msg
1865
+ };
1866
+ }
1867
+ if (!canOmitPort && !anySeparatorFound) {
1868
+ hostOnlyResult = hostOnlyResult ?? parseHost(trimmed);
1869
+ if (hostOnlyResult.success) {
1870
+ const errorMsg$1 = options?.errors?.missingPort;
1871
+ const msg = typeof errorMsg$1 === "function" ? errorMsg$1(input) : errorMsg$1 ?? message`Port number is required but was not specified.`;
1773
1872
  return {
1774
1873
  success: false,
1775
1874
  error: msg
1776
1875
  };
1777
1876
  }
1778
- } else {
1779
- const portResult = portParser.parse(portPart);
1780
- if (!portResult.success) {
1781
- const errorMsg = options?.errors?.invalidFormat;
1782
- const msg = typeof errorMsg === "function" ? errorMsg(input) : errorMsg ?? message`Expected a socket address in format host${separator}port, but got ${input}.`;
1877
+ }
1878
+ if (firstHostError !== void 0) {
1879
+ const errorMsg$1 = options?.errors?.invalidFormat;
1880
+ if (errorMsg$1) {
1881
+ const msg = typeof errorMsg$1 === "function" ? errorMsg$1(input) : errorMsg$1;
1783
1882
  return {
1784
1883
  success: false,
1785
1884
  error: msg
1786
1885
  };
1787
1886
  }
1788
- validatedPort = portResult.value;
1887
+ }
1888
+ const errorMsg = options?.errors?.invalidFormat;
1889
+ if (errorMsg) {
1890
+ const msg = typeof errorMsg === "function" ? errorMsg(input) : errorMsg;
1891
+ return {
1892
+ success: false,
1893
+ error: msg
1894
+ };
1895
+ }
1896
+ if (hostOnlyResult !== void 0 && !hostOnlyResult.success) {
1897
+ if (looksLikeIpv4(trimmed) || looksLikeAltIpv4Literal(trimmed)) return {
1898
+ success: false,
1899
+ error: hostOnlyResult.error
1900
+ };
1789
1901
  }
1790
1902
  return {
1791
- success: true,
1792
- value: {
1793
- host: validatedHost,
1794
- port: validatedPort
1795
- }
1903
+ success: false,
1904
+ error: message`Expected a socket address in format host${separator}port, but got ${input}.`
1796
1905
  };
1797
1906
  },
1798
1907
  format(value) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.0-dev.1484+220c0bfd",
3
+ "version": "1.0.0-dev.1492+3035da3f",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",