@optique/core 0.10.0-dev.296 → 0.10.0-dev.298

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,5 @@
1
1
  import { message, metavar, optionName, optionNames } from "./message.js";
2
- import { DependencyId, createDeferredParseState, createDependencySourceState, createPendingDependencySourceState, isDeferredParseState, isDependencySource, isDependencySourceState, isDerivedValueParser, isPendingDependencySourceState } from "./dependency.js";
2
+ import { DefaultValues, DependencyId, DependencyIds, SuggestWithDependency, createDeferredParseState, createDependencySourceState, createPendingDependencySourceState, isDeferredParseState, isDependencySource, isDependencySourceState, isDerivedValueParser, isPendingDependencySourceState } from "./dependency.js";
3
3
  import { extractCommandNames, extractOptionNames } from "./usage.js";
4
4
  import { DEFAULT_FIND_SIMILAR_OPTIONS, createErrorWithSuggestions, findSimilar } from "./suggestion.js";
5
5
  import { isValueParser } from "./valueparser.js";
@@ -53,6 +53,42 @@ function constant(value) {
53
53
  };
54
54
  }
55
55
  /**
56
+ * Internal helper to get suggestions from a value parser, using dependency values
57
+ * if the parser is a derived parser and dependency values are available.
58
+ * @internal
59
+ */
60
+ function* getSuggestionsWithDependency(valueParser, prefix, dependencyRegistry) {
61
+ if (!valueParser.suggest) return;
62
+ if (isDerivedValueParser(valueParser) && SuggestWithDependency in valueParser) {
63
+ const derived = valueParser;
64
+ const suggestWithDep = derived[SuggestWithDependency];
65
+ if (suggestWithDep && dependencyRegistry) {
66
+ const depIds = DependencyIds in derived ? derived[DependencyIds] : [derived[DependencyId]];
67
+ const defaults = DefaultValues in derived ? derived[DefaultValues]?.() : void 0;
68
+ const registry = dependencyRegistry;
69
+ const dependencyValues = [];
70
+ let hasAnyValue = false;
71
+ for (let i = 0; i < depIds.length; i++) {
72
+ const depId = depIds[i];
73
+ if (registry.has(depId)) {
74
+ dependencyValues.push(registry.get(depId));
75
+ hasAnyValue = true;
76
+ } else if (defaults && i < defaults.length) dependencyValues.push(defaults[i]);
77
+ else {
78
+ yield* valueParser.suggest(prefix);
79
+ return;
80
+ }
81
+ }
82
+ if (hasAnyValue) {
83
+ const depValue = depIds.length === 1 ? dependencyValues[0] : dependencyValues;
84
+ yield* suggestWithDep(prefix, depValue);
85
+ return;
86
+ }
87
+ }
88
+ }
89
+ yield* valueParser.suggest(prefix);
90
+ }
91
+ /**
56
92
  * Internal sync helper for option suggest functionality.
57
93
  * @internal
58
94
  */
@@ -64,7 +100,7 @@ function* suggestOptionSync(optionNames$1, valueParser, hidden, context, prefix)
64
100
  const valuePart = prefix.slice(equalsIndex + 1);
65
101
  if (optionNames$1.includes(optionPart)) {
66
102
  if (valueParser && valueParser.suggest) {
67
- const valueSuggestions = valueParser.suggest(valuePart);
103
+ const valueSuggestions = getSuggestionsWithDependency(valueParser, valuePart, context.dependencyRegistry);
68
104
  for (const suggestion of valueSuggestions) if (suggestion.kind === "literal") yield {
69
105
  kind: "literal",
70
106
  text: `${optionPart}=${suggestion.text}`,
@@ -93,9 +129,45 @@ function* suggestOptionSync(optionNames$1, valueParser, hidden, context, prefix)
93
129
  const lastToken = context.buffer[context.buffer.length - 1];
94
130
  if (optionNames$1.includes(lastToken)) shouldSuggestValues = true;
95
131
  } else if (context.state === void 0 && context.buffer.length === 0) shouldSuggestValues = true;
96
- if (shouldSuggestValues) yield* valueParser.suggest(prefix);
132
+ if (shouldSuggestValues) yield* getSuggestionsWithDependency(valueParser, prefix, context.dependencyRegistry);
133
+ }
134
+ }
135
+ }
136
+ /**
137
+ * Internal async helper to get suggestions from a value parser, using dependency values
138
+ * if the parser is a derived parser and dependency values are available.
139
+ * @internal
140
+ */
141
+ async function* getSuggestionsWithDependencyAsync(valueParser, prefix, dependencyRegistry) {
142
+ if (!valueParser.suggest) return;
143
+ if (isDerivedValueParser(valueParser) && SuggestWithDependency in valueParser) {
144
+ const derived = valueParser;
145
+ const suggestWithDep = derived[SuggestWithDependency];
146
+ if (suggestWithDep && dependencyRegistry) {
147
+ const depIds = DependencyIds in derived ? derived[DependencyIds] : [derived[DependencyId]];
148
+ const defaults = DefaultValues in derived ? derived[DefaultValues]?.() : void 0;
149
+ const registry = dependencyRegistry;
150
+ const dependencyValues = [];
151
+ let hasAnyValue = false;
152
+ for (let i = 0; i < depIds.length; i++) {
153
+ const depId = depIds[i];
154
+ if (registry.has(depId)) {
155
+ dependencyValues.push(registry.get(depId));
156
+ hasAnyValue = true;
157
+ } else if (defaults && i < defaults.length) dependencyValues.push(defaults[i]);
158
+ else {
159
+ for await (const suggestion of valueParser.suggest(prefix)) yield suggestion;
160
+ return;
161
+ }
162
+ }
163
+ if (hasAnyValue) {
164
+ const depValue = depIds.length === 1 ? dependencyValues[0] : dependencyValues;
165
+ for await (const suggestion of suggestWithDep(prefix, depValue)) yield suggestion;
166
+ return;
167
+ }
97
168
  }
98
169
  }
170
+ for await (const suggestion of valueParser.suggest(prefix)) yield suggestion;
99
171
  }
