@kjanat/dreamcli 2.5.0 → 3.0.0-rc.10

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 (202) hide show
  1. package/CHANGELOG.md +344 -1
  2. package/README.md +9 -2
  3. package/dist/core/cli/dispatch.d.mts +6 -5
  4. package/dist/core/cli/dispatch.mjs +205 -1
  5. package/dist/core/cli/help-links.d.mts +0 -1
  6. package/dist/core/cli/help-links.mjs +41 -1
  7. package/dist/core/cli/index.d.mts +185 -19
  8. package/dist/core/cli/index.mjs +945 -1
  9. package/dist/core/cli/planner.d.mts +32 -6
  10. package/dist/core/cli/planner.mjs +300 -1
  11. package/dist/core/cli/plugin.d.mts +1 -1
  12. package/dist/core/cli/plugin.mjs +41 -1
  13. package/dist/core/cli/propagate.d.mts +1 -1
  14. package/dist/core/cli/propagate.mjs +57 -1
  15. package/dist/core/cli/root-help.d.mts +18 -3
  16. package/dist/core/cli/root-help.mjs +226 -6
  17. package/dist/core/cli/root-surface.d.mts +30 -4
  18. package/dist/core/cli/root-surface.mjs +28 -1
  19. package/dist/core/cli/runtime-preflight.d.mts +30 -3
  20. package/dist/core/cli/runtime-preflight.mjs +169 -1
  21. package/dist/core/completion/index.d.mts +32 -2
  22. package/dist/core/completion/index.mjs +90 -1
  23. package/dist/core/completion/shells/bash.d.mts +0 -1
  24. package/dist/core/completion/shells/bash.mjs +308 -3
  25. package/dist/core/completion/shells/fish.d.mts +0 -1
  26. package/dist/core/completion/shells/fish.mjs +162 -2
  27. package/dist/core/completion/shells/powershell.d.mts +0 -1
  28. package/dist/core/completion/shells/powershell.mjs +358 -2
  29. package/dist/core/completion/shells/shared.d.mts +23 -2
  30. package/dist/core/completion/shells/shared.mjs +176 -1
  31. package/dist/core/completion/shells/zsh.d.mts +0 -1
  32. package/dist/core/completion/shells/zsh.mjs +251 -2
  33. package/dist/core/config/index.d.mts +2 -3
  34. package/dist/core/config/index.mjs +246 -1
  35. package/dist/core/config/package-json.d.mts +27 -4
  36. package/dist/core/config/package-json.mjs +386 -3
  37. package/dist/core/errors/index.d.mts +3 -3
  38. package/dist/core/errors/index.mjs +87 -1
  39. package/dist/core/execution/index.d.mts +0 -1
  40. package/dist/core/execution/index.mjs +201 -1
  41. package/dist/core/help/ansi.d.mts +11 -5
  42. package/dist/core/help/ansi.mjs +121 -2
  43. package/dist/core/help/index.d.mts +65 -2
  44. package/dist/core/help/index.mjs +341 -7
  45. package/dist/core/help/theme.d.mts +68 -0
  46. package/dist/core/help/theme.mjs +46 -0
  47. package/dist/core/json-schema/index.d.mts +15 -2
  48. package/dist/core/json-schema/index.mjs +536 -12
  49. package/dist/core/json-schema/meta-descriptions.generated.d.mts +22 -5
  50. package/dist/core/json-schema/meta-descriptions.generated.mjs +102 -28
  51. package/dist/core/output/activity.d.mts +0 -1
  52. package/dist/core/output/activity.mjs +452 -1
  53. package/dist/core/output/bind.mjs +56 -1
  54. package/dist/core/output/contracts.d.mts +4 -5
  55. package/dist/core/output/contracts.mjs +57 -1
  56. package/dist/core/output/display-value.mjs +32 -1
  57. package/dist/core/output/index.d.mts +21 -1
  58. package/dist/core/output/index.mjs +442 -2
  59. package/dist/core/output/renderers.d.mts +0 -1
  60. package/dist/core/output/renderers.mjs +46 -1
  61. package/dist/core/output/writer.mjs +12 -1
  62. package/dist/core/parse/index.d.mts +54 -7
  63. package/dist/core/parse/index.mjs +727 -1
  64. package/dist/core/prompt/index.d.mts +12 -4
  65. package/dist/core/prompt/index.mjs +348 -6
  66. package/dist/core/resolve/args.d.mts +1 -1
  67. package/dist/core/resolve/args.mjs +105 -1
  68. package/dist/core/resolve/coerce.d.mts +1 -1
  69. package/dist/core/resolve/coerce.mjs +336 -1
  70. package/dist/core/resolve/config.mjs +19 -1
  71. package/dist/core/resolve/contracts.d.mts +9 -4
  72. package/dist/core/resolve/errors.d.mts +0 -1
  73. package/dist/core/resolve/errors.mjs +140 -2
  74. package/dist/core/resolve/flags.d.mts +7 -2
  75. package/dist/core/resolve/flags.mjs +251 -1
  76. package/dist/core/resolve/index.d.mts +2 -2
  77. package/dist/core/resolve/index.mjs +85 -1
  78. package/dist/core/resolve/property.d.mts +6 -2
  79. package/dist/core/resolve/property.mjs +53 -1
  80. package/dist/core/schema/activity.d.mts +31 -11
  81. package/dist/core/schema/arg.d.mts +92 -13
  82. package/dist/core/schema/arg.mjs +463 -1
  83. package/dist/core/schema/command.d.mts +29 -7
  84. package/dist/core/schema/command.mjs +726 -1
  85. package/dist/core/schema/flag.d.mts +577 -21
  86. package/dist/core/schema/flag.mjs +705 -1
  87. package/dist/core/schema/index.d.mts +5 -2
  88. package/dist/core/schema/middleware.d.mts +4 -3
  89. package/dist/core/schema/middleware.mjs +41 -1
  90. package/dist/core/schema/number-constraints.d.mts +96 -0
  91. package/dist/core/schema/number-constraints.mjs +60 -0
  92. package/dist/core/schema/prompt.d.mts +21 -3
  93. package/dist/core/schema/run.d.mts +24 -5
  94. package/dist/core/schema/string-constraints.d.mts +97 -0
  95. package/dist/core/schema/string-constraints.mjs +69 -0
  96. package/dist/core/schema/value-parsers.d.mts +78 -0
  97. package/dist/core/schema/value-parsers.mjs +142 -0
  98. package/dist/core/schema-dsl/runtime.mjs +271 -2
  99. package/dist/core/schema-dsl/to-json-schema.d.mts +0 -1
  100. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  101. package/dist/core/testkit/index.d.mts +9 -36
  102. package/dist/core/testkit/index.mjs +83 -1
  103. package/dist/index.d.mts +12 -6
  104. package/dist/index.mjs +22 -1
  105. package/dist/runtime/adapter.d.mts +38 -2
  106. package/dist/runtime/adapter.mjs +87 -1
  107. package/dist/runtime/auto.d.mts +2 -3
  108. package/dist/runtime/auto.mjs +45 -1
  109. package/dist/runtime/deno.d.mts +24 -5
  110. package/dist/runtime/deno.mjs +262 -2
  111. package/dist/runtime/detect.mjs +45 -1
  112. package/dist/runtime/index.d.mts +3 -4
  113. package/dist/runtime/node.d.mts +8 -2
  114. package/dist/runtime/node.mjs +220 -2
  115. package/dist/runtime/paths.mjs +48 -1
  116. package/dist/runtime.d.mts +4 -4
  117. package/dist/runtime.mjs +6 -1
  118. package/dist/strings.mjs +22 -1
  119. package/dist/testkit.d.mts +1 -0
  120. package/dist/testkit.mjs +5 -1
  121. package/dist/version.d.mts +7 -3
  122. package/dist/version.mjs +7 -1
  123. package/dreamcli.schema.json +36 -7
  124. package/examples/basic.ts +5 -1
  125. package/examples/gh/package.json +2 -7
  126. package/examples/pwsh-demo/package.json +1 -7
  127. package/examples/pwsh-demo/src/main.test.ts +1 -4
  128. package/examples/pwsh-demo/src/main.ts +14 -4
  129. package/examples/transport-launcher.ts +85 -0
  130. package/package.json +43 -49
  131. package/dist/runtime/bun.d.mts +0 -28
  132. package/dist/runtime/bun.mjs +0 -1
  133. package/dist/runtime/support.d.mts +0 -42
  134. package/dist/runtime/support.mjs +0 -1
  135. package/src/core/cli/dispatch.ts +0 -349
  136. package/src/core/cli/help-links.ts +0 -82
  137. package/src/core/cli/index.ts +0 -1413
  138. package/src/core/cli/planner.ts +0 -487
  139. package/src/core/cli/plugin.ts +0 -117
  140. package/src/core/cli/propagate.ts +0 -97
  141. package/src/core/cli/root-help.ts +0 -252
  142. package/src/core/cli/root-surface.ts +0 -74
  143. package/src/core/cli/runtime-preflight.ts +0 -438
  144. package/src/core/completion/index.ts +0 -88
  145. package/src/core/completion/shells/bash.ts +0 -459
  146. package/src/core/completion/shells/fish.ts +0 -259
  147. package/src/core/completion/shells/powershell.ts +0 -439
  148. package/src/core/completion/shells/shared.ts +0 -283
  149. package/src/core/completion/shells/zsh.ts +0 -381
  150. package/src/core/config/index.ts +0 -417
  151. package/src/core/config/package-json.ts +0 -560
  152. package/src/core/errors/index.ts +0 -193
  153. package/src/core/execution/index.ts +0 -268
  154. package/src/core/help/ansi.ts +0 -144
  155. package/src/core/help/index.ts +0 -516
  156. package/src/core/json-schema/index.ts +0 -891
  157. package/src/core/json-schema/meta-descriptions.generated.ts +0 -185
  158. package/src/core/output/activity.ts +0 -575
  159. package/src/core/output/bind.ts +0 -59
  160. package/src/core/output/contracts.ts +0 -194
  161. package/src/core/output/display-value.ts +0 -48
  162. package/src/core/output/index.ts +0 -704
  163. package/src/core/output/renderers.ts +0 -125
  164. package/src/core/output/writer.ts +0 -46
  165. package/src/core/parse/index.ts +0 -681
  166. package/src/core/prompt/index.ts +0 -583
  167. package/src/core/resolve/args.ts +0 -144
  168. package/src/core/resolve/coerce.ts +0 -432
  169. package/src/core/resolve/config.ts +0 -26
  170. package/src/core/resolve/contracts.ts +0 -135
  171. package/src/core/resolve/errors.ts +0 -227
  172. package/src/core/resolve/flags.ts +0 -255
  173. package/src/core/resolve/index.ts +0 -108
  174. package/src/core/resolve/property.ts +0 -99
  175. package/src/core/schema/activity.ts +0 -240
  176. package/src/core/schema/arg.ts +0 -719
  177. package/src/core/schema/command.ts +0 -1498
  178. package/src/core/schema/flag.ts +0 -801
  179. package/src/core/schema/index.ts +0 -91
  180. package/src/core/schema/middleware.ts +0 -171
  181. package/src/core/schema/prompt.ts +0 -156
  182. package/src/core/schema/run.ts +0 -198
  183. package/src/core/schema-dsl/index.ts +0 -109
  184. package/src/core/schema-dsl/parse.ts +0 -262
  185. package/src/core/schema-dsl/runtime.ts +0 -609
  186. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  187. package/src/core/testkit/index.ts +0 -123
  188. package/src/index.ts +0 -179
  189. package/src/runtime/adapter.ts +0 -303
  190. package/src/runtime/auto.ts +0 -110
  191. package/src/runtime/bun.ts +0 -55
  192. package/src/runtime/deno.ts +0 -369
  193. package/src/runtime/detect.ts +0 -105
  194. package/src/runtime/index.ts +0 -21
  195. package/src/runtime/node-builtins.d.ts +0 -27
  196. package/src/runtime/node.ts +0 -300
  197. package/src/runtime/paths.ts +0 -64
  198. package/src/runtime/support.ts +0 -135
  199. package/src/runtime.ts +0 -31
  200. package/src/strings.ts +0 -34
  201. package/src/testkit.ts +0 -25
  202. 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 };