@optique/core 1.0.0-dev.400 → 1.0.0-dev.401

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/doc.cjs CHANGED
@@ -3,6 +3,28 @@ const require_usage = require('./usage.cjs');
3
3
 
4
4
  //#region src/doc.ts
5
5
  /**
6
+ * Classifies a {@link DocSection} by its content type for use in the
7
+ * default smart sort.
8
+ *
9
+ * @returns `0` for command-only sections, `1` for mixed sections, `2` for
10
+ * option/argument/passthrough-only sections.
11
+ */
12
+ function classifySection(section) {
13
+ const hasCommand = section.entries.some((e) => e.term.type === "command");
14
+ const hasNonCommand = section.entries.some((e) => e.term.type !== "command");
15
+ if (hasCommand && !hasNonCommand) return 0;
16
+ if (hasCommand && hasNonCommand) return 1;
17
+ return 2;
18
+ }
19
+ /**
20
+ * The default section comparator: command-only sections come first, then
21
+ * mixed sections, then option/argument-only sections. Sections with the
22
+ * same score preserve their original relative order (stable sort).
23
+ */
24
+ function defaultSectionOrder(a, b) {
25
+ return classifySection(a) - classifySection(b);
26
+ }
27
+ /**
6
28
  * Formats a documentation page into a human-readable string.
7
29
  *
8
30
  * This function takes a structured {@link DocPage} and converts it into
@@ -64,7 +86,16 @@ function formatDocPage(programName, page, options = {}) {
64
86
  });
65
87
  output += "\n";
66
88
  }
67
- const sections = page.sections.toSorted((a, b) => a.title == null && b.title == null ? 0 : a.title == null ? -1 : 1);
89
+ const comparator = options.sectionOrder ?? defaultSectionOrder;
90
+ const sections = page.sections.map((s, i) => ({
91
+ section: s,
92
+ index: i
93
+ })).toSorted((a, b) => {
94
+ const cmp = comparator(a.section, b.section);
95
+ if (cmp !== 0) return cmp;
96
+ const titleCmp = (a.section.title == null ? 0 : 1) - (b.section.title == null ? 0 : 1);
97
+ return titleCmp !== 0 ? titleCmp : a.index - b.index;
98
+ }).map(({ section }) => section);
68
99
  for (const section of sections) {
69
100
  if (section.entries.length < 1) continue;
70
101
  output += "\n";
package/dist/doc.d.cts CHANGED
@@ -228,6 +228,28 @@ interface DocPageFormatOptions {
228
228
  * ```
229
229
  */
230
230
  showChoices?: boolean | ShowChoicesOptions;
231
+ /**
232
+ * A custom comparator function to control the order of sections in the
233
+ * help output. When provided, it is used instead of the default smart
234
+ * sort (command-only sections first, then mixed, then option/argument-only
235
+ * sections). Sections that compare equal (return `0`) preserve their
236
+ * original relative order (stable sort).
237
+ *
238
+ * @param a The first section to compare.
239
+ * @param b The second section to compare.
240
+ * @returns A negative number if `a` should appear before `b`, a positive
241
+ * number if `a` should appear after `b`, or `0` if they are equal.
242
+ * @since 1.0.0
243
+ *
244
+ * @example
245
+ * ```typescript
246
+ * // Sort sections alphabetically by title
247
+ * {
248
+ * sectionOrder: (a, b) => (a.title ?? "").localeCompare(b.title ?? "")
249
+ * }
250
+ * ```
251
+ */
252
+ sectionOrder?: (a: DocSection, b: DocSection) => number;
231
253
  }
232
254
  /**
233
255
  * Formats a documentation page into a human-readable string.
package/dist/doc.d.ts CHANGED
@@ -228,6 +228,28 @@ interface DocPageFormatOptions {
228
228
  * ```
229
229
  */
230
230
  showChoices?: boolean | ShowChoicesOptions;
231
+ /**
232
+ * A custom comparator function to control the order of sections in the
233
+ * help output. When provided, it is used instead of the default smart
234
+ * sort (command-only sections first, then mixed, then option/argument-only
235
+ * sections). Sections that compare equal (return `0`) preserve their
236
+ * original relative order (stable sort).
237
+ *
238
+ * @param a The first section to compare.
239
+ * @param b The second section to compare.
240
+ * @returns A negative number if `a` should appear before `b`, a positive
241
+ * number if `a` should appear after `b`, or `0` if they are equal.
242
+ * @since 1.0.0
243
+ *
244
+ * @example
245
+ * ```typescript
246
+ * // Sort sections alphabetically by title
247
+ * {
248
+ * sectionOrder: (a, b) => (a.title ?? "").localeCompare(b.title ?? "")
249
+ * }
250
+ * ```
251
+ */
252
+ sectionOrder?: (a: DocSection, b: DocSection) => number;
231
253
  }
