@kjanat/dreamcli 3.0.0-rc.1 → 3.0.0-rc.2

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.
Files changed (136) hide show
  1. package/CHANGELOG.md +26 -1
  2. package/dist/core/cli/dispatch.mjs +214 -1
  3. package/dist/core/cli/help-links.mjs +41 -1
  4. package/dist/core/cli/index.mjs +901 -1
  5. package/dist/core/cli/planner.mjs +300 -1
  6. package/dist/core/cli/plugin.mjs +41 -1
  7. package/dist/core/cli/propagate.mjs +57 -1
  8. package/dist/core/cli/root-help.mjs +208 -4
  9. package/dist/core/cli/root-surface.mjs +27 -1
  10. package/dist/core/cli/runtime-preflight.mjs +168 -1
  11. package/dist/core/completion/index.mjs +90 -1
  12. package/dist/core/completion/shells/bash.mjs +304 -3
  13. package/dist/core/completion/shells/fish.mjs +159 -2
  14. package/dist/core/completion/shells/powershell.mjs +353 -2
  15. package/dist/core/completion/shells/shared.mjs +154 -1
  16. package/dist/core/completion/shells/zsh.mjs +244 -2
  17. package/dist/core/config/index.mjs +246 -1
  18. package/dist/core/config/package-json.mjs +385 -3
  19. package/dist/core/errors/index.mjs +87 -1
  20. package/dist/core/execution/index.mjs +189 -1
  21. package/dist/core/help/ansi.mjs +122 -2
  22. package/dist/core/help/index.mjs +329 -7
  23. package/dist/core/json-schema/index.mjs +487 -9
  24. package/dist/core/json-schema/meta-descriptions.generated.mjs +93 -28
  25. package/dist/core/output/activity.mjs +452 -1
  26. package/dist/core/output/bind.mjs +56 -1
  27. package/dist/core/output/contracts.mjs +57 -1
  28. package/dist/core/output/display-value.mjs +32 -1
  29. package/dist/core/output/index.d.mts +21 -0
  30. package/dist/core/output/index.mjs +442 -2
  31. package/dist/core/output/renderers.mjs +46 -1
  32. package/dist/core/output/writer.mjs +12 -1
  33. package/dist/core/parse/index.mjs +555 -1
  34. package/dist/core/prompt/index.d.mts +0 -1
  35. package/dist/core/prompt/index.mjs +348 -6
  36. package/dist/core/resolve/args.mjs +105 -1
  37. package/dist/core/resolve/coerce.mjs +279 -1
  38. package/dist/core/resolve/config.mjs +19 -1
  39. package/dist/core/resolve/errors.mjs +140 -2
  40. package/dist/core/resolve/flags.mjs +196 -1
  41. package/dist/core/resolve/index.mjs +85 -1
  42. package/dist/core/resolve/property.d.mts +0 -1
  43. package/dist/core/resolve/property.mjs +45 -1
  44. package/dist/core/schema/arg.mjs +463 -1
  45. package/dist/core/schema/command.d.mts +18 -0
  46. package/dist/core/schema/command.mjs +712 -1
  47. package/dist/core/schema/flag.mjs +453 -1
  48. package/dist/core/schema/middleware.mjs +41 -1
  49. package/dist/core/schema/number-constraints.mjs +60 -1
  50. package/dist/core/schema-dsl/runtime.mjs +271 -2
  51. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  52. package/dist/core/testkit/index.mjs +83 -1
  53. package/dist/index.d.mts +2 -1
  54. package/dist/index.mjs +22 -1
  55. package/dist/runtime/adapter.mjs +82 -1
  56. package/dist/runtime/auto.mjs +46 -1
  57. package/dist/runtime/bun.mjs +28 -1
  58. package/dist/runtime/deno.mjs +217 -2
  59. package/dist/runtime/detect.mjs +45 -1
  60. package/dist/runtime/node.mjs +169 -2
  61. package/dist/runtime/paths.mjs +48 -1
  62. package/dist/runtime.mjs +7 -1
  63. package/dist/strings.mjs +22 -1
  64. package/dist/testkit.mjs +5 -1
  65. package/dist/version.mjs +7 -1
  66. package/package.json +17 -26
  67. package/src/core/cli/dispatch.ts +0 -355
  68. package/src/core/cli/help-links.ts +0 -82
  69. package/src/core/cli/index.ts +0 -1614
  70. package/src/core/cli/planner.ts +0 -656
  71. package/src/core/cli/plugin.ts +0 -117
  72. package/src/core/cli/propagate.ts +0 -97
  73. package/src/core/cli/root-help.ts +0 -342
  74. package/src/core/cli/root-surface.ts +0 -87
  75. package/src/core/cli/runtime-preflight.ts +0 -456
  76. package/src/core/completion/index.ts +0 -138
  77. package/src/core/completion/shells/bash.ts +0 -459
  78. package/src/core/completion/shells/fish.ts +0 -259
  79. package/src/core/completion/shells/powershell.ts +0 -439
  80. package/src/core/completion/shells/shared.ts +0 -310
  81. package/src/core/completion/shells/zsh.ts +0 -381
  82. package/src/core/config/index.ts +0 -417
  83. package/src/core/config/package-json.ts +0 -560
  84. package/src/core/errors/index.ts +0 -193
  85. package/src/core/execution/index.ts +0 -265
  86. package/src/core/help/ansi.ts +0 -144
  87. package/src/core/help/index.ts +0 -563
  88. package/src/core/json-schema/index.ts +0 -910
  89. package/src/core/json-schema/meta-descriptions.generated.ts +0 -202
  90. package/src/core/output/activity.ts +0 -575
  91. package/src/core/output/bind.ts +0 -59
  92. package/src/core/output/contracts.ts +0 -194
  93. package/src/core/output/display-value.ts +0 -48
  94. package/src/core/output/index.ts +0 -704
  95. package/src/core/output/renderers.ts +0 -125
  96. package/src/core/output/writer.ts +0 -46
  97. package/src/core/parse/index.ts +0 -742
  98. package/src/core/prompt/index.ts +0 -623
  99. package/src/core/resolve/args.ts +0 -144
  100. package/src/core/resolve/coerce.ts +0 -500
  101. package/src/core/resolve/config.ts +0 -26
  102. package/src/core/resolve/contracts.ts +0 -135
  103. package/src/core/resolve/errors.ts +0 -227
  104. package/src/core/resolve/flags.ts +0 -269
  105. package/src/core/resolve/index.ts +0 -108
  106. package/src/core/resolve/property.ts +0 -99
  107. package/src/core/schema/activity.ts +0 -240
  108. package/src/core/schema/arg.ts +0 -841
  109. package/src/core/schema/command.ts +0 -1500
  110. package/src/core/schema/flag.ts +0 -917
  111. package/src/core/schema/index.ts +0 -96
  112. package/src/core/schema/middleware.ts +0 -171
  113. package/src/core/schema/number-constraints.ts +0 -153
  114. package/src/core/schema/prompt.ts +0 -171
  115. package/src/core/schema/run.ts +0 -198
  116. package/src/core/schema-dsl/index.ts +0 -109
  117. package/src/core/schema-dsl/parse.ts +0 -262
  118. package/src/core/schema-dsl/runtime.ts +0 -609
  119. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  120. package/src/core/testkit/index.ts +0 -147
  121. package/src/index.ts +0 -181
  122. package/src/runtime/adapter.ts +0 -303
  123. package/src/runtime/auto.ts +0 -86
  124. package/src/runtime/bun.ts +0 -48
  125. package/src/runtime/deno.ts +0 -367
  126. package/src/runtime/detect.ts +0 -105
  127. package/src/runtime/index.ts +0 -21
  128. package/src/runtime/node-builtins.d.ts +0 -27
  129. package/src/runtime/node.ts +0 -289
  130. package/src/runtime/paths.ts +0 -64
  131. package/src/runtime/support.ts +0 -77
  132. package/src/runtime.ts +0 -31
  133. package/src/schema.ts +0 -16
  134. package/src/strings.ts +0 -34
  135. package/src/testkit.ts +0 -25
  136. package/src/version.ts +0 -20
