@optique/core 1.4.0-dev.2616 → 1.4.0-dev.2618
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.
- package/dist/constructs.cjs +87 -144
- package/dist/constructs.js +87 -144
- package/dist/execution-context.cjs +1 -2
- package/dist/execution-context.js +1 -3
- package/dist/extension.cjs +0 -15
- package/dist/extension.d.cts +1 -12
- package/dist/extension.d.ts +1 -12
- package/dist/extension.js +1 -15
- package/dist/facade.cjs +140 -6
- package/dist/facade.d.cts +32 -2
- package/dist/facade.d.ts +32 -2
- package/dist/facade.js +141 -7
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/modifiers.cjs +4 -5
- package/dist/modifiers.js +4 -5
- package/dist/primitives.cjs +27 -69
- package/dist/primitives.d.cts +3 -3
- package/dist/primitives.d.ts +3 -3
- package/dist/primitives.js +27 -69
- package/dist/usage-internals.cjs +0 -73
- package/dist/usage-internals.js +1 -71
- package/package.json +2 -2
- package/skills/optique/SKILL.md +12 -12
- package/dist/short-option.cjs +0 -311
- package/dist/short-option.js +0 -297
package/dist/short-option.cjs
DELETED
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
const require_internal_annotations = require('./internal/annotations.cjs');
|
|
2
|
-
const require_usage = require('./usage.cjs');
|
|
3
|
-
const require_mode_dispatch = require('./internal/mode-dispatch.cjs');
|
|
4
|
-
const require_execution_context = require('./execution-context.cjs');
|
|
5
|
-
const require_usage_internals = require('./usage-internals.cjs');
|
|
6
|
-
|
|
7
|
-
//#region src/short-option.ts
|
|
8
|
-
const optionScopeKey = Symbol("optionScope");
|
|
9
|
-
const optionPathKey = Symbol("optionScopePath");
|
|
10
|
-
const scopeSourceKey = Symbol("optionScopeSource");
|
|
11
|
-
const scopeLocations = /* @__PURE__ */ new WeakMap();
|
|
12
|
-
const scopeBindings = /* @__PURE__ */ new WeakMap();
|
|
13
|
-
function getScopeBindings(selections) {
|
|
14
|
-
let bindings = scopeBindings.get(selections);
|
|
15
|
-
if (bindings == null) {
|
|
16
|
-
bindings = /* @__PURE__ */ new WeakMap();
|
|
17
|
-
scopeBindings.set(selections, bindings);
|
|
18
|
-
}
|
|
19
|
-
return bindings;
|
|
20
|
-
}
|
|
21
|
-
function bindScope(source, path, selections) {
|
|
22
|
-
const boundScopes = getScopeBindings(selections);
|
|
23
|
-
let node = boundScopes.get(source);
|
|
24
|
-
if (node == null) {
|
|
25
|
-
node = {
|
|
26
|
-
children: /* @__PURE__ */ new Map(),
|
|
27
|
-
bound: (selections$1, arities, context) => source(selections$1, arities, context, [])
|
|
28
|
-
};
|
|
29
|
-
boundScopes.set(source, node);
|
|
30
|
-
scopeLocations.set(node.bound, []);
|
|
31
|
-
}
|
|
32
|
-
for (let i = 0; i < path.length; i++) {
|
|
33
|
-
let child = node.children.get(path[i]);
|
|
34
|
-
if (child == null) {
|
|
35
|
-
const occurrence = path.slice(0, i + 1);
|
|
36
|
-
child = {
|
|
37
|
-
children: /* @__PURE__ */ new Map(),
|
|
38
|
-
bound: (selections$1, arities, context) => source(selections$1, arities, context, occurrence)
|
|
39
|
-
};
|
|
40
|
-
node.children.set(path[i], child);
|
|
41
|
-
scopeLocations.set(child.bound, occurrence);
|
|
42
|
-
}
|
|
43
|
-
node = child;
|
|
44
|
-
}
|
|
45
|
-
return node.bound;
|
|
46
|
-
}
|
|
47
|
-
function scopePath(context) {
|
|
48
|
-
return context.exec?.path ?? context[optionPathKey] ?? [];
|
|
49
|
-
}
|
|
50
|
-
/** Tracks child occurrences even for contexts without execution metadata. @internal */
|
|
51
|
-
function withOptionScopeChild(context, segment) {
|
|
52
|
-
return {
|
|
53
|
-
...context,
|
|
54
|
-
[optionPathKey]: [...scopePath(context), segment]
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
const primitiveScopes = /* @__PURE__ */ new WeakMap();
|
|
58
|
-
function parserScope(parser) {
|
|
59
|
-
const declared = parser[scopeSourceKey];
|
|
60
|
-
if (declared != null) return declared;
|
|
61
|
-
let source = primitiveScopes.get(parser);
|
|
62
|
-
if (source == null) {
|
|
63
|
-
source = (_selections, arities) => {
|
|
64
|
-
if (arities != null) require_usage_internals.collectOptionNamesAtCurrentCommandDepth(parser.usage, arities, false);
|
|
65
|
-
return parser.leadingNames;
|
|
66
|
-
};
|
|
67
|
-
primitiveScopes.set(parser, source);
|
|
68
|
-
}
|
|
69
|
-
return source;
|
|
70
|
-
}
|
|
71
|
-
/** Preserves dynamic option ownership through a transparent wrapper. @internal */
|
|
72
|
-
function inheritOptionScope(target, inner) {
|
|
73
|
-
Object.defineProperty(target, scopeSourceKey, {
|
|
74
|
-
value: parserScope(inner),
|
|
75
|
-
enumerable: true,
|
|
76
|
-
configurable: true
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
/** Combines declarations without flattening conditional ownership. @internal */
|
|
80
|
-
function combinedOptionScope(parsers, segments) {
|
|
81
|
-
return (selections, arities, context, path = []) => new Set(parsers.flatMap((parser, index) => [...parserScope(parser)(selections, arities, context, segments == null ? path : [...path, segments[index]])]));
|
|
82
|
-
}
|
|
83
|
-
/** Evaluates repeated declarations at their actual item occurrences. @internal */
|
|
84
|
-
function repeatedOptionScope(parser) {
|
|
85
|
-
return (selections, arities, context, path = []) => {
|
|
86
|
-
const indices = /* @__PURE__ */ new Set();
|
|
87
|
-
const collect = (location) => {
|
|
88
|
-
if (location != null && path.every((segment, index) => location[index] === segment)) {
|
|
89
|
-
const index = location[path.length];
|
|
90
|
-
if (typeof index === "number") indices.add(index);
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
for (const selected of selections.keys()) collect(scopeLocations.get(selected));
|
|
94
|
-
if (context != null) collect(scopePath(context));
|
|
95
|
-
if (indices.size === 0) indices.add(0);
|
|
96
|
-
return new Set([...indices].flatMap((index) => [...parserScope(parser)(selections, arities, context, [...path, index])]));
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
/** Tracks declarations after a command or discriminator routes input. @internal */
|
|
100
|
-
function selectableOptionScope(initial) {
|
|
101
|
-
const source = (selections, arities, context, path = []) => (selections.get(bindScope(source, path, selections)) ?? initial)(selections, arities, context, path);
|
|
102
|
-
return {
|
|
103
|
-
source,
|
|
104
|
-
select(context, selected) {
|
|
105
|
-
const selections = context[optionScopeKey]?.selections;
|
|
106
|
-
if (selections == null) return;
|
|
107
|
-
selections.set(bindScope(source, scopePath(context), selections), selected);
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
/** Keeps selected conditional names distinct from sibling declarations. @internal */
|
|
112
|
-
function conditionalOptionScope(discriminator, fallback, candidates = []) {
|
|
113
|
-
const scope = selectableOptionScope((selections, arities, context, path) => {
|
|
114
|
-
const canSkip = discriminator.canSkip?.(context == null ? discriminator.initialState : require_internal_annotations.inheritAnnotations(context.state, discriminator.initialState), context?.exec == null ? void 0 : require_execution_context.withChildExecPath({
|
|
115
|
-
...context.exec,
|
|
116
|
-
path: path ?? scopePath(context)
|
|
117
|
-
}, "_discriminator")) === true;
|
|
118
|
-
return combinedOptionScope([
|
|
119
|
-
discriminator,
|
|
120
|
-
...canSkip ? candidates : [],
|
|
121
|
-
...fallback == null ? [] : [fallback]
|
|
122
|
-
], [
|
|
123
|
-
"_discriminator",
|
|
124
|
-
...canSkip ? candidates.map(() => "_branch") : [],
|
|
125
|
-
...fallback == null ? [] : ["_branch"]
|
|
126
|
-
])(selections, arities, context, path);
|
|
127
|
-
});
|
|
128
|
-
return {
|
|
129
|
-
source: scope.source,
|
|
130
|
-
select(context, branch) {
|
|
131
|
-
scope.select(context, combinedOptionScope([discriminator, branch], ["_discriminator", "_branch"]));
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
/** Returns the names reachable in this parse scope. @internal */
|
|
136
|
-
function getOptionScope(context) {
|
|
137
|
-
const scoped = context[optionScopeKey];
|
|
138
|
-
const names = scoped == null ? require_usage.extractOptionNames(context.usage, true) : new Set([...scoped.names, ...[...scoped.sources].flatMap((source) => [...source(scoped.selections, void 0, context)])]);
|
|
139
|
-
return names;
|
|
140
|
-
}
|
|
141
|
-
/** Adds reachable names without consulting the flattened help tree. @internal */
|
|
142
|
-
function withOptionScope(context, names) {
|
|
143
|
-
const inherited = context[optionScopeKey];
|
|
144
|
-
return {
|
|
145
|
-
...context,
|
|
146
|
-
[optionScopeKey]: {
|
|
147
|
-
names: new Set([...inherited?.names ?? [], ...names]),
|
|
148
|
-
sources: inherited?.sources ?? /* @__PURE__ */ new Set(),
|
|
149
|
-
selections: inherited?.selections ?? /* @__PURE__ */ new Map()
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
/** Adds a parser's owned declarations, retaining modifier metadata. @internal */
|
|
154
|
-
function withParserOptionScope(context, parser, segment) {
|
|
155
|
-
const inherited = context[optionScopeKey];
|
|
156
|
-
const selections = inherited?.selections ?? /* @__PURE__ */ new Map();
|
|
157
|
-
return {
|
|
158
|
-
...context,
|
|
159
|
-
[optionScopeKey]: {
|
|
160
|
-
names: inherited?.names ?? /* @__PURE__ */ new Set(),
|
|
161
|
-
sources: new Set([...inherited?.sources ?? [], bindScope(parserScope(parser), segment == null ? scopePath(context) : [...scopePath(context), segment], selections)]),
|
|
162
|
-
selections
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
/** Isolates route selections while probing a competing branch. @internal */
|
|
167
|
-
function forkOptionScope(context) {
|
|
168
|
-
const scoped = context[optionScopeKey];
|
|
169
|
-
if (scoped == null) return context;
|
|
170
|
-
const selections = new Map(scoped.selections);
|
|
171
|
-
scopeBindings.set(selections, getScopeBindings(scoped.selections));
|
|
172
|
-
return {
|
|
173
|
-
...context,
|
|
174
|
-
[optionScopeKey]: {
|
|
175
|
-
...scoped,
|
|
176
|
-
selections
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
/** Publishes winning routes without persisting temporary candidate names. @internal */
|
|
181
|
-
function adoptOptionScope(context, next) {
|
|
182
|
-
const own = context[optionScopeKey];
|
|
183
|
-
const child = next[optionScopeKey];
|
|
184
|
-
if (own != null && child != null) for (const [source, selected] of child.selections) own.selections.set(source, selected);
|
|
185
|
-
return context;
|
|
186
|
-
}
|
|
187
|
-
/** Removes names reserved only for a speculative probe. @internal */
|
|
188
|
-
function restoreOptionScope(next, parent) {
|
|
189
|
-
adoptOptionScope(parent, next);
|
|
190
|
-
const own = parent[optionScopeKey];
|
|
191
|
-
const child = next[optionScopeKey];
|
|
192
|
-
return own == null || child == null ? next : {
|
|
193
|
-
...next,
|
|
194
|
-
[optionScopeKey]: {
|
|
195
|
-
...child,
|
|
196
|
-
names: own.names,
|
|
197
|
-
selections: own.selections
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
/** Decorates construct entry points while retaining internal metadata. @internal */
|
|
202
|
-
function scopeParser(parser, source = () => parser.leadingNames, prepare, isolate = false) {
|
|
203
|
-
const parse = parser.parse;
|
|
204
|
-
const suggest = parser.suggest;
|
|
205
|
-
Object.defineProperties(parser, {
|
|
206
|
-
[scopeSourceKey]: {
|
|
207
|
-
value: source,
|
|
208
|
-
enumerable: true
|
|
209
|
-
},
|
|
210
|
-
parse: {
|
|
211
|
-
enumerable: true,
|
|
212
|
-
configurable: true,
|
|
213
|
-
writable: true,
|
|
214
|
-
value: (context) => {
|
|
215
|
-
const scoped = withParserOptionScope(isolate ? forkOptionScope(context) : context, parser);
|
|
216
|
-
prepare?.(scoped);
|
|
217
|
-
return require_mode_dispatch.mapModeValue(parser.mode, parse.call(parser, scoped), (result) => {
|
|
218
|
-
if (!result.success) return result;
|
|
219
|
-
let next = result.next;
|
|
220
|
-
if (isolate) {
|
|
221
|
-
adoptOptionScope(context, next);
|
|
222
|
-
const parent = context[optionScopeKey];
|
|
223
|
-
const child = next[optionScopeKey];
|
|
224
|
-
if (parent != null && child != null) next = {
|
|
225
|
-
...next,
|
|
226
|
-
[optionScopeKey]: {
|
|
227
|
-
...child,
|
|
228
|
-
selections: parent.selections
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
return {
|
|
233
|
-
...result,
|
|
234
|
-
next: {
|
|
235
|
-
...next,
|
|
236
|
-
[optionPathKey]: scopePath(context)
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
},
|
|
242
|
-
suggest: {
|
|
243
|
-
enumerable: true,
|
|
244
|
-
configurable: true,
|
|
245
|
-
writable: true,
|
|
246
|
-
value: (context, prefix) => {
|
|
247
|
-
const scoped = withParserOptionScope(context, parser);
|
|
248
|
-
prepare?.(scoped);
|
|
249
|
-
return suggest.call(parser, scoped, prefix);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
});
|
|
253
|
-
return parser;
|
|
254
|
-
}
|
|
255
|
-
/** Tests full-name ownership before splitting a short prefix. @internal */
|
|
256
|
-
function fullNameOwnsToken(context, token) {
|
|
257
|
-
if (!/^-[^-]/.test(token)) return false;
|
|
258
|
-
for (const name of getOptionScope(context)) if (name.startsWith("-") && !name.startsWith("--") && name.length > 2 && !/^-[^-]$/u.test(name) && (token === name || token.startsWith(`${name}=`))) return true;
|
|
259
|
-
return false;
|
|
260
|
-
}
|
|
261
|
-
/** Finds the value part of an attached short-option completion. @internal */
|
|
262
|
-
function attachedValuePrefix(context, prefix, optionNames) {
|
|
263
|
-
if (!/^-[^-]/.test(prefix)) return void 0;
|
|
264
|
-
const scope = getOptionScope(context);
|
|
265
|
-
const names = {
|
|
266
|
-
value: /* @__PURE__ */ new Set(),
|
|
267
|
-
flag: /* @__PURE__ */ new Set()
|
|
268
|
-
};
|
|
269
|
-
const scoped = context[optionScopeKey];
|
|
270
|
-
if (scoped == null) require_usage_internals.collectOptionNamesAtCurrentCommandDepth(context.usage, names, false);
|
|
271
|
-
else for (const source of scoped.sources) source(scoped.selections, names, context);
|
|
272
|
-
for (let index = 1; index < prefix.length;) {
|
|
273
|
-
const character = String.fromCodePoint(prefix.codePointAt(index));
|
|
274
|
-
const nextIndex = index + character.length;
|
|
275
|
-
const remainder = `-${prefix.slice(index)}`;
|
|
276
|
-
for (const name of scope) if (name.startsWith("-") && !name.startsWith("--") && name.length > 2 && !/^-[^-]$/u.test(name) && (name.startsWith(remainder) || remainder.startsWith(`${name}=`))) return void 0;
|
|
277
|
-
const short = `-${character}`;
|
|
278
|
-
if (optionNames.includes(short)) return {
|
|
279
|
-
head: prefix.slice(0, nextIndex),
|
|
280
|
-
value: prefix.slice(nextIndex)
|
|
281
|
-
};
|
|
282
|
-
if (!scope.has(short) || !names.flag.has(short) || names.value.has(short)) return void 0;
|
|
283
|
-
index = nextIndex;
|
|
284
|
-
}
|
|
285
|
-
return void 0;
|
|
286
|
-
}
|
|
287
|
-
/** Preserves the prefix when embedding value suggestions in one token. @internal */
|
|
288
|
-
function prefixSuggestion(head, suggestion) {
|
|
289
|
-
return {
|
|
290
|
-
kind: "literal",
|
|
291
|
-
text: head + (suggestion.kind === "literal" ? suggestion.text : suggestion.pattern ?? ""),
|
|
292
|
-
...suggestion.description != null ? { description: suggestion.description } : {}
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
//#endregion
|
|
297
|
-
exports.adoptOptionScope = adoptOptionScope;
|
|
298
|
-
exports.attachedValuePrefix = attachedValuePrefix;
|
|
299
|
-
exports.combinedOptionScope = combinedOptionScope;
|
|
300
|
-
exports.conditionalOptionScope = conditionalOptionScope;
|
|
301
|
-
exports.forkOptionScope = forkOptionScope;
|
|
302
|
-
exports.fullNameOwnsToken = fullNameOwnsToken;
|
|
303
|
-
exports.inheritOptionScope = inheritOptionScope;
|
|
304
|
-
exports.prefixSuggestion = prefixSuggestion;
|
|
305
|
-
exports.repeatedOptionScope = repeatedOptionScope;
|
|
306
|
-
exports.restoreOptionScope = restoreOptionScope;
|
|
307
|
-
exports.scopeParser = scopeParser;
|
|
308
|
-
exports.selectableOptionScope = selectableOptionScope;
|
|
309
|
-
exports.withOptionScope = withOptionScope;
|
|
310
|
-
exports.withOptionScopeChild = withOptionScopeChild;
|
|
311
|
-
exports.withParserOptionScope = withParserOptionScope;
|
package/dist/short-option.js
DELETED
|
@@ -1,297 +0,0 @@
|
|
|
1
|
-
import { inheritAnnotations } from "./internal/annotations.js";
|
|
2
|
-
import { extractOptionNames } from "./usage.js";
|
|
3
|
-
import { mapModeValue } from "./internal/mode-dispatch.js";
|
|
4
|
-
import { withChildExecPath } from "./execution-context.js";
|
|
5
|
-
import { collectOptionNamesAtCurrentCommandDepth } from "./usage-internals.js";
|
|
6
|
-
|
|
7
|
-
//#region src/short-option.ts
|
|
8
|
-
const optionScopeKey = Symbol("optionScope");
|
|
9
|
-
const optionPathKey = Symbol("optionScopePath");
|
|
10
|
-
const scopeSourceKey = Symbol("optionScopeSource");
|
|
11
|
-
const scopeLocations = /* @__PURE__ */ new WeakMap();
|
|
12
|
-
const scopeBindings = /* @__PURE__ */ new WeakMap();
|
|
13
|
-
function getScopeBindings(selections) {
|
|
14
|
-
let bindings = scopeBindings.get(selections);
|
|
15
|
-
if (bindings == null) {
|
|
16
|
-
bindings = /* @__PURE__ */ new WeakMap();
|
|
17
|
-
scopeBindings.set(selections, bindings);
|
|
18
|
-
}
|
|
19
|
-
return bindings;
|
|
20
|
-
}
|
|
21
|
-
function bindScope(source, path, selections) {
|
|
22
|
-
const boundScopes = getScopeBindings(selections);
|
|
23
|
-
let node = boundScopes.get(source);
|
|
24
|
-
if (node == null) {
|
|
25
|
-
node = {
|
|
26
|
-
children: /* @__PURE__ */ new Map(),
|
|
27
|
-
bound: (selections$1, arities, context) => source(selections$1, arities, context, [])
|
|
28
|
-
};
|
|
29
|
-
boundScopes.set(source, node);
|
|
30
|
-
scopeLocations.set(node.bound, []);
|
|
31
|
-
}
|
|
32
|
-
for (let i = 0; i < path.length; i++) {
|
|
33
|
-
let child = node.children.get(path[i]);
|
|
34
|
-
if (child == null) {
|
|
35
|
-
const occurrence = path.slice(0, i + 1);
|
|
36
|
-
child = {
|
|
37
|
-
children: /* @__PURE__ */ new Map(),
|
|
38
|
-
bound: (selections$1, arities, context) => source(selections$1, arities, context, occurrence)
|
|
39
|
-
};
|
|
40
|
-
node.children.set(path[i], child);
|
|
41
|
-
scopeLocations.set(child.bound, occurrence);
|
|
42
|
-
}
|
|
43
|
-
node = child;
|
|
44
|
-
}
|
|
45
|
-
return node.bound;
|
|
46
|
-
}
|
|
47
|
-
function scopePath(context) {
|
|
48
|
-
return context.exec?.path ?? context[optionPathKey] ?? [];
|
|
49
|
-
}
|
|
50
|
-
/** Tracks child occurrences even for contexts without execution metadata. @internal */
|
|
51
|
-
function withOptionScopeChild(context, segment) {
|
|
52
|
-
return {
|
|
53
|
-
...context,
|
|
54
|
-
[optionPathKey]: [...scopePath(context), segment]
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
const primitiveScopes = /* @__PURE__ */ new WeakMap();
|
|
58
|
-
function parserScope(parser) {
|
|
59
|
-
const declared = parser[scopeSourceKey];
|
|
60
|
-
if (declared != null) return declared;
|
|
61
|
-
let source = primitiveScopes.get(parser);
|
|
62
|
-
if (source == null) {
|
|
63
|
-
source = (_selections, arities) => {
|
|
64
|
-
if (arities != null) collectOptionNamesAtCurrentCommandDepth(parser.usage, arities, false);
|
|
65
|
-
return parser.leadingNames;
|
|
66
|
-
};
|
|
67
|
-
primitiveScopes.set(parser, source);
|
|
68
|
-
}
|
|
69
|
-
return source;
|
|
70
|
-
}
|
|
71
|
-
/** Preserves dynamic option ownership through a transparent wrapper. @internal */
|
|
72
|
-
function inheritOptionScope(target, inner) {
|
|
73
|
-
Object.defineProperty(target, scopeSourceKey, {
|
|
74
|
-
value: parserScope(inner),
|
|
75
|
-
enumerable: true,
|
|
76
|
-
configurable: true
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
/** Combines declarations without flattening conditional ownership. @internal */
|
|
80
|
-
function combinedOptionScope(parsers, segments) {
|
|
81
|
-
return (selections, arities, context, path = []) => new Set(parsers.flatMap((parser, index) => [...parserScope(parser)(selections, arities, context, segments == null ? path : [...path, segments[index]])]));
|
|
82
|
-
}
|
|
83
|
-
/** Evaluates repeated declarations at their actual item occurrences. @internal */
|
|
84
|
-
function repeatedOptionScope(parser) {
|
|
85
|
-
return (selections, arities, context, path = []) => {
|
|
86
|
-
const indices = /* @__PURE__ */ new Set();
|
|
87
|
-
const collect = (location) => {
|
|
88
|
-
if (location != null && path.every((segment, index) => location[index] === segment)) {
|
|
89
|
-
const index = location[path.length];
|
|
90
|
-
if (typeof index === "number") indices.add(index);
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
for (const selected of selections.keys()) collect(scopeLocations.get(selected));
|
|
94
|
-
if (context != null) collect(scopePath(context));
|
|
95
|
-
if (indices.size === 0) indices.add(0);
|
|
96
|
-
return new Set([...indices].flatMap((index) => [...parserScope(parser)(selections, arities, context, [...path, index])]));
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
/** Tracks declarations after a command or discriminator routes input. @internal */
|
|
100
|
-
function selectableOptionScope(initial) {
|
|
101
|
-
const source = (selections, arities, context, path = []) => (selections.get(bindScope(source, path, selections)) ?? initial)(selections, arities, context, path);
|
|
102
|
-
return {
|
|
103
|
-
source,
|
|
104
|
-
select(context, selected) {
|
|
105
|
-
const selections = context[optionScopeKey]?.selections;
|
|
106
|
-
if (selections == null) return;
|
|
107
|
-
selections.set(bindScope(source, scopePath(context), selections), selected);
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
/** Keeps selected conditional names distinct from sibling declarations. @internal */
|
|
112
|
-
function conditionalOptionScope(discriminator, fallback, candidates = []) {
|
|
113
|
-
const scope = selectableOptionScope((selections, arities, context, path) => {
|
|
114
|
-
const canSkip = discriminator.canSkip?.(context == null ? discriminator.initialState : inheritAnnotations(context.state, discriminator.initialState), context?.exec == null ? void 0 : withChildExecPath({
|
|
115
|
-
...context.exec,
|
|
116
|
-
path: path ?? scopePath(context)
|
|
117
|
-
}, "_discriminator")) === true;
|
|
118
|
-
return combinedOptionScope([
|
|
119
|
-
discriminator,
|
|
120
|
-
...canSkip ? candidates : [],
|
|
121
|
-
...fallback == null ? [] : [fallback]
|
|
122
|
-
], [
|
|
123
|
-
"_discriminator",
|
|
124
|
-
...canSkip ? candidates.map(() => "_branch") : [],
|
|
125
|
-
...fallback == null ? [] : ["_branch"]
|
|
126
|
-
])(selections, arities, context, path);
|
|
127
|
-
});
|
|
128
|
-
return {
|
|
129
|
-
source: scope.source,
|
|
130
|
-
select(context, branch) {
|
|
131
|
-
scope.select(context, combinedOptionScope([discriminator, branch], ["_discriminator", "_branch"]));
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
/** Returns the names reachable in this parse scope. @internal */
|
|
136
|
-
function getOptionScope(context) {
|
|
137
|
-
const scoped = context[optionScopeKey];
|
|
138
|
-
const names = scoped == null ? extractOptionNames(context.usage, true) : new Set([...scoped.names, ...[...scoped.sources].flatMap((source) => [...source(scoped.selections, void 0, context)])]);
|
|
139
|
-
return names;
|
|
140
|
-
}
|
|
141
|
-
/** Adds reachable names without consulting the flattened help tree. @internal */
|
|
142
|
-
function withOptionScope(context, names) {
|
|
143
|
-
const inherited = context[optionScopeKey];
|
|
144
|
-
return {
|
|
145
|
-
...context,
|
|
146
|
-
[optionScopeKey]: {
|
|
147
|
-
names: new Set([...inherited?.names ?? [], ...names]),
|
|
148
|
-
sources: inherited?.sources ?? /* @__PURE__ */ new Set(),
|
|
149
|
-
selections: inherited?.selections ?? /* @__PURE__ */ new Map()
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
/** Adds a parser's owned declarations, retaining modifier metadata. @internal */
|
|
154
|
-
function withParserOptionScope(context, parser, segment) {
|
|
155
|
-
const inherited = context[optionScopeKey];
|
|
156
|
-
const selections = inherited?.selections ?? /* @__PURE__ */ new Map();
|
|
157
|
-
return {
|
|
158
|
-
...context,
|
|
159
|
-
[optionScopeKey]: {
|
|
160
|
-
names: inherited?.names ?? /* @__PURE__ */ new Set(),
|
|
161
|
-
sources: new Set([...inherited?.sources ?? [], bindScope(parserScope(parser), segment == null ? scopePath(context) : [...scopePath(context), segment], selections)]),
|
|
162
|
-
selections
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
/** Isolates route selections while probing a competing branch. @internal */
|
|
167
|
-
function forkOptionScope(context) {
|
|
168
|
-
const scoped = context[optionScopeKey];
|
|
169
|
-
if (scoped == null) return context;
|
|
170
|
-
const selections = new Map(scoped.selections);
|
|
171
|
-
scopeBindings.set(selections, getScopeBindings(scoped.selections));
|
|
172
|
-
return {
|
|
173
|
-
...context,
|
|
174
|
-
[optionScopeKey]: {
|
|
175
|
-
...scoped,
|
|
176
|
-
selections
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
/** Publishes winning routes without persisting temporary candidate names. @internal */
|
|
181
|
-
function adoptOptionScope(context, next) {
|
|
182
|
-
const own = context[optionScopeKey];
|
|
183
|
-
const child = next[optionScopeKey];
|
|
184
|
-
if (own != null && child != null) for (const [source, selected] of child.selections) own.selections.set(source, selected);
|
|
185
|
-
return context;
|
|
186
|
-
}
|
|
187
|
-
/** Removes names reserved only for a speculative probe. @internal */
|
|
188
|
-
function restoreOptionScope(next, parent) {
|
|
189
|
-
adoptOptionScope(parent, next);
|
|
190
|
-
const own = parent[optionScopeKey];
|
|
191
|
-
const child = next[optionScopeKey];
|
|
192
|
-
return own == null || child == null ? next : {
|
|
193
|
-
...next,
|
|
194
|
-
[optionScopeKey]: {
|
|
195
|
-
...child,
|
|
196
|
-
names: own.names,
|
|
197
|
-
selections: own.selections
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
/** Decorates construct entry points while retaining internal metadata. @internal */
|
|
202
|
-
function scopeParser(parser, source = () => parser.leadingNames, prepare, isolate = false) {
|
|
203
|
-
const parse = parser.parse;
|
|
204
|
-
const suggest = parser.suggest;
|
|
205
|
-
Object.defineProperties(parser, {
|
|
206
|
-
[scopeSourceKey]: {
|
|
207
|
-
value: source,
|
|
208
|
-
enumerable: true
|
|
209
|
-
},
|
|
210
|
-
parse: {
|
|
211
|
-
enumerable: true,
|
|
212
|
-
configurable: true,
|
|
213
|
-
writable: true,
|
|
214
|
-
value: (context) => {
|
|
215
|
-
const scoped = withParserOptionScope(isolate ? forkOptionScope(context) : context, parser);
|
|
216
|
-
prepare?.(scoped);
|
|
217
|
-
return mapModeValue(parser.mode, parse.call(parser, scoped), (result) => {
|
|
218
|
-
if (!result.success) return result;
|
|
219
|
-
let next = result.next;
|
|
220
|
-
if (isolate) {
|
|
221
|
-
adoptOptionScope(context, next);
|
|
222
|
-
const parent = context[optionScopeKey];
|
|
223
|
-
const child = next[optionScopeKey];
|
|
224
|
-
if (parent != null && child != null) next = {
|
|
225
|
-
...next,
|
|
226
|
-
[optionScopeKey]: {
|
|
227
|
-
...child,
|
|
228
|
-
selections: parent.selections
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
return {
|
|
233
|
-
...result,
|
|
234
|
-
next: {
|
|
235
|
-
...next,
|
|
236
|
-
[optionPathKey]: scopePath(context)
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
},
|
|
242
|
-
suggest: {
|
|
243
|
-
enumerable: true,
|
|
244
|
-
configurable: true,
|
|
245
|
-
writable: true,
|
|
246
|
-
value: (context, prefix) => {
|
|
247
|
-
const scoped = withParserOptionScope(context, parser);
|
|
248
|
-
prepare?.(scoped);
|
|
249
|
-
return suggest.call(parser, scoped, prefix);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
});
|
|
253
|
-
return parser;
|
|
254
|
-
}
|
|
255
|
-
/** Tests full-name ownership before splitting a short prefix. @internal */
|
|
256
|
-
function fullNameOwnsToken(context, token) {
|
|
257
|
-
if (!/^-[^-]/.test(token)) return false;
|
|
258
|
-
for (const name of getOptionScope(context)) if (name.startsWith("-") && !name.startsWith("--") && name.length > 2 && !/^-[^-]$/u.test(name) && (token === name || token.startsWith(`${name}=`))) return true;
|
|
259
|
-
return false;
|
|
260
|
-
}
|
|
261
|
-
/** Finds the value part of an attached short-option completion. @internal */
|
|
262
|
-
function attachedValuePrefix(context, prefix, optionNames) {
|
|
263
|
-
if (!/^-[^-]/.test(prefix)) return void 0;
|
|
264
|
-
const scope = getOptionScope(context);
|
|
265
|
-
const names = {
|
|
266
|
-
value: /* @__PURE__ */ new Set(),
|
|
267
|
-
flag: /* @__PURE__ */ new Set()
|
|
268
|
-
};
|
|
269
|
-
const scoped = context[optionScopeKey];
|
|
270
|
-
if (scoped == null) collectOptionNamesAtCurrentCommandDepth(context.usage, names, false);
|
|
271
|
-
else for (const source of scoped.sources) source(scoped.selections, names, context);
|
|
272
|
-
for (let index = 1; index < prefix.length;) {
|
|
273
|
-
const character = String.fromCodePoint(prefix.codePointAt(index));
|
|
274
|
-
const nextIndex = index + character.length;
|
|
275
|
-
const remainder = `-${prefix.slice(index)}`;
|
|
276
|
-
for (const name of scope) if (name.startsWith("-") && !name.startsWith("--") && name.length > 2 && !/^-[^-]$/u.test(name) && (name.startsWith(remainder) || remainder.startsWith(`${name}=`))) return void 0;
|
|
277
|
-
const short = `-${character}`;
|
|
278
|
-
if (optionNames.includes(short)) return {
|
|
279
|
-
head: prefix.slice(0, nextIndex),
|
|
280
|
-
value: prefix.slice(nextIndex)
|
|
281
|
-
};
|
|
282
|
-
if (!scope.has(short) || !names.flag.has(short) || names.value.has(short)) return void 0;
|
|
283
|
-
index = nextIndex;
|
|
284
|
-
}
|
|
285
|
-
return void 0;
|
|
286
|
-
}
|
|
287
|
-
/** Preserves the prefix when embedding value suggestions in one token. @internal */
|
|
288
|
-
function prefixSuggestion(head, suggestion) {
|
|
289
|
-
return {
|
|
290
|
-
kind: "literal",
|
|
291
|
-
text: head + (suggestion.kind === "literal" ? suggestion.text : suggestion.pattern ?? ""),
|
|
292
|
-
...suggestion.description != null ? { description: suggestion.description } : {}
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
//#endregion
|
|
297
|
-
export { adoptOptionScope, attachedValuePrefix, combinedOptionScope, conditionalOptionScope, forkOptionScope, fullNameOwnsToken, inheritOptionScope, prefixSuggestion, repeatedOptionScope, restoreOptionScope, scopeParser, selectableOptionScope, withOptionScope, withOptionScopeChild, withParserOptionScope };
|