232
254
  /**
233
255
  * Formats a documentation page into a human-readable string.
package/dist/doc.js CHANGED
@@ -3,6 +3,28 @@ import { formatUsage, formatUsageTerm } from "./usage.js";
3
3
 
4
4
  //#region src/doc.ts
5
5
  /**
6
+ * Classifies a {@link DocSection} by its content type for use in the
7
+ * default smart sort.
8
+ *
9
+ * @returns `0` for command-only sections, `1` for mixed sections, `2` for
10
+ * option/argument/passthrough-only sections.
11
+ */
12
+ function classifySection(section) {
13
+ const hasCommand = section.entries.some((e) => e.term.type === "command");
14
+ const hasNonCommand = section.entries.some((e) => e.term.type !== "command");
15
+ if (hasCommand && !hasNonCommand) return 0;
16
+ if (hasCommand && hasNonCommand) return 1;
17
+ return 2;
18
+ }
19
+ /**
20
+ * The default section comparator: command-only sections come first, then
21
+ * mixed sections, then option/argument-only sections. Sections with the
22
+ * same score preserve their original relative order (stable sort).
23
+ */
24
+ function defaultSectionOrder(a, b) {
25
+ return classifySection(a) - classifySection(b);
26
+ }
27
+ /**
6
28
  * Formats a documentation page into a human-readable string.
7
29
  *
8
30
  * This function takes a structured {@link DocPage} and converts it into
@@ -64,7 +86,16 @@ function formatDocPage(programName, page, options = {}) {
64
86
  });
65
87
  output += "\n";
66
88
  }
67
- const sections = page.sections.toSorted((a, b) => a.title == null && b.title == null ? 0 : a.title == null ? -1 : 1);
89
+ const comparator = options.sectionOrder ?? defaultSectionOrder;
90
+ const sections = page.sections.map((s, i) => ({
91
+ section: s,
92
+ index: i
93
+ })).toSorted((a, b) => {
94
+ const cmp = comparator(a.section, b.section);
95
+ if (cmp !== 0) return cmp;
96
+ const titleCmp = (a.section.title == null ? 0 : 1) - (b.section.title == null ? 0 : 1);
97
+ return titleCmp !== 0 ? titleCmp : a.index - b.index;
98
+ }).map(({ section }) => section);
68
99
  for (const section of sections) {
69
100
  if (section.entries.length < 1) continue;
70
101
  output += "\n";
package/dist/facade.cjs CHANGED
@@ -369,7 +369,7 @@ function classifyResult(result, args) {
369
369
  * Handles shell completion requests.
370
370
  * @since 0.6.0
371
371
  */