100
172
  /**
101
173
  * Internal async helper for option suggest functionality.
@@ -109,7 +181,7 @@ async function* suggestOptionAsync(optionNames$1, valueParser, hidden, context,
109
181
  const valuePart = prefix.slice(equalsIndex + 1);
110
182
  if (optionNames$1.includes(optionPart)) {
111
183
  if (valueParser && valueParser.suggest) {
112
- const valueSuggestions = valueParser.suggest(valuePart);
184
+ const valueSuggestions = getSuggestionsWithDependencyAsync(valueParser, valuePart, context.dependencyRegistry);
113
185
  for await (const suggestion of valueSuggestions) if (suggestion.kind === "literal") yield {
114
186
  kind: "literal",
115
187
  text: `${optionPart}=${suggestion.text}`,
@@ -138,7 +210,7 @@ async function* suggestOptionAsync(optionNames$1, valueParser, hidden, context,
138
210
  const lastToken = context.buffer[context.buffer.length - 1];
139
211
  if (optionNames$1.includes(lastToken)) shouldSuggestValues = true;
140
212
  } else if (context.state === void 0 && context.buffer.length === 0) shouldSuggestValues = true;
141
- if (shouldSuggestValues) for await (const suggestion of valueParser.suggest(prefix)) yield suggestion;
213
+ if (shouldSuggestValues) for await (const suggestion of getSuggestionsWithDependencyAsync(valueParser, prefix, context.dependencyRegistry)) yield suggestion;
142
214
  }
143
215
  }
144
216
  }
@@ -146,17 +218,17 @@ async function* suggestOptionAsync(optionNames$1, valueParser, hidden, context,
146
218
  * Internal sync helper for argument suggest functionality.
147
219
  * @internal
148
220
  */
149
- function* suggestArgumentSync(valueParser, hidden, prefix) {
221
+ function* suggestArgumentSync(valueParser, hidden, prefix, dependencyRegistry) {
150
222
  if (hidden) return;
151
- if (valueParser.suggest) yield* valueParser.suggest(prefix);
223
+ if (valueParser.suggest) yield* getSuggestionsWithDependency(valueParser, prefix, dependencyRegistry);
152
224
  }
153
225
  /**
154
226
  * Internal async helper for argument suggest functionality.
155
227
  * @internal
156
228
  */
157
- async function* suggestArgumentAsync(valueParser, hidden, prefix) {
229
+ async function* suggestArgumentAsync(valueParser, hidden, prefix, dependencyRegistry) {
158
230
  if (hidden) return;
159
- if (valueParser.suggest) for await (const suggestion of valueParser.suggest(prefix)) yield suggestion;
231
+ if (valueParser.suggest) yield* getSuggestionsWithDependencyAsync(valueParser, prefix, dependencyRegistry);
160
232
  }
161
233
  function option(...args) {
162
234
  const lastArg = args.at(-1);
@@ -709,9 +781,9 @@ function argument(valueParser, options = {}) {
709
781
  error: options.errors?.invalidValue ? typeof options.errors.invalidValue === "function" ? options.errors.invalidValue(state.error) : options.errors.invalidValue : message`${metavar(valueParser.metavar)}: ${state.error}`
710
782
  };
711
783
  },
712
- suggest(_context, prefix) {
713
- if (isAsync) return suggestArgumentAsync(valueParser, options.hidden ?? false, prefix);
714
- return suggestArgumentSync(valueParser, options.hidden ?? false, prefix);
784
+ suggest(context, prefix) {
785
+ if (isAsync) return suggestArgumentAsync(valueParser, options.hidden ?? false, prefix, context.dependencyRegistry);
786
+ return suggestArgumentSync(valueParser, options.hidden ?? false, prefix, context.dependencyRegistry);
715
787
  },
716
788
  getDocFragments(_state, defaultValue) {
717
789
  if (options.hidden) return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "0.10.0-dev.296+80e4ca45",
3
+ "version": "0.10.0-dev.298+bbbd9714",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",