@optique/core 1.0.0-dev.1785 → 1.0.0-dev.1787

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/facade.cjs CHANGED
@@ -5,13 +5,13 @@ const require_validate = require('./validate.cjs');
5
5
  const require_usage = require('./usage.cjs');
6
6
  const require_doc = require('./doc.cjs');
7
7
  const require_mode_dispatch = require('./mode-dispatch.cjs');
8
+ const require_dependency_runtime = require('./dependency-runtime.cjs');
8
9
  const require_input_trace = require('./input-trace.cjs');
9
10
  const require_phase2_seed = require('./phase2-seed.cjs');
10
11
  const require_modifiers = require('./modifiers.cjs');
11
12
  const require_valueparser = require('./valueparser.cjs');
12
13
  const require_primitives = require('./primitives.cjs');
13
14
  const require_parser = require('./parser.cjs');
14
- const require_dependency_runtime = require('./dependency-runtime.cjs');
15
15
  const require_constructs = require('./constructs.cjs');
16
16
 
17
17
  //#region src/facade.ts
package/dist/facade.js CHANGED
@@ -5,13 +5,13 @@ import { validateCommandNames, validateContextIds, validateMetaNameCollisions, v
5
5
  import { extractCommandNames, extractLiteralValues, extractOptionNames, formatUsage } from "./usage.js";
6
6
  import { formatDocPage } from "./doc.js";
7
7
  import { dispatchByMode } from "./mode-dispatch.js";
8
+ import { createDependencyRuntimeContext } from "./dependency-runtime.js";
8
9
  import { createInputTrace } from "./input-trace.js";
9
10
  import { completeOrExtractPhase2Seed } from "./phase2-seed.js";
10
11
  import { multiple, optional, withDefault } from "./modifiers.js";
11
12
  import { string } from "./valueparser.js";
12
13
  import { argument, command, constant, flag, option } from "./primitives.js";
13
14
  import { createParserContext, getDocPage, parseAsync, parseSync, suggest, suggestAsync } from "./parser.js";
14
- import { createDependencyRuntimeContext } from "./dependency-runtime.js";
15
15
  import { group, longestMatch, object } from "./constructs.js";
16
16
 
17
17
  //#region src/facade.ts
package/dist/index.cjs CHANGED
@@ -1,9 +1,9 @@
1
1
  const require_annotations = require('./annotations.cjs');
2
2
  const require_message = require('./message.cjs');
3
3
  const require_completion = require('./completion.cjs');
4
- const require_dependency = require('./dependency.cjs');
5
4
  const require_usage = require('./usage.cjs');
6
5
  const require_doc = require('./doc.cjs');
6
+ const require_dependency = require('./dependency.cjs');
7
7
  const require_modifiers = require('./modifiers.cjs');
8
8
  const require_nonempty = require('./nonempty.cjs');
9
9
  const require_valueparser = require('./valueparser.cjs');
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import { annotationKey, getAnnotations } from "./annotations.js";
2
2
  import { commandLine, envVar, formatMessage, lineBreak, link, message, metavar, optionName, optionNames, text, value, valueSet, values } from "./message.js";
3
3
  import { bash, fish, nu, pwsh, zsh } from "./completion.js";
4
- import { DependencyRegistry, createDeferredParseState, createDependencySourceState, createPendingDependencySourceState, defaultDependencyValueSnapshot, defaultValues, deferredParseMarker, dependency, dependencyId, dependencyIds, dependencySourceMarker, dependencySourceStateMarker, deriveFrom, deriveFromAsync, deriveFromSync, derivedValueParserMarker, formatDependencyError, getDefaultValuesFunction, getDependencyIds, getSnapshottedDefaultDependencyValues, isDeferredParseState, isDependencySource, isDependencySourceState, isDerivedValueParser, isPendingDependencySourceState, isWrappedDependencySource, parseWithDependency, pendingDependencySourceStateMarker, singleDefaultValue, snapshotDefaultDependencyValues, suggestWithDependency, transformsDependencyValue, transformsDependencyValueMarker, wrappedDependencySourceMarker } from "./dependency.js";
5
4
  import { cloneUsage, cloneUsageTerm, extractArgumentMetavars, extractCommandNames, extractLiteralValues, extractOptionNames, formatUsage, formatUsageTerm, isDocHidden, isSuggestionHidden, isUsageHidden, mergeHidden, normalizeUsage } from "./usage.js";
6
5
  import { cloneDocEntry, deduplicateDocEntries, deduplicateDocFragments, formatDocPage, isDocEntryHidden } from "./doc.js";
6
+ import { DependencyRegistry, createDeferredParseState, createDependencySourceState, createPendingDependencySourceState, defaultDependencyValueSnapshot, defaultValues, deferredParseMarker, dependency, dependencyId, dependencyIds, dependencySourceMarker, dependencySourceStateMarker, deriveFrom, deriveFromAsync, deriveFromSync, derivedValueParserMarker, formatDependencyError, getDefaultValuesFunction, getDependencyIds, getSnapshottedDefaultDependencyValues, isDeferredParseState, isDependencySource, isDependencySourceState, isDerivedValueParser, isPendingDependencySourceState, isWrappedDependencySource, parseWithDependency, pendingDependencySourceStateMarker, singleDefaultValue, snapshotDefaultDependencyValues, suggestWithDependency, transformsDependencyValue, transformsDependencyValueMarker, wrappedDependencySourceMarker } from "./dependency.js";
7
7
  import { WithDefaultError, map, multiple, nonEmpty, optional, withDefault } from "./modifiers.js";
8
8
  import { ensureNonEmptyString, isNonEmptyString } from "./nonempty.js";
9
9
  import { checkBooleanOption, checkEnumOption, choice, cidr, domain, email, float, hostname, integer, ip, ipv4, ipv6, isValueParser, locale, macAddress, port, portRange, socketAddress, string, url, uuid } from "./valueparser.js";
package/dist/parser.cjs CHANGED
@@ -3,10 +3,10 @@ const require_message = require('./message.cjs');
3
3
  const require_usage = require('./usage.cjs');
4
4
  const require_doc = require('./doc.cjs');
5
5
  const require_mode_dispatch = require('./mode-dispatch.cjs');
6
+ const require_dependency_runtime = require('./dependency-runtime.cjs');
6
7
  const require_input_trace = require('./input-trace.cjs');
7
8
  const require_modifiers = require('./modifiers.cjs');
8
9
  const require_primitives = require('./primitives.cjs');
9
- const require_dependency_runtime = require('./dependency-runtime.cjs');
10
10
  const require_constructs = require('./constructs.cjs');
11
11
 
12
12
  //#region src/parser.ts
package/dist/parser.js CHANGED
@@ -3,10 +3,10 @@ import { cloneMessage, message } from "./message.js";
3
3
  import { cloneUsage, normalizeUsage } from "./usage.js";
4
4
  import { cloneDocEntry, isDocEntryHidden } from "./doc.js";
5
5
  import { dispatchByMode } from "./mode-dispatch.js";
6
+ import { collectExplicitSourceValues, collectExplicitSourceValuesAsync, createDependencyRuntimeContext } from "./dependency-runtime.js";
6
7
  import { createInputTrace } from "./input-trace.js";
7
8
  import { WithDefaultError, map, multiple, nonEmpty, optional, withDefault } from "./modifiers.js";
8
9
  import { argument, command, constant, fail, flag, option, passThrough } from "./primitives.js";
9
- import { collectExplicitSourceValues, collectExplicitSourceValuesAsync, createDependencyRuntimeContext } from "./dependency-runtime.js";
10
10
  import { DuplicateOptionError, concat, conditional, group, longestMatch, merge, object, or, tuple } from "./constructs.js";
11
11
 
12
12
  //#region src/parser.ts
@@ -1,15 +1,16 @@
1
1
  const require_annotations = require('./annotations.cjs');
2
2
  const require_message = require('./message.cjs');
3
- const require_dependency = require('./dependency.cjs');
4
3
  const require_validate = require('./validate.cjs');
5
4
  const require_usage = require('./usage.cjs');
6
5
  const require_mode_dispatch = require('./mode-dispatch.cjs');
6
+ const require_dependency = require('./dependency.cjs');
7
+ const require_dependency_runtime = require('./dependency-runtime.cjs');
7
8
  const require_dependency_metadata = require('./dependency-metadata.cjs');
8
9
  const require_phase2_seed = require('./phase2-seed.cjs');
9
10
  const require_suggestion = require('./suggestion.cjs');
10
11
  const require_usage_internals = require('./usage-internals.cjs');
11
12
  const require_valueparser = require('./valueparser.cjs');
12
- const require_dependency_runtime = require('./dependency-runtime.cjs');
13
+ const require_annotation_state = require('./annotation-state.cjs');
13
14
 
14
15
  //#region src/primitives.ts
15
16
  /**
@@ -307,7 +308,7 @@ function* suggestOptionSync(optionNames$1, valueParser, hidden, context, prefix)
307
308
  if (context.buffer.length > 0) {
308
309
  const lastToken = context.buffer[context.buffer.length - 1];
309
310
  if (optionNames$1.includes(lastToken)) shouldSuggestValues = true;
310
- } else if (context.state === void 0 && context.buffer.length === 0 && (context.exec?.path?.length ?? 0) === 0 && !(prefix.startsWith("--") || prefix.startsWith("-") || prefix.startsWith("/"))) shouldSuggestValues = true;
311
+ } else if (require_annotation_state.isAnnotationWrappedInitialState(context.state) && context.buffer.length === 0 && (context.exec?.path?.length ?? 0) === 0 && !(prefix.startsWith("--") || prefix.startsWith("-") || prefix.startsWith("/"))) shouldSuggestValues = true;
311
312
  if (shouldSuggestValues) yield* getSuggestionsWithDependency(valueParser, prefix, context.dependencyRegistry, context.exec);
312
313
  }
313
314
  }
@@ -391,7 +392,7 @@ async function* suggestOptionAsync(optionNames$1, valueParser, hidden, context,
391
392
  if (context.buffer.length > 0) {
392
393
  const lastToken = context.buffer[context.buffer.length - 1];
393
394
  if (optionNames$1.includes(lastToken)) shouldSuggestValues = true;
394
- } else if (context.state === void 0 && context.buffer.length === 0 && (context.exec?.path?.length ?? 0) === 0 && !(prefix.startsWith("--") || prefix.startsWith("-") || prefix.startsWith("/"))) shouldSuggestValues = true;
395
+ } else if (require_annotation_state.isAnnotationWrappedInitialState(context.state) && context.buffer.length === 0 && (context.exec?.path?.length ?? 0) === 0 && !(prefix.startsWith("--") || prefix.startsWith("-") || prefix.startsWith("/"))) shouldSuggestValues = true;
395
396
  if (shouldSuggestValues) for await (const suggestion of getSuggestionsWithDependencyAsync(valueParser, prefix, context.dependencyRegistry, context.exec)) yield suggestion;
396
397
  }
397
398
  }
@@ -996,6 +997,7 @@ function argument(valueParser, options = {}) {
996
997
  acceptingAnyToken: true,
997
998
  initialState: void 0,
998
999
  parse(context) {
1000
+ const localState = require_annotation_state.normalizeInjectedAnnotationState(context.state);
999
1001
  if (context.buffer.length < 1) return {
1000
1002
  success: false,
1001
1003
  consumed: 0,
@@ -1018,7 +1020,7 @@ function argument(valueParser, options = {}) {
1018
1020
  consumed: i,
1019
1021
  error: require_message.message`Expected an argument, but got end of input.`
1020
1022
  };
1021
- if (context.state != null) return {
1023
+ if (localState != null) return {
1022
1024
  success: false,
1023
1025
  consumed: i,
1024
1026
  error: options.errors?.multiple ? typeof options.errors.multiple === "function" ? options.errors.multiple(valueParser.metavar) : options.errors.multiple : require_message.message`The argument ${require_message.metavar(valueParser.metavar)} cannot be used multiple times.`
@@ -1077,7 +1079,7 @@ function argument(valueParser, options = {}) {
1077
1079
  return require_mode_dispatch.dispatchByMode(valueParser.$mode, completeSync, completeAsync);
1078
1080
  },
1079
1081
  suggest(context, prefix) {
1080
- if (context.state != null) return require_mode_dispatch.dispatchIterableByMode(valueParser.$mode, function* () {}, async function* () {});
1082
+ if (require_annotation_state.normalizeInjectedAnnotationState(context.state) != null) return require_mode_dispatch.dispatchIterableByMode(valueParser.$mode, function* () {}, async function* () {});
1081
1083
  if (isAsync) return suggestArgumentAsync(valueParser, require_usage.isSuggestionHidden(options.hidden), prefix, context.dependencyRegistry, context.exec);
1082
1084
  return suggestArgumentSync(valueParser, require_usage.isSuggestionHidden(options.hidden), prefix, context.dependencyRegistry, context.exec);
1083
1085
  },
@@ -1166,30 +1168,44 @@ function argument(valueParser, options = {}) {
1166
1168
  });
1167
1169
  return result;
1168
1170
  }
1171
+ function normalizeCommandState(state) {
1172
+ return require_annotation_state.normalizeInjectedAnnotationState(state);
1173
+ }
1174
+ function getCommandParseChildState(commandState, childState, parser) {
1175
+ return require_annotation_state.getWrappedChildParseState(commandState, childState, parser);
1176
+ }
1177
+ function getCommandChildState(commandState, childState, parser) {
1178
+ return require_annotation_state.getWrappedChildState(commandState, childState, parser);
1179
+ }
1180
+ function createCommandState(sourceState, state) {
1181
+ return require_annotations.annotateFreshArray(sourceState, state);
1182
+ }
1169
1183
  function* suggestCommandSync(context, prefix, name, parser, options) {
1170
1184
  if (require_usage.isSuggestionHidden(options.hidden)) return;
1171
- if (context.state === void 0) {
1185
+ const state = normalizeCommandState(context.state);
1186
+ if (state === void 0) {
1172
1187
  if (name.startsWith(prefix)) yield {
1173
1188
  kind: "literal",
1174
1189
  text: name,
1175
1190
  ...options.description && { description: options.description }
1176
1191
  };
1177
- } else if (context.state[0] === "matched") yield* parser.suggest(withChildContext(context, name, parser.initialState, parser.usage), prefix);
1178
- else if (context.state[0] === "parsing") yield* parser.suggest(withChildContext(context, name, context.state[1], parser.usage), prefix);
1192
+ } else if (state[0] === "matched") yield* parser.suggest(withChildContext(context, name, getCommandChildState(context.state, parser.initialState, parser), parser.usage), prefix);
1193
+ else if (state[0] === "parsing") yield* parser.suggest(withChildContext(context, name, getCommandChildState(context.state, state[1], parser), parser.usage), prefix);
1179
1194
  }
1180
1195
  async function* suggestCommandAsync(context, prefix, name, parser, options) {
1181
1196
  if (require_usage.isSuggestionHidden(options.hidden)) return;
1182
- if (context.state === void 0) {
1197
+ const state = normalizeCommandState(context.state);
1198
+ if (state === void 0) {
1183
1199
  if (name.startsWith(prefix)) yield {
1184
1200
  kind: "literal",
1185
1201
  text: name,
1186
1202
  ...options.description && { description: options.description }
1187
1203
  };
1188
- } else if (context.state[0] === "matched") {
1189
- const suggestions = parser.suggest(withChildContext(context, name, parser.initialState, parser.usage), prefix);
1204
+ } else if (state[0] === "matched") {
1205
+ const suggestions = parser.suggest(withChildContext(context, name, getCommandChildState(context.state, parser.initialState, parser), parser.usage), prefix);
1190
1206
  for await (const s of suggestions) yield s;
1191
- } else if (context.state[0] === "parsing") {
1192
- const suggestions = parser.suggest(withChildContext(context, name, context.state[1], parser.usage), prefix);
1207
+ } else if (state[0] === "parsing") {
1208
+ const suggestions = parser.suggest(withChildContext(context, name, getCommandChildState(context.state, state[1], parser), parser.usage), prefix);
1193
1209
  for await (const s of suggestions) yield s;
1194
1210
  }
1195
1211
  }
@@ -1230,8 +1246,9 @@ function command(name, parser, options = {}) {
1230
1246
  acceptingAnyToken: false,
1231
1247
  initialState: void 0,
1232
1248
  getSuggestRuntimeNodes(state, path) {
1233
- if (state === void 0) return [];
1234
- const childState = state[0] === "matched" ? parser.initialState : state[1];
1249
+ const normalizedState = normalizeCommandState(state);
1250
+ if (normalizedState === void 0) return [];
1251
+ const childState = normalizedState[0] === "matched" ? getCommandChildState(state, parser.initialState, parser) : getCommandChildState(state, normalizedState[1], parser);
1235
1252
  const childPath = [...path, name];
1236
1253
  return parser.getSuggestRuntimeNodes?.(childState, childPath) ?? (parser.dependencyMetadata?.source != null ? [{
1237
1254
  path: childPath,
@@ -1240,7 +1257,8 @@ function command(name, parser, options = {}) {
1240
1257
  }] : []);
1241
1258
  },
1242
1259
  parse(context) {
1243
- if (context.state === void 0) {
1260
+ const state = normalizeCommandState(context.state);
1261
+ if (state === void 0) {
1244
1262
  if (context.buffer.length < 1 || context.buffer[0] !== name) {
1245
1263
  const actual = context.buffer.length > 0 ? context.buffer[0] : null;
1246
1264
  const leadingCmds = require_usage_internals.extractLeadingCommandNames(context.usage);
@@ -1275,12 +1293,12 @@ function command(name, parser, options = {}) {
1275
1293
  next: {
1276
1294
  ...context,
1277
1295
  buffer: context.buffer.slice(1),
1278
- state: ["matched", name]
1296
+ state: createCommandState(context.state, ["matched", name])
1279
1297
  },
1280
1298
  consumed: context.buffer.slice(0, 1)
1281
1299
  };
1282
- } else if (context.state[0] === "matched" || context.state[0] === "parsing") {
1283
- const innerState = context.state[0] === "matched" ? parser.initialState : context.state[1];
1300
+ } else if (state[0] === "matched" || state[0] === "parsing") {
1301
+ const innerState = state[0] === "matched" ? getCommandParseChildState(context.state, parser.initialState, parser) : getCommandParseChildState(context.state, state[1], parser);
1284
1302
  const wrapState = (parseResult) => {
1285
1303
  if (parseResult.success) {
1286
1304
  const mergedExec = mergeChildExec(context.exec, parseResult.next.exec);
@@ -1288,7 +1306,7 @@ function command(name, parser, options = {}) {
1288
1306
  success: true,
1289
1307
  next: {
1290
1308
  ...parseResult.next,
1291
- state: ["parsing", parseResult.next.state],
1309
+ state: createCommandState(context.state, ["parsing", parseResult.next.state]),
1292
1310
  ...mergedExec != null ? {
1293
1311
  exec: mergedExec,
1294
1312
  dependencyRegistry: mergedExec.dependencyRegistry
@@ -1308,17 +1326,18 @@ function command(name, parser, options = {}) {
1308
1326
  };
1309
1327
  },
1310
1328
  complete(state, exec) {
1311
- if (typeof state === "undefined") return {
1329
+ const normalizedState = normalizeCommandState(state);
1330
+ if (typeof normalizedState === "undefined") return {
1312
1331
  success: false,
1313
1332
  error: options.errors?.notFound ?? require_message.message`Command ${require_message.optionName(name)} was not matched.`
1314
1333
  };
1315
- else if (state[0] === "matched") {
1334
+ else if (normalizedState[0] === "matched") {
1316
1335
  const childExec = withChildExecPath(exec, name);
1317
1336
  const childContext = {
1318
1337
  buffer: [],
1319
1338
  optionsTerminated: false,
1320
1339
  usage: parser.usage,
1321
- state: parser.initialState,
1340
+ state: getCommandParseChildState(state, parser.initialState, parser),
1322
1341
  ...childExec != null ? {
1323
1342
  exec: childExec,
1324
1343
  trace: childExec.trace,
@@ -1328,15 +1347,15 @@ function command(name, parser, options = {}) {
1328
1347
  return require_mode_dispatch.dispatchByMode(parser.$mode, () => {
1329
1348
  const parseResult = syncInnerParser.parse(childContext);
1330
1349
  const nextExec = parseResult.success ? mergeChildExec(childExec, parseResult.next.exec) : childExec;
1331
- return syncInnerParser.complete(parseResult.success ? parseResult.next.state : syncInnerParser.initialState, nextExec);
1350
+ return syncInnerParser.complete(parseResult.success ? getCommandChildState(state, parseResult.next.state, parser) : getCommandChildState(state, syncInnerParser.initialState, parser), nextExec);
1332
1351
  }, async () => {
1333
1352
  const parseResult = await asyncInnerParser.parse(childContext);
1334
1353
  const nextExec = parseResult.success ? mergeChildExec(childExec, parseResult.next.exec) : childExec;
1335
- return asyncInnerParser.complete(parseResult.success ? parseResult.next.state : parser.initialState, nextExec);
1354
+ return asyncInnerParser.complete(parseResult.success ? getCommandChildState(state, parseResult.next.state, parser) : getCommandChildState(state, parser.initialState, parser), nextExec);
1336
1355
  });
1337
- } else if (state[0] === "parsing") {
1356
+ } else if (normalizedState[0] === "parsing") {
1338
1357
  const childExec = withChildExecPath(exec, name);
1339
- return require_mode_dispatch.dispatchByMode(parser.$mode, () => syncInnerParser.complete(state[1], childExec), async () => await asyncInnerParser.complete(state[1], childExec));
1358
+ return require_mode_dispatch.dispatchByMode(parser.$mode, () => syncInnerParser.complete(getCommandChildState(state, normalizedState[1], parser), childExec), async () => await asyncInnerParser.complete(getCommandChildState(state, normalizedState[1], parser), childExec));
1340
1359
  }
1341
1360
  return {
1342
1361
  success: false,
@@ -1344,14 +1363,15 @@ function command(name, parser, options = {}) {
1344
1363
  };
1345
1364
  },
1346
1365
  [require_phase2_seed.extractPhase2SeedKey](state, exec) {
1347
- if (typeof state === "undefined") return require_mode_dispatch.wrapForMode(parser.$mode, null);
1348
- if (state[0] === "matched") {
1366
+ const normalizedState = normalizeCommandState(state);
1367
+ if (typeof normalizedState === "undefined") return require_mode_dispatch.wrapForMode(parser.$mode, null);
1368
+ if (normalizedState[0] === "matched") {
1349
1369
  const childExec = withChildExecPath(exec, name);
1350
1370
  const childContext = {
1351
1371
  buffer: [],
1352
1372
  optionsTerminated: false,
1353
1373
  usage: parser.usage,
1354
- state: parser.initialState,
1374
+ state: getCommandParseChildState(state, parser.initialState, parser),
1355
1375
  ...childExec != null ? {
1356
1376
  exec: childExec,
1357
1377
  trace: childExec.trace,
@@ -1361,14 +1381,14 @@ function command(name, parser, options = {}) {
1361
1381
  return require_mode_dispatch.dispatchByMode(parser.$mode, () => {
1362
1382
  const parseResult = syncInnerParser.parse(childContext);
1363
1383
  const nextExec = parseResult.success ? mergeChildExec(childExec, parseResult.next.exec) : childExec;
1364
- return require_phase2_seed.completeOrExtractPhase2Seed(syncInnerParser, parseResult.success ? parseResult.next.state : syncInnerParser.initialState, nextExec);
1384
+ return require_phase2_seed.completeOrExtractPhase2Seed(syncInnerParser, parseResult.success ? getCommandChildState(state, parseResult.next.state, parser) : getCommandChildState(state, syncInnerParser.initialState, parser), nextExec);
1365
1385
  }, async () => {
1366
1386
  const parseResult = await asyncInnerParser.parse(childContext);
1367
1387
  const nextExec = parseResult.success ? mergeChildExec(childExec, parseResult.next.exec) : childExec;
1368
- return await require_phase2_seed.completeOrExtractPhase2Seed(asyncInnerParser, parseResult.success ? parseResult.next.state : parser.initialState, nextExec);
1388
+ return await require_phase2_seed.completeOrExtractPhase2Seed(asyncInnerParser, parseResult.success ? getCommandChildState(state, parseResult.next.state, parser) : getCommandChildState(state, parser.initialState, parser), nextExec);
1369
1389
  });
1370
1390
  }
1371
- if (state[0] === "parsing") return require_phase2_seed.completeOrExtractPhase2Seed(parser, state[1], withChildExecPath(exec, name));
1391
+ if (normalizedState[0] === "parsing") return require_phase2_seed.completeOrExtractPhase2Seed(parser, getCommandChildState(state, normalizedState[1], parser), withChildExecPath(exec, name));
1372
1392
  return require_mode_dispatch.wrapForMode(parser.$mode, null);
1373
1393
  },
1374
1394
  suggest(context, prefix) {
@@ -1376,7 +1396,8 @@ function command(name, parser, options = {}) {
1376
1396
  return suggestCommandSync(context, prefix, name, parser, options);
1377
1397
  },
1378
1398
  getDocFragments(state, defaultValue) {
1379
- if (state.kind === "unavailable" || typeof state.state === "undefined") {
1399
+ const commandState = state.kind === "available" ? normalizeCommandState(state.state) : void 0;
1400
+ if (state.kind === "unavailable" || typeof commandState === "undefined") {
1380
1401
  if (require_usage.isDocHidden(options.hidden)) return {
1381
1402
  fragments: [],
1382
1403
  description: options.description
@@ -1393,12 +1414,12 @@ function command(name, parser, options = {}) {
1393
1414
  }]
1394
1415
  };
1395
1416
  }
1396
- const innerState = state.state[0] === "parsing" ? {
1417
+ const innerState = commandState[0] === "parsing" ? {
1397
1418
  kind: "available",
1398
- state: state.state[1]
1419
+ state: getCommandChildState(state.state, commandState[1], parser)
1399
1420
  } : {
1400
1421
  kind: "available",
1401
- state: parser.initialState
1422
+ state: getCommandChildState(state.state, parser.initialState, parser)
1402
1423
  };
1403
1424
  const innerFragments = parser.getDocFragments(innerState, defaultValue);
1404
1425
  return {
@@ -1,15 +1,16 @@
1
1
  import { annotateFreshArray, getAnnotations } from "./annotations.js";
2
2
  import { message, metavar, optionName, optionNames, text, valueSet } from "./message.js";
3
- import { getDefaultValuesFunction, getDependencyIds, getSnapshottedDefaultDependencyValues, isDerivedValueParser, suggestWithDependency } from "./dependency.js";
4
3
  import { validateCommandNames, validateOptionNames } from "./validate.js";
5
4
  import { extractOptionNames, isDocHidden, isSuggestionHidden } from "./usage.js";
6
5
  import { dispatchByMode, dispatchIterableByMode, wrapForMode } from "./mode-dispatch.js";
6
+ import { getDefaultValuesFunction, getDependencyIds, getSnapshottedDefaultDependencyValues, isDerivedValueParser, suggestWithDependency } from "./dependency.js";
7
+ import { replayDerivedParser, replayDerivedParserAsync } from "./dependency-runtime.js";
7
8
  import { extractDependencyMetadata } from "./dependency-metadata.js";
8
9
  import { completeOrExtractPhase2Seed, extractPhase2SeedKey } from "./phase2-seed.js";
9
10
  import { DEFAULT_FIND_SIMILAR_OPTIONS, createErrorWithSuggestions, createSuggestionMessage, findSimilar } from "./suggestion.js";
10
11
  import { extractLeadingCommandNames } from "./usage-internals.js";
11
12
  import { isValueParser } from "./valueparser.js";
12
- import { replayDerivedParser, replayDerivedParserAsync } from "./dependency-runtime.js";
13
+ import { getWrappedChildParseState, getWrappedChildState, isAnnotationWrappedInitialState, normalizeInjectedAnnotationState } from "./annotation-state.js";
13
14
 
14
15
  //#region src/primitives.ts
15
16
  /**
@@ -307,7 +308,7 @@ function* suggestOptionSync(optionNames$1, valueParser, hidden, context, prefix)
307
308
  if (context.buffer.length > 0) {
308
309
  const lastToken = context.buffer[context.buffer.length - 1];
309
310
  if (optionNames$1.includes(lastToken)) shouldSuggestValues = true;
310
- } else if (context.state === void 0 && context.buffer.length === 0 && (context.exec?.path?.length ?? 0) === 0 && !(prefix.startsWith("--") || prefix.startsWith("-") || prefix.startsWith("/"))) shouldSuggestValues = true;
311
+ } else if (isAnnotationWrappedInitialState(context.state) && context.buffer.length === 0 && (context.exec?.path?.length ?? 0) === 0 && !(prefix.startsWith("--") || prefix.startsWith("-") || prefix.startsWith("/"))) shouldSuggestValues = true;
311
312
  if (shouldSuggestValues) yield* getSuggestionsWithDependency(valueParser, prefix, context.dependencyRegistry, context.exec);
312
313
  }
313
314
  }
@@ -391,7 +392,7 @@ async function* suggestOptionAsync(optionNames$1, valueParser, hidden, context,
391
392
  if (context.buffer.length > 0) {
392
393
  const lastToken = context.buffer[context.buffer.length - 1];
393
394
  if (optionNames$1.includes(lastToken)) shouldSuggestValues = true;
394
- } else if (context.state === void 0 && context.buffer.length === 0 && (context.exec?.path?.length ?? 0) === 0 && !(prefix.startsWith("--") || prefix.startsWith("-") || prefix.startsWith("/"))) shouldSuggestValues = true;
395
+ } else if (isAnnotationWrappedInitialState(context.state) && context.buffer.length === 0 && (context.exec?.path?.length ?? 0) === 0 && !(prefix.startsWith("--") || prefix.startsWith("-") || prefix.startsWith("/"))) shouldSuggestValues = true;
395
396
  if (shouldSuggestValues) for await (const suggestion of getSuggestionsWithDependencyAsync(valueParser, prefix, context.dependencyRegistry, context.exec)) yield suggestion;
396
397
  }
397
398
  }
@@ -996,6 +997,7 @@ function argument(valueParser, options = {}) {
996
997
  acceptingAnyToken: true,
997
998
  initialState: void 0,
998
999
  parse(context) {
1000
+ const localState = normalizeInjectedAnnotationState(context.state);
999
1001
  if (context.buffer.length < 1) return {
1000
1002
  success: false,
1001
1003
  consumed: 0,
@@ -1018,7 +1020,7 @@ function argument(valueParser, options = {}) {
1018
1020
  consumed: i,
1019
1021
  error: message`Expected an argument, but got end of input.`
1020
1022
  };
1021
- if (context.state != null) return {
1023
+ if (localState != null) return {
1022
1024
  success: false,
1023
1025
  consumed: i,
1024
1026
  error: options.errors?.multiple ? typeof options.errors.multiple === "function" ? options.errors.multiple(valueParser.metavar) : options.errors.multiple : message`The argument ${metavar(valueParser.metavar)} cannot be used multiple times.`
@@ -1077,7 +1079,7 @@ function argument(valueParser, options = {}) {
1077
1079
  return dispatchByMode(valueParser.$mode, completeSync, completeAsync);
1078
1080
  },
1079
1081
  suggest(context, prefix) {
1080
- if (context.state != null) return dispatchIterableByMode(valueParser.$mode, function* () {}, async function* () {});
1082
+ if (normalizeInjectedAnnotationState(context.state) != null) return dispatchIterableByMode(valueParser.$mode, function* () {}, async function* () {});
1081
1083
  if (isAsync) return suggestArgumentAsync(valueParser, isSuggestionHidden(options.hidden), prefix, context.dependencyRegistry, context.exec);
1082
1084
  return suggestArgumentSync(valueParser, isSuggestionHidden(options.hidden), prefix, context.dependencyRegistry, context.exec);
1083
1085
  },
@@ -1166,30 +1168,44 @@ function argument(valueParser, options = {}) {
1166
1168
  });
1167
1169
  return result;
1168
1170
  }
1171
+ function normalizeCommandState(state) {
1172
+ return normalizeInjectedAnnotationState(state);
1173
+ }
1174
+ function getCommandParseChildState(commandState, childState, parser) {
1175
+ return getWrappedChildParseState(commandState, childState, parser);
1176
+ }
1177
+ function getCommandChildState(commandState, childState, parser) {
1178
+ return getWrappedChildState(commandState, childState, parser);
1179
+ }
1180
+ function createCommandState(sourceState, state) {
1181
+ return annotateFreshArray(sourceState, state);
1182
+ }
1169
1183
  function* suggestCommandSync(context, prefix, name, parser, options) {
1170
1184
  if (isSuggestionHidden(options.hidden)) return;
1171
- if (context.state === void 0) {
1185
+ const state = normalizeCommandState(context.state);
1186
+ if (state === void 0) {
1172
1187
  if (name.startsWith(prefix)) yield {
1173
1188
  kind: "literal",
1174
1189
  text: name,
1175
1190
  ...options.description && { description: options.description }
1176
1191
  };
1177
- } else if (context.state[0] === "matched") yield* parser.suggest(withChildContext(context, name, parser.initialState, parser.usage), prefix);
1178
- else if (context.state[0] === "parsing") yield* parser.suggest(withChildContext(context, name, context.state[1], parser.usage), prefix);
1192
+ } else if (state[0] === "matched") yield* parser.suggest(withChildContext(context, name, getCommandChildState(context.state, parser.initialState, parser), parser.usage), prefix);
1193
+ else if (state[0] === "parsing") yield* parser.suggest(withChildContext(context, name, getCommandChildState(context.state, state[1], parser), parser.usage), prefix);
1179
1194
  }
1180
1195
  async function* suggestCommandAsync(context, prefix, name, parser, options) {
1181
1196
  if (isSuggestionHidden(options.hidden)) return;
1182
- if (context.state === void 0) {
1197
+ const state = normalizeCommandState(context.state);
1198
+ if (state === void 0) {
1183
1199
  if (name.startsWith(prefix)) yield {
1184
1200
  kind: "literal",
1185
1201
  text: name,
1186
1202
  ...options.description && { description: options.description }
1187
1203
  };
1188
- } else if (context.state[0] === "matched") {
1189
- const suggestions = parser.suggest(withChildContext(context, name, parser.initialState, parser.usage), prefix);
1204
+ } else if (state[0] === "matched") {
1205
+ const suggestions = parser.suggest(withChildContext(context, name, getCommandChildState(context.state, parser.initialState, parser), parser.usage), prefix);
1190
1206
  for await (const s of suggestions) yield s;
1191
- } else if (context.state[0] === "parsing") {
1192
- const suggestions = parser.suggest(withChildContext(context, name, context.state[1], parser.usage), prefix);
1207
+ } else if (state[0] === "parsing") {
1208
+ const suggestions = parser.suggest(withChildContext(context, name, getCommandChildState(context.state, state[1], parser), parser.usage), prefix);
1193
1209
  for await (const s of suggestions) yield s;
1194
1210
  }
1195
1211
  }
@@ -1230,8 +1246,9 @@ function command(name, parser, options = {}) {
1230
1246
  acceptingAnyToken: false,
1231
1247
  initialState: void 0,
1232
1248
  getSuggestRuntimeNodes(state, path) {
1233
- if (state === void 0) return [];
1234
- const childState = state[0] === "matched" ? parser.initialState : state[1];
1249
+ const normalizedState = normalizeCommandState(state);
1250
+ if (normalizedState === void 0) return [];
1251
+ const childState = normalizedState[0] === "matched" ? getCommandChildState(state, parser.initialState, parser) : getCommandChildState(state, normalizedState[1], parser);
1235
1252
  const childPath = [...path, name];
1236
1253
  return parser.getSuggestRuntimeNodes?.(childState, childPath) ?? (parser.dependencyMetadata?.source != null ? [{
1237
1254
  path: childPath,
@@ -1240,7 +1257,8 @@ function command(name, parser, options = {}) {
1240
1257
  }] : []);
1241
1258
  },
1242
1259
  parse(context) {
1243
- if (context.state === void 0) {
1260
+ const state = normalizeCommandState(context.state);
1261
+ if (state === void 0) {
1244
1262
  if (context.buffer.length < 1 || context.buffer[0] !== name) {
1245
1263
  const actual = context.buffer.length > 0 ? context.buffer[0] : null;
1246
1264
  const leadingCmds = extractLeadingCommandNames(context.usage);
@@ -1275,12 +1293,12 @@ function command(name, parser, options = {}) {
1275
1293
  next: {
1276
1294
  ...context,
1277
1295
  buffer: context.buffer.slice(1),
1278
- state: ["matched", name]
1296
+ state: createCommandState(context.state, ["matched", name])
1279
1297
  },
1280
1298
  consumed: context.buffer.slice(0, 1)
1281
1299
  };
1282
- } else if (context.state[0] === "matched" || context.state[0] === "parsing") {
1283
- const innerState = context.state[0] === "matched" ? parser.initialState : context.state[1];
1300
+ } else if (state[0] === "matched" || state[0] === "parsing") {
1301
+ const innerState = state[0] === "matched" ? getCommandParseChildState(context.state, parser.initialState, parser) : getCommandParseChildState(context.state, state[1], parser);
1284
1302
  const wrapState = (parseResult) => {
1285
1303
  if (parseResult.success) {
1286
1304
  const mergedExec = mergeChildExec(context.exec, parseResult.next.exec);
@@ -1288,7 +1306,7 @@ function command(name, parser, options = {}) {
1288
1306
  success: true,
1289
1307
  next: {
1290
1308
  ...parseResult.next,
1291
- state: ["parsing", parseResult.next.state],
1309
+ state: createCommandState(context.state, ["parsing", parseResult.next.state]),
1292
1310
  ...mergedExec != null ? {
1293
1311
  exec: mergedExec,
1294
1312
  dependencyRegistry: mergedExec.dependencyRegistry
@@ -1308,17 +1326,18 @@ function command(name, parser, options = {}) {
1308
1326
  };
1309
1327
  },
1310
1328
  complete(state, exec) {
1311
- if (typeof state === "undefined") return {
1329
+ const normalizedState = normalizeCommandState(state);
1330
+ if (typeof normalizedState === "undefined") return {
1312
1331
  success: false,
1313
1332
  error: options.errors?.notFound ?? message`Command ${optionName(name)} was not matched.`
1314
1333
  };
1315
- else if (state[0] === "matched") {
1334
+ else if (normalizedState[0] === "matched") {
1316
1335
  const childExec = withChildExecPath(exec, name);
1317
1336
  const childContext = {
1318
1337
  buffer: [],
1319
1338
  optionsTerminated: false,
1320
1339
  usage: parser.usage,
1321
- state: parser.initialState,
1340
+ state: getCommandParseChildState(state, parser.initialState, parser),
1322
1341
  ...childExec != null ? {
1323
1342
  exec: childExec,
1324
1343
  trace: childExec.trace,
@@ -1328,15 +1347,15 @@ function command(name, parser, options = {}) {
1328
1347
  return dispatchByMode(parser.$mode, () => {
1329
1348
  const parseResult = syncInnerParser.parse(childContext);
1330
1349
  const nextExec = parseResult.success ? mergeChildExec(childExec, parseResult.next.exec) : childExec;
1331
- return syncInnerParser.complete(parseResult.success ? parseResult.next.state : syncInnerParser.initialState, nextExec);
1350
+ return syncInnerParser.complete(parseResult.success ? getCommandChildState(state, parseResult.next.state, parser) : getCommandChildState(state, syncInnerParser.initialState, parser), nextExec);
1332
1351
  }, async () => {
1333
1352
  const parseResult = await asyncInnerParser.parse(childContext);
1334
1353
  const nextExec = parseResult.success ? mergeChildExec(childExec, parseResult.next.exec) : childExec;
1335
- return asyncInnerParser.complete(parseResult.success ? parseResult.next.state : parser.initialState, nextExec);
1354
+ return asyncInnerParser.complete(parseResult.success ? getCommandChildState(state, parseResult.next.state, parser) : getCommandChildState(state, parser.initialState, parser), nextExec);
1336
1355
  });
1337
- } else if (state[0] === "parsing") {
1356
+ } else if (normalizedState[0] === "parsing") {
1338
1357
  const childExec = withChildExecPath(exec, name);
1339
- return dispatchByMode(parser.$mode, () => syncInnerParser.complete(state[1], childExec), async () => await asyncInnerParser.complete(state[1], childExec));
1358
+ return dispatchByMode(parser.$mode, () => syncInnerParser.complete(getCommandChildState(state, normalizedState[1], parser), childExec), async () => await asyncInnerParser.complete(getCommandChildState(state, normalizedState[1], parser), childExec));
1340
1359
  }
1341
1360
  return {
1342
1361
  success: false,
@@ -1344,14 +1363,15 @@ function command(name, parser, options = {}) {
1344
1363
  };
1345
1364
  },
1346
1365
  [extractPhase2SeedKey](state, exec) {
1347
- if (typeof state === "undefined") return wrapForMode(parser.$mode, null);
1348
- if (state[0] === "matched") {
1366
+ const normalizedState = normalizeCommandState(state);
1367
+ if (typeof normalizedState === "undefined") return wrapForMode(parser.$mode, null);
1368
+ if (normalizedState[0] === "matched") {
1349
1369
  const childExec = withChildExecPath(exec, name);
1350
1370
  const childContext = {
1351
1371
  buffer: [],
1352
1372
  optionsTerminated: false,
1353
1373
  usage: parser.usage,
1354
- state: parser.initialState,
1374
+ state: getCommandParseChildState(state, parser.initialState, parser),
1355
1375
  ...childExec != null ? {
1356
1376
  exec: childExec,
1357
1377
  trace: childExec.trace,
@@ -1361,14 +1381,14 @@ function command(name, parser, options = {}) {
1361
1381
  return dispatchByMode(parser.$mode, () => {
1362
1382
  const parseResult = syncInnerParser.parse(childContext);
1363
1383
  const nextExec = parseResult.success ? mergeChildExec(childExec, parseResult.next.exec) : childExec;
1364
- return completeOrExtractPhase2Seed(syncInnerParser, parseResult.success ? parseResult.next.state : syncInnerParser.initialState, nextExec);
1384
+ return completeOrExtractPhase2Seed(syncInnerParser, parseResult.success ? getCommandChildState(state, parseResult.next.state, parser) : getCommandChildState(state, syncInnerParser.initialState, parser), nextExec);
1365
1385
  }, async () => {
1366
1386
  const parseResult = await asyncInnerParser.parse(childContext);
1367
1387
  const nextExec = parseResult.success ? mergeChildExec(childExec, parseResult.next.exec) : childExec;
1368
- return await completeOrExtractPhase2Seed(asyncInnerParser, parseResult.success ? parseResult.next.state : parser.initialState, nextExec);
1388
+ return await completeOrExtractPhase2Seed(asyncInnerParser, parseResult.success ? getCommandChildState(state, parseResult.next.state, parser) : getCommandChildState(state, parser.initialState, parser), nextExec);
1369
1389
  });
1370
1390
  }
1371
- if (state[0] === "parsing") return completeOrExtractPhase2Seed(parser, state[1], withChildExecPath(exec, name));
1391
+ if (normalizedState[0] === "parsing") return completeOrExtractPhase2Seed(parser, getCommandChildState(state, normalizedState[1], parser), withChildExecPath(exec, name));
1372
1392
  return wrapForMode(parser.$mode, null);
1373
1393
  },
1374
1394
  suggest(context, prefix) {
@@ -1376,7 +1396,8 @@ function command(name, parser, options = {}) {
1376
1396
  return suggestCommandSync(context, prefix, name, parser, options);
1377
1397
  },
1378
1398
  getDocFragments(state, defaultValue) {
1379
- if (state.kind === "unavailable" || typeof state.state === "undefined") {
1399
+ const commandState = state.kind === "available" ? normalizeCommandState(state.state) : void 0;
1400
+ if (state.kind === "unavailable" || typeof commandState === "undefined") {
1380
1401
  if (isDocHidden(options.hidden)) return {
1381
1402
  fragments: [],
1382
1403
  description: options.description
@@ -1393,12 +1414,12 @@ function command(name, parser, options = {}) {
1393
1414
  }]
1394
1415
  };
1395
1416
  }
1396
- const innerState = state.state[0] === "parsing" ? {
1417
+ const innerState = commandState[0] === "parsing" ? {
1397
1418
  kind: "available",
1398
- state: state.state[1]
1419
+ state: getCommandChildState(state.state, commandState[1], parser)
1399
1420
  } : {
1400
1421
  kind: "available",
1401
- state: parser.initialState
1422
+ state: getCommandChildState(state.state, parser.initialState, parser)
1402
1423
  };
1403
1424
  const innerFragments = parser.getDocFragments(innerState, defaultValue);
1404
1425
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.0-dev.1785+6a8af6b8",
3
+ "version": "1.0.0-dev.1787+4a55dc3b",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",