@optique/core 0.7.6 → 0.7.7

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.
@@ -825,8 +825,16 @@ function merge(...args) {
825
825
  });
826
826
  },
827
827
  getDocFragments(state, _defaultValue) {
828
- const fragments = parsers.flatMap((p) => {
829
- const parserState = p.initialState === void 0 ? { kind: "unavailable" } : state.kind === "unavailable" ? { kind: "unavailable" } : {
828
+ const fragments = parsers.flatMap((p, i) => {
829
+ let parserState;
830
+ if (p.initialState === void 0) {
831
+ const key = `__parser_${i}`;
832
+ if (state.kind === "available" && state.state && typeof state.state === "object" && key in state.state) parserState = {
833
+ kind: "available",
834
+ state: state.state[key]
835
+ };
836
+ else parserState = { kind: "unavailable" };
837
+ } else parserState = state.kind === "unavailable" ? { kind: "unavailable" } : {
830
838
  kind: "available",
831
839
  state: state.state
832
840
  };
@@ -825,8 +825,16 @@ function merge(...args) {
825
825
  });
826
826
  },
827
827
  getDocFragments(state, _defaultValue) {
828
- const fragments = parsers.flatMap((p) => {
829
- const parserState = p.initialState === void 0 ? { kind: "unavailable" } : state.kind === "unavailable" ? { kind: "unavailable" } : {
828
+ const fragments = parsers.flatMap((p, i) => {
829
+ let parserState;
830
+ if (p.initialState === void 0) {
831
+ const key = `__parser_${i}`;
832
+ if (state.kind === "available" && state.state && typeof state.state === "object" && key in state.state) parserState = {
833
+ kind: "available",
834
+ state: state.state[key]
835
+ };
836
+ else parserState = { kind: "unavailable" };
837
+ } else parserState = state.kind === "unavailable" ? { kind: "unavailable" } : {
830
838
  kind: "available",
831
839
  state: state.state
832
840
  };
package/dist/parser.cjs CHANGED
@@ -95,6 +95,26 @@ function suggest(parser, args) {
95
95
  return Array.from(parser.suggest(context, prefix));
96
96
  }
97
97
  /**
98
+ * Recursively searches for a command within nested exclusive usage terms.
99
+ * When the command is found, returns the expanded usage terms for that command.
100
+ *
101
+ * @param term The usage term to search in
102
+ * @param commandName The command name to find
103
+ * @returns The expanded usage terms if found, null otherwise
104
+ */
105
+ function findCommandInExclusive(term, commandName) {
106
+ if (term.type !== "exclusive") return null;
107
+ for (const termGroup of term.terms) {
108
+ const firstTerm = termGroup[0];
109
+ if (firstTerm?.type === "command" && firstTerm.name === commandName) return termGroup;
110
+ if (firstTerm?.type === "exclusive") {
111
+ const found = findCommandInExclusive(firstTerm, commandName);
112
+ if (found) return [...found, ...termGroup.slice(1)];
113
+ }
114
+ }
115
+ return null;
116
+ }
117
+ /**
98
118
  * Generates a documentation page for a parser based on its current state after
99
119
  * attempting to parse the provided arguments. This function is useful for
100
120
  * creating help documentation that reflects the current parsing context.
@@ -155,11 +175,9 @@ function getDocPage(parser, args = []) {
155
175
  let i = 0;
156
176
  for (const arg of args) {
157
177
  const term = usage[i];
158
- if (usage.length > i && term.type === "exclusive") for (const termGroup of term.terms) {
159
- const firstTerm = termGroup[0];
160
- if (firstTerm?.type !== "command" || firstTerm.name !== arg) continue;
161
- usage.splice(i, 1, ...termGroup);
162
- break;
178
+ if (usage.length > i && term.type === "exclusive") {
179
+ const found = findCommandInExclusive(term, arg);
180
+ if (found) usage.splice(i, 1, ...found);
163
181
  }
164
182
  i++;
165
183
  }
package/dist/parser.js CHANGED
@@ -95,6 +95,26 @@ function suggest(parser, args) {
95
95
  return Array.from(parser.suggest(context, prefix));
96
96
  }
97
97
  /**
98
+ * Recursively searches for a command within nested exclusive usage terms.
99
+ * When the command is found, returns the expanded usage terms for that command.
100
+ *
101
+ * @param term The usage term to search in
102
+ * @param commandName The command name to find
103
+ * @returns The expanded usage terms if found, null otherwise
104
+ */
105
+ function findCommandInExclusive(term, commandName) {
106
+ if (term.type !== "exclusive") return null;
107
+ for (const termGroup of term.terms) {
108
+ const firstTerm = termGroup[0];
109
+ if (firstTerm?.type === "command" && firstTerm.name === commandName) return termGroup;
110
+ if (firstTerm?.type === "exclusive") {
111
+ const found = findCommandInExclusive(firstTerm, commandName);
112
+ if (found) return [...found, ...termGroup.slice(1)];
113
+ }
114
+ }
115
+ return null;
116
+ }
117
+ /**
98
118
  * Generates a documentation page for a parser based on its current state after
99
119
  * attempting to parse the provided arguments. This function is useful for
100
120
  * creating help documentation that reflects the current parsing context.
@@ -155,11 +175,9 @@ function getDocPage(parser, args = []) {
155
175
  let i = 0;
156
176
  for (const arg of args) {
157
177
  const term = usage[i];
158
- if (usage.length > i && term.type === "exclusive") for (const termGroup of term.terms) {
159
- const firstTerm = termGroup[0];
160
- if (firstTerm?.type !== "command" || firstTerm.name !== arg) continue;
161
- usage.splice(i, 1, ...termGroup);
162
- break;
178
+ if (usage.length > i && term.type === "exclusive") {
179
+ const found = findCommandInExclusive(term, arg);
180
+ if (found) usage.splice(i, 1, ...found);
163
181
  }
164
182
  i++;
165
183
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "0.7.6",
3
+ "version": "0.7.7",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",