@optique/core 0.10.0-dev.330 → 0.10.0-dev.332

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,3 +1,4 @@
1
+ import { annotationKey } from "./annotations.js";
1
2
  import { message } from "./message.js";
2
3
  import { normalizeUsage } from "./usage.js";
3
4
  import { DuplicateOptionError, concat, conditional, group, longestMatch, merge, object, or, tuple } from "./constructs.js";
@@ -18,18 +19,25 @@ import { argument, command, constant, flag, option, passThrough } from "./primit
18
19
  * arguments. Must be a synchronous parser.
19
20
  * @param args The array of command-line arguments to parse. Usually this is
20
21
  * `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
22
+ * @param options Optional {@link ParseOptions} for customizing parsing behavior.
21
23
  * @returns A {@link Result} object indicating whether the parsing was
22
24
  * successful or not. If successful, it contains the parsed value of
23
25
  * type `T`. If not, it contains an error message describing the
24
26
  * failure.
25
27
  * @since 0.9.0 Renamed from the original `parse` function which now delegates
26
28
  * to this for sync parsers.
29
+ * @since 0.10.0 Added optional `options` parameter for annotations support.
27
30
  */
28
- function parseSync(parser, args) {
31
+ function parseSync(parser, args, options) {
32
+ let initialState = parser.initialState;
33
+ if (options?.annotations) initialState = {
34
+ ...typeof initialState === "object" && initialState !== null ? initialState : {},
35
+ [annotationKey]: options.annotations
36
+ };
29
37
  let context = {
30
38
  buffer: args,
31
39
  optionsTerminated: false,
32
- state: parser.initialState,
40
+ state: initialState,
33
41
  usage: parser.usage
34
42
  };
35
43
  do {
@@ -67,15 +75,22 @@ function parseSync(parser, args) {
67
75
  * arguments.
68
76
  * @param args The array of command-line arguments to parse. Usually this is
69
77
  * `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
78
+ * @param options Optional {@link ParseOptions} for customizing parsing behavior.
70
79
  * @returns A Promise that resolves to a {@link Result} object indicating
71
80
  * whether the parsing was successful or not.
72
81
  * @since 0.9.0
82
+ * @since 0.10.0 Added optional `options` parameter for annotations support.
73
83
  */
