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