@optique/core 0.9.1 → 0.9.3

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/facade.cjs CHANGED
@@ -500,6 +500,59 @@ function runParser(parser, programName, args, options = {}) {
500
500
  else if (commandParsers.length === 2) helpGeneratorParser = require_constructs.longestMatch(commandParsers[0], commandParsers[1]);
501
501
  else helpGeneratorParser = require_constructs.longestMatch(...commandParsers);
502
502
  }
503
+ const reportInvalidHelpCommand = (validationError) => {
504
+ stderr(`Usage: ${indentLines(require_usage.formatUsage(programName, augmentedParser.usage, {
505
+ colors,
506
+ maxWidth: maxWidth == null ? void 0 : maxWidth - 7,
507
+ expandCommands: true
508
+ }), 7)}`);
509
+ const errorMessage = require_message.formatMessage(validationError, {
510
+ colors,
511
+ quotes: !colors
512
+ });
513
+ stderr(`Error: ${errorMessage}`);
514
+ return onError(1);
515
+ };
516
+ if (classified.commands.length > 0) {
517
+ let validationContext = {
518
+ buffer: [...classified.commands],
519
+ optionsTerminated: false,
520
+ state: helpGeneratorParser.initialState,
521
+ usage: helpGeneratorParser.usage
522
+ };
523
+ const processStep = (stepResult) => {
524
+ if (!stepResult.success) return stepResult.error;
525
+ if (stepResult.consumed.length < 1) return require_message.message`Unexpected option or subcommand: ${require_message.optionName(validationContext.buffer[0])}.`;
526
+ validationContext = {
527
+ ...validationContext,
528
+ buffer: stepResult.next.buffer,
529
+ optionsTerminated: stepResult.next.optionsTerminated,
530
+ state: stepResult.next.state,
531
+ usage: stepResult.next.usage ?? validationContext.usage
532
+ };
533
+ return validationContext.buffer.length > 0 ? "continue" : null;
534
+ };
535
+ let validationResult = "continue";
536
+ while (validationResult === "continue") {
537
+ const stepResult = helpGeneratorParser.parse(validationContext);
538
+ if (stepResult instanceof Promise) {
539
+ const asyncValidate = async (result$1) => {
540
+ let res = processStep(result$1);
541
+ while (res === "continue") {
542
+ const next = helpGeneratorParser.parse(validationContext);
543
+ const resolved = next instanceof Promise ? await next : next;
544
+ res = processStep(resolved);
545
+ }
546
+ if (res != null) return reportInvalidHelpCommand(res);
547
+ const docOrPromise$1 = require_parser.getDocPage(helpGeneratorParser, classified.commands);
548
+ return docOrPromise$1 instanceof Promise ? docOrPromise$1.then(displayHelp) : displayHelp(docOrPromise$1);
549
+ };
550
+ return stepResult.then(asyncValidate);
551
+ }
552
+ validationResult = processStep(stepResult);
553
+ }
554
+ if (validationResult != null) return reportInvalidHelpCommand(validationResult);
555
+ }
503
556
  const displayHelp = (doc) => {
504
557
  if (doc != null) {
505
558
  const isMetaCommandHelp = (completionName === "singular" || completionName === "both" ? requestedCommand === "completion" : false) || (completionName === "plural" || completionName === "both" ? requestedCommand === "completions" : false) || requestedCommand === "help" || requestedCommand === "version";
package/dist/facade.js CHANGED
@@ -500,6 +500,59 @@ function runParser(parser, programName, args, options = {}) {
500
500
  else if (commandParsers.length === 2) helpGeneratorParser = longestMatch(commandParsers[0], commandParsers[1]);
501
501
  else helpGeneratorParser = longestMatch(...commandParsers);
502
502
  }
503
+ const reportInvalidHelpCommand = (validationError) => {
504
+ stderr(`Usage: ${indentLines(formatUsage(programName, augmentedParser.usage, {
505
+ colors,
506
+ maxWidth: maxWidth == null ? void 0 : maxWidth - 7,
507
+ expandCommands: true
508
+ }), 7)}`);
509
+ const errorMessage = formatMessage(validationError, {
510
+ colors,
511
+ quotes: !colors
512
+ });
513
+ stderr(`Error: ${errorMessage}`);
514
+ return onError(1);
515
+ };
516
+ if (classified.commands.length > 0) {
517
+ let validationContext = {
518
+ buffer: [...classified.commands],
519
+ optionsTerminated: false,
520
+ state: helpGeneratorParser.initialState,
521
+ usage: helpGeneratorParser.usage
522
+ };
523
+ const processStep = (stepResult) => {
524
+ if (!stepResult.success) return stepResult.error;
525
+ if (stepResult.consumed.length < 1) return message`Unexpected option or subcommand: ${optionName(validationContext.buffer[0])}.`;
526
+ validationContext = {
527
+ ...validationContext,
528
+ buffer: stepResult.next.buffer,
529
+ optionsTerminated: stepResult.next.optionsTerminated,
530
+ state: stepResult.next.state,
531
+ usage: stepResult.next.usage ?? validationContext.usage
532
+ };
533
+ return validationContext.buffer.length > 0 ? "continue" : null;
534
+ };
535
+ let validationResult = "continue";
536
+ while (validationResult === "continue") {
537
+ const stepResult = helpGeneratorParser.parse(validationContext);
538
+ if (stepResult instanceof Promise) {
539
+ const asyncValidate = async (result$1) => {
540
+ let res = processStep(result$1);
541
+ while (res === "continue") {
542
+ const next = helpGeneratorParser.parse(validationContext);
543
+ const resolved = next instanceof Promise ? await next : next;
544
+ res = processStep(resolved);
545
+ }
546
+ if (res != null) return reportInvalidHelpCommand(res);
547
+ const docOrPromise$1 = getDocPage(helpGeneratorParser, classified.commands);
548
+ return docOrPromise$1 instanceof Promise ? docOrPromise$1.then(displayHelp) : displayHelp(docOrPromise$1);
549
+ };
550
+ return stepResult.then(asyncValidate);
551
+ }
552
+ validationResult = processStep(stepResult);
553
+ }
554
+ if (validationResult != null) return reportInvalidHelpCommand(validationResult);
555
+ }
503
556
  const displayHelp = (doc) => {
504
557
  if (doc != null) {
505
558
  const isMetaCommandHelp = (completionName === "singular" || completionName === "both" ? requestedCommand === "completion" : false) || (completionName === "plural" || completionName === "both" ? requestedCommand === "completions" : false) || requestedCommand === "help" || requestedCommand === "version";
package/dist/parser.cjs CHANGED
@@ -356,11 +356,9 @@ function buildDocPage(parser, context, args) {
356
356
  const term = usage[i];
357
357
  if (term.type === "exclusive") {
358
358
  const found = findCommandInExclusive(term, arg);
359
- if (found) {
360
- usage.splice(i, 1, ...found);
361
- i += found.length;
362
- } else i++;
363
- } else i++;
359
+ if (found) usage.splice(i, 1, ...found);
360
+ }
361
+ i++;
364
362
  }
365
363
  return {
366
364
  usage,
package/dist/parser.js CHANGED
@@ -356,11 +356,9 @@ function buildDocPage(parser, context, args) {
356
356
  const term = usage[i];
357
357
  if (term.type === "exclusive") {
358
358
  const found = findCommandInExclusive(term, arg);
359
- if (found) {
360
- usage.splice(i, 1, ...found);
361
- i += found.length;
362
- } else i++;
363
- } else i++;
359
+ if (found) usage.splice(i, 1, ...found);
360
+ }
361
+ i++;
364
362
  }
365
363
  return {
366
364
  usage,
@@ -840,21 +840,23 @@ function command(name, parser, options = {}) {
840
840
  return suggestCommandSync(context, prefix, name, parser, options);
841
841
  },
842
842
  getDocFragments(state, defaultValue) {
843
- if (options.hidden) return {
844
- fragments: [],
845
- description: options.description
846
- };
847
- if (state.kind === "unavailable" || typeof state.state === "undefined") return {
848
- description: options.description,
849
- fragments: [{
850
- type: "entry",
851
- term: {
852
- type: "command",
853
- name
854
- },
855
- description: options.brief ?? options.description
856
- }]
857
- };
843
+ if (state.kind === "unavailable" || typeof state.state === "undefined") {
844
+ if (options.hidden) return {
845
+ fragments: [],
846
+ description: options.description
847
+ };
848
+ return {
849
+ description: options.description,
850
+ fragments: [{
851
+ type: "entry",
852
+ term: {
853
+ type: "command",
854
+ name
855
+ },
856
+ description: options.brief ?? options.description
857
+ }]
858
+ };
859
+ }
858
860
  const innerState = state.state[0] === "parsing" ? {
859
861
  kind: "available",
860
862
  state: state.state[1]
@@ -840,21 +840,23 @@ function command(name, parser, options = {}) {
840
840
  return suggestCommandSync(context, prefix, name, parser, options);
841
841
  },
842
842
  getDocFragments(state, defaultValue) {
843
- if (options.hidden) return {
844
- fragments: [],
845
- description: options.description
846
- };
847
- if (state.kind === "unavailable" || typeof state.state === "undefined") return {
848
- description: options.description,
849
- fragments: [{
850
- type: "entry",
851
- term: {
852
- type: "command",
853
- name
854
- },
855
- description: options.brief ?? options.description
856
- }]
857
- };
843
+ if (state.kind === "unavailable" || typeof state.state === "undefined") {
844
+ if (options.hidden) return {
845
+ fragments: [],
846
+ description: options.description
847
+ };
848
+ return {
849
+ description: options.description,
850
+ fragments: [{
851
+ type: "entry",
852
+ term: {
853
+ type: "command",
854
+ name
855
+ },
856
+ description: options.brief ?? options.description
857
+ }]
858
+ };
859
+ }
858
860
  const innerState = state.state[0] === "parsing" ? {
859
861
  kind: "available",
860
862
  state: state.state[1]
package/dist/usage.cjs CHANGED
@@ -123,6 +123,8 @@ function formatUsage(programName, usage, options = {}) {
123
123
  if (usage.length > 0 && usage.slice(0, -1).every((t) => t.type === "command") && lastTerm.type === "exclusive" && lastTerm.terms.every((t) => t.length > 0 && (t[0].type === "command" || t[0].type === "option" || t[0].type === "argument" || t[0].type === "optional" && t[0].terms.length === 1 && (t[0].terms[0].type === "command" || t[0].terms[0].type === "option" || t[0].terms[0].type === "argument")))) {
124
124
  const lines = [];
125
125
  for (let command of lastTerm.terms) {
126
+ const firstTerm = command[0];
127
+ if (firstTerm?.type === "command" && firstTerm.hidden) continue;
126
128
  if (usage.length > 1) command = [...usage.slice(0, -1), ...command];
127
129
  lines.push(formatUsage(programName, command, options));
128
130
  }
package/dist/usage.js CHANGED
@@ -122,6 +122,8 @@ function formatUsage(programName, usage, options = {}) {
122
122
  if (usage.length > 0 && usage.slice(0, -1).every((t) => t.type === "command") && lastTerm.type === "exclusive" && lastTerm.terms.every((t) => t.length > 0 && (t[0].type === "command" || t[0].type === "option" || t[0].type === "argument" || t[0].type === "optional" && t[0].terms.length === 1 && (t[0].terms[0].type === "command" || t[0].terms[0].type === "option" || t[0].terms[0].type === "argument")))) {
123
123
  const lines = [];
124
124
  for (let command of lastTerm.terms) {
125
+ const firstTerm = command[0];
126
+ if (firstTerm?.type === "command" && firstTerm.hidden) continue;
125
127
  if (usage.length > 1) command = [...usage.slice(0, -1), ...command];
126
128
  lines.push(formatUsage(programName, command, options));
127
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "0.9.1",
3
+ "version": "0.9.3",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",