@optique/core 1.4.0-dev.2606 → 1.4.0-dev.2607

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.
@@ -0,0 +1,197 @@
1
+ const require_internal_annotations = require('./internal/annotations.cjs');
2
+ const require_usage = require('./usage.cjs');
3
+ const require_usage_internals = require('./usage-internals.cjs');
4
+
5
+ //#region src/short-option.ts
6
+ const optionScopeKey = Symbol("optionScope");
7
+ const scopeSourceKey = Symbol("optionScopeSource");
8
+ const primitiveScopes = /* @__PURE__ */ new WeakMap();
9
+ function parserScope(parser) {
10
+ const declared = parser[scopeSourceKey];
11
+ if (declared != null) return declared;
12
+ let source = primitiveScopes.get(parser);
13
+ if (source == null) {
14
+ source = (_selections, arities) => {
15
+ if (arities != null) require_usage_internals.collectOptionNamesAtCurrentCommandDepth(parser.usage, arities, false);
16
+ return parser.leadingNames;
17
+ };
18
+ primitiveScopes.set(parser, source);
19
+ }
20
+ return source;
21
+ }
22
+ /** Combines declarations without flattening conditional ownership. @internal */
23
+ function combinedOptionScope(parsers) {
24
+ return (selections, arities, context) => new Set(parsers.flatMap((parser) => [...parserScope(parser)(selections, arities, context)]));
25
+ }
26
+ /** Tracks declarations after a command or discriminator routes input. @internal */
27
+ function selectableOptionScope(initial) {
28
+ const source = (selections, arities, context) => (selections.get(source) ?? initial)(selections, arities, context);
29
+ return {
30
+ source,
31
+ select(context, selected) {
32
+ context[optionScopeKey]?.selections.set(source, selected);
33
+ }
34
+ };
35
+ }
36
+ /** Keeps selected conditional names distinct from sibling declarations. @internal */
37
+ function conditionalOptionScope(discriminator, fallback, candidates = []) {
38
+ const scope = selectableOptionScope((selections, arities, context) => {
39
+ const canSkip = discriminator.canSkip?.(context == null ? discriminator.initialState : require_internal_annotations.inheritAnnotations(context.state, discriminator.initialState), context?.exec) === true;
40
+ return combinedOptionScope([
41
+ discriminator,
42
+ ...canSkip ? candidates : [],
43
+ ...fallback == null ? [] : [fallback]
44
+ ])(selections, arities, context);
45
+ });
46
+ return {
47
+ source: scope.source,
48
+ select(context, branch) {
49
+ scope.select(context, combinedOptionScope([discriminator, branch]));
50
+ }
51
+ };
52
+ }
53
+ /** Returns the names reachable in this parse scope. @internal */
54
+ function getOptionScope(context) {
55
+ const scoped = context[optionScopeKey];
56
+ const names = scoped == null ? require_usage.extractOptionNames(context.usage, true) : new Set([...scoped.names, ...[...scoped.sources].flatMap((source) => [...source(scoped.selections, void 0, context)])]);
57
+ return names;
58
+ }
59
+ /** Adds reachable names without consulting the flattened help tree. @internal */
60
+ function withOptionScope(context, names) {
61
+ const inherited = context[optionScopeKey];
62
+ return {
63
+ ...context,
64
+ [optionScopeKey]: {
65
+ names: new Set([...inherited?.names ?? [], ...names]),
66
+ sources: inherited?.sources ?? /* @__PURE__ */ new Set(),
67
+ selections: inherited?.selections ?? /* @__PURE__ */ new Map()
68
+ }
69
+ };
70
+ }
71
+ /** Adds a parser's owned declarations, retaining modifier metadata. @internal */
72
+ function withParserOptionScope(context, parser) {
73
+ const inherited = context[optionScopeKey];
74
+ return {
75
+ ...context,
76
+ [optionScopeKey]: {
77
+ names: inherited?.names ?? /* @__PURE__ */ new Set(),
78
+ sources: new Set([...inherited?.sources ?? [], parserScope(parser)]),
79
+ selections: inherited?.selections ?? /* @__PURE__ */ new Map()
80
+ }
81
+ };
82
+ }
83
+ /** Isolates route selections while probing a competing branch. @internal */
84
+ function forkOptionScope(context) {
85
+ const scoped = context[optionScopeKey];
86
+ return scoped == null ? context : {
87
+ ...context,
88
+ [optionScopeKey]: {
89
+ ...scoped,
90
+ selections: new Map(scoped.selections)
91
+ }
92
+ };
93
+ }
94
+ /** Publishes winning routes without persisting temporary candidate names. @internal */
95
+ function adoptOptionScope(context, next) {
96
+ const own = context[optionScopeKey];
97
+ const child = next[optionScopeKey];
98
+ if (own != null && child != null) for (const [source, selected] of child.selections) own.selections.set(source, selected);
99
+ return context;
100
+ }
101
+ /** Removes names reserved only for a speculative probe. @internal */
102
+ function restoreOptionScope(next, parent) {
103
+ adoptOptionScope(parent, next);
104
+ const own = parent[optionScopeKey];
105
+ const child = next[optionScopeKey];
106
+ return own == null || child == null ? next : {
107
+ ...next,
108
+ [optionScopeKey]: {
109
+ ...child,
110
+ names: own.names,
111
+ selections: own.selections
112
+ }
113
+ };
114
+ }
115
+ /** Decorates construct entry points while retaining internal metadata. @internal */
116
+ function scopeParser(parser, source = () => parser.leadingNames, prepare) {
117
+ const parse = parser.parse;
118
+ const suggest = parser.suggest;
119
+ Object.defineProperties(parser, {
120
+ [scopeSourceKey]: {
121
+ value: source,
122
+ enumerable: true
123
+ },
124
+ parse: {
125
+ enumerable: true,
126
+ configurable: true,
127
+ writable: true,
128
+ value: (context) => {
129
+ const scoped = withParserOptionScope(context, parser);
130
+ prepare?.(scoped);
131
+ return parse.call(parser, scoped);
132
+ }
133
+ },
134
+ suggest: {
135
+ enumerable: true,
136
+ configurable: true,
137
+ writable: true,
138
+ value: (context, prefix) => {
139
+ const scoped = withParserOptionScope(context, parser);
140
+ prepare?.(scoped);
141
+ return suggest.call(parser, scoped, prefix);
142
+ }
143
+ }
144
+ });
145
+ return parser;
146
+ }
147
+ /** Tests full-name ownership before splitting a short prefix. @internal */
148
+ function fullNameOwnsToken(context, token) {
149
+ if (!/^-[^-]/.test(token)) return false;
150
+ for (const name of getOptionScope(context)) if (name.startsWith("-") && !name.startsWith("--") && name.length > 2 && (token === name || token.startsWith(`${name}=`))) return true;
151
+ return false;
152
+ }
153
+ /** Finds the value part of an attached short-option completion. @internal */
154
+ function attachedValuePrefix(context, prefix, optionNames) {
155
+ if (!/^-[^-]/.test(prefix)) return void 0;
156
+ const scope = getOptionScope(context);
157
+ const names = {
158
+ value: /* @__PURE__ */ new Set(),
159
+ flag: /* @__PURE__ */ new Set()
160
+ };
161
+ const scoped = context[optionScopeKey];
162
+ if (scoped == null) require_usage_internals.collectOptionNamesAtCurrentCommandDepth(context.usage, names, false);
163
+ else for (const source of scoped.sources) source(scoped.selections, names, context);
164
+ for (let index = 1; index < prefix.length; index++) {
165
+ const remainder = `-${prefix.slice(index)}`;
166
+ for (const name of scope) if (name.startsWith("-") && !name.startsWith("--") && name.length > 2 && (name.startsWith(remainder) || remainder.startsWith(`${name}=`))) return void 0;
167
+ const short = `-${prefix[index]}`;
168
+ if (optionNames.includes(short)) return {
169
+ head: prefix.slice(0, index + 1),
170
+ value: prefix.slice(index + 1)
171
+ };
172
+ if (!scope.has(short) || !names.flag.has(short) || names.value.has(short)) return void 0;
173
+ }
174
+ return void 0;
175
+ }
176
+ /** Preserves the prefix when embedding value suggestions in one token. @internal */
177
+ function prefixSuggestion(head, suggestion) {
178
+ return {
179
+ kind: "literal",
180
+ text: head + (suggestion.kind === "literal" ? suggestion.text : suggestion.pattern ?? ""),
181
+ ...suggestion.description != null ? { description: suggestion.description } : {}
182
+ };
183
+ }
184
+
185
+ //#endregion
186
+ exports.adoptOptionScope = adoptOptionScope;
187
+ exports.attachedValuePrefix = attachedValuePrefix;
188
+ exports.combinedOptionScope = combinedOptionScope;
189
+ exports.conditionalOptionScope = conditionalOptionScope;
190
+ exports.forkOptionScope = forkOptionScope;
191
+ exports.fullNameOwnsToken = fullNameOwnsToken;
192
+ exports.prefixSuggestion = prefixSuggestion;
193
+ exports.restoreOptionScope = restoreOptionScope;
194
+ exports.scopeParser = scopeParser;
195
+ exports.selectableOptionScope = selectableOptionScope;
196
+ exports.withOptionScope = withOptionScope;
197
+ exports.withParserOptionScope = withParserOptionScope;
@@ -0,0 +1,186 @@
1
+ import { inheritAnnotations } from "./internal/annotations.js";
2
+ import { extractOptionNames } from "./usage.js";
3
+ import { collectOptionNamesAtCurrentCommandDepth } from "./usage-internals.js";
4
+
5
+ //#region src/short-option.ts
6
+ const optionScopeKey = Symbol("optionScope");
7
+ const scopeSourceKey = Symbol("optionScopeSource");
8
+ const primitiveScopes = /* @__PURE__ */ new WeakMap();
9
+ function parserScope(parser) {
10
+ const declared = parser[scopeSourceKey];
11
+ if (declared != null) return declared;
12
+ let source = primitiveScopes.get(parser);
13
+ if (source == null) {
14
+ source = (_selections, arities) => {
15
+ if (arities != null) collectOptionNamesAtCurrentCommandDepth(parser.usage, arities, false);
16
+ return parser.leadingNames;
17
+ };
18
+ primitiveScopes.set(parser, source);
19
+ }
20
+ return source;
21
+ }
22
+ /** Combines declarations without flattening conditional ownership. @internal */
23
+ function combinedOptionScope(parsers) {
24
+ return (selections, arities, context) => new Set(parsers.flatMap((parser) => [...parserScope(parser)(selections, arities, context)]));
25
+ }
26
+ /** Tracks declarations after a command or discriminator routes input. @internal */
27
+ function selectableOptionScope(initial) {
28
+ const source = (selections, arities, context) => (selections.get(source) ?? initial)(selections, arities, context);
29
+ return {
30
+ source,
31
+ select(context, selected) {
32
+ context[optionScopeKey]?.selections.set(source, selected);
33
+ }
34
+ };
35
+ }
36
+ /** Keeps selected conditional names distinct from sibling declarations. @internal */
37
+ function conditionalOptionScope(discriminator, fallback, candidates = []) {
38
+ const scope = selectableOptionScope((selections, arities, context) => {
39
+ const canSkip = discriminator.canSkip?.(context == null ? discriminator.initialState : inheritAnnotations(context.state, discriminator.initialState), context?.exec) === true;
40
+ return combinedOptionScope([
41
+ discriminator,
42
+ ...canSkip ? candidates : [],
43
+ ...fallback == null ? [] : [fallback]
44
+ ])(selections, arities, context);
45
+ });
46
+ return {
47
+ source: scope.source,
48
+ select(context, branch) {
49
+ scope.select(context, combinedOptionScope([discriminator, branch]));
50
+ }
51
+ };
52
+ }
53
+ /** Returns the names reachable in this parse scope. @internal */
54
+ function getOptionScope(context) {
55
+ const scoped = context[optionScopeKey];
56
+ const names = scoped == null ? extractOptionNames(context.usage, true) : new Set([...scoped.names, ...[...scoped.sources].flatMap((source) => [...source(scoped.selections, void 0, context)])]);
57
+ return names;
58
+ }
59
+ /** Adds reachable names without consulting the flattened help tree. @internal */
60
+ function withOptionScope(context, names) {
61
+ const inherited = context[optionScopeKey];
62
+ return {
63
+ ...context,
64
+ [optionScopeKey]: {
65
+ names: new Set([...inherited?.names ?? [], ...names]),
66
+ sources: inherited?.sources ?? /* @__PURE__ */ new Set(),
67
+ selections: inherited?.selections ?? /* @__PURE__ */ new Map()
68
+ }
69
+ };
70
+ }
71
+ /** Adds a parser's owned declarations, retaining modifier metadata. @internal */
72
+ function withParserOptionScope(context, parser) {
73
+ const inherited = context[optionScopeKey];
74
+ return {
75
+ ...context,
76
+ [optionScopeKey]: {
77
+ names: inherited?.names ?? /* @__PURE__ */ new Set(),
78
+ sources: new Set([...inherited?.sources ?? [], parserScope(parser)]),
79
+ selections: inherited?.selections ?? /* @__PURE__ */ new Map()
80
+ }
81
+ };
82
+ }
83
+ /** Isolates route selections while probing a competing branch. @internal */
84
+ function forkOptionScope(context) {
85
+ const scoped = context[optionScopeKey];
86
+ return scoped == null ? context : {
87
+ ...context,
88
+ [optionScopeKey]: {
89
+ ...scoped,
90
+ selections: new Map(scoped.selections)
91
+ }
92
+ };
93
+ }
94
+ /** Publishes winning routes without persisting temporary candidate names. @internal */
95
+ function adoptOptionScope(context, next) {
96
+ const own = context[optionScopeKey];
97
+ const child = next[optionScopeKey];
98
+ if (own != null && child != null) for (const [source, selected] of child.selections) own.selections.set(source, selected);
99
+ return context;
100
+ }
101
+ /** Removes names reserved only for a speculative probe. @internal */
102
+ function restoreOptionScope(next, parent) {
103
+ adoptOptionScope(parent, next);
104
+ const own = parent[optionScopeKey];
105
+ const child = next[optionScopeKey];
106
+ return own == null || child == null ? next : {
107
+ ...next,
108
+ [optionScopeKey]: {
109
+ ...child,
110
+ names: own.names,
111
+ selections: own.selections
112
+ }
113
+ };
114
+ }
115
+ /** Decorates construct entry points while retaining internal metadata. @internal */
116
+ function scopeParser(parser, source = () => parser.leadingNames, prepare) {
117
+ const parse = parser.parse;
118
+ const suggest = parser.suggest;
119
+ Object.defineProperties(parser, {
120
+ [scopeSourceKey]: {
121
+ value: source,
122
+ enumerable: true
123
+ },
124
+ parse: {
125
+ enumerable: true,
126
+ configurable: true,
127
+ writable: true,
128
+ value: (context) => {
129
+ const scoped = withParserOptionScope(context, parser);
130
+ prepare?.(scoped);
131
+ return parse.call(parser, scoped);
132
+ }
133
+ },
134
+ suggest: {
135
+ enumerable: true,
136
+ configurable: true,
137
+ writable: true,
138
+ value: (context, prefix) => {
139
+ const scoped = withParserOptionScope(context, parser);
140
+ prepare?.(scoped);
141
+ return suggest.call(parser, scoped, prefix);
142
+ }
143
+ }
144
+ });
145
+ return parser;
146
+ }
147
+ /** Tests full-name ownership before splitting a short prefix. @internal */
148
+ function fullNameOwnsToken(context, token) {
149
+ if (!/^-[^-]/.test(token)) return false;
150
+ for (const name of getOptionScope(context)) if (name.startsWith("-") && !name.startsWith("--") && name.length > 2 && (token === name || token.startsWith(`${name}=`))) return true;
151
+ return false;
152
+ }
153
+ /** Finds the value part of an attached short-option completion. @internal */
154
+ function attachedValuePrefix(context, prefix, optionNames) {
155
+ if (!/^-[^-]/.test(prefix)) return void 0;
156
+ const scope = getOptionScope(context);
157
+ const names = {
158
+ value: /* @__PURE__ */ new Set(),
159
+ flag: /* @__PURE__ */ new Set()
160
+ };
161
+ const scoped = context[optionScopeKey];
162
+ if (scoped == null) collectOptionNamesAtCurrentCommandDepth(context.usage, names, false);
163
+ else for (const source of scoped.sources) source(scoped.selections, names, context);
164
+ for (let index = 1; index < prefix.length; index++) {
165
+ const remainder = `-${prefix.slice(index)}`;
166
+ for (const name of scope) if (name.startsWith("-") && !name.startsWith("--") && name.length > 2 && (name.startsWith(remainder) || remainder.startsWith(`${name}=`))) return void 0;
167
+ const short = `-${prefix[index]}`;
168
+ if (optionNames.includes(short)) return {
169
+ head: prefix.slice(0, index + 1),
170
+ value: prefix.slice(index + 1)
171
+ };
172
+ if (!scope.has(short) || !names.flag.has(short) || names.value.has(short)) return void 0;
173
+ }
174
+ return void 0;
175
+ }
176
+ /** Preserves the prefix when embedding value suggestions in one token. @internal */
177
+ function prefixSuggestion(head, suggestion) {
178
+ return {
179
+ kind: "literal",
180
+ text: head + (suggestion.kind === "literal" ? suggestion.text : suggestion.pattern ?? ""),
181
+ ...suggestion.description != null ? { description: suggestion.description } : {}
182
+ };
183
+ }
184
+
185
+ //#endregion
186
+ export { adoptOptionScope, attachedValuePrefix, combinedOptionScope, conditionalOptionScope, forkOptionScope, fullNameOwnsToken, prefixSuggestion, restoreOptionScope, scopeParser, selectableOptionScope, withOptionScope, withParserOptionScope };
@@ -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;
@@ -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.2606",
3
+ "version": "1.4.0-dev.2607",
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.2606+b74e9130",
240
+ "@optique/env": "1.4.0-dev.2607+84d98355",
241
241
  "@types/node": "^24.0.0",
242
242
  "fast-check": "^4.7.0",
243
243
  "tsdown": "^0.13.0",
@@ -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 the parsed value type from the parser. Do not
34
- hand-maintain a separate interface for the result unless another API
35
- boundary requires it.
36
- - Most parsers are required until you wrap them. `optional(p)` yields
37
- `undefined`; `withDefault(p, value)` yields a fallback value. For Boolean
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.