@optique/core 1.0.10 → 1.0.11-dev.2654

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.
@@ -1,5 +1,6 @@
1
1
  const require_annotations = require('./internal/annotations.cjs');
2
2
  const require_message = require('./message.cjs');
3
+ const require_passthrough = require('./internal/passthrough.cjs');
3
4
  const require_validate = require('./validate.cjs');
4
5
  const require_usage = require('./usage.cjs');
5
6
  const require_mode_dispatch = require('./internal/mode-dispatch.cjs');
@@ -137,6 +138,7 @@ function constant(value) {
137
138
  return { fragments: [] };
138
139
  }
139
140
  };
141
+ require_passthrough.defineKnownCompletion(result, (state) => require_annotation_state.normalizeInjectedAnnotationState(state));
140
142
  Object.defineProperty(result, "placeholder", {
141
143
  value,
142
144
  configurable: true,
@@ -570,10 +572,15 @@ function option(...args) {
570
572
  }
571
573
  const invalidOption = context.buffer[0];
572
574
  if (options.errors?.noMatch) {
575
+ if (typeof options.errors.noMatch !== "function") return {
576
+ success: false,
577
+ consumed: 0,
578
+ error: options.errors.noMatch
579
+ };
573
580
  const candidates = /* @__PURE__ */ new Set();
574
581
  for (const name of require_usage.extractOptionNames(context.usage)) candidates.add(name);
575
582
  const suggestions = require_suggestion.findSimilar(invalidOption, candidates, require_suggestion.DEFAULT_FIND_SIMILAR_OPTIONS);
576
- const errorMessage = typeof options.errors.noMatch === "function" ? options.errors.noMatch(invalidOption, suggestions) : options.errors.noMatch;
583
+ const errorMessage = options.errors.noMatch(invalidOption, suggestions);
577
584
  return {
578
585
  success: false,
579
586
  consumed: 0,
@@ -854,10 +861,15 @@ function flag(...args) {
854
861
  }
855
862
  const invalidOption = context.buffer[0];
856
863
  if (options.errors?.noMatch) {
864
+ if (typeof options.errors.noMatch !== "function") return {
865
+ success: false,
866
+ consumed: 0,
867
+ error: options.errors.noMatch
868
+ };
857
869
  const candidates = /* @__PURE__ */ new Set();
858
870
  for (const name of require_usage.extractOptionNames(context.usage)) candidates.add(name);
859
871
  const suggestions = require_suggestion.findSimilar(invalidOption, candidates, require_suggestion.DEFAULT_FIND_SIMILAR_OPTIONS);
860
- const errorMessage = typeof options.errors.noMatch === "function" ? options.errors.noMatch(invalidOption, suggestions) : options.errors.noMatch;
872
+ const errorMessage = options.errors.noMatch(invalidOption, suggestions);
861
873
  return {
862
874
  success: false,
863
875
  consumed: 0,
@@ -1424,6 +1436,22 @@ function command(name, parser, options = {}) {
1424
1436
  configurable: true,
1425
1437
  enumerable: false
1426
1438
  });
1439
+ require_passthrough.definePassThroughPriority(result, [parser], (state, token) => {
1440
+ const active = normalizeCommandState(state);
1441
+ if (active == null) return void 0;
1442
+ const childState = active[0] === "matched" ? parser.initialState : active[1];
1443
+ return require_passthrough.getPassThroughPriority(parser, getCommandParseChildState(state, childState, parser), token);
1444
+ });
1445
+ require_passthrough.defineOptionMatch(result, (state, token) => {
1446
+ const active = normalizeCommandState(state);
1447
+ if (active == null) return void 0;
1448
+ const childState = active[0] === "matched" ? parser.initialState : active[1];
1449
+ const match = require_passthrough.getOptionMatch(parser, getCommandParseChildState(state, childState, parser), token);
1450
+ return match == null ? void 0 : {
1451
+ ...match,
1452
+ continuesCommand: true
1453
+ };
1454
+ });
1427
1455
  return result;
1428
1456
  }
1429
1457
  /**
@@ -1476,7 +1504,7 @@ function passThrough(options = {}) {
1476
1504
  const format = options.format ?? "equalsOnly";
1477
1505
  const optionPattern = /^-[a-z0-9-]|^--[a-z0-9-]+/i;
1478
1506
  const equalsOptionPattern = /^--[a-z0-9-]+=/i;
1479
- return {
1507
+ const result = {
1480
1508
  $valueType: [],
1481
1509
  $stateType: [],
1482
1510
  mode: "sync",
@@ -1494,6 +1522,8 @@ function passThrough(options = {}) {
1494
1522
  consumed: 0,
1495
1523
  error: require_message.message`No input to pass through.`
1496
1524
  };
1525
+ const failure = require_passthrough.getPassThroughFailure(context, this?.priority ?? -10);
1526
+ if (failure != null) return failure.failure;
1497
1527
  const token = context.buffer[0];
1498
1528
  if (format === "greedy") {
1499
1529
  const captured = [...context.buffer];
@@ -1605,6 +1635,12 @@ function passThrough(options = {}) {
1605
1635
  return `passThrough(${format})`;
1606
1636
  }
1607
1637
  };
1638
+ require_passthrough.definePassThroughPriority(result, [result], (_state, token) => {
1639
+ if (token == null || format === "greedy") return result.priority;
1640
+ const accepts = format === "equalsOnly" ? equalsOptionPattern.test(token) : format === "nextToken" && optionPattern.test(token);
1641
+ return accepts ? result.priority : void 0;
1642
+ });
1643
+ return result;
1608
1644
  }
1609
1645
 
1610
1646
  //#endregion
@@ -1,5 +1,6 @@
1
1
  import { annotateFreshArray, getAnnotations } from "./internal/annotations.js";
2
2
  import { message, metavar, optionName, optionNames, text, valueSet } from "./message.js";
3
+ import { defineKnownCompletion, defineOptionMatch, definePassThroughPriority, getOptionMatch, getPassThroughFailure, getPassThroughPriority } from "./internal/passthrough.js";
3
4
  import { validateCommandNames, validateOptionNames } from "./validate.js";
4
5
  import { extractOptionNames, isDocHidden, isSuggestionHidden } from "./usage.js";
5
6
  import { dispatchByMode, dispatchIterableByMode, wrapForMode } from "./internal/mode-dispatch.js";
@@ -137,6 +138,7 @@ function constant(value) {
137
138
  return { fragments: [] };
138
139
  }
139
140
  };
141
+ defineKnownCompletion(result, (state) => normalizeInjectedAnnotationState(state));
140
142
  Object.defineProperty(result, "placeholder", {
141
143
  value,
142
144
  configurable: true,
@@ -570,10 +572,15 @@ function option(...args) {
570
572
  }
571
573
  const invalidOption = context.buffer[0];
572
574
  if (options.errors?.noMatch) {
575
+ if (typeof options.errors.noMatch !== "function") return {
576
+ success: false,
577
+ consumed: 0,
578
+ error: options.errors.noMatch
579
+ };
573
580
  const candidates = /* @__PURE__ */ new Set();
574
581
  for (const name of extractOptionNames(context.usage)) candidates.add(name);
575
582
  const suggestions = findSimilar(invalidOption, candidates, DEFAULT_FIND_SIMILAR_OPTIONS);
576
- const errorMessage = typeof options.errors.noMatch === "function" ? options.errors.noMatch(invalidOption, suggestions) : options.errors.noMatch;
583
+ const errorMessage = options.errors.noMatch(invalidOption, suggestions);
577
584
  return {
578
585
  success: false,
579
586
  consumed: 0,
@@ -854,10 +861,15 @@ function flag(...args) {
854
861
  }
855
862
  const invalidOption = context.buffer[0];
856
863
  if (options.errors?.noMatch) {
864
+ if (typeof options.errors.noMatch !== "function") return {
865
+ success: false,
866
+ consumed: 0,
867
+ error: options.errors.noMatch
868
+ };
857
869
  const candidates = /* @__PURE__ */ new Set();
858
870
  for (const name of extractOptionNames(context.usage)) candidates.add(name);
859
871
  const suggestions = findSimilar(invalidOption, candidates, DEFAULT_FIND_SIMILAR_OPTIONS);
860
- const errorMessage = typeof options.errors.noMatch === "function" ? options.errors.noMatch(invalidOption, suggestions) : options.errors.noMatch;
872
+ const errorMessage = options.errors.noMatch(invalidOption, suggestions);
861
873
  return {
862
874
  success: false,
863
875
  consumed: 0,
@@ -1424,6 +1436,22 @@ function command(name, parser, options = {}) {
1424
1436
  configurable: true,
1425
1437
  enumerable: false
1426
1438
  });
1439
+ definePassThroughPriority(result, [parser], (state, token) => {
1440
+ const active = normalizeCommandState(state);
1441
+ if (active == null) return void 0;
1442
+ const childState = active[0] === "matched" ? parser.initialState : active[1];
1443
+ return getPassThroughPriority(parser, getCommandParseChildState(state, childState, parser), token);
1444
+ });
1445
+ defineOptionMatch(result, (state, token) => {
1446
+ const active = normalizeCommandState(state);
1447
+ if (active == null) return void 0;
1448
+ const childState = active[0] === "matched" ? parser.initialState : active[1];
1449
+ const match = getOptionMatch(parser, getCommandParseChildState(state, childState, parser), token);
1450
+ return match == null ? void 0 : {
1451
+ ...match,
1452
+ continuesCommand: true
1453
+ };
1454
+ });
1427
1455
  return result;
1428
1456
  }
1429
1457
  /**
@@ -1476,7 +1504,7 @@ function passThrough(options = {}) {
1476
1504
  const format = options.format ?? "equalsOnly";
1477
1505
  const optionPattern = /^-[a-z0-9-]|^--[a-z0-9-]+/i;
1478
1506
  const equalsOptionPattern = /^--[a-z0-9-]+=/i;
1479
- return {
1507
+ const result = {
1480
1508
  $valueType: [],
1481
1509
  $stateType: [],
1482
1510
  mode: "sync",
@@ -1494,6 +1522,8 @@ function passThrough(options = {}) {
1494
1522
  consumed: 0,
1495
1523
  error: message`No input to pass through.`
1496
1524
  };
1525
+ const failure = getPassThroughFailure(context, this?.priority ?? -10);
1526
+ if (failure != null) return failure.failure;
1497
1527
  const token = context.buffer[0];
1498
1528
  if (format === "greedy") {
1499
1529
  const captured = [...context.buffer];
@@ -1605,6 +1635,12 @@ function passThrough(options = {}) {
1605
1635
  return `passThrough(${format})`;
1606
1636
  }
1607
1637
  };
1638
+ definePassThroughPriority(result, [result], (_state, token) => {
1639
+ if (token == null || format === "greedy") return result.priority;
1640
+ const accepts = format === "equalsOnly" ? equalsOptionPattern.test(token) : format === "nextToken" && optionPattern.test(token);
1641
+ return accepts ? result.priority : void 0;
1642
+ });
1643
+ return result;
1608
1644
  }
1609
1645
 
1610
1646
  //#endregion
@@ -86,8 +86,10 @@ function findSimilar(input, candidates, options = {}) {
86
86
  const normalizedCandidate = caseSensitive ? candidate : candidate.toLowerCase();
87
87
  if (seen.has(candidate)) continue;
88
88
  seen.add(candidate);
89
+ if (normalizedInput === normalizedCandidate) return [candidate];
90
+ const minimumDistance = Math.abs(normalizedInput.length - normalizedCandidate.length);
91
+ if (minimumDistance > maxDistance || minimumDistance / input.length > maxDistanceRatio) continue;
89
92
  const distance = levenshteinDistance(normalizedInput, normalizedCandidate);
90
- if (distance === 0) return [candidate];
91
93
  const distanceRatio = distance / input.length;
92
94
  if (distance <= maxDistance && distanceRatio <= maxDistanceRatio) matches.push({
93
95
  candidate,
@@ -86,8 +86,10 @@ function findSimilar(input, candidates, options = {}) {
86
86
  const normalizedCandidate = caseSensitive ? candidate : candidate.toLowerCase();
87
87
  if (seen.has(candidate)) continue;
88
88
  seen.add(candidate);
89
+ if (normalizedInput === normalizedCandidate) return [candidate];
90
+ const minimumDistance = Math.abs(normalizedInput.length - normalizedCandidate.length);
91
+ if (minimumDistance > maxDistance || minimumDistance / input.length > maxDistanceRatio) continue;
89
92
  const distance = levenshteinDistance(normalizedInput, normalizedCandidate);
90
- if (distance === 0) return [candidate];
91
93
  const distanceRatio = distance / input.length;
92
94
  if (distance <= maxDistance && distanceRatio <= maxDistanceRatio) matches.push({
93
95
  candidate,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.10",
3
+ "version": "1.0.11-dev.2654",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",