372
- function handleCompletion(completionArgs, programName, parser, completionParser, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName) {
372
+ function handleCompletion(completionArgs, programName, parser, completionParser, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName, sectionOrder) {
373
373
  const shellName = completionArgs[0] || "";
374
374
  const args = completionArgs.slice(1);
375
375
  const callOnError = (code) => {
@@ -392,7 +392,8 @@ function handleCompletion(completionArgs, programName, parser, completionParser,
392
392
  const doc = require_parser.getDocPage(completionParser, ["completion"]);
393
393
  if (doc) stderr(require_doc.formatDocPage(programName, doc, {
394
394
  colors,
395
- maxWidth
395
+ maxWidth,
396
+ sectionOrder
396
397
  }));
397
398
  }
398
399
  if (parser.$mode === "async") return Promise.resolve(callOnError(1));
@@ -457,7 +458,7 @@ function runParser(parserOrProgram, programNameOrArgs, argsOrOptions, optionsPar
457
458
  args = argsOrOptions;
458
459
  options = optionsParam ?? {};
459
460
  }
460
- const { colors, maxWidth, showDefault, showChoices, aboveError = "usage", onError = () => {
461
+ const { colors, maxWidth, showDefault, showChoices, sectionOrder, aboveError = "usage", onError = () => {
461
462
  throw new RunParserError("Failed to parse command line arguments.");
462
463
  }, stderr = console.error, stdout = console.log, brief, description, examples, author, bugs, footer } = options;
463
464
  const helpMode = options.help?.mode ?? "option";
@@ -500,7 +501,7 @@ function runParser(parserOrProgram, programNameOrArgs, argsOrOptions, optionsPar
500
501
  } : createCompletionParser(completion, programName, availableShells, completionName, completionHelpVisibility);
501
502
  if (options.completion) {
502
503
  const hasHelpOption = args.includes("--help");
503
- if ((completionMode === "command" || completionMode === "both") && args.length >= 1 && ((completionName === "singular" || completionName === "both" ? args[0] === "completion" : false) || (completionName === "plural" || completionName === "both" ? args[0] === "completions" : false)) && !hasHelpOption) return handleCompletion(args.slice(1), programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName);
504
+ if ((completionMode === "command" || completionMode === "both") && args.length >= 1 && ((completionName === "singular" || completionName === "both" ? args[0] === "completion" : false) || (completionName === "plural" || completionName === "both" ? args[0] === "completions" : false)) && !hasHelpOption) return handleCompletion(args.slice(1), programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName, sectionOrder);
504
505
  if (completionMode === "option" || completionMode === "both") for (let i = 0; i < args.length; i++) {
505
506
  const arg = args[i];
506
507
  const singularMatch = completionName === "singular" || completionName === "both" ? arg.startsWith("--completion=") : false;
@@ -508,14 +509,14 @@ function runParser(parserOrProgram, programNameOrArgs, argsOrOptions, optionsPar
508
509
  if (singularMatch || pluralMatch) {
509
510
  const shell = arg.slice(arg.indexOf("=") + 1);
510
511
  const completionArgs = args.slice(i + 1);
511
- return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName);
512
+ return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName, sectionOrder);
512
513
  } else {
513
514
  const singularMatchExact = completionName === "singular" || completionName === "both" ? arg === "--completion" : false;
514
515
  const pluralMatchExact = completionName === "plural" || completionName === "both" ? arg === "--completions" : false;
515
516
  if (singularMatchExact || pluralMatchExact) {
516
517
  const shell = i + 1 < args.length ? args[i + 1] : "";
517
518
  const completionArgs = i + 1 < args.length ? args.slice(i + 2) : [];
518
- return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName);
519
+ return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName, sectionOrder);
519
520
  }
520
521
  }
521
522
  }
@@ -636,7 +637,8 @@ function runParser(parserOrProgram, programNameOrArgs, argsOrOptions, optionsPar
636
637
  colors,
637
638
  maxWidth,
638
639
  showDefault,
639
- showChoices
640
+ showChoices,
641
+ sectionOrder
640
642
  }));
641
643
  }
642
644
  try {
package/dist/facade.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Message } from "./message.cjs";
2
- import { ShowChoicesOptions, ShowDefaultOptions } from "./doc.cjs";
2
+ import { DocSection, ShowChoicesOptions, ShowDefaultOptions } from "./doc.cjs";
3
3
  import { InferMode, InferValue, Mode, ModeValue, Parser } from "./parser.cjs";
4
4
  import { ShellCompletion } from "./completion.cjs";
5
5
  import { ParserValuePlaceholder, SourceContext } from "./context.cjs";
@@ -148,6 +148,20 @@ interface RunOptions<THelp, TError> {
148
148
  * @since 0.10.0
149
149
  */
150
150
  readonly showChoices?: boolean | ShowChoicesOptions;
151
+ /**
152
+ * A custom comparator function to control the order of sections in the
153
+ * help output. When provided, it is used instead of the default smart
154
+ * sort (command-only sections first, then mixed, then option/argument-only
155
+ * sections). Sections that compare equal (return `0`) preserve their
156
+ * original relative order.
157
+ *
158
+ * @param a The first section to compare.
159
+ * @param b The second section to compare.
160
+ * @returns A negative number if `a` should appear before `b`, a positive
161
+ * number if `a` should appear after `b`, or `0` if they are equal.
162
+ * @since 1.0.0
163
+ */
164
+ readonly sectionOrder?: (a: DocSection, b: DocSection) => number;
151
165
  /**
152
166
  * Help configuration. When provided, enables help functionality.
153
167
  */
package/dist/facade.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Message } from "./message.js";
2
- import { ShowChoicesOptions, ShowDefaultOptions } from "./doc.js";
2
+ import { DocSection, ShowChoicesOptions, ShowDefaultOptions } from "./doc.js";
3
3
  import { InferMode, InferValue, Mode, ModeValue, Parser } from "./parser.js";
4
4
  import { ShellCompletion } from "./completion.js";
5
5
  import { ParserValuePlaceholder, SourceContext } from "./context.js";
