@optique/core 1.0.0-dev.523 → 1.0.0-dev.552

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/parser.js CHANGED
@@ -1,4 +1,4 @@
1
- import { annotationKey } from "./annotations.js";
1
+ import { injectAnnotations, isInjectedAnnotationWrapper, unwrapInjectedAnnotationWrapper } from "./annotations.js";
2
2
  import { message } from "./message.js";
3
3
  import { dispatchByMode } from "./mode-dispatch.js";
4
4
  import { normalizeUsage } from "./usage.js";
@@ -7,6 +7,11 @@ import { WithDefaultError, map, multiple, nonEmpty, optional, withDefault } from
7
7
  import { argument, command, constant, fail, flag, option, passThrough } from "./primitives.js";
8
8
 
9
9
  //#region src/parser.ts
10
+ function injectAnnotationsIntoState(state, options) {
11
+ const annotations = options?.annotations;
12
+ if (annotations == null) return state;
13
+ return injectAnnotations(state, annotations);
14
+ }
10
15
  /**
11
16
  * Parses an array of command-line arguments using the provided combined parser.
12
17
  * This function processes the input arguments, applying the parser to each
@@ -30,11 +35,8 @@ import { argument, command, constant, fail, flag, option, passThrough } from "./
30
35
  * @since 0.10.0 Added optional `options` parameter for annotations support.
31
36
  */