@@ -1,563 +0,0 @@
1
- /**
2
- * Auto-generated help text from command schemas.
3
- *
4
- * Renders usage line, description, positional args, flags table, and
5
- * examples from a {@linkcode CommandSchema}. Designed for TTY output with
6
- * configurable width.
7
- *
8
- * @module dreamcli/core/help
9
- */
10
-
11
- import { formatDisplayValue } from '#internals/core/output/display-value.ts';
12
- import { getFlagAliasNames } from '#internals/core/schema/flag.ts';
13
- import type {
14
- ArgSchema,
15
- CommandArgEntry,
16
- CommandExample,
17
- CommandSchema,
18
- FlagSchema,
19
- } from '#internals/core/schema/index.ts';
20
- import { padEnd, wrapText } from './ansi.ts';
21
-
22
- // --- Configuration
23
-
24
- /** Options for customising help output. */
25
- interface HelpOptions {
26
- /** Maximum line width (columns). Defaults to 80. */
27
- readonly width?: number;
28
- /** Binary/program name shown in the usage line. Defaults to command name. */
29
- readonly binName?: string;
30
- /**
31
- * Emit OSC 8 hyperlinks where link metadata is available (currently the
32
- * root-help header name/version configured via `CLIBuilder.links()`).
33
- * Defaults to `false`; `CLIBuilder.execute()`/`.run()` enable it
34
- * automatically when stdout is a TTY.
35
- */
36
- readonly hyperlinks?: boolean;
37
- /**
38
- * Render the default command's arguments and flags inline in root help.
39
- *
40
- * Only affects root-level help. When `false`, root help lists commands and
41
- * defers default-command details to `<bin> --help`-style hints.
42
- *
43
- * @defaultValue `true`
44
- */
45
- readonly inlineDefault?: boolean;
46
- /**
47
- * List the default command in the root `Commands:` table.
48
- *
49
- * By default the default command is treated as the root surface and omitted
50
- * from the command list (its args/flags render inline instead).
51
- *
52
- * @defaultValue `false`
53
- */
54
- readonly showDefaultInCommands?: boolean;
55
- /**
56
- * Show the `Run '<bin> <command> --help' for more information.` footer.
57
- *
58
- * Defaults to showing the hint only when visible subcommands exist; set
59
- * explicitly to force it on or off.
60
- */
61
- readonly footer?: boolean;
62
- /** @internal Whether this usage line is being rendered as merged root/default help. */
63
- readonly isDefaultHelp?: boolean;
64
- }
65
-
66
- /** Resolved help options with defaults applied. */
67
- interface ResolvedHelpOptions {
68
- readonly width: number;
69
- readonly binName: string | undefined;
70
- readonly isDefaultHelp: boolean;
71
- }
72
-
73
- const DEFAULT_WIDTH = 80;
74
-
75
- /**
76
- * Apply defaults to optional {@link HelpOptions}.
77
- *
78
- * @param options - User-supplied help options, or `undefined` for all defaults.
79
- * @returns Fully resolved options with defaults applied.
80
- */
81
- function resolveOptions(options?: HelpOptions): ResolvedHelpOptions {
82
- return {
83
- width: options?.width ?? DEFAULT_WIDTH,
84
- binName: options?.binName,
85
- isDefaultHelp: options?.isDefaultHelp ?? false,
86
- };
87
- }
88
-
89
- // --- Deprecation formatting
90
-
91
- /**
92
- * Format a deprecation annotation for help text.
93
- *
94
- * @param deprecated - `true` for a generic marker, or a string explaining the deprecation.
95
- * @returns Bracketed deprecation label, e.g. `[deprecated]` or `[deprecated: use --foo]`.
96
- */
97
- function formatDeprecated(deprecated: string | true): string {
98
- return typeof deprecated === 'string' ? `[deprecated: ${deprecated}]` : '[deprecated]';
99
- }
100
-
101
- /** Format a default value for help output, preserving nullish sentinels. */
102
- function formatHelpDefaultValue(value: unknown): string {
103
- if (value === undefined) return 'undefined';
104
- if (value === null) return 'null';
105
- return formatDisplayValue(value);
106
- }
107
-
108
- // --- Flag formatting
109
-
110
- /** Formatted flag entry for the flags table. @internal */
111
- interface FlagEntry {
112
- readonly left: string;
113
- readonly description: string;
114
- }
115
-
116
- /**
117
- * Format a flag's left column: `-a, --name <type>`.
118
- *
119
- * @param name - Long flag name (without `--` prefix).
120
- * @param schema - The {@link FlagSchema} describing the flag.
121
- * @returns Formatted left-column string for the flags table.
122
- */
123
- function formatFlagLeft(name: string, schema: FlagSchema): string {
124
- const visibleShortAliases = getFlagAliasNames(schema, { kind: 'short' });
125
- const visibleLongAliases = getFlagAliasNames(schema, { kind: 'long' });
126
- const forms = [
127
- ...visibleShortAliases.map((alias) => `-${alias}`),
128
- `--${name}`,
129
- ...visibleLongAliases.map((alias) => `--${alias}`),
130
- ];
131
-
132
- // Value placeholder (skip for boolean)
133
- if (schema.kind !== 'boolean') {
134
- return `${forms.join(', ')} ${formatValueHint(schema)}`;
135
- }
136
-
137
- return forms.join(', ');
138
- }
139
-
140
- /**
141
- * Produce a type hint like `<string>`, `<number>`, `<us|eu|ap>`.
142
- *
143
- * @param schema - The {@link FlagSchema} whose value type determines the hint.
144
- * @returns Angle-bracketed hint string, or empty string for booleans.
145
- */
146
- function formatValueHint(schema: FlagSchema): string {
147
- switch (schema.kind) {
148
- case 'string':
149
- return '<string>';
150
- case 'number':
151
- return '<number>';
152
- case 'enum': {
153
- const vals = schema.enumValues ?? [];
154
- return `<${vals.join('|')}>`;
155
- }
156
- case 'array': {
157
- const elemHint =
158
- schema.elementSchema !== undefined
159
- ? formatValueHint(schema.elementSchema).slice(1, -1) // strip < >
160
- : 'value';
161
- return `<${elemHint}>...`;
162
- }
163
- case 'boolean':
164
- return '';
165
- case 'custom':
166
- return '<value>';
167
- }
168
- }
169
-
170
- /**
171
- * Build description with env/config/prompt/default/required/deprecated annotations.
172
- *
173
- * @param schema - The {@link FlagSchema} to describe.
174
- * @returns Concatenated description string with metadata annotations.
175
- */
176
- function formatFlagDescription(schema: FlagSchema): string {
177
- const parts: string[] = [];
178
-
179
- if (schema.description !== undefined) {
180
- parts.push(schema.description);
181
- }
182
-
183
- // Deprecation annotation — prominent, before other metadata
184
- if (schema.deprecated !== undefined) {
185
- parts.push(formatDeprecated(schema.deprecated));
186
- }
187
-
188
- // Resolution source annotations — show users where values can come from
189
- if (schema.envVar !== undefined) {
190
- parts.push(`[env: ${schema.envVar}]`);
191
- }
192
- if (schema.configPath !== undefined) {
193
- parts.push(`[config: ${schema.configPath}]`);
194
- }
195
- if (schema.prompt !== undefined) {
196
- parts.push('[prompt]');
197
- }
198
-
199
- if (schema.presence === 'required') {
200
- parts.push('[required]');
201
- } else if (schema.presence === 'defaulted' && schema.kind !== 'boolean') {
202
- // Don't show "(default: false)" for boolean — it's obvious
203
- parts.push(`(default: ${formatHelpDefaultValue(schema.defaultValue)})`);
204
- }
205
-
206
- return parts.join(' ');
207
- }
208
-
209
- /**
210
- * Build the flag entries sorted: short-aliased first, then alphabetical.
211
- *
212
- * @param flags - Map of flag names to {@link FlagSchema} definitions.
213
- * @returns Sorted array of {@link FlagEntry} objects for the flags table.
214
- */
215
- function buildFlagEntries(flags: Readonly<Record<string, FlagSchema>>): readonly FlagEntry[] {
216
- const names = Object.keys(flags);
217
- if (names.length === 0) return [];
218
-
219
- // Sort: flags with short aliases first, then alphabetically by name
220
- const sorted = [...names].sort((a, b) => {
221
- const aSchema = flags[a];
222
- const bSchema = flags[b];
223
- if (aSchema === undefined || bSchema === undefined) return 0;
224
- const aHasShort = getFlagAliasNames(aSchema, { kind: 'short' }).length > 0;
225
- const bHasShort = getFlagAliasNames(bSchema, { kind: 'short' }).length > 0;
226
- if (aHasShort && !bHasShort) return -1;
227
- if (!aHasShort && bHasShort) return 1;
228
- return a.localeCompare(b);
229
- });
230
-
231
- const entries: FlagEntry[] = [];
232
- for (const name of sorted) {
233
- const schema = flags[name];
234
- if (schema === undefined) continue;
235
- entries.push({
236
- left: formatFlagLeft(name, schema),
237
- description: formatFlagDescription(schema),
238
- });
239
- }
240
- return entries;
241
- }
242
-
243
- // --- Arg formatting
244
-
245
- /**
246
- * Format a positional arg for the usage line.
247
- *
248
- * @param entry - The {@link CommandArgEntry} containing name and schema.
249
- * @returns Bracketed arg token, e.g. `<file>` or `[output]...`.
250
- */
251
- function formatArgUsage(entry: CommandArgEntry): string {
252
- const { name, schema } = entry;
253
- const label =
254
- schema.kind === 'enum' && schema.enumValues !== undefined ? schema.enumValues.join('|') : name;
255
- const variadicSuffix = schema.variadic ? '...' : '';
256
- if (schema.presence === 'required') {
257
- return `<${label}>${variadicSuffix}`;
258
- }
259
- return `[${label}]${variadicSuffix}`;
260
- }
261
-
262
- /**
263
- * Format arg description with annotations.
264
- *
265
- * @param schema - The {@link ArgSchema} to describe.
266
- * @returns Concatenated description string with metadata annotations.
267
- */
268
- function formatArgDescription(schema: ArgSchema): string {
269
- const parts: string[] = [];
270
-
271
- if (schema.description !== undefined) {
272
- parts.push(schema.description);
273
- }
274
-
275
- if (schema.deprecated !== undefined) {
276
- parts.push(formatDeprecated(schema.deprecated));
277
- }
278
-
279
- if (schema.envVar !== undefined) {
280
- parts.push(`[env: ${schema.envVar}]`);
281
- }
282
-
283
- if (schema.presence === 'defaulted') {
284
- parts.push(`(default: ${formatHelpDefaultValue(schema.defaultValue)})`);
285
- }
286
-
287
- return parts.join(' ');
288
- }
289
-
290
- // --- Main generator
291
-
292
- /**
293
- * Build the ordered help sections without joining them.
294
- *
295
- * @internal
296
- * @param schema - The {@link CommandSchema} to render.
297
- * @param options - Optional {@link HelpOptions} for width/bin name.
298
- * @returns Array of section strings (usage, description, commands, args, flags, examples).
299
- */
300
- function formatHelpSections(schema: CommandSchema, options?: HelpOptions): readonly string[] {
301
- const opts = resolveOptions(options);
302
- const sections: string[] = [];
303
-
304
- // ---- Usage line ---------------------------------------------------------
305
- sections.push(formatUsageLine(schema, opts));
306
-
307
- // ---- Description --------------------------------------------------------
308
- if (schema.description !== undefined) {
309
- sections.push(schema.description);
310
- }
311
-
312
- // ---- Commands (subcommands) ---------------------------------------------
313
- const visibleCommands = schema.commands.filter((c) => !c.hidden);
314
- if (visibleCommands.length > 0) {
315
- sections.push(formatCommandsSection(visibleCommands, opts));
316
- }
317
-
318
- // ---- Arguments ----------------------------------------------------------
319
- if (schema.args.length > 0) {
320
- sections.push(formatArgsSection(schema.args, opts));
321
- }
322
-
323
- // ---- Flags --------------------------------------------------------------
324
- const flagNames = Object.keys(schema.flags);
325
- if (flagNames.length > 0) {
326
- sections.push(formatFlagsSection(schema.flags, opts));
327
- }
328
-
329
- // ---- Examples -----------------------------------------------------------
330
- if (schema.examples.length > 0) {
331
- sections.push(formatExamplesSection(schema.examples));
332
- }
333
-
334
- return sections;
335
- }
336
-
337
- /**
338
- * Generate help text from a command schema.
339
- *
340
- * Low-level formatter: most applications reach this through `--help`,
341
- * `help <command>`, or root help rendering in `CLIBuilder`. Call
342
- * `formatHelp()` directly when embedding DreamCLI help text into custom UIs,
343
- * tests, or generated docs.
344
- *
345
- * Sections rendered (in order):
346
- * 1. **Usage** line — `program <command> [flags] <args>`
347
- * 2. **Description** — the command's `.description()` text
348
- * 3. **Commands** — subcommands table (if any, skips hidden)
349
- * 4. **Arguments** — positional args table (if any)
350
- * 5. **Flags** — flags table with type hints and defaults
351
- * 6. **Examples** — usage examples (if any)
352
- *
353
- * @param schema - The command schema to render help for.
354
- * @param options - Formatting options (width, binary name).
355
- * @returns The formatted help string.
356
- *
357
- * @example
358
- * ```ts
359
- * const text = formatHelp(deploy.schema, { binName: 'mycli' });
360
- * ```
361
- */
362
- function formatHelp(schema: CommandSchema, options?: HelpOptions): string {
363
- return `${formatHelpSections(schema, options).join('\n\n')}\n`;
364
- }
365
-
366
- // --- Section renderers
367
-
368
- /**
369
- * Render the `Usage:` line for a command.
370
- *
371
- * @param schema - The {@link CommandSchema} to summarize.
372
- * @param opts - Resolved help options (bin name, default-help flag).
373
- * @returns Single-line usage string, e.g. `Usage: mycli deploy [flags] <env>`.
374
- */
375
- function formatUsageLine(schema: CommandSchema, opts: ResolvedHelpOptions): string {
376
- const parts: string[] = ['Usage:'];
377
- // For the default command rendered as the root surface (`isDefaultHelp`), the
378
- // command name is NOT a route — `.default()` does not register a named command,
379
- // so `mycli <name>` would be consumed as the default's first positional, not a
380
- // dispatch. Render usage with only the bin name to avoid teaching a fake form.
381
- const cmdName =
382
- opts.binName === undefined
383
- ? schema.name
384
- : opts.isDefaultHelp
385
- ? opts.binName
386
- : `${opts.binName} ${schema.name}`;
387
- parts.push(cmdName);
388
-
389
- // Subcommand placeholder — groups show <command> before flags/args
390
- if (schema.commands.length > 0) {
391
- parts.push('<command>');
392
- }
393
-
394
- // Flags placeholder
395
- const flagNames = Object.keys(schema.flags);
396
- if (flagNames.length > 0) {
397
- parts.push('[flags]');
398
- }
399
-
400
- // Positional args
401
- for (const entry of schema.args) {
402
- parts.push(formatArgUsage(entry));
403
- }
404
-
405
- return parts.join(' ');
406
- }
407
-
408
- /**
409
- * Render the `Arguments:` help section.
410
- *
411
- * @param args - Positional arg entries from the {@link CommandSchema}.
412
- * @param opts - Resolved help options for line-width wrapping.
413
- * @returns Multi-line arguments section string.
414
- */
415
- function formatArgsSection(args: readonly CommandArgEntry[], opts: ResolvedHelpOptions): string {
416
- const lines: string[] = ['Arguments:'];
417
- const GAP = 2;
418
-
419
- // Compute max left width
420
- let maxLeft = 0;
421
- const entries: Array<{ left: string; desc: string }> = [];
422
- for (const entry of args) {
423
- const left = ` ${formatArgUsage(entry)}`;
424
- const desc = formatArgDescription(entry.schema);
425
- entries.push({ left, desc });
426
- if (left.length > maxLeft) maxLeft = left.length;
427
- }
428
-
429
- const descCol = maxLeft + GAP;
430
- for (const { left, desc } of entries) {
431
- if (desc.length === 0) {
432
- lines.push(left);
433
- } else {
434
- const padded = padEnd(left, descCol);
435
- const wrapped = wrapText(desc, opts.width, descCol);
436
- lines.push(`${padded}${wrapped}`);
437
- }
438
- }
439
-
440
- return lines.join('\n');
441
- }
442
-
443
- /**
444
- * Render the `Flags:` help section.
445
- *
446
- * @param flags - Map of flag names to {@link FlagSchema} definitions.
447
- * @param opts - Resolved help options for line-width wrapping.
448
- * @returns Multi-line flags section string.
449
- */
450
- function formatFlagsSection(
451
- flags: Readonly<Record<string, FlagSchema>>,
452
- opts: ResolvedHelpOptions,
453
- ): string {
454
- return formatFlagEntriesBlock('Flags:', buildFlagEntries(flags), opts.width);
455
- }
456
-
457
- /**
458
- * Render a titled two-column flag block from pre-built {@link FlagEntry} rows.
459
- *
460
- * Shared by the per-command `Flags:` section and the root-help `Global options:`
461
- * block (built-in flags), so column alignment and description wrapping stay
462
- * identical across both. Returns `''` when there are no entries.
463
- *
464
- * @param title - Section heading (e.g. `'Flags:'`, `'Global options:'`).
465
- * @param entries - Formatted left/description rows.
466
- * @param width - Terminal width for description wrapping.
467
- * @returns Multi-line block string, or `''` when `entries` is empty.
468
- * @internal
469
- */
470
- function formatFlagEntriesBlock(
471
- title: string,
472
- entries: readonly FlagEntry[],
473
- width: number,
474
- ): string {
475
- if (entries.length === 0) return '';
476
- const lines: string[] = [title];
477
- const GAP = 2;
478
-
479
- let maxLeft = 0;
480
- for (const entry of entries) {
481
- const indented = ` ${entry.left}`;
482
- if (indented.length > maxLeft) maxLeft = indented.length;
483
- }
484
-
485
- const descCol = maxLeft + GAP;
486
- for (const entry of entries) {
487
- const left = ` ${entry.left}`;
488
- if (entry.description.length === 0) {
489
- lines.push(left);
490
- } else {
491
- const padded = padEnd(left, descCol);
492
- const wrapped = wrapText(entry.description, width, descCol);
493
- lines.push(`${padded}${wrapped}`);
494
- }
495
- }
496
-
497
- return lines.join('\n');
498
- }
499
-
500
- /**
501
- * Render the `Commands:` help section.
502
- *
503
- * @param commands - Visible subcommand schemas (hidden commands pre-filtered).
504
- * @param opts - Resolved help options for line-width wrapping.
505
- * @returns Multi-line commands section string.
506
- */
507
- function formatCommandsSection(
508
- commands: readonly CommandSchema[],
509
- opts: ResolvedHelpOptions,
510
- ): string {
511
- const lines: string[] = ['Commands:'];
512
- const GAP = 2;
513
-
514
- // Compute max name length for alignment
515
- let maxNameLen = 0;
516
- for (const cmd of commands) {
517
- if (cmd.name.length > maxNameLen) {
518
- maxNameLen = cmd.name.length;
519
- }
520
- }
521
-
522
- const descCol = 2 + maxNameLen + GAP; // 2 for indent
523
- for (const cmd of commands) {
524
- const left = ` ${cmd.name}`;
525
- const desc = cmd.description ?? '';
526
- if (desc.length === 0) {
527
- lines.push(left);
528
- } else {
529
- const padded = padEnd(left, descCol);
530
- const wrapped = wrapText(desc, opts.width, descCol);
531
- lines.push(`${padded}${wrapped}`);
532
- }
533
- }
534
-
535
- return lines.join('\n');
536
- }
537
-
538
- /**
539
- * Render the `Examples:` help section.
540
- *
541
- * @param examples - Array of {@link CommandExample} entries.
542
- * @returns Multi-line examples section string.
543
- */
544
- function formatExamplesSection(examples: readonly CommandExample[]): string {
545
- const lines: string[] = ['Examples:'];
546
-
547
- for (const example of examples) {
548
- if (example.description !== undefined) {
549
- lines.push(` ${example.description}:`);
550
- lines.push(` $ ${example.command}`);
551
- } else {
552
- lines.push(` $ ${example.command}`);
553
- }
554
- }
555
-
556
- return lines.join('\n');
557
- }
558
-
559
- // --- Exports
560
-
561
- export { osc8, visibleWidth } from './ansi.ts';
562
- export type { FlagEntry, HelpOptions };
563
- export { formatFlagEntriesBlock, formatHelp, formatHelpSections };