@@ -148,6 +148,20 @@ interface RunOptions<THelp, TError> {
148
148
  * @since 0.10.0
149
149
  */
150
150
  readonly showChoices?: boolean | ShowChoicesOptions;
151
+ /**
152
+ * A custom comparator function to control the order of sections in the
153
+ * help output. When provided, it is used instead of the default smart
154
+ * sort (command-only sections first, then mixed, then option/argument-only
155
+ * sections). Sections that compare equal (return `0`) preserve their
156
+ * original relative order.
157
+ *
158
+ * @param a The first section to compare.
159
+ * @param b The second section to compare.
160
+ * @returns A negative number if `a` should appear before `b`, a positive
161
+ * number if `a` should appear after `b`, or `0` if they are equal.
162
+ * @since 1.0.0
163
+ */
164
+ readonly sectionOrder?: (a: DocSection, b: DocSection) => number;
151
165
  /**
152
166
  * Help configuration. When provided, enables help functionality.
153
167
  */
package/dist/facade.js CHANGED
@@ -369,7 +369,7 @@ function classifyResult(result, args) {
369
369
  * Handles shell completion requests.
370
370
  * @since 0.6.0
371
371
  */
372
- function handleCompletion(completionArgs, programName, parser, completionParser, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName) {
372
+ function handleCompletion(completionArgs, programName, parser, completionParser, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName, sectionOrder) {
373
373
  const shellName = completionArgs[0] || "";
374
374
  const args = completionArgs.slice(1);
375
375
  const callOnError = (code) => {
@@ -392,7 +392,8 @@ function handleCompletion(completionArgs, programName, parser, completionParser,
392
392
  const doc = getDocPage(completionParser, ["completion"]);
393
393
  if (doc) stderr(formatDocPage(programName, doc, {
394
394
  colors,
395
- maxWidth
395
+ maxWidth,
396
+ sectionOrder
396
397
  }));
397
398
  }
398
399
  if (parser.$mode === "async") return Promise.resolve(callOnError(1));
@@ -457,7 +458,7 @@ function runParser(parserOrProgram, programNameOrArgs, argsOrOptions, optionsPar
457
458
  args = argsOrOptions;
458
459
  options = optionsParam ?? {};
459
460
  }
460
- const { colors, maxWidth, showDefault, showChoices, aboveError = "usage", onError = () => {
461
+ const { colors, maxWidth, showDefault, showChoices, sectionOrder, aboveError = "usage", onError = () => {
461
462
  throw new RunParserError("Failed to parse command line arguments.");
462
463
  }, stderr = console.error, stdout = console.log, brief, description, examples, author, bugs, footer } = options;
463
464
  const helpMode = options.help?.mode ?? "option";
@@ -500,7 +501,7 @@ function runParser(parserOrProgram, programNameOrArgs, argsOrOptions, optionsPar
500
501
  } : createCompletionParser(completion, programName, availableShells, completionName, completionHelpVisibility);
501
502
  if (options.completion) {
502
503
  const hasHelpOption = args.includes("--help");
503
- if ((completionMode === "command" || completionMode === "both") && args.length >= 1 && ((completionName === "singular" || completionName === "both" ? args[0] === "completion" : false) || (completionName === "plural" || completionName === "both" ? args[0] === "completions" : false)) && !hasHelpOption) return handleCompletion(args.slice(1), programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName);
504
+ if ((completionMode === "command" || completionMode === "both") && args.length >= 1 && ((completionName === "singular" || completionName === "both" ? args[0] === "completion" : false) || (completionName === "plural" || completionName === "both" ? args[0] === "completions" : false)) && !hasHelpOption) return handleCompletion(args.slice(1), programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName, sectionOrder);
504
505
  if (completionMode === "option" || completionMode === "both") for (let i = 0; i < args.length; i++) {
505
506
  const arg = args[i];
506
507
  const singularMatch = completionName === "singular" || completionName === "both" ? arg.startsWith("--completion=") : false;
@@ -508,14 +509,14 @@ function runParser(parserOrProgram, programNameOrArgs, argsOrOptions, optionsPar
508
509
  if (singularMatch || pluralMatch) {
509
510
  const shell = arg.slice(arg.indexOf("=") + 1);
510
511
  const completionArgs = args.slice(i + 1);
511
- return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName);
512
+ return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName, sectionOrder);
512
513
  } else {
513
514
  const singularMatchExact = completionName === "singular" || completionName === "both" ? arg === "--completion" : false;
514
515
  const pluralMatchExact = completionName === "plural" || completionName === "both" ? arg === "--completions" : false;
515
516
  if (singularMatchExact || pluralMatchExact) {
516
517
  const shell = i + 1 < args.length ? args[i + 1] : "";
517
518
  const completionArgs = i + 1 < args.length ? args.slice(i + 2) : [];
518
- return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName);
519
+ return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName, sectionOrder);
519
520
  }
520
521
  }
521
522
  }
@@ -636,7 +637,8 @@ function runParser(parserOrProgram, programNameOrArgs, argsOrOptions, optionsPar
636
637
  colors,
637
638
  maxWidth,
638
639
  showDefault,
639
- showChoices
640
+ showChoices,
641
+ sectionOrder
640
642
  }));
641
643
  }
642
644
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.0-dev.400+a38bcb54",
3
+ "version": "1.0.0-dev.401+5769648d",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",