@optique/core 1.4.0-dev.2611 → 1.4.0-dev.2614
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 +143 -86
- package/dist/constructs.js +143 -86
- package/dist/execution-context.cjs +2 -1
- package/dist/execution-context.js +3 -1
- package/dist/facade.cjs +3 -69
- package/dist/facade.js +1 -67
- package/dist/modifiers.cjs +5 -4
- package/dist/modifiers.js +5 -4
- package/dist/primitives.cjs +68 -26
- package/dist/primitives.d.cts +3 -3
- package/dist/primitives.d.ts +3 -3
- package/dist/primitives.js +68 -26
- package/dist/short-option.cjs +283 -0
- package/dist/short-option.js +270 -0
- package/dist/usage-internals.cjs +73 -0
- package/dist/usage-internals.js +71 -1
- package/package.json +2 -2
- package/skills/optique/SKILL.md +7 -7
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { inheritAnnotations } from "./internal/annotations.js";
|
|
2
|
+
import { extractOptionNames } from "./usage.js";
|
|
3
|
+
import { mapModeValue } from "./internal/mode-dispatch.js";
|
|
4
|
+
import { collectOptionNamesAtCurrentCommandDepth } from "./usage-internals.js";
|
|
5
|
+
|
|
6
|
+
//#region src/short-option.ts
|
|
7
|
+
const optionScopeKey = Symbol("optionScope");
|
|
8
|
+
const optionPathKey = Symbol("optionScopePath");
|
|
9
|
+
const scopeSourceKey = Symbol("optionScopeSource");
|
|
10
|
+
const scopeLocations = /* @__PURE__ */ new WeakMap();
|
|
11
|
+
const boundScopes = /* @__PURE__ */ new WeakMap();
|
|
12
|
+
function bindScope(source, path) {
|
|
13
|
+
let node = boundScopes.get(source);
|
|
14
|
+
if (node == null) {
|
|
15
|
+
node = {
|
|
16
|
+
children: /* @__PURE__ */ new Map(),
|
|
17
|
+
bound: (selections, arities, context) => source(selections, arities, context, [])
|
|
18
|
+
};
|
|
19
|
+
boundScopes.set(source, node);
|
|
20
|
+
scopeLocations.set(node.bound, []);
|
|
21
|
+
}
|
|
22
|
+
for (let i = 0; i < path.length; i++) {
|
|
23
|
+
let child = node.children.get(path[i]);
|
|
24
|
+
if (child == null) {
|
|
25
|
+
const occurrence = path.slice(0, i + 1);
|
|
26
|
+
child = {
|
|
27
|
+
children: /* @__PURE__ */ new Map(),
|
|
28
|
+
bound: (selections, arities, context) => source(selections, arities, context, occurrence)
|
|
29
|
+
};
|
|
30
|
+
node.children.set(path[i], child);
|
|
31
|
+
scopeLocations.set(child.bound, occurrence);
|
|
32
|
+
}
|
|
33
|
+
node = child;
|
|
34
|
+
}
|
|
35
|
+
return node.bound;
|
|
36
|
+
}
|
|
37
|
+
function scopePath(context) {
|
|
38
|
+
return context.exec?.path ?? context[optionPathKey] ?? [];
|
|
39
|
+
}
|
|
40
|
+
/** Tracks child occurrences even for contexts without execution metadata. @internal */
|
|
41
|
+
function withOptionScopeChild(context, segment) {
|
|
42
|
+
return {
|
|
43
|
+
...context,
|
|
44
|
+
[optionPathKey]: [...scopePath(context), segment]
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const primitiveScopes = /* @__PURE__ */ new WeakMap();
|
|
48
|
+
function parserScope(parser) {
|
|
49
|
+
const declared = parser[scopeSourceKey];
|
|
50
|
+
if (declared != null) return declared;
|
|
51
|
+
let source = primitiveScopes.get(parser);
|
|
52
|
+
if (source == null) {
|
|
53
|
+
source = (_selections, arities) => {
|
|
54
|
+
if (arities != null) collectOptionNamesAtCurrentCommandDepth(parser.usage, arities, false);
|
|
55
|
+
return parser.leadingNames;
|
|
56
|
+
};
|
|
57
|
+
primitiveScopes.set(parser, source);
|
|
58
|
+
}
|
|
59
|
+
return source;
|
|
60
|
+
}
|
|
61
|
+
/** Combines declarations without flattening conditional ownership. @internal */
|
|
62
|
+
function combinedOptionScope(parsers, segments) {
|
|
63
|
+
return (selections, arities, context, path = []) => new Set(parsers.flatMap((parser, index) => [...parserScope(parser)(selections, arities, context, segments == null ? path : [...path, segments[index]])]));
|
|
64
|
+
}
|
|
65
|
+
/** Evaluates repeated declarations at their actual item occurrences. @internal */
|
|
66
|
+
function repeatedOptionScope(parser) {
|
|
67
|
+
return (selections, arities, context, path = []) => {
|
|
68
|
+
const indices = /* @__PURE__ */ new Set();
|
|
69
|
+
const collect = (location) => {
|
|
70
|
+
if (location != null && path.every((segment, index) => location[index] === segment)) {
|
|
71
|
+
const index = location[path.length];
|
|
72
|
+
if (typeof index === "number") indices.add(index);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
for (const selected of selections.keys()) collect(scopeLocations.get(selected));
|
|
76
|
+
if (context != null) collect(scopePath(context));
|
|
77
|
+
if (indices.size === 0) indices.add(0);
|
|
78
|
+
return new Set([...indices].flatMap((index) => [...parserScope(parser)(selections, arities, context, [...path, index])]));
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/** Tracks declarations after a command or discriminator routes input. @internal */
|
|
82
|
+
function selectableOptionScope(initial) {
|
|
83
|
+
const source = (selections, arities, context, path = []) => (selections.get(bindScope(source, path)) ?? initial)(selections, arities, context, path);
|
|
84
|
+
return {
|
|
85
|
+
source,
|
|
86
|
+
select(context, selected) {
|
|
87
|
+
context[optionScopeKey]?.selections.set(bindScope(source, scopePath(context)), selected);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** Keeps selected conditional names distinct from sibling declarations. @internal */
|
|
92
|
+
function conditionalOptionScope(discriminator, fallback, candidates = []) {
|
|
93
|
+
const scope = selectableOptionScope((selections, arities, context, path) => {
|
|
94
|
+
const canSkip = discriminator.canSkip?.(context == null ? discriminator.initialState : inheritAnnotations(context.state, discriminator.initialState), context?.exec) === true;
|
|
95
|
+
return combinedOptionScope([
|
|
96
|
+
discriminator,
|
|
97
|
+
...canSkip ? candidates : [],
|
|
98
|
+
...fallback == null ? [] : [fallback]
|
|
99
|
+
], [
|
|
100
|
+
"_discriminator",
|
|
101
|
+
...canSkip ? candidates.map(() => "_branch") : [],
|
|
102
|
+
...fallback == null ? [] : ["_branch"]
|
|
103
|
+
])(selections, arities, context, path);
|
|
104
|
+
});
|
|
105
|
+
return {
|
|
106
|
+
source: scope.source,
|
|
107
|
+
select(context, branch) {
|
|
108
|
+
scope.select(context, combinedOptionScope([discriminator, branch], ["_discriminator", "_branch"]));
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/** Returns the names reachable in this parse scope. @internal */
|
|
113
|
+
function getOptionScope(context) {
|
|
114
|
+
const scoped = context[optionScopeKey];
|
|
115
|
+
const names = scoped == null ? extractOptionNames(context.usage, true) : new Set([...scoped.names, ...[...scoped.sources].flatMap((source) => [...source(scoped.selections, void 0, context)])]);
|
|
116
|
+
return names;
|
|
117
|
+
}
|
|
118
|
+
/** Adds reachable names without consulting the flattened help tree. @internal */
|
|
119
|
+
function withOptionScope(context, names) {
|
|
120
|
+
const inherited = context[optionScopeKey];
|
|
121
|
+
return {
|
|
122
|
+
...context,
|
|
123
|
+
[optionScopeKey]: {
|
|
124
|
+
names: new Set([...inherited?.names ?? [], ...names]),
|
|
125
|
+
sources: inherited?.sources ?? /* @__PURE__ */ new Set(),
|
|
126
|
+
selections: inherited?.selections ?? /* @__PURE__ */ new Map()
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/** Adds a parser's owned declarations, retaining modifier metadata. @internal */
|
|
131
|
+
function withParserOptionScope(context, parser, segment) {
|
|
132
|
+
const inherited = context[optionScopeKey];
|
|
133
|
+
return {
|
|
134
|
+
...context,
|
|
135
|
+
[optionScopeKey]: {
|
|
136
|
+
names: inherited?.names ?? /* @__PURE__ */ new Set(),
|
|
137
|
+
sources: new Set([...inherited?.sources ?? [], bindScope(parserScope(parser), segment == null ? scopePath(context) : [...scopePath(context), segment])]),
|
|
138
|
+
selections: inherited?.selections ?? /* @__PURE__ */ new Map()
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
/** Isolates route selections while probing a competing branch. @internal */
|
|
143
|
+
function forkOptionScope(context) {
|
|
144
|
+
const scoped = context[optionScopeKey];
|
|
145
|
+
return scoped == null ? context : {
|
|
146
|
+
...context,
|
|
147
|
+
[optionScopeKey]: {
|
|
148
|
+
...scoped,
|
|
149
|
+
selections: new Map(scoped.selections)
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/** Publishes winning routes without persisting temporary candidate names. @internal */
|
|
154
|
+
function adoptOptionScope(context, next) {
|
|
155
|
+
const own = context[optionScopeKey];
|
|
156
|
+
const child = next[optionScopeKey];
|
|
157
|
+
if (own != null && child != null) for (const [source, selected] of child.selections) own.selections.set(source, selected);
|
|
158
|
+
return context;
|
|
159
|
+
}
|
|
160
|
+
/** Removes names reserved only for a speculative probe. @internal */
|
|
161
|
+
function restoreOptionScope(next, parent) {
|
|
162
|
+
adoptOptionScope(parent, next);
|
|
163
|
+
const own = parent[optionScopeKey];
|
|
164
|
+
const child = next[optionScopeKey];
|
|
165
|
+
return own == null || child == null ? next : {
|
|
166
|
+
...next,
|
|
167
|
+
[optionScopeKey]: {
|
|
168
|
+
...child,
|
|
169
|
+
names: own.names,
|
|
170
|
+
selections: own.selections
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/** Decorates construct entry points while retaining internal metadata. @internal */
|
|
175
|
+
function scopeParser(parser, source = () => parser.leadingNames, prepare, isolate = false) {
|
|
176
|
+
const parse = parser.parse;
|
|
177
|
+
const suggest = parser.suggest;
|
|
178
|
+
Object.defineProperties(parser, {
|
|
179
|
+
[scopeSourceKey]: {
|
|
180
|
+
value: source,
|
|
181
|
+
enumerable: true
|
|
182
|
+
},
|
|
183
|
+
parse: {
|
|
184
|
+
enumerable: true,
|
|
185
|
+
configurable: true,
|
|
186
|
+
writable: true,
|
|
187
|
+
value: (context) => {
|
|
188
|
+
const scoped = withParserOptionScope(isolate ? forkOptionScope(context) : context, parser);
|
|
189
|
+
prepare?.(scoped);
|
|
190
|
+
return mapModeValue(parser.mode, parse.call(parser, scoped), (result) => {
|
|
191
|
+
if (!result.success) return result;
|
|
192
|
+
let next = result.next;
|
|
193
|
+
if (isolate) {
|
|
194
|
+
adoptOptionScope(context, next);
|
|
195
|
+
const parent = context[optionScopeKey];
|
|
196
|
+
const child = next[optionScopeKey];
|
|
197
|
+
if (parent != null && child != null) next = {
|
|
198
|
+
...next,
|
|
199
|
+
[optionScopeKey]: {
|
|
200
|
+
...child,
|
|
201
|
+
selections: parent.selections
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
...result,
|
|
207
|
+
next: {
|
|
208
|
+
...next,
|
|
209
|
+
[optionPathKey]: scopePath(context)
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
suggest: {
|
|
216
|
+
enumerable: true,
|
|
217
|
+
configurable: true,
|
|
218
|
+
writable: true,
|
|
219
|
+
value: (context, prefix) => {
|
|
220
|
+
const scoped = withParserOptionScope(context, parser);
|
|
221
|
+
prepare?.(scoped);
|
|
222
|
+
return suggest.call(parser, scoped, prefix);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
return parser;
|
|
227
|
+
}
|
|
228
|
+
/** Tests full-name ownership before splitting a short prefix. @internal */
|
|
229
|
+
function fullNameOwnsToken(context, token) {
|
|
230
|
+
if (!/^-[^-]/.test(token)) return false;
|
|
231
|
+
for (const name of getOptionScope(context)) if (name.startsWith("-") && !name.startsWith("--") && name.length > 2 && !/^-[^-]$/u.test(name) && (token === name || token.startsWith(`${name}=`))) return true;
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
/** Finds the value part of an attached short-option completion. @internal */
|
|
235
|
+
function attachedValuePrefix(context, prefix, optionNames) {
|
|
236
|
+
if (!/^-[^-]/.test(prefix)) return void 0;
|
|
237
|
+
const scope = getOptionScope(context);
|
|
238
|
+
const names = {
|
|
239
|
+
value: /* @__PURE__ */ new Set(),
|
|
240
|
+
flag: /* @__PURE__ */ new Set()
|
|
241
|
+
};
|
|
242
|
+
const scoped = context[optionScopeKey];
|
|
243
|
+
if (scoped == null) collectOptionNamesAtCurrentCommandDepth(context.usage, names, false);
|
|
244
|
+
else for (const source of scoped.sources) source(scoped.selections, names, context);
|
|
245
|
+
for (let index = 1; index < prefix.length;) {
|
|
246
|
+
const character = String.fromCodePoint(prefix.codePointAt(index));
|
|
247
|
+
const nextIndex = index + character.length;
|
|
248
|
+
const remainder = `-${prefix.slice(index)}`;
|
|
249
|
+
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;
|
|
250
|
+
const short = `-${character}`;
|
|
251
|
+
if (optionNames.includes(short)) return {
|
|
252
|
+
head: prefix.slice(0, nextIndex),
|
|
253
|
+
value: prefix.slice(nextIndex)
|
|
254
|
+
};
|
|
255
|
+
if (!scope.has(short) || !names.flag.has(short) || names.value.has(short)) return void 0;
|
|
256
|
+
index = nextIndex;
|
|
257
|
+
}
|
|
258
|
+
return void 0;
|
|
259
|
+
}
|
|
260
|
+
/** Preserves the prefix when embedding value suggestions in one token. @internal */
|
|
261
|
+
function prefixSuggestion(head, suggestion) {
|
|
262
|
+
return {
|
|
263
|
+
kind: "literal",
|
|
264
|
+
text: head + (suggestion.kind === "literal" ? suggestion.text : suggestion.pattern ?? ""),
|
|
265
|
+
...suggestion.description != null ? { description: suggestion.description } : {}
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
//#endregion
|
|
270
|
+
export { adoptOptionScope, attachedValuePrefix, combinedOptionScope, conditionalOptionScope, forkOptionScope, fullNameOwnsToken, prefixSuggestion, repeatedOptionScope, restoreOptionScope, scopeParser, selectableOptionScope, withOptionScope, withOptionScopeChild, withParserOptionScope };
|
package/dist/usage-internals.cjs
CHANGED
|
@@ -76,7 +76,80 @@ function extractLeadingCommandNames(usage) {
|
|
|
76
76
|
collectLeadingCandidates(usage, options, commands);
|
|
77
77
|
return commands;
|
|
78
78
|
}
|
|
79
|
+
/** Collects arities along an entered command path. @internal */
|
|
80
|
+
function collectActiveOptionNames(usage, commandPath, names, fromExclusive, includeDirectAfterCommandOptions) {
|
|
81
|
+
if (commandPath.length === 0) {
|
|
82
|
+
collectOptionNamesAtCurrentCommandDepth(usage, names, false);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const [commandName, ...rest] = commandPath;
|
|
86
|
+
for (let i = 0; i < usage.length; i++) {
|
|
87
|
+
const term = usage[i];
|
|
88
|
+
if (term.type === "command" && term.name === commandName) {
|
|
89
|
+
const remainingUsage = usage.slice(i + 1);
|
|
90
|
+
if (rest.length === 0) collectOptionNamesAtCurrentCommandDepth(remainingUsage, names, !fromExclusive && includeDirectAfterCommandOptions);
|
|
91
|
+
else collectActiveOptionNames(remainingUsage, rest, names, fromExclusive, includeDirectAfterCommandOptions);
|
|
92
|
+
} else if (term.type === "exclusive") for (const branch of term.terms) collectActiveOptionNames(branch, commandPath, names, true, includeDirectAfterCommandOptions);
|
|
93
|
+
else if (term.type === "optional" || term.type === "multiple" || term.type === "sequence") collectActiveOptionNames(term.terms, commandPath, names, fromExclusive, includeDirectAfterCommandOptions);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/** Collects arities without entering further commands. @internal */
|
|
97
|
+
function collectOptionNamesAtCurrentCommandDepth(usage, names, afterMatchedCommand) {
|
|
98
|
+
for (const term of usage) if (collectOptionNamesAtCurrentCommandDepthFromTerm(term, names, afterMatchedCommand)) return;
|
|
99
|
+
}
|
|
100
|
+
function collectOptionNamesAtCurrentCommandDepthFromTerm(term, names, afterMatchedCommand) {
|
|
101
|
+
switch (term.type) {
|
|
102
|
+
case "command": return !afterMatchedCommand;
|
|
103
|
+
case "option":
|
|
104
|
+
if (term.metavar != null) for (const name of term.names) names.value.add(name);
|
|
105
|
+
else for (const name of term.names) names.flag.add(name);
|
|
106
|
+
return false;
|
|
107
|
+
case "optional":
|
|
108
|
+
case "multiple":
|
|
109
|
+
case "sequence":
|
|
110
|
+
collectOptionNamesAtCurrentCommandDepth(term.terms, names, afterMatchedCommand);
|
|
111
|
+
return false;
|
|
112
|
+
case "exclusive":
|
|
113
|
+
for (const branch of term.terms) collectOptionNamesAtCurrentCommandDepth(branch, names, false);
|
|
114
|
+
return false;
|
|
115
|
+
case "argument":
|
|
116
|
+
case "literal":
|
|
117
|
+
case "passthrough":
|
|
118
|
+
case "ellipsis": return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/** Collects scoped root arities across sibling commands. @internal */
|
|
122
|
+
function collectRootOptionNames(usage, names, rootLeadingNames) {
|
|
123
|
+
for (const term of usage) collectRootOptionNamesFromTerm(term, names, rootLeadingNames);
|
|
124
|
+
}
|
|
125
|
+
function collectRootOptionNamesFromTerm(term, names, rootLeadingNames) {
|
|
126
|
+
switch (term.type) {
|
|
127
|
+
case "option":
|
|
128
|
+
for (const name of term.names) {
|
|
129
|
+
if (!rootLeadingNames.has(name)) continue;
|
|
130
|
+
if (term.metavar != null) names.value.add(name);
|
|
131
|
+
else names.flag.add(name);
|
|
132
|
+
}
|
|
133
|
+
return;
|
|
134
|
+
case "optional":
|
|
135
|
+
case "multiple":
|
|
136
|
+
case "sequence":
|
|
137
|
+
collectRootOptionNames(term.terms, names, rootLeadingNames);
|
|
138
|
+
return;
|
|
139
|
+
case "exclusive":
|
|
140
|
+
for (const branch of term.terms) collectRootOptionNames(branch, names, rootLeadingNames);
|
|
141
|
+
return;
|
|
142
|
+
case "argument":
|
|
143
|
+
case "command":
|
|
144
|
+
case "literal":
|
|
145
|
+
case "passthrough":
|
|
146
|
+
case "ellipsis": return;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
79
149
|
|
|
80
150
|
//#endregion
|
|
151
|
+
exports.collectActiveOptionNames = collectActiveOptionNames;
|
|
81
152
|
exports.collectLeadingCandidates = collectLeadingCandidates;
|
|
153
|
+
exports.collectOptionNamesAtCurrentCommandDepth = collectOptionNamesAtCurrentCommandDepth;
|
|
154
|
+
exports.collectRootOptionNames = collectRootOptionNames;
|
|
82
155
|
exports.extractLeadingCommandNames = extractLeadingCommandNames;
|
package/dist/usage-internals.js
CHANGED
|
@@ -76,6 +76,76 @@ function extractLeadingCommandNames(usage) {
|
|
|
76
76
|
collectLeadingCandidates(usage, options, commands);
|
|
77
77
|
return commands;
|
|
78
78
|
}
|
|
79
|
+
/** Collects arities along an entered command path. @internal */
|
|
80
|
+
function collectActiveOptionNames(usage, commandPath, names, fromExclusive, includeDirectAfterCommandOptions) {
|
|
81
|
+
if (commandPath.length === 0) {
|
|
82
|
+
collectOptionNamesAtCurrentCommandDepth(usage, names, false);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const [commandName, ...rest] = commandPath;
|
|
86
|
+
for (let i = 0; i < usage.length; i++) {
|
|
87
|
+
const term = usage[i];
|
|
88
|
+
if (term.type === "command" && term.name === commandName) {
|
|
89
|
+
const remainingUsage = usage.slice(i + 1);
|
|
90
|
+
if (rest.length === 0) collectOptionNamesAtCurrentCommandDepth(remainingUsage, names, !fromExclusive && includeDirectAfterCommandOptions);
|
|
91
|
+
else collectActiveOptionNames(remainingUsage, rest, names, fromExclusive, includeDirectAfterCommandOptions);
|
|
92
|
+
} else if (term.type === "exclusive") for (const branch of term.terms) collectActiveOptionNames(branch, commandPath, names, true, includeDirectAfterCommandOptions);
|
|
93
|
+
else if (term.type === "optional" || term.type === "multiple" || term.type === "sequence") collectActiveOptionNames(term.terms, commandPath, names, fromExclusive, includeDirectAfterCommandOptions);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/** Collects arities without entering further commands. @internal */
|
|
97
|
+
function collectOptionNamesAtCurrentCommandDepth(usage, names, afterMatchedCommand) {
|
|
98
|
+
for (const term of usage) if (collectOptionNamesAtCurrentCommandDepthFromTerm(term, names, afterMatchedCommand)) return;
|
|
99
|
+
}
|
|
100
|
+
function collectOptionNamesAtCurrentCommandDepthFromTerm(term, names, afterMatchedCommand) {
|
|
101
|
+
switch (term.type) {
|
|
102
|
+
case "command": return !afterMatchedCommand;
|
|
103
|
+
case "option":
|
|
104
|
+
if (term.metavar != null) for (const name of term.names) names.value.add(name);
|
|
105
|
+
else for (const name of term.names) names.flag.add(name);
|
|
106
|
+
return false;
|
|
107
|
+
case "optional":
|
|
108
|
+
case "multiple":
|
|
109
|
+
case "sequence":
|
|
110
|
+
collectOptionNamesAtCurrentCommandDepth(term.terms, names, afterMatchedCommand);
|
|
111
|
+
return false;
|
|
112
|
+
case "exclusive":
|
|
113
|
+
for (const branch of term.terms) collectOptionNamesAtCurrentCommandDepth(branch, names, false);
|
|
114
|
+
return false;
|
|
115
|
+
case "argument":
|
|
116
|
+
case "literal":
|
|
117
|
+
case "passthrough":
|
|
118
|
+
case "ellipsis": return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/** Collects scoped root arities across sibling commands. @internal */
|
|
122
|
+
function collectRootOptionNames(usage, names, rootLeadingNames) {
|
|
123
|
+
for (const term of usage) collectRootOptionNamesFromTerm(term, names, rootLeadingNames);
|
|
124
|
+
}
|
|
125
|
+
function collectRootOptionNamesFromTerm(term, names, rootLeadingNames) {
|
|
126
|
+
switch (term.type) {
|
|
127
|
+
case "option":
|
|
128
|
+
for (const name of term.names) {
|
|
129
|
+
if (!rootLeadingNames.has(name)) continue;
|
|
130
|
+
if (term.metavar != null) names.value.add(name);
|
|
131
|
+
else names.flag.add(name);
|
|
132
|
+
}
|
|
133
|
+
return;
|
|
134
|
+
case "optional":
|
|
135
|
+
case "multiple":
|
|
136
|
+
case "sequence":
|
|
137
|
+
collectRootOptionNames(term.terms, names, rootLeadingNames);
|
|
138
|
+
return;
|
|
139
|
+
case "exclusive":
|
|
140
|
+
for (const branch of term.terms) collectRootOptionNames(branch, names, rootLeadingNames);
|
|
141
|
+
return;
|
|
142
|
+
case "argument":
|
|
143
|
+
case "command":
|
|
144
|
+
case "literal":
|
|
145
|
+
case "passthrough":
|
|
146
|
+
case "ellipsis": return;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
79
149
|
|
|
80
150
|
//#endregion
|
|
81
|
-
export { collectLeadingCandidates, extractLeadingCommandNames };
|
|
151
|
+
export { collectActiveOptionNames, collectLeadingCandidates, collectOptionNamesAtCurrentCommandDepth, collectRootOptionNames, extractLeadingCommandNames };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@optique/core",
|
|
3
|
-
"version": "1.4.0-dev.
|
|
3
|
+
"version": "1.4.0-dev.2614",
|
|
4
4
|
"description": "Type-safe combinatorial command-line interface parser",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"CLI",
|
|
@@ -237,7 +237,7 @@
|
|
|
237
237
|
},
|
|
238
238
|
"sideEffects": false,
|
|
239
239
|
"devDependencies": {
|
|
240
|
-
"@optique/env": "1.4.0-dev.
|
|
240
|
+
"@optique/env": "1.4.0-dev.2614+5915742e",
|
|
241
241
|
"@types/node": "^24.0.0",
|
|
242
242
|
"fast-check": "^4.7.0",
|
|
243
243
|
"tsdown": "^0.13.0",
|
package/skills/optique/SKILL.md
CHANGED
|
@@ -30,13 +30,11 @@ Core rules
|
|
|
30
30
|
Pin `colors`/`maxWidth` in tests; defaults use terminal and environment.
|
|
31
31
|
- Compose parsers with `object()`, `tuple()`, `seq()`, `or()`, `merge()`, and
|
|
32
32
|
modifiers. Do not hand-write argument scanners around Optique parsers.
|
|
33
|
-
- Let TypeScript infer
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
flags, use `withDefault(flag("--name"), false)` when absence should mean
|
|
39
|
-
`false`.
|
|
33
|
+
- Let TypeScript infer parser result types. Only write a separate result
|
|
34
|
+
interface when another API boundary requires it.
|
|
35
|
+
- Most parsers are required. `optional(p)` yields `undefined`, and
|
|
36
|
+
`withDefault(p, value)` supplies a fallback. For absent Boolean flags,
|
|
37
|
+
use `withDefault(flag("--name"), false)`.
|
|
40
38
|
- Use semantic `message` helpers for descriptions and errors. Since 1.3.0,
|
|
41
39
|
customize terminal output with `theme` or `messageFormatter`, preserving
|
|
42
40
|
`initialWidth`, quoting, and width options. Keep canonical errors unthemed.
|
|
@@ -49,6 +47,8 @@ Core rules
|
|
|
49
47
|
`@optique/run/valueparser` for file-system paths. Write a custom
|
|
50
48
|
`{ mode, metavar, parse, format }` value parser only when the catalog does
|
|
51
49
|
not cover the domain.
|
|
50
|
+
- Since 1.4.0, `-p8080`/`-vp8080` accept attached values. Values consume
|
|
51
|
+
the literal suffix (`-p=5` gives `"=5"`); full single-dash names win.
|
|
52
52
|
- Async value parsers like *@optique/git* make containing parsers async.
|
|
53
53
|
Await `run()`/`parse()`/`runParser()` or, for `bindKeyring()`, `runAsync()`.
|
|
54
54
|
- Use `dependency()` when one value parser controls another's valid values.
|