@optique/core 0.6.9-dev.197 → 0.6.10

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright 2025 Hong Minhee
3
+ Copyright 2025–2026 Hong Minhee
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
  this software and associated documentation files (the "Software"), to deal in
@@ -269,14 +269,17 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
269
269
  parsers = labelOrParsers;
270
270
  options = maybeParsersOrOptions ?? {};
271
271
  }
272
- const parserPairs = Object.entries(parsers);
272
+ const parserKeys = Reflect.ownKeys(parsers);
273
+ const parserPairs = parserKeys.map((k) => [k, parsers[k]]);
273
274
  parserPairs.sort(([_, parserA], [__, parserB]) => parserB.priority - parserA.priority);
275
+ const initialState = {};
276
+ for (const key of parserKeys) initialState[key] = parsers[key].initialState;
274
277
  return {
275
278
  $valueType: [],
276
279
  $stateType: [],
277
- priority: Math.max(...Object.values(parsers).map((p) => p.priority)),
280
+ priority: Math.max(...parserKeys.map((k) => parsers[k].priority)),
278
281
  usage: parserPairs.flatMap(([_, p]) => p.usage),
279
- initialState: Object.fromEntries(Object.entries(parsers).map(([key, parser]) => [key, parser.initialState])),
282
+ initialState,
280
283
  parse(context) {
281
284
  let error = {
282
285
  consumed: 0,
@@ -342,8 +345,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
342
345
  },
343
346
  complete(state) {
344
347
  const result = {};
345
- for (const field in state) {
346
- if (!(field in parsers)) continue;
348
+ for (const field of parserKeys) {
347
349
  const valueResult = parsers[field].complete(state[field]);
348
350
  if (valueResult.success) result[field] = valueResult.value;
349
351
  else return {
@@ -269,14 +269,17 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
269
269
  parsers = labelOrParsers;
270
270
  options = maybeParsersOrOptions ?? {};
271
271
  }
272
- const parserPairs = Object.entries(parsers);
272
+ const parserKeys = Reflect.ownKeys(parsers);
273
+ const parserPairs = parserKeys.map((k) => [k, parsers[k]]);
273
274
  parserPairs.sort(([_, parserA], [__, parserB]) => parserB.priority - parserA.priority);
275
+ const initialState = {};
276
+ for (const key of parserKeys) initialState[key] = parsers[key].initialState;
274
277
  return {
275
278
  $valueType: [],
276
279
  $stateType: [],
277
- priority: Math.max(...Object.values(parsers).map((p) => p.priority)),
280
+ priority: Math.max(...parserKeys.map((k) => parsers[k].priority)),
278
281
  usage: parserPairs.flatMap(([_, p]) => p.usage),
279
- initialState: Object.fromEntries(Object.entries(parsers).map(([key, parser]) => [key, parser.initialState])),
282
+ initialState,
280
283
  parse(context) {
281
284
  let error = {
282
285
  consumed: 0,
@@ -342,8 +345,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
342
345
  },
343
346
  complete(state) {
344
347
  const result = {};
345
- for (const field in state) {
346
- if (!(field in parsers)) continue;
348
+ for (const field of parserKeys) {
347
349
  const valueResult = parsers[field].complete(state[field]);
348
350
  if (valueResult.success) result[field] = valueResult.value;
349
351
  else return {
package/dist/parser.cjs CHANGED
@@ -93,6 +93,26 @@ function suggest(parser, args) {
93
93
  return Array.from(parser.suggest(context, prefix));
94
94
  }
95
95
  /**
96
+ * Recursively searches for a command within nested exclusive usage terms.
97
+ * When the command is found, returns the expanded usage terms for that command.
98
+ *
99
+ * @param term The usage term to search in
100
+ * @param commandName The command name to find
101
+ * @returns The expanded usage terms if found, null otherwise
102
+ */
103
+ function findCommandInExclusive(term, commandName) {
104
+ if (term.type !== "exclusive") return null;
105
+ for (const termGroup of term.terms) {
106
+ const firstTerm = termGroup[0];
107
+ if (firstTerm?.type === "command" && firstTerm.name === commandName) return termGroup;
108
+ if (firstTerm?.type === "exclusive") {
109
+ const found = findCommandInExclusive(firstTerm, commandName);
110
+ if (found) return [...found, ...termGroup.slice(1)];
111
+ }
112
+ }
113
+ return null;
114
+ }
115
+ /**
96
116
  * Generates a documentation page for a parser based on its current state after
97
117
  * attempting to parse the provided arguments. This function is useful for
98
118
  * creating help documentation that reflects the current parsing context.
@@ -149,15 +169,14 @@ function getDocPage(parser, args = []) {
149
169
  }
150
170
  if (entries.length > 0) sections.push({ entries });
151
171
  const usage = [...require_usage.normalizeUsage(parser.usage)];
152
- for (const arg of args) for (let i = 0; i < usage.length; i++) {
172
+ let i = 0;
173
+ for (const arg of args) {
153
174
  const term = usage[i];
154
- if (term.type !== "exclusive") continue;
155
- for (const termGroup of term.terms) {
156
- const firstTerm = termGroup[0];
157
- if (firstTerm?.type !== "command" || firstTerm.name !== arg) continue;
158
- usage.splice(i, 1, ...termGroup);
159
- break;
175
+ if (usage.length > i && term.type === "exclusive") {
176
+ const found = findCommandInExclusive(term, arg);
177
+ if (found) usage.splice(i, 1, ...found);
160
178
  }
179
+ i++;
161
180
  }
162
181
  return {
163
182
  usage,
package/dist/parser.js CHANGED
@@ -93,6 +93,26 @@ function suggest(parser, args) {
93
93
  return Array.from(parser.suggest(context, prefix));
94
94
  }
95
95
  /**
96
+ * Recursively searches for a command within nested exclusive usage terms.
97
+ * When the command is found, returns the expanded usage terms for that command.
98
+ *
99
+ * @param term The usage term to search in
100
+ * @param commandName The command name to find
101
+ * @returns The expanded usage terms if found, null otherwise
102
+ */
103
+ function findCommandInExclusive(term, commandName) {
104
+ if (term.type !== "exclusive") return null;
105
+ for (const termGroup of term.terms) {
106
+ const firstTerm = termGroup[0];
107
+ if (firstTerm?.type === "command" && firstTerm.name === commandName) return termGroup;
108
+ if (firstTerm?.type === "exclusive") {
109
+ const found = findCommandInExclusive(firstTerm, commandName);
110
+ if (found) return [...found, ...termGroup.slice(1)];
111
+ }
112
+ }
113
+ return null;
114
+ }
115
+ /**
96
116
  * Generates a documentation page for a parser based on its current state after
97
117
  * attempting to parse the provided arguments. This function is useful for
98
118
  * creating help documentation that reflects the current parsing context.
@@ -149,15 +169,14 @@ function getDocPage(parser, args = []) {
149
169
  }
150
170
  if (entries.length > 0) sections.push({ entries });
151
171
  const usage = [...normalizeUsage(parser.usage)];
152
- for (const arg of args) for (let i = 0; i < usage.length; i++) {
172
+ let i = 0;
173
+ for (const arg of args) {
153
174
  const term = usage[i];
154
- if (term.type !== "exclusive") continue;
155
- for (const termGroup of term.terms) {
156
- const firstTerm = termGroup[0];
157
- if (firstTerm?.type !== "command" || firstTerm.name !== arg) continue;
158
- usage.splice(i, 1, ...termGroup);
159
- break;
175
+ if (usage.length > i && term.type === "exclusive") {
176
+ const found = findCommandInExclusive(term, arg);
177
+ if (found) usage.splice(i, 1, ...found);
160
178
  }
179
+ i++;
161
180
  }
162
181
  return {
163
182
  usage,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "0.6.9-dev.197+945d8f0c",
3
+ "version": "0.6.10",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",