@optique/core 0.8.4 → 0.8.6

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
@@ -73,7 +73,8 @@ function analyzeNoMatchContext(parsers) {
73
73
  */
74
74
  var DuplicateOptionError = class extends Error {
75
75
  constructor(optionName$1, sources) {
76
- super(`Duplicate option name "${optionName$1}" found in fields: ${sources.join(", ")}. Each option name must be unique within a parser combinator.`);
76
+ const sourceNames = sources.map((s) => typeof s === "symbol" ? s.description ?? s.toString() : s);
77
+ super(`Duplicate option name "${optionName$1}" found in fields: ${sourceNames.join(", ")}. Each option name must be unique within a parser combinator.`);
77
78
  this.optionName = optionName$1;
78
79
  this.sources = sources;
79
80
  this.name = "DuplicateOptionError";
@@ -367,17 +368,35 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
367
368
  parsers = labelOrParsers;
368
369
  options = maybeParsersOrOptions ?? {};
369
370
  }
370
- const parserPairs = Object.entries(parsers);
371
+ const parserKeys = Reflect.ownKeys(parsers);
372
+ const parserPairs = parserKeys.map((k) => [k, parsers[k]]);
371
373
  parserPairs.sort(([_, parserA], [__, parserB]) => parserB.priority - parserA.priority);
374
+ const initialState = {};
375
+ for (const key of parserKeys) initialState[key] = parsers[key].initialState;
372
376
  if (!options.allowDuplicates) checkDuplicateOptionNames(parserPairs.map(([field, parser]) => [field, parser.usage]));
373
- const noMatchContext = analyzeNoMatchContext(Object.values(parsers));
377
+ const noMatchContext = analyzeNoMatchContext(parserKeys.map((k) => parsers[k]));
374
378
  return {
375
379
  $valueType: [],
376
380
  $stateType: [],
377
- priority: Math.max(...Object.values(parsers).map((p) => p.priority)),
381
+ priority: Math.max(...parserKeys.map((k) => parsers[k].priority)),
378
382
  usage: parserPairs.flatMap(([_, p]) => p.usage),
379
- initialState: Object.fromEntries(Object.entries(parsers).map(([key, parser]) => [key, parser.initialState])),
383
+ initialState,
380
384
  parse(context) {
385
+ if (!options.allowDuplicates) {
386
+ const optionNameSources = /* @__PURE__ */ new Map();
387
+ for (const [field, parser] of parserPairs) {
388
+ const names = require_usage.extractOptionNames(parser.usage);
389
+ for (const name of names) {
390
+ if (!optionNameSources.has(name)) optionNameSources.set(name, []);
391
+ optionNameSources.get(name).push(field);
392
+ }
393
+ }
394
+ for (const [name, sources] of optionNameSources) if (sources.length > 1) return {
395
+ success: false,
396
+ consumed: 0,
397
+ 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
+ };
399
+ }
381
400
  let error = {
382
401
  consumed: 0,
383
402
  error: context.buffer.length > 0 ? (() => {
@@ -447,8 +466,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
447
466
  },
448
467
  complete(state) {
449
468
  const result = {};
450
- for (const field in state) {
451
- if (!(field in parsers)) continue;
469
+ for (const field of parserKeys) {
452
470
  const valueResult = parsers[field].complete(state[field]);
453
471
  if (valueResult.success) result[field] = valueResult.value;
454
472
  else return {
@@ -760,8 +778,16 @@ function merge(...args) {
760
778
  return require_suggestion.deduplicateSuggestions(suggestions);
761
779
  },
762
780
  getDocFragments(state, _defaultValue) {
763
- const fragments = parsers.flatMap((p) => {
764
- const parserState = p.initialState === void 0 ? { kind: "unavailable" } : state.kind === "unavailable" ? { kind: "unavailable" } : {
781
+ const fragments = parsers.flatMap((p, i) => {
782
+ let parserState;
783
+ if (p.initialState === void 0) {
784
+ const key = `__parser_${i}`;
785
+ if (state.kind === "available" && state.state && typeof state.state === "object" && key in state.state) parserState = {
786
+ kind: "available",
787
+ state: state.state[key]
788
+ };
789
+ else parserState = { kind: "unavailable" };
790
+ } else parserState = state.kind === "unavailable" ? { kind: "unavailable" } : {
765
791
  kind: "available",
766
792
  state: state.state
767
793
  };
@@ -83,8 +83,8 @@ interface OrErrorOptions {
83
83
  */
84
84
  declare class DuplicateOptionError extends Error {
85
85
  readonly optionName: string;
86
- readonly sources: string[];
87
- constructor(optionName: string, sources: string[]);
86
+ readonly sources: readonly (string | symbol)[];
87
+ constructor(optionName: string, sources: readonly (string | symbol)[]);
88
88
  }
89
89
  /**
90
90
  * Creates a parser that combines two mutually exclusive parsers into one.
@@ -83,8 +83,8 @@ interface OrErrorOptions {
83
83
  */
84
84
  declare class DuplicateOptionError extends Error {
85
85
  readonly optionName: string;
86
- readonly sources: string[];
87
- constructor(optionName: string, sources: string[]);
86
+ readonly sources: readonly (string | symbol)[];
87
+ constructor(optionName: string, sources: readonly (string | symbol)[]);
88
88
  }
89
89
  /**
90
90
  * Creates a parser that combines two mutually exclusive parsers into one.
@@ -73,7 +73,8 @@ function analyzeNoMatchContext(parsers) {
73
73
  */
74
74
  var DuplicateOptionError = class extends Error {
75
75
  constructor(optionName$1, sources) {
76
- super(`Duplicate option name "${optionName$1}" found in fields: ${sources.join(", ")}. Each option name must be unique within a parser combinator.`);
76
+ const sourceNames = sources.map((s) => typeof s === "symbol" ? s.description ?? s.toString() : s);
77
+ super(`Duplicate option name "${optionName$1}" found in fields: ${sourceNames.join(", ")}. Each option name must be unique within a parser combinator.`);
77
78
  this.optionName = optionName$1;
78
79
  this.sources = sources;
79
80
  this.name = "DuplicateOptionError";
@@ -367,17 +368,35 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
367
368
  parsers = labelOrParsers;
368
369
  options = maybeParsersOrOptions ?? {};
369
370
  }
370
- const parserPairs = Object.entries(parsers);
371
+ const parserKeys = Reflect.ownKeys(parsers);
372
+ const parserPairs = parserKeys.map((k) => [k, parsers[k]]);
371
373
  parserPairs.sort(([_, parserA], [__, parserB]) => parserB.priority - parserA.priority);
374
+ const initialState = {};
375
+ for (const key of parserKeys) initialState[key] = parsers[key].initialState;
372
376
  if (!options.allowDuplicates) checkDuplicateOptionNames(parserPairs.map(([field, parser]) => [field, parser.usage]));
373
- const noMatchContext = analyzeNoMatchContext(Object.values(parsers));
377
+ const noMatchContext = analyzeNoMatchContext(parserKeys.map((k) => parsers[k]));
374
378
  return {
375
379
  $valueType: [],
376
380
  $stateType: [],
377
- priority: Math.max(...Object.values(parsers).map((p) => p.priority)),
381
+ priority: Math.max(...parserKeys.map((k) => parsers[k].priority)),
378
382
  usage: parserPairs.flatMap(([_, p]) => p.usage),
379
- initialState: Object.fromEntries(Object.entries(parsers).map(([key, parser]) => [key, parser.initialState])),
383
+ initialState,
380
384
  parse(context) {
385
+ if (!options.allowDuplicates) {
386
+ const optionNameSources = /* @__PURE__ */ new Map();
387
+ for (const [field, parser] of parserPairs) {
388
+ const names = extractOptionNames(parser.usage);
389
+ for (const name of names) {
390
+ if (!optionNameSources.has(name)) optionNameSources.set(name, []);
391
+ optionNameSources.get(name).push(field);
392
+ }
393
+ }
394
+ for (const [name, sources] of optionNameSources) if (sources.length > 1) return {
395
+ success: false,
396
+ consumed: 0,
397
+ 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
+ };
399
+ }
381
400
  let error = {
382
401
  consumed: 0,
383
402
  error: context.buffer.length > 0 ? (() => {
@@ -447,8 +466,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
447
466
  },
448
467
  complete(state) {
449
468
  const result = {};
450
- for (const field in state) {
451
- if (!(field in parsers)) continue;
469
+ for (const field of parserKeys) {
452
470
  const valueResult = parsers[field].complete(state[field]);
453
471
  if (valueResult.success) result[field] = valueResult.value;
454
472
  else return {
@@ -760,8 +778,16 @@ function merge(...args) {
760
778
  return deduplicateSuggestions(suggestions);
761
779
  },
762
780
  getDocFragments(state, _defaultValue) {
763
- const fragments = parsers.flatMap((p) => {
764
- const parserState = p.initialState === void 0 ? { kind: "unavailable" } : state.kind === "unavailable" ? { kind: "unavailable" } : {
781
+ const fragments = parsers.flatMap((p, i) => {
782
+ let parserState;
783
+ if (p.initialState === void 0) {
784
+ const key = `__parser_${i}`;
785
+ if (state.kind === "available" && state.state && typeof state.state === "object" && key in state.state) parserState = {
786
+ kind: "available",
787
+ state: state.state[key]
788
+ };
789
+ else parserState = { kind: "unavailable" };
790
+ } else parserState = state.kind === "unavailable" ? { kind: "unavailable" } : {
765
791
  kind: "available",
766
792
  state: state.state
767
793
  };
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.
@@ -157,14 +177,11 @@ function getDocPage(parser, args = []) {
157
177
  if (i >= usage.length) break;
158
178
  const term = usage[i];
159
179
  if (term.type === "exclusive") {
160
- for (const termGroup of term.terms) {
161
- const firstTerm = termGroup[0];
162
- if (firstTerm?.type !== "command" || firstTerm.name !== arg) continue;
163
- usage.splice(i, 1, ...termGroup);
164
- i += termGroup.length;
165
- break;
166
- }
167
- if (usage[i] === term) i++;
180
+ const found = findCommandInExclusive(term, arg);
181
+ if (found) {
182
+ usage.splice(i, 1, ...found);
183
+ i += found.length;
184
+ } else i++;
168
185
  } else i++;
169
186
  }
170
187
  return {
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.
@@ -157,14 +177,11 @@ function getDocPage(parser, args = []) {
157
177
  if (i >= usage.length) break;
158
178
  const term = usage[i];
159
179
  if (term.type === "exclusive") {
160
- for (const termGroup of term.terms) {
161
- const firstTerm = termGroup[0];
162
- if (firstTerm?.type !== "command" || firstTerm.name !== arg) continue;
163
- usage.splice(i, 1, ...termGroup);
164
- i += termGroup.length;
165
- break;
166
- }
167
- if (usage[i] === term) i++;
180
+ const found = findCommandInExclusive(term, arg);
181
+ if (found) {
182
+ usage.splice(i, 1, ...found);
183
+ i += found.length;
184
+ } else i++;
168
185
  } else i++;
169
186
  }
170
187
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "0.8.4",
3
+ "version": "0.8.6",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",