@optique/core 0.7.6 → 0.7.8

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
@@ -372,15 +372,18 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
372
372
  parsers = labelOrParsers;
373
373
  options = maybeParsersOrOptions ?? {};
374
374
  }
375
- const parserPairs = Object.entries(parsers);
375
+ const parserKeys = Reflect.ownKeys(parsers);
376
+ const parserPairs = parserKeys.map((k) => [k, parsers[k]]);
376
377
  parserPairs.sort(([_, parserA], [__, parserB]) => parserB.priority - parserA.priority);
377
- const noMatchContext = analyzeNoMatchContext(Object.values(parsers));
378
+ const initialState = {};
379
+ for (const key of parserKeys) initialState[key] = parsers[key].initialState;
380
+ const noMatchContext = analyzeNoMatchContext(parserKeys.map((k) => parsers[k]));
378
381
  return {
379
382
  $valueType: [],
380
383
  $stateType: [],
381
- priority: Math.max(...Object.values(parsers).map((p) => p.priority)),
384
+ priority: Math.max(...parserKeys.map((k) => parsers[k].priority)),
382
385
  usage: parserPairs.flatMap(([_, p]) => p.usage),
383
- initialState: Object.fromEntries(Object.entries(parsers).map(([key, parser]) => [key, parser.initialState])),
386
+ initialState,
384
387
  parse(context) {
385
388
  if (!options.allowDuplicates) {
386
389
  const optionNameSources = /* @__PURE__ */ new Map();
@@ -394,7 +397,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
394
397
  for (const [name, sources] of optionNameSources) if (sources.length > 1) return {
395
398
  success: false,
396
399
  consumed: 0,
397
- error: require_message.message`Duplicate option name ${require_message.optionName(name)} found in fields: ${require_message.values(sources)}. Each option name must be unique within a parser combinator.`
400
+ error: require_message.message`Duplicate option name ${require_message.optionName(name)} found in fields: ${require_message.values(sources.map((s) => typeof s === "symbol" ? s.description ?? s.toString() : s))}. Each option name must be unique within a parser combinator.`
398
401
  };
399
402
  }
400
403
  let error = {
@@ -466,8 +469,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
466
469
  },
467
470
  complete(state) {
468
471
  const result = {};
469
- for (const field in state) {
470
- if (!(field in parsers)) continue;
472
+ for (const field of parserKeys) {
471
473
  const valueResult = parsers[field].complete(state[field]);
472
474
  if (valueResult.success) result[field] = valueResult.value;
473
475
  else return {
@@ -825,8 +827,16 @@ function merge(...args) {
825
827
  });
826
828
  },
827
829
  getDocFragments(state, _defaultValue) {
828
- const fragments = parsers.flatMap((p) => {
829
- const parserState = p.initialState === void 0 ? { kind: "unavailable" } : state.kind === "unavailable" ? { kind: "unavailable" } : {
830
+ const fragments = parsers.flatMap((p, i) => {
831
+ let parserState;
832
+ if (p.initialState === void 0) {
833
+ const key = `__parser_${i}`;
834
+ if (state.kind === "available" && state.state && typeof state.state === "object" && key in state.state) parserState = {
835
+ kind: "available",
836
+ state: state.state[key]
837
+ };
838
+ else parserState = { kind: "unavailable" };
839
+ } else parserState = state.kind === "unavailable" ? { kind: "unavailable" } : {
830
840
  kind: "available",
831
841
  state: state.state
832
842
  };
@@ -372,15 +372,18 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
372
372
  parsers = labelOrParsers;
373
373
  options = maybeParsersOrOptions ?? {};
374
374
  }
375
- const parserPairs = Object.entries(parsers);
375
+ const parserKeys = Reflect.ownKeys(parsers);
376
+ const parserPairs = parserKeys.map((k) => [k, parsers[k]]);
376
377
  parserPairs.sort(([_, parserA], [__, parserB]) => parserB.priority - parserA.priority);
377
- const noMatchContext = analyzeNoMatchContext(Object.values(parsers));
378
+ const initialState = {};
379
+ for (const key of parserKeys) initialState[key] = parsers[key].initialState;
380
+ const noMatchContext = analyzeNoMatchContext(parserKeys.map((k) => parsers[k]));
378
381
  return {
379
382
  $valueType: [],
380
383
  $stateType: [],
381
- priority: Math.max(...Object.values(parsers).map((p) => p.priority)),
384
+ priority: Math.max(...parserKeys.map((k) => parsers[k].priority)),
382
385
  usage: parserPairs.flatMap(([_, p]) => p.usage),
383
- initialState: Object.fromEntries(Object.entries(parsers).map(([key, parser]) => [key, parser.initialState])),
386
+ initialState,
384
387
  parse(context) {
385
388
  if (!options.allowDuplicates) {
386
389
  const optionNameSources = /* @__PURE__ */ new Map();
@@ -394,7 +397,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
394
397
  for (const [name, sources] of optionNameSources) if (sources.length > 1) return {
395
398
  success: false,
396
399
  consumed: 0,
397
- error: message`Duplicate option name ${optionName(name)} found in fields: ${values(sources)}. Each option name must be unique within a parser combinator.`
400
+ error: message`Duplicate option name ${optionName(name)} found in fields: ${values(sources.map((s) => typeof s === "symbol" ? s.description ?? s.toString() : s))}. Each option name must be unique within a parser combinator.`
398
401
  };
399
402
  }
400
403
  let error = {
@@ -466,8 +469,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
466
469
  },
467
470
  complete(state) {
468
471
  const result = {};
469
- for (const field in state) {
470
- if (!(field in parsers)) continue;
472
+ for (const field of parserKeys) {
471
473
  const valueResult = parsers[field].complete(state[field]);
472
474
  if (valueResult.success) result[field] = valueResult.value;
473
475
  else return {
@@ -825,8 +827,16 @@ function merge(...args) {
825
827
  });
826
828
  },
827
829
  getDocFragments(state, _defaultValue) {
828
- const fragments = parsers.flatMap((p) => {
829
- const parserState = p.initialState === void 0 ? { kind: "unavailable" } : state.kind === "unavailable" ? { kind: "unavailable" } : {
830
+ const fragments = parsers.flatMap((p, i) => {
831
+ let parserState;
832
+ if (p.initialState === void 0) {
833
+ const key = `__parser_${i}`;
834
+ if (state.kind === "available" && state.state && typeof state.state === "object" && key in state.state) parserState = {
835
+ kind: "available",
836
+ state: state.state[key]
837
+ };
838
+ else parserState = { kind: "unavailable" };
839
+ } else parserState = state.kind === "unavailable" ? { kind: "unavailable" } : {
830
840
  kind: "available",
831
841
  state: state.state
832
842
  };
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.8",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",