32
37
  function parseSync(parser, args, options) {
33
- let initialState = parser.initialState;
34
- if (options?.annotations) initialState = {
35
- ...typeof initialState === "object" && initialState !== null ? initialState : {},
36
- [annotationKey]: options.annotations
37
- };
38
+ const initialState = injectAnnotationsIntoState(parser.initialState, options);
39
+ const shouldUnwrapAnnotatedValue = options?.annotations != null || isInjectedAnnotationWrapper(parser.initialState);
38
40
  let context = {
39
41
  buffer: args,
40
42
  optionsTerminated: false,
@@ -57,7 +59,7 @@ function parseSync(parser, args, options) {
57
59
  const endResult = parser.complete(context.state);
58
60
  return endResult.success ? {
59
61
  success: true,
60
- value: endResult.value
62
+ value: shouldUnwrapAnnotatedValue ? unwrapInjectedAnnotationWrapper(endResult.value) : endResult.value
61
63
  } : {
62
64
  success: false,
63
65
  error: endResult.error
@@ -83,11 +85,8 @@ function parseSync(parser, args, options) {
83
85
  * @since 0.10.0 Added optional `options` parameter for annotations support.
84
86
  */
85
87
  async function parseAsync(parser, args, options) {
86
- let initialState = parser.initialState;
87
- if (options?.annotations) initialState = {
88
- ...typeof initialState === "object" && initialState !== null ? initialState : {},
89
- [annotationKey]: options.annotations
90
- };
88
+ const initialState = injectAnnotationsIntoState(parser.initialState, options);
89
+ const shouldUnwrapAnnotatedValue = options?.annotations != null || isInjectedAnnotationWrapper(parser.initialState);
91
90
  let context = {
92
91
  buffer: args,
93
92
  optionsTerminated: false,
@@ -110,7 +109,7 @@ async function parseAsync(parser, args, options) {
110
109
  const endResult = await parser.complete(context.state);
111
110
  return endResult.success ? {
112
111
  success: true,
113
- value: endResult.value
112
+ value: shouldUnwrapAnnotatedValue ? unwrapInjectedAnnotationWrapper(endResult.value) : endResult.value
114
113
  } : {
115
114
  success: false,
116
115
  error: endResult.error
@@ -180,11 +179,7 @@ function parse(parser, args, options) {
180
179
  function suggestSync(parser, args, options) {
181
180
  const allButLast = args.slice(0, -1);
182
181
  const prefix = args[args.length - 1];
183
- let initialState = parser.initialState;
184
- if (options?.annotations) initialState = {
185
- ...typeof initialState === "object" && initialState !== null ? initialState : {},
186
- [annotationKey]: options.annotations
187
- };
182
+ const initialState = injectAnnotationsIntoState(parser.initialState, options);
188
183
  let context = {
189
184
  buffer: allButLast,
190
185
  optionsTerminated: false,
@@ -223,11 +218,7 @@ function suggestSync(parser, args, options) {
223
218
  async function suggestAsync(parser, args, options) {
224
219
  const allButLast = args.slice(0, -1);
225
220
  const prefix = args[args.length - 1];
226
- let initialState = parser.initialState;
227
- if (options?.annotations) initialState = {
228
- ...typeof initialState === "object" && initialState !== null ? initialState : {},
229
- [annotationKey]: options.annotations
230
- };
221
+ const initialState = injectAnnotationsIntoState(parser.initialState, options);
231
222
  let context = {
232
223
  buffer: allButLast,
233
224
  optionsTerminated: false,
@@ -338,11 +329,7 @@ function getDocPage(parser, args = [], options) {
338
329
  * Internal sync implementation of getDocPage.
339
330
  */
340
331
  function getDocPageSyncImpl(parser, args, options) {
341
- let initialState = parser.initialState;
342
- if (options?.annotations) initialState = {
343
- ...typeof initialState === "object" && initialState !== null ? initialState : {},
344
- [annotationKey]: options.annotations
345
- };
332
+ const initialState = injectAnnotationsIntoState(parser.initialState, options);
346
333
  let context = {
347
334
  buffer: args,
348
335
  optionsTerminated: false,
@@ -360,11 +347,7 @@ function getDocPageSyncImpl(parser, args, options) {
360
347
  * Internal async implementation of getDocPage.
361
348
  */
362
349
  async function getDocPageAsyncImpl(parser, args, options) {
363
- let initialState = parser.initialState;
364
- if (options?.annotations) initialState = {
365
- ...typeof initialState === "object" && initialState !== null ? initialState : {},
366
- [annotationKey]: options.annotations
367
- };
350
+ const initialState = injectAnnotationsIntoState(parser.initialState, options);
368
351
  let context = {
369
352
  buffer: args,
370
353
  optionsTerminated: false,
@@ -1,3 +1,4 @@
1
+ const require_annotations = require('./annotations.cjs');
1
2
  const require_message = require('./message.cjs');
2
3
  const require_dependency = require('./dependency.cjs');
3
4
  const require_mode_dispatch = require('./mode-dispatch.cjs');
@@ -1115,6 +1116,13 @@ function passThrough(options = {}) {
1115
1116
  const format = options.format ?? "equalsOnly";
1116
1117
  const optionPattern = /^-[a-z0-9-]|^--[a-z0-9-]+/i;
1117
1118
  const equalsOptionPattern = /^--[a-z0-9-]+=/i;
1119
+ const annotateFreshArray = (source, target) => {
1120
+ const annotations = require_annotations.getAnnotations(source);
1121
+ if (annotations === void 0) return target;
1122
+ const annotated = target;
1123
+ annotated[require_annotations.annotationKey] = annotations;
1124
+ return annotated;
1125
+ };
1118
1126
  return {
1119
1127
  $valueType: [],
1120
1128
  $stateType: [],
@@ -1139,7 +1147,7 @@ function passThrough(options = {}) {
1139
1147
  next: {
1140
1148
  ...context,
1141
1149
  buffer: [],
1142
- state: [...context.state, ...captured]
1150
+ state: annotateFreshArray(context.state, [...context.state, ...captured])
1143
1151
  },
1144
1152
  consumed: captured
1145
1153
  };
@@ -1160,7 +1168,7 @@ function passThrough(options = {}) {
1160
1168
  next: {
1161
1169
  ...context,
1162
1170
  buffer: context.buffer.slice(1),
1163
- state: [...context.state, token]
1171
+ state: annotateFreshArray(context.state, [...context.state, token])
1164
1172
  },
1165
1173
  consumed: [token]
1166
1174
  };
@@ -1176,7 +1184,7 @@ function passThrough(options = {}) {
1176
1184
  next: {
1177
1185
  ...context,
1178
1186
  buffer: context.buffer.slice(1),
1179
- state: [...context.state, token]
1187
+ state: annotateFreshArray(context.state, [...context.state, token])
1180
1188
  },
1181
1189
  consumed: [token]
1182
1190
  };
@@ -1186,11 +1194,11 @@ function passThrough(options = {}) {
1186
1194
  next: {
1187
1195
  ...context,
1188
1196
  buffer: context.buffer.slice(2),
1189
- state: [
1197
+ state: annotateFreshArray(context.state, [
1190
1198
  ...context.state,
1191
1199
  token,
1192
1200
  nextToken
1193
- ]
1201
+ ])
1194
1202
  },
1195
1203
  consumed: [token, nextToken]
1196
1204
  };
@@ -1199,7 +1207,7 @@ function passThrough(options = {}) {
1199
1207
  next: {
1200
1208
  ...context,
1201
1209
  buffer: context.buffer.slice(1),
1202
- state: [...context.state, token]
1210
+ state: annotateFreshArray(context.state, [...context.state, token])
1203
1211
  },
1204
1212
  consumed: [token]
1205
1213
  };
@@ -1211,10 +1219,15 @@ function passThrough(options = {}) {
1211
1219
  };
1212
1220
  },
1213
1221
  complete(state) {
1214
- return {
1222
+ if (require_annotations.getAnnotations(state) == null) return {
1215
1223
  success: true,
1216
1224
  value: state
1217
1225
  };
1226
+ const copied = [...state];
1227
+ return {
1228
+ success: true,
1229
+ value: copied
1230
+ };
1218
1231
  },
1219
1232
  suggest(_context, _prefix) {
1220
1233
  return [];
@@ -1,3 +1,4 @@
1
+ import { annotationKey, getAnnotations } from "./annotations.js";
1
2
  import { message, metavar, optionName, optionNames, text, valueSet } from "./message.js";
2
3
  import { createDeferredParseState, createDependencySourceState, createPendingDependencySourceState, dependencyId, getDefaultValuesFunction, getDependencyIds, isDeferredParseState, isDependencySource, isDependencySourceState, isDerivedValueParser, isPendingDependencySourceState, suggestWithDependency } from "./dependency.js";
3
4
  import { dispatchIterableByMode } from "./mode-dispatch.js";
@@ -1115,6 +1116,13 @@ function passThrough(options = {}) {
1115
1116
  const format = options.format ?? "equalsOnly";
1116
1117
  const optionPattern = /^-[a-z0-9-]|^--[a-z0-9-]+/i;
1117
1118
  const equalsOptionPattern = /^--[a-z0-9-]+=/i;
1119
+ const annotateFreshArray = (source, target) => {
1120
+ const annotations = getAnnotations(source);
1121
+ if (annotations === void 0) return target;
1122
+ const annotated = target;
1123
+ annotated[annotationKey] = annotations;
1124
+ return annotated;
1125
+ };
1118
1126
  return {
1119
1127
  $valueType: [],
1120
1128
  $stateType: [],
@@ -1139,7 +1147,7 @@ function passThrough(options = {}) {
1139
1147
  next: {
1140
1148
  ...context,
1141
1149
  buffer: [],
1142
- state: [...context.state, ...captured]
1150
+ state: annotateFreshArray(context.state, [...context.state, ...captured])
1143
1151
  },
1144
1152
  consumed: captured
1145
1153
  };
@@ -1160,7 +1168,7 @@ function passThrough(options = {}) {
1160
1168
  next: {
1161
1169
  ...context,
1162
1170
  buffer: context.buffer.slice(1),
1163
- state: [...context.state, token]
1171
+ state: annotateFreshArray(context.state, [...context.state, token])
1164
1172
  },
1165
1173
  consumed: [token]
1166
1174
  };
@@ -1176,7 +1184,7 @@ function passThrough(options = {}) {
1176
1184
  next: {
1177
1185
  ...context,
1178
1186
  buffer: context.buffer.slice(1),
1179
- state: [...context.state, token]
1187
+ state: annotateFreshArray(context.state, [...context.state, token])
1180
1188
  },
1181
1189
  consumed: [token]
1182
1190
  };
@@ -1186,11 +1194,11 @@ function passThrough(options = {}) {
1186
1194
  next: {
1187
1195
  ...context,
1188
1196
  buffer: context.buffer.slice(2),
1189
- state: [
1197
+ state: annotateFreshArray(context.state, [
1190
1198
  ...context.state,
1191
1199
  token,
1192
1200
  nextToken
1193
- ]
1201
+ ])
1194
1202
  },
1195
1203
  consumed: [token, nextToken]
1196
1204
  };
@@ -1199,7 +1207,7 @@ function passThrough(options = {}) {
1199
1207
  next: {
1200
1208
  ...context,
1201
1209
  buffer: context.buffer.slice(1),
1202
- state: [...context.state, token]
1210
+ state: annotateFreshArray(context.state, [...context.state, token])
1203
1211
  },
1204
1212
  consumed: [token]
1205
1213
  };
@@ -1211,10 +1219,15 @@ function passThrough(options = {}) {
1211
1219
  };
1212
1220
  },
1213
1221
  complete(state) {
1214
- return {
1222
+ if (getAnnotations(state) == null) return {
1215
1223
  success: true,
1216
1224
  value: state
1217
1225
  };
1226
+ const copied = [...state];
1227
+ return {
1228
+ success: true,
1229
+ value: copied
1230
+ };
1218
1231
  },
1219
1232
  suggest(_context, _prefix) {
1220
1233
  return [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.0-dev.523+c2f04b11",
3
+ "version": "1.0.0-dev.552+30c7ea22",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",