74
- async function parseAsync(parser, args) {
84
+ async function parseAsync(parser, args, options) {
85
+ let initialState = parser.initialState;
86
+ if (options?.annotations) initialState = {
87
+ ...typeof initialState === "object" && initialState !== null ? initialState : {},
88
+ [annotationKey]: options.annotations
89
+ };
75
90
  let context = {
76
91
  buffer: args,
77
92
  optionsTerminated: false,
78
- state: parser.initialState,
93
+ state: initialState,
79
94
  usage: parser.usage
80
95
  };
81
96
  do {
@@ -117,12 +132,14 @@ async function parseAsync(parser, args) {
117
132
  * arguments.
118
133
  * @param args The array of command-line arguments to parse. Usually this is
119
134
  * `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
135
+ * @param options Optional {@link ParseOptions} for customizing parsing behavior.
120
136
  * @returns A {@link Result} object (for sync) or Promise thereof (for async)
121
137
  * indicating whether the parsing was successful or not.
138
+ * @since 0.10.0 Added optional `options` parameter for annotations support.
122
139
  */
123
- function parse(parser, args) {
124
- if (parser.$mode === "async") return parseAsync(parser, args);
125
- return parseSync(parser, args);
140
+ function parse(parser, args, options) {
141
+ if (parser.$mode === "async") return parseAsync(parser, args, options);
142
+ return parseSync(parser, args, options);
126
143
  }
127
144
  /**
128
145
  * Generates command-line suggestions based on current parsing state.
@@ -138,6 +155,7 @@ function parse(parser, args) {
138
155
  * @param args The array of command-line arguments including the partial
139
156
  * argument to complete. The last element is treated as
140
157
  * the prefix for suggestions.
158
+ * @param options Optional {@link ParseOptions} for customizing parsing behavior.
141
159
  * @returns An array of {@link Suggestion} objects containing completion
142
160
  * candidates.
143
161
  * @example
@@ -157,14 +175,20 @@ function parse(parser, args) {
157
175
  * ```
158
176
  * @since 0.6.0
159
177
  * @since 0.9.0 Renamed from the original `suggest` function.
178
+ * @since 0.10.0 Added optional `options` parameter for annotations support.
160
179
  */
161
- function suggestSync(parser, args) {
180
+ function suggestSync(parser, args, options) {
162
181
  const allButLast = args.slice(0, -1);
163
182
  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
+ };
164
188
  let context = {
165
189
  buffer: allButLast,
166
190
  optionsTerminated: false,
167
- state: parser.initialState,
191
+ state: initialState,
168
192
  usage: parser.usage
169
193
  };
170
194
  while (context.buffer.length > 0) {
@@ -190,17 +214,24 @@ function suggestSync(parser, args) {
190
214
  * @param args The array of command-line arguments including the partial
191
215
  * argument to complete. The last element is treated as
192
216
  * the prefix for suggestions.
217
+ * @param options Optional {@link ParseOptions} for customizing parsing behavior.
193
218
  * @returns A Promise that resolves to an array of {@link Suggestion} objects
194
219
  * containing completion candidates.
195
220
  * @since 0.9.0
221
+ * @since 0.10.0 Added optional `options` parameter for annotations support.
196
222
  */
197
- async function suggestAsync(parser, args) {
223
+ async function suggestAsync(parser, args, options) {
198
224
  const allButLast = args.slice(0, -1);
199
225
  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
+ };
200
231
  let context = {
201
232
  buffer: allButLast,
202
233
  optionsTerminated: false,
203
- state: parser.initialState,
234
+ state: initialState,
204
235
  usage: parser.usage
205
236
  };
206
237
  while (context.buffer.length > 0) {
@@ -235,13 +266,15 @@ async function suggestAsync(parser, args) {
235
266
  * @param args The array of command-line arguments including the partial
236
267
  * argument to complete. The last element is treated as
237
268
  * the prefix for suggestions.
269
+ * @param options Optional {@link ParseOptions} for customizing parsing behavior.
238
270
  * @returns An array of {@link Suggestion} objects (for sync) or Promise thereof
239
271
  * (for async) containing completion candidates.
240
272
  * @since 0.6.0
273
+ * @since 0.10.0 Added optional `options` parameter for annotations support.
241
274
  */
242
- function suggest(parser, args) {
243
- if (parser.$mode === "async") return suggestAsync(parser, args);
244
- return suggestSync(parser, args);
275
+ function suggest(parser, args, options) {
276
+ if (parser.$mode === "async") return suggestAsync(parser, args, options);
277
+ return suggestSync(parser, args, options);
245
278
  }
246
279
  /**
247
280
  * Recursively searches for a command within nested exclusive usage terms.
@@ -272,11 +305,13 @@ function findCommandInExclusive(term, commandName) {
272
305
  *
273
306
  * @param parser The sync parser to generate documentation for.
274
307
  * @param args Optional array of command-line arguments for context.
308
+ * @param options Optional {@link ParseOptions} for customizing parsing behavior.
275
309
  * @returns A {@link DocPage} or `undefined`.
276
310
  * @since 0.9.0
311
+ * @since 0.10.0 Added optional `options` parameter for annotations support.
277
312
  */
278
- function getDocPageSync(parser, args = []) {
279
- return getDocPageSyncImpl(parser, args);
313
+ function getDocPageSync(parser, args = [], options) {
314
+ return getDocPageSyncImpl(parser, args, options);
280
315
  }
281
316
  /**
282
317
  * Generates a documentation page for any parser, returning a Promise.
@@ -287,25 +322,32 @@ function getDocPageSync(parser, args = []) {
287
322
  *
288
323
  * @param parser The parser to generate documentation for.
289
324
  * @param args Optional array of command-line arguments for context.
325
+ * @param options Optional {@link ParseOptions} for customizing parsing behavior.
290
326
  * @returns A Promise of {@link DocPage} or `undefined`.
291
327
  * @since 0.9.0
328
+ * @since 0.10.0 Added optional `options` parameter for annotations support.
292
329
  */
293
- function getDocPageAsync(parser, args = []) {
294
- if (parser.$mode === "sync") return Promise.resolve(getDocPageSyncImpl(parser, args));
295
- return getDocPageAsyncImpl(parser, args);
330
+ function getDocPageAsync(parser, args = [], options) {
331
+ if (parser.$mode === "sync") return Promise.resolve(getDocPageSyncImpl(parser, args, options));
332
+ return getDocPageAsyncImpl(parser, args, options);
296
333
  }
297
- function getDocPage(parser, args = []) {
298
- if (parser.$mode === "sync") return getDocPageSyncImpl(parser, args);
299
- return getDocPageAsyncImpl(parser, args);
334
+ function getDocPage(parser, args = [], options) {
335
+ if (parser.$mode === "sync") return getDocPageSyncImpl(parser, args, options);
336
+ return getDocPageAsyncImpl(parser, args, options);
300
337
  }
301
338
  /**
302
339
  * Internal sync implementation of getDocPage.
303
340
  */
304
- function getDocPageSyncImpl(parser, args) {
341
+ function getDocPageSyncImpl(parser, args, options) {
342
+ let initialState = parser.initialState;
343
+ if (options?.annotations) initialState = {
344
+ ...typeof initialState === "object" && initialState !== null ? initialState : {},
345
+ [annotationKey]: options.annotations
346
+ };
305
347
  let context = {
306
348
  buffer: args,
307
349
  optionsTerminated: false,
308
- state: parser.initialState,
350
+ state: initialState,
309
351
  usage: parser.usage
310
352
  };
311
353
  do {
@@ -318,11 +360,16 @@ function getDocPageSyncImpl(parser, args) {
318
360
  /**
319
361
  * Internal async implementation of getDocPage.
320
362
  */
321
- async function getDocPageAsyncImpl(parser, args) {
363
+ async function getDocPageAsyncImpl(parser, args, options) {
364
+ let initialState = parser.initialState;
365
+ if (options?.annotations) initialState = {
366
+ ...typeof initialState === "object" && initialState !== null ? initialState : {},
367
+ [annotationKey]: options.annotations
368
+ };
322
369
  let context = {
323
370
  buffer: args,
324
371
  optionsTerminated: false,
325
- state: parser.initialState,
372
+ state: initialState,
326
373
  usage: parser.usage
327
374
  };
328
375
  do {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "0.10.0-dev.330+04823df7",
3
+ "version": "0.10.0-dev.332+be2882ab",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",
@@ -51,6 +51,14 @@
51
51
  "import": "./dist/index.js",
52
52
  "require": "./dist/index.cjs"
53
53
  },
54
+ "./annotations": {
55
+ "types": {
56
+ "import": "./dist/annotations.d.ts",
57
+ "require": "./dist/annotations.d.cts"
58
+ },
59
+ "import": "./dist/annotations.js",
60
+ "require": "./dist/annotations.cjs"
61
+ },
54
62
  "./completion": {
55
63
  "types": {
56
64
  "import": "./dist/completion.d.ts",
@@ -59,6 +67,14 @@
59
67
  "import": "./dist/completion.js",
60
68
  "require": "./dist/completion.cjs"
61
69
  },
70
+ "./context": {
71
+ "types": {
72
+ "import": "./dist/context.d.ts",
73
+ "require": "./dist/context.d.cts"
74
+ },
75
+ "import": "./dist/context.js",
76
+ "require": "./dist/context.cjs"
77
+ },
62
78
  "./constructs": {
63
79
  "types": {
64
80
  "import": "./dist/constructs.d.ts",