monochange 0.6.1

Manage versions and releases for your multiplatform, multilanguage monorepo
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
//! Beautiful, colored CLI help renderer for `mc help <command>`.
//!
//! This module provides a custom help experience that goes beyond clap's
//! built-in `--help` output. It adds detailed descriptions, multiple examples
//! per command, tips, and cross-references — all rendered with ANSI colors
//! when the terminal supports them.

#![allow(clippy::format_push_string, clippy::single_char_add_str)]

use std::io::IsTerminal;

use monochange_core::CliCommandDefinition;
use monochange_core::CliInputDefinition;
use monochange_core::CliInputKind;
use monochange_core::CliStepDefinition;

// ---------------------------------------------------------------------------
// Color theme
// ---------------------------------------------------------------------------

/// Whether ANSI colors should be emitted.
fn color_enabled() -> bool {
	color_enabled_impl(
		std::env::var_os("NO_COLOR").is_some(),
		std::env::var_os("TERM").is_some_and(|v| v == "dumb"),
		std::io::stdout().is_terminal(),
	)
}

/// Testable implementation for 100% branch coverage.
fn color_enabled_impl(no_color: bool, term_dumb: bool, is_terminal: bool) -> bool {
	if no_color {
		return false;
	}
	if term_dumb {
		return false;
	}
	is_terminal
}

/// Apply an `anstyle::Style` to `text` when colors are enabled.
fn paint(text: &str, style: anstyle::Style) -> String {
	paint_impl(text, style, color_enabled())
}

/// Testable implementation for 100% branch coverage.
fn paint_impl(text: &str, style: anstyle::Style, enabled: bool) -> String {
	if enabled {
		format!("{style}{text}{style:#}")
	} else {
		text.to_string()
	}
}

// Shorthand style constructors using the monochange palette.
// These delegate to `crate::cli_theme` so clap `--help` and
// `mc help` share the exact same ANSI styles.

fn accent() -> anstyle::Style {
	crate::cli_theme::header()
}

fn header() -> anstyle::Style {
	crate::cli_theme::usage()
}

fn flag_style() -> anstyle::Style {
	crate::cli_theme::literal()
}

fn value_style() -> anstyle::Style {
	crate::cli_theme::placeholder()
}

fn muted() -> anstyle::Style {
	crate::cli_theme::muted()
}

fn error_style() -> anstyle::Style {
	crate::cli_theme::error()
}

fn code_style() -> anstyle::Style {
	crate::cli_theme::valid()
}

// ---------------------------------------------------------------------------
// Bordered header
// ---------------------------------------------------------------------------

/// Render a bordered header like:
///
/// ```text
/// ╭──────────────────────────────────────────────╮
/// │  mc change                                     │
/// │  Create a change file for one or more packages │
/// ╰──────────────────────────────────────────────╯
/// ```
fn bordered_header(command: &str, description: &str, width: usize) -> String {
	let inner = width - 4; // account for │ and spaces
	let name_line = format!("  {command}");
	let desc_line = if description.len() > inner {
		&description[..inner]
	} else {
		description
	};

	let border_width = width.saturating_sub(2);
	let mut output = String::new();
	output.push('╭');
	for _ in 0..border_width {
		output.push('─');
	}
	output.push_str("╮\n");

	let name_pad = border_width.saturating_sub(name_line.len());
	output.push('│');
	output.push_str(&name_line);
	for _ in 0..name_pad {
		output.push(' ');
	}
	output.push_str("│\n");

	let desc_pad = width.saturating_sub(4).saturating_sub(desc_line.len());
	output.push_str("│  ");
	output.push_str(desc_line);
	for _ in 0..desc_pad {
		output.push(' ');
	}
	output.push_str("│\n");

	output.push('╰');
	for _ in 0..border_width {
		output.push('─');
	}
	output.push('╯');
	output
}

// ---------------------------------------------------------------------------
// Section helpers
// ---------------------------------------------------------------------------

fn section_heading(title: &str) -> String {
	format!("{} {}", paint("▸", accent()), paint(title, header()))
}

fn example_block(description: &str, command: &str) -> String {
	let desc = paint(description, muted());
	let cmd = paint(command, code_style());
	format!("  {desc}\n    {cmd}")
}

// ---------------------------------------------------------------------------
// Per-command detailed help content
// ---------------------------------------------------------------------------

const BUILTIN_COMMAND_NAMES: &[&str] = &[
	"init",
	"populate",
	"command",
	"skill",
	"subagents",
	"analyze",
	"migrate",
	"step:release-record",
	"step:publish-readiness",
	"step:placeholder-publish",
	"step:tag-release",
	"lint",
	"mcp",
	"check",
	"step:validate",
	"help",
];

#[derive(Clone)]
struct OwnedCommandHelp {
	name: String,
	summary: String,
	description: String,
	usage: String,
	options: Vec<(String, String, String)>,
	examples: Vec<(String, String)>,
	tips: Vec<String>,
	see_also: Vec<String>,
}

impl From<&CommandHelp> for OwnedCommandHelp {
	fn from(help: &CommandHelp) -> Self {
		Self {
			name: help.name.to_string(),
			summary: help.summary.to_string(),
			description: help.description.to_string(),
			usage: help.usage.to_string(),
			options: help
				.options
				.iter()
				.map(|(flag, type_name, desc)| {
					(
						(*flag).to_string(),
						(*type_name).to_string(),
						(*desc).to_string(),
					)
				})
				.collect(),
			examples: help
				.examples
				.iter()
				.map(|(description, command)| ((*description).to_string(), (*command).to_string()))
				.collect(),
			tips: help.tips.iter().map(|tip| (*tip).to_string()).collect(),
			see_also: help
				.see_also
				.iter()
				.map(|command| (*command).to_string())
				.collect(),
		}
	}
}

struct CommandListItem {
	name: String,
	summary: String,
}

struct CommandHelp {
	name: &'static str,
	summary: &'static str,
	description: &'static str,
	usage: &'static str,
	options: &'static [(&'static str, &'static str, &'static str)], // (flag, type, help)
	examples: &'static [(&'static str, &'static str)],              // (description, command)
	tips: &'static [&'static str],
	see_also: &'static [&'static str],
}

fn builtin_command_helps() -> Vec<CommandHelp> {
	vec![
		CommandHelp {
			name: "init",
			summary: "Generate monochange.toml with detected packages",
			description: "Scans the workspace for supported package manifests (Cargo.toml, package.json, \
				deno.json, pubspec.yaml) and generates a monochange.toml configuration file with \
				discovered packages, version groups, and default CLI commands.\n\n\
				Use --provider to scaffold source-control integration (GitHub, GitLab, Gitea, Forgejo) \
				with release automation CLI commands.",
			usage: "mc init [OPTIONS]",
			options: &[
				("--force", "", "Overwrite an existing monochange.toml file"),
				(
					"--provider",
					"<PROVIDER>",
					"Source-control provider (github, gitlab, gitea)",
				),
			],
			examples: &[
				("Initialize a fresh workspace:", "mc init"),
				("Overwrite existing config:", "mc init --force"),
				(
					"Initialize with GitHub integration:",
					"mc init --provider github",
				),
				(
					"Initialize with GitLab integration:",
					"mc init --provider gitlab",
				),
				(
					"Initialize with Gitea integration:",
					"mc init --provider gitea",
				),
			],
			tips: &[
				"Run mc init at the root of your monorepo.",
				"The generated config is a starting point — customize packages, groups, and CLI commands in monochange.toml.",
				"Use --provider=github to get GitHub Actions workflow templates included.",
			],
			see_also: &["populate", "step:validate", "discover"],
		},
		CommandHelp {
			name: "populate",
			summary: "Add missing built-in CLI commands to monochange.toml",
			description: "Compares the built-in default CLI commands against what is defined in \
				monochange.toml and appends any missing commands so you can customize them. \
				Existing command definitions are never modified.",
			usage: "mc populate",
			options: &[],
			examples: &[("Add any missing default commands:", "mc populate")],
			tips: &[
				"Run this after upgrading monochange to pick up new commands.",
				"This is a safe, additive-only operation.",
			],
			see_also: &["init", "step:validate"],
		},
		CommandHelp {
			name: "command",
			summary: "Add or edit config-defined CLI commands interactively",
			description: "Opens an interactive dashboard for creating and revising [cli.<name>] command entries in monochange.toml. The wizard can add a new command, edit or rename an existing command, keep existing steps unchanged, or replace steps with built-in workflow steps and shell Command steps.",
			usage: "mc command",
			options: &[],
			examples: &[("Open the command dashboard:", "mc command")],
			tips: &[
				"Choose edit to revise an existing [cli.<name>] command without rewriting its steps.",
				"Use the editor action when you need advanced TOML fields that the wizard does not prompt for yet.",
			],
			see_also: &["populate", "validate"],
		},
		CommandHelp {
			name: "skill",
			summary: "Install the monochange skill bundle for AI agents",
			description: "Installs monochange-specific skills into the current project using the \
				`skills` CLI package manager. Skills enable AI coding agents to understand \
				and use monochange effectively.\n\n\
				All arguments after `mc skill` are forwarded to `skills add <monochange-source>`.",
			usage: "mc skill [FLAGS...]",
			options: &[("(forwarded)", "", "All args are forwarded to `skills add`")],
			examples: &[
				("List available skills:", "mc skill --list"),
				("Install for Claude Code:", "mc skill -a claude-code"),
				("Install for pi globally:", "mc skill -g -a pi -y"),
				(
					"Install specific skill with copy:",
					"mc skill --skill monochange --copy -y",
				),
				(
					"Install for multiple agents:",
					"mc skill -a claude-code -a codex",
				),
			],
			tips: &[
				"Runner selection is automatic: npx → pnpm dlx → bunx.",
				"Use --copy instead of symlinks for immutable installs.",
				"Use -y to skip confirmation prompts.",
			],
			see_also: &["subagents"],
		},
		CommandHelp {
			name: "subagents",
			summary: "Generate repo-local monochange subagents and agent guidance",
			description: "Generates AI agent configuration files (markdown instructions, MCP config, \
				agent definitions) for supported coding assistant platforms. Generated agents \
				are CLI-first and prefer `mc` over library APIs.",
			usage: "mc subagents <TARGET(S)> [OPTIONS]",
			options: &[
				("<TARGET>", "", "claude, vscode, copilot, pi, codex, cursor"),
				("--all", "", "Generate for all supported targets"),
				("--force", "", "Overwrite existing files"),
				("--dry-run", "", "Preview without writing"),
				("--format", "<FORMAT>", "Output format (text, json)"),
				("--no-mcp", "", "Skip MCP config files"),
			],
			examples: &[
				("Generate for Claude:", "mc subagents claude"),
				("Generate for multiple targets:", "mc subagents pi codex"),
				("Generate for all targets:", "mc subagents --all"),
				("Preview without writing:", "mc subagents --all --dry-run"),
				(
					"Generate without MCP config:",
					"mc subagents vscode copilot --no-mcp",
				),
			],
			tips: &[
				"Target mapping:\n    • claude  → .claude/agents/*.md and .mcp.json\n    • vscode  → .github/agents/*.agent.md and .vscode/mcp.json\n    • copilot → .github/agents/*.agent.md and .vscode/mcp.json\n    • pi      → .pi/agents/*.md\n    • codex   → .codex/agents/*.toml\n    • cursor  → .cursor/rules/*.mdc",
				"Generated agents prefer: mc → monochange → npx -y @monochange/cli",
			],
			see_also: &["skill", "mcp"],
		},
		CommandHelp {
			name: "analyze",
			summary: "Analyze semantic changes for a package",
			description: "Runs package-scoped semantic analysis comparing a package's public API across \
				main, head, and optional release baselines. Produces a structured assessment of \
				what changed and the implied semver bump.\n\n\
				Defaults --release-ref to the newest tag for the package or its version group. \
				If no prior release tag exists, falls back to first-release analysis using \
				only main → head.",
			usage: "mc analyze --package <PACKAGE> [OPTIONS]",
			options: &[
				(
					"--package",
					"<PACKAGE>",
					"Package id, name, manifest path, or directory (required)",
				),
				("--release-ref", "<REF>", "Explicit release baseline ref"),
				(
					"--main-ref",
					"<REF>",
					"Base branch ref (default: detected default branch)",
				),
				("--head-ref", "<REF>", "Head ref to analyze (default: HEAD)"),
				(
					"--detection-level",
					"<LEVEL>",
					"basic, signature, semantic (default: signature)",
				),
				("--format", "<FORMAT>", "text, json (default: text)"),
			],
			examples: &[
				("Analyze a package:", "mc analyze --package core"),
				("JSON output:", "mc analyze --package core --format json"),
				(
					"Against a specific release tag:",
					"mc analyze --package core --release-ref core/v1.2.3",
				),
				(
					"Custom main and head refs:",
					"mc analyze --package core --main-ref main --head-ref HEAD",
				),
			],
			tips: &[
				"Use the package id from monochange.toml for the most reliable resolution.",
				"Add --format json for scripting and LLM consumption.",
			],
			see_also: &["release", "versions", "change"],
		},
		CommandHelp {
			name: "change",
			summary: "Create a change file for one or more packages",
			description: "Creates a structured changeset markdown file in .changeset/ that describes \
				what changed, which packages are affected, and the requested semver bump. \
				These changeset files are consumed during release planning to produce \
				version bumps, changelogs, and release manifests.\n\n\
				You can target individual packages or entire version groups. Dependents and \
				group members are propagated automatically during planning. Use --caused-by \
				to mark dependency-only follow-ups.",
			usage: "mc change [OPTIONS]",
			options: &[
				(
					"-i, --interactive",
					"",
					"Select packages, bumps, and options interactively",
				),
				(
					"--package",
					"<PACKAGE>",
					"Package or group to include (repeatable)",
				),
				(
					"--bump",
					"<BUMP>",
					"none, patch, minor, major (default: patch)",
				),
				(
					"--version",
					"<VERSION>",
					"Pin an explicit version for this release",
				),
				("--reason", "<REASON>", "Short release-note summary"),
				(
					"--type",
					"<TYPE>",
					"Release-note type (feat, fix, security, etc.)",
				),
				(
					"--caused-by",
					"<CAUSED_BY>",
					"Ids that caused this dependent change (repeatable)",
				),
				("--details", "<DETAILS>", "Multi-line release-note details"),
				(
					"--output",
					"<PATH>",
					"Write the change file to a specific path",
				),
			],
			examples: &[
				(
					"Quick patch for a single package:",
					r#"mc change --package core --bump patch --reason "fix null pointer""#,
				),
				(
					"Minor feature with output path:",
					r#"mc change --package api --bump minor --reason "add pagination" --output .changeset/api-pagination.md"#,
				),
				(
					"Group-level change:",
					r#"mc change --package sdk --bump minor --reason "coordinated release""#,
				),
				(
					"Dependency-only follow-up:",
					r#"mc change --package utils --bump patch --caused-by core --reason "bump for core compat""#,
				),
				("Interactive mode:", "mc change --interactive"),
				(
					"Explicit version pin:",
					r#"mc change --package core --bump major --version 2.0.0 --reason "promote to stable""#,
				),
			],
			tips: &[
				"Prefer configured package ids over manifest paths.",
				"Use a group id only when the change is intentionally owned by the whole group.",
				"Dependents and grouped members propagate automatically during planning.",
				"--caused-by marks a package as only changing because another moved first.",
				"Legacy manifest paths resolve during migration, but declared ids are the stable interface.",
			],
			see_also: &["release", "versions", "affected"],
		},
		CommandHelp {
			name: "release",
			summary: "Prepare a release from discovered change files",
			description: "Reads all changeset files in .changeset/, plans version bumps and changelog \
				updates, and prepares the release artifacts. By default, output is rendered in \
				markdown format.\n\n\
				In dry-run mode, no files are modified. Use --diff to see unified file diffs \
				for the planned changes. Use --prepared-release to read or write a cached \
				release artifact for multi-step workflows.",
			usage: "mc release [OPTIONS]",
			options: &[
				("--dry-run", "", "Preview without modifying files"),
				("--diff", "", "Show unified file diffs for the release"),
				(
					"--format",
					"<FORMAT>",
					"markdown, text, json (default: markdown)",
				),
				(
					"--prepared-release",
					"<PATH>",
					"Read/write prepared release artifact path",
				),
			],
			examples: &[
				(
					"Dry-run preview in text format:",
					"mc release --dry-run --format text",
				),
				(
					"Dry-run with JSON for scripting:",
					"mc release --dry-run --format json",
				),
				("Preview with file diffs:", "mc release --dry-run --diff"),
				("Execute the release:", "mc release"),
			],
			tips: &[
				"Direct package changes propagate to dependents using defaults.parent_bump.",
				"Group synchronization happens before final output is rendered.",
				"Explicit versions on grouped members propagate to the whole group.",
				"Use --prepared-release to cache the release for multi-step workflows.",
			],
			see_also: &["change", "versions", "commit-release", "release-pr"],
		},
		CommandHelp {
			name: "versions",
			summary: "Display planned versions without modifying files",
			description: "Computes the same planned versions used by mc release but renders them without \
				mutating any manifests, changelogs, or changesets. This is a read-only preview \
				of what a release would produce.",
			usage: "mc versions [OPTIONS]",
			options: &[(
				"--format",
				"<FORMAT>",
				"text, markdown, json (default: text)",
			)],
			examples: &[
				("Show planned versions:", "mc versions"),
				("Markdown output:", "mc versions --format markdown"),
				("JSON for scripting:", "mc versions --format json"),
			],
			tips: &[
				"This command is read-only — it does not update manifests or changelogs.",
				"It computes the same planned versions used by monochange release workflows.",
			],
			see_also: &["release", "change"],
		},
		CommandHelp {
			name: "commit-release",
			summary: "Create a local release commit with an embedded release record",
			description: "Creates a git commit that embeds a durable monochange release record in the \
				commit body. The release record allows later steps (tag-release, repair-release) \
				to reconstruct the full release tag set from the commit alone.\n\n\
				Requires a previous PrepareRelease step or a prepared-release artifact.",
			usage: "mc commit-release [OPTIONS]",
			options: &[
				("--dry-run", "", "Preview the commit without creating it"),
				("--diff", "", "Show file diffs for the release"),
				(
					"--format",
					"<FORMAT>",
					"markdown, text, json (default: markdown)",
				),
			],
			examples: &[
				("Preview the commit:", "mc commit-release --dry-run"),
				("Preview with diffs:", "mc commit-release --dry-run --diff"),
				("JSON preview:", "mc commit-release --dry-run --format json"),
				("Execute the commit:", "mc commit-release"),
			],
			tips: &[
				"Reuses the standard monochange release commit subject/body contract.",
				"Embeds a durable release record block in the commit body.",
				"Can run before OpenReleaseRequest in the same workflow.",
			],
			see_also: &["release", "step:tag-release", "release-pr"],
		},
		CommandHelp {
			name: "release-pr",
			summary: "Open or update a hosted release pull request",
			description: "Opens (or updates an existing) pull request on the configured source host \
				(GitHub, GitLab, Gitea, Forgejo) with the prepared release changes. Requires [source] \
				configuration in monochange.toml.",
			usage: "mc release-pr [OPTIONS]",
			options: &[
				("--dry-run", "", "Preview without creating the PR"),
				("--diff", "", "Show file diffs for the release"),
				(
					"--format",
					"<FORMAT>",
					"markdown, text, json (default: markdown)",
				),
			],
			examples: &[
				("Preview the PR:", "mc release-pr --dry-run"),
				("Preview with markdown diff:", "mc release-pr --dry-run"),
				("Create the PR:", "mc release-pr"),
			],
			tips: &[
				"Requires [source] configuration with provider, owner, and repo.",
				"Labels and auto-merge settings come from [source.pull_requests].",
			],
			see_also: &["commit-release", "release"],
		},
		CommandHelp {
			name: "affected",
			summary: "Evaluate affected packages and changeset coverage",
			description: "CI-oriented command that evaluates whether changed paths are adequately covered \
				by changeset files. Useful in pull request checks to verify that every touched \
				package has a corresponding changeset.\n\n\
				Returns exit code 0 when coverage passes, non-zero otherwise.",
			usage: "mc affected [OPTIONS]",
			options: &[
				(
					"--changed-paths",
					"<PATHS>",
					"File paths changed in the PR (repeatable)",
				),
				(
					"--from",
					"<REF>",
					"Git ref to diff against (e.g. origin/main)",
				),
				(
					"--verify",
					"",
					"Verify changeset coverage for affected packages",
				),
				(
					"--label",
					"<LABELS>",
					"PR labels that may skip verification (repeatable)",
				),
				("--format", "<FORMAT>", "text, json (default: text)"),
			],
			examples: &[
				(
					"Check specific changed paths:",
					"mc affected --changed-paths crates/core/src/lib.rs --format json",
				),
				(
					"Compare against a branch:",
					"mc affected --from origin/main --verify",
				),
				(
					"In CI with labels:",
					"mc affected --from origin/main --label skip-changeset",
				),
			],
			tips: &[
				"Prefer package ids in .changeset files.",
				"Group-owned changesets cover all members of that group.",
				"Ignored paths and skip labels are configured in [changesets.affected].",
			],
			see_also: &["change", "check"],
		},
		CommandHelp {
			name: "diagnostics",
			summary: "Inspect parsed changeset data, provenance, and metadata",
			description: "Dumps detailed structured information about changeset files including: \
				target packages/groups, requested bumps, the commit SHA that introduced \
				and last updated each changeset, linked review requests, and related issue \
				references.",
			usage: "mc step:diagnose-changesets [OPTIONS]",
			options: &[
				(
					"--changeset",
					"<PATH>",
					"Specific changeset file(s) to diagnose (repeatable)",
				),
				("--format", "<FORMAT>", "text, json (default: text)"),
			],
			examples: &[
				("Diagnose all changesets:", "mc step:diagnose-changesets"),
				("JSON output:", "mc step:diagnose-changesets --format json"),
				(
					"Specific changeset:",
					"mc step:diagnose-changesets --changeset .changeset/feature.md",
				),
			],
			tips: &[
				"Use --format json for LLM and scripting consumption.",
				"When [source] is configured, diagnostics include hosted metadata (PR links, issue refs).",
			],
			see_also: &["affected", "change"],
		},
		CommandHelp {
			name: "repair-release",
			summary: "Repair a recent release by retargeting its tag set",
			description: "Finds the release record from history and moves the full release tag set to \
				a new target commit. Defaults to descendant-only retargets for safety; use \
				--force to retarget to non-descendant commits.\n\n\
				Can also sync hosted releases on GitHub/GitLab/Gitea when source is configured.",
			usage: "mc repair-release --from <REF> [OPTIONS]",
			options: &[
				(
					"--from",
					"<REF>",
					"Tag or commit-ish locating the release (required)",
				),
				("--target", "<REF>", "Target commit (default: HEAD)"),
				("--force", "", "Allow retarget to non-descendant commits"),
				(
					"--sync-provider",
					"=BOOL",
					"Sync hosted release (default: true)",
				),
				("--dry-run", "", "Preview without modifying tags"),
				("--format", "<FORMAT>", "text, json (default: text)"),
			],
			examples: &[
				(
					"Dry-run repair:",
					"mc repair-release --from v1.2.3 --dry-run",
				),
				(
					"Repair to HEAD:",
					"mc repair-release --from v1.2.3 --target HEAD",
				),
				(
					"Force retarget:",
					"mc repair-release --from v1.2.3 --target HEAD --force",
				),
				(
					"Skip provider sync:",
					"mc repair-release --from v1.2.3 --sync-provider=false",
				),
			],
			tips: &[
				"Defaults to descendant-only retargets unless --force is set.",
				"Hosted release sync runs by default; disable with --sync-provider=false.",
				"Use mc step:tag-release to create tags from a fresh release commit instead.",
			],
			see_also: &["step:tag-release", "step:release-record", "release"],
		},
		CommandHelp {
			name: "step:tag-release",
			summary: "Create and push release tags from an embedded release record",
			description: "Reads the monochange release record embedded in a commit's body and creates \
				the full tag set declared by that record. Pushes tags to origin by default. \
				Reruns on the same commit are treated as already up to date.",
			usage: "mc step:tag-release --from <REF> [OPTIONS]",
			options: &[
				("--from", "<REF>", "Release commit ref (required)"),
				("--push", "=BOOL", "Push tags to origin (default: true)"),
				("--dry-run", "", "Preview without creating/pushing tags"),
				("--format", "<FORMAT>", "text, json (default: text)"),
			],
			examples: &[
				("Create and push tags:", "mc step:tag-release --from HEAD"),
				(
					"Dry-run preview:",
					"mc step:tag-release --from HEAD --dry-run",
				),
				(
					"Create without pushing:",
					"mc step:tag-release --from HEAD --push=false",
				),
				(
					"JSON output:",
					"mc step:tag-release --from HEAD --dry-run --format json",
				),
			],
			tips: &[
				"Requires the resolved ref itself to be the monochange release commit.",
				"Creates the full tag set declared by that release record.",
				"Reruns on the same commit are treated as already up to date.",
				"Use mc repair-release to move existing tags later.",
			],
			see_also: &["repair-release", "step:release-record", "commit-release"],
		},
		CommandHelp {
			name: "step:release-record",
			summary: "Inspect the monochange release record for a tag or commit",
			description: "Resolves the supplied ref to a commit, then walks first-parent ancestry until \
				it finds a monochange release record embed. Renders the full release record \
				including targets, versions, changed files, and changelogs.",
			usage: "mc step:release-record --from <REF> [OPTIONS]",
			options: &[
				("--from", "<REF>", "Tag or commit-ish to locate (required)"),
				("--format", "<FORMAT>", "text, json (default: text)"),
			],
			examples: &[
				("Inspect by tag:", "mc step:release-record --from v1.2.3"),
				("Inspect by commit:", "mc step:release-record --from HEAD"),
				(
					"JSON output:",
					"mc step:release-record --from v1.2.3 --format json",
				),
			],
			tips: &[
				"Fails loudly if it encounters a malformed release record block.",
				"Walks first-parent ancestry to find the record.",
			],
			see_also: &["step:tag-release", "repair-release"],
		},
		CommandHelp {
			name: "check",
			summary: "Validate configuration, changesets, and run manifest lint rules",
			description: "Validates monochange.toml, changeset files, and runs ecosystem-specific \
				manifest lint rules (e.g., Cargo.toml sorting, package.json constraints). \
				Use --fix to auto-fix issues where possible.",
			usage: "mc check [OPTIONS]",
			options: &[
				("-f, --fix", "", "Auto-fix lint issues where possible"),
				(
					"-e, --ecosystem",
					"<ECOSYSTEMS>",
					"Limit to specific ecosystem suites (comma-sep)",
				),
				(
					"--only",
					"<RULES>",
					"Run only specific lint rule ids (comma-sep)",
				),
				("--format", "<FORMAT>", "text, json (default: text)"),
			],
			examples: &[
				("Run all checks:", "mc check"),
				("Auto-fix issues:", "mc check --fix"),
				("Specific ecosystem:", "mc check --ecosystem cargo,npm"),
				(
					"Specific rule:",
					"mc check --only cargo/sorted-dependencies",
				),
			],
			tips: &[
				"Lint rules are configured in [lints] of monochange.toml.",
				"Use mc lint list to see available rules and presets.",
			],
			see_also: &["lint", "step:validate", "affected"],
		},
		CommandHelp {
			name: "lint",
			summary: "Inspect and scaffold manifest lint rules",
			description: "Subcommand group for listing, explaining, and creating lint rules that \
				enforce manifest quality standards across your monorepo.",
			usage: "mc lint <SUBCOMMAND>",
			options: &[
				("list", "", "List registered lint rules and presets"),
				("explain <ID>", "", "Explain a lint rule or preset"),
				("new <ID>", "", "Scaffold a new lint rule (ecosystem/name)"),
			],
			examples: &[
				("List all rules:", "mc lint list"),
				(
					"Explain a rule:",
					"mc lint explain cargo/sorted-dependencies",
				),
				(
					"Create a new rule:",
					"mc lint new cargo/no-path-dependencies",
				),
				(
					"Create npm rule:",
					"mc lint new npm/require-package-manager",
				),
			],
			tips: &[
				"Rule ids follow the <ecosystem>/<name> pattern.",
				"Use mc check to run lint rules, mc lint to manage them.",
			],
			see_also: &["check", "step:validate"],
		},
		CommandHelp {
			name: "mcp",
			summary: "Start the monochange MCP server over stdin/stdout",
			description: "Starts a Model Context Protocol (MCP) server that exposes monochange \
				capabilities as tools for AI assistants. The server communicates over \
				stdin/stdout using the MCP protocol.\n\n\
				AI agents can use the MCP server to discover packages, create changes, \
				plan releases, and more — all through structured tool calls.",
			usage: "mc mcp",
			options: &[],
			examples: &[("Start the MCP server:", "mc mcp")],
			tips: &[
				"The MCP server is designed for AI agent consumption, not direct human use.",
				"Configure your agent's MCP settings to point to this command.",
			],
			see_also: &["subagents", "skill"],
		},
		CommandHelp {
			name: "step:validate",
			summary: "Validate monochange configuration and changesets",
			description: "Validates the monochange.toml configuration, package manifests, version \
				groups, changeset files, and workspace consistency. This is the same \
				validation step that runs at the start of release commands.",
			usage: "mc step:validate",
			options: &[],
			examples: &[("Validate the workspace:", "mc step:validate")],
			tips: &[
				"Runs automatically before release commands.",
				"Standalone use is for pre-commit hooks or CI gates.",
			],
			see_also: &["check", "discover"],
		},
		CommandHelp {
			name: "discover",
			summary: "Discover packages across supported ecosystems",
			description: "Scans the workspace for packages across Cargo, npm/pnpm/Bun, Deno, and \
				Dart/Flutter ecosystems and renders a structured discovery report.",
			usage: "mc discover [OPTIONS]",
			options: &[("--format", "<FORMAT>", "text, json (default: text)")],
			examples: &[
				("Discover all packages:", "mc discover"),
				("JSON output:", "mc discover --format json"),
			],
			tips: &[
				"Discovery is read-only and does not modify any files.",
				"JSON output is useful for scripting and LLM consumption.",
			],
			see_also: &["step:validate", "init"],
		},
		CommandHelp {
			name: "step:publish-readiness",
			summary: "Check package registry publishing readiness without publishing packages",
			description: "Evaluates the package publications recorded on a release commit against the\
				current workspace configuration and target registries. The command is read-only: it\
				runs registry existence checks in dry-run mode, reports packages that are ready,\
				already published, or unsupported by built-in publishing, and can write a JSON\
				readiness artifact for later publish orchestration.",
			usage: "mc step:publish-readiness --from <REF> [OPTIONS]",
			options: &[
				(
					"--from",
					"<REF>",
					"Tag or commit-ish used to locate the release record",
				),
				(
					"--format",
					"<FORMAT>",
					"text, markdown, json (default: markdown)",
				),
				(
					"--package",
					"<PACKAGE>",
					"Restrict to specific package ids (repeatable)",
				),
				("--output", "<PATH>", "Write a JSON readiness artifact"),
			],
			examples: &[
				(
					"Check the current release commit:",
					"mc step:publish-readiness --from HEAD",
				),
				(
					"Write a readiness artifact:",
					"mc step:publish-readiness --from HEAD --output .monochange/local/readiness.json",
				),
				(
					"JSON for one package:",
					"mc step:publish-readiness --from v1.2.3 --package core --format json",
				),
			],
			tips: &[
				"Run readiness before mutating registry state with mc publish.",
				"Already-published versions are reported as resumable instead of blocking.",
			],
			see_also: &["publish-plan", "publish", "placeholder-publish"],
		},
		CommandHelp {
			name: "step:placeholder-publish",
			summary: "Publish first-time placeholder package versions for a release record",
			description: "Reads the package publications embedded in a release commit, narrows them with\
				optional package filters, and runs placeholder publishing for that release package set.\
				The command can write a JSON bootstrap result artifact for CI logs or manual retry\
				notes. Use --dry-run first to inspect work without mutating registries.",
			usage: "mc step:placeholder-publish --from <REF> [OPTIONS]",
			options: &[
				(
					"--from",
					"<REF>",
					"Tag or commit-ish used to locate the release record",
				),
				(
					"--format",
					"<FORMAT>",
					"text, markdown, json (default: markdown)",
				),
				(
					"--package",
					"<PACKAGE>",
					"Restrict to release-record package ids (repeatable)",
				),
				(
					"--dry-run",
					"",
					"Preview placeholder publishing without publishing",
				),
				(
					"--output",
					"<PATH>",
					"Write a JSON publish bootstrap result artifact",
				),
			],
			examples: &[
				(
					"Preview bootstrap work:",
					"mc step:placeholder-publish --from HEAD --dry-run",
				),
				(
					"Write a bootstrap result:",
					"mc step:placeholder-publish --from HEAD --output .monochange/local/bootstrap-result.json",
				),
				(
					"JSON for one package:",
					"mc step:placeholder-publish --from HEAD --package core --format json",
				),
			],
			tips: &[
				"Run mc step:publish-readiness again after bootstrap before mc publish.",
				"Existing placeholder versions are skipped and treated as resumable.",
			],
			see_also: &[
				"step:publish-readiness",
				"publish-plan",
				"publish",
				"placeholder-publish",
			],
		},
		CommandHelp {
			name: "placeholder-publish",
			summary: "Publish placeholder versions for missing registry packages",
			description: "Packages that have never been published to their target registry (crates.io, \
				npm, pub.dev, JSR) need an initial placeholder version before automated \
				publishing can work. This command publishes those placeholders and reports \
				only packages that need action unless --show-all is set.",
			usage: "mc placeholder-publish [OPTIONS]",
			options: &[
				(
					"--format",
					"<FORMAT>",
					"text, markdown, json (default: text)",
				),
				(
					"--package",
					"<PACKAGE>",
					"Restrict to specific package ids (repeatable)",
				),
				(
					"--show-all",
					"",
					"Include already-published and skipped packages in the report",
				),
				("--dry-run", "", "Preview without publishing"),
			],
			examples: &[
				("Dry-run all:", "mc placeholder-publish --dry-run"),
				(
					"Specific package:",
					"mc placeholder-publish --package core --dry-run",
				),
				(
					"JSON output:",
					"mc placeholder-publish --dry-run --format json",
				),
			],
			tips: &[
				"Placeholder versions are 0.0.0 by default.",
				"Only unpublished packages are included.",
			],
			see_also: &["release", "publish-packages"],
		},
		CommandHelp {
			name: "publish-packages",
			summary: "Publish package versions from release state",
			description: "Publishes package versions to their target registries using the prepared \
				release data. Supports trusted publishing on supported registries.",
			usage: "mc publish-packages [OPTIONS]",
			options: &[
				(
					"--format",
					"<FORMAT>",
					"text, markdown, json (default: text)",
				),
				(
					"--package",
					"<PACKAGE>",
					"Restrict to specific package ids (repeatable)",
				),
				("--dry-run", "", "Preview without publishing"),
			],
			examples: &[
				("Dry-run all:", "mc publish-packages --dry-run"),
				("Specific package:", "mc publish-packages --package core"),
				(
					"JSON format:",
					"mc publish-packages --dry-run --format json",
				),
			],
			tips: &[
				"Requires a prepared release from a previous release step.",
				"Trusted publishing is used when running in GitHub Actions with OIDC.",
			],
			see_also: &["placeholder-publish", "release"],
		},
	]
}

// ---------------------------------------------------------------------------
// Render full help for a named command
// ---------------------------------------------------------------------------

/// Render beautiful, detailed help for the named command.
#[allow(dead_code)]
pub fn render_command_help(bin_name: &str, command_name: &str) -> String {
	render_command_help_with_cli(bin_name, command_name, &[])
}

/// Render beautiful, detailed help for the named command with config-defined commands.
pub fn render_command_help_with_cli(
	bin_name: &str,
	command_name: &str,
	cli: &[CliCommandDefinition],
) -> String {
	let builtin_helps = builtin_command_helps();
	if let Some(help) = builtin_helps
		.iter()
		.find(|help| help.name == command_name && BUILTIN_COMMAND_NAMES.contains(&help.name))
	{
		return render_single_command_help(bin_name, help);
	}

	if command_name.starts_with("step:")
		&& let Some(help) = step_command_help(command_name)
	{
		return render_owned_command_help(bin_name, &help);
	}

	if let Some(cli_command) = cli.iter().find(|command| command.name == command_name) {
		if let Some(help) = builtin_helps.iter().find(|help| help.name == command_name) {
			return render_single_command_help(bin_name, help);
		}
		return render_owned_command_help(bin_name, &configured_command_help(cli_command));
	}

	if let Some(help) = builtin_helps.iter().find(|help| help.name == command_name) {
		return render_single_command_help(bin_name, help);
	}

	render_unknown_command_help(bin_name, command_name, &available_command_items(cli))
}

/// Render top-level help listing all commands.
#[allow(dead_code)]
pub fn render_overview_help(bin_name: &str) -> String {
	render_overview_help_with_cli(bin_name, &[])
}

/// Render top-level help listing all built-in, step, and config-defined commands.
pub fn render_overview_help_with_cli(bin_name: &str, cli: &[CliCommandDefinition]) -> String {
	let builtin_helps = builtin_command_helps();
	let mut out = String::new();

	out.push_str(&bordered_header(
		bin_name,
		"monochange — versioning & releases for your monorepo",
		60,
	));
	out.push_str("\n\n");

	out.push_str(&section_heading("Description"));
	out.push_str("\n\n");
	out.push_str(&paint(
		"monochange discovers packages across Cargo, npm/pnpm/Bun, Deno, and Dart/Flutter, \
		then coordinates version bumps, changelogs, and release automation from a single \
		monochange.toml config.\n\n\
		Run `mc help <command>` or `mc <command> -h` for detailed examples and usage tips.",
		muted(),
	));
	out.push_str("\n\n");

	out.push_str(&section_heading("Usage"));
	out.push_str("\n\n");
	out.push_str(&format!(
		"  {}\n\n",
		paint(&format!("Usage: {bin_name} [OPTIONS] <COMMAND>"), accent())
	));

	render_command_section(
		&mut out,
		"Built-in Commands",
		builtin_helps
			.iter()
			.filter(|help| BUILTIN_COMMAND_NAMES.contains(&help.name))
			.map(|help| {
				CommandListItem {
					name: help.name.to_string(),
					summary: help.summary.to_string(),
				}
			})
			.collect(),
	);
	render_command_section(&mut out, "Step Commands", step_command_items());
	render_command_section(
		&mut out,
		"User-defined Commands",
		configured_command_items(cli),
	);

	out.push_str("\n");
	out.push_str(&section_heading("Global Flags"));
	out.push_str("\n\n");
	out.push_str(&format!(
		"  {}   {}\n",
		paint("--quiet  ", flag_style()),
		paint("Suppress output, run in dry-run mode", muted()),
	));
	out.push_str(&format!(
		"  {} {}\n",
		paint("--progress-format", flag_style()),
		paint("<FORMAT>  auto, unicode, ascii, json", muted()),
	));
	out.push_str(&format!(
		"  {}\n",
		paint(
			"Use `mc help <command>` or `mc <command> -h` for detailed command help.",
			accent()
		),
	));

	out
}

fn render_command_section(out: &mut String, title: &str, items: Vec<CommandListItem>) {
	if items.is_empty() {
		return;
	}

	out.push_str(&section_heading(title));
	out.push_str("\n\n");
	let name_width = items.iter().map(|item| item.name.len()).max().unwrap_or(20);
	for item in items {
		let padded = format!("{:width$}", item.name, width = name_width);
		out.push_str(&format!(
			"  {}  {}\n",
			paint(&padded, flag_style()),
			paint(&item.summary, muted()),
		));
	}
	out.push_str("\n");
}

fn configured_command_items(cli: &[CliCommandDefinition]) -> Vec<CommandListItem> {
	cli.iter()
		.filter(|command| !command.name.starts_with("step:"))
		.filter(|command| !BUILTIN_COMMAND_NAMES.contains(&command.name.as_str()))
		.map(|command| {
			CommandListItem {
				name: command.name.clone(),
				summary: command_summary(command),
			}
		})
		.collect()
}

fn available_command_items(cli: &[CliCommandDefinition]) -> Vec<CommandListItem> {
	let mut items = builtin_command_helps()
		.iter()
		.filter(|help| BUILTIN_COMMAND_NAMES.contains(&help.name))
		.map(|help| {
			CommandListItem {
				name: help.name.to_string(),
				summary: help.summary.to_string(),
			}
		})
		.collect::<Vec<_>>();
	items.extend(step_command_items());
	items.extend(configured_command_items(cli));
	items
}

fn step_command_items() -> Vec<CommandListItem> {
	monochange_core::all_step_variants()
		.into_iter()
		.map(|step| {
			let name = format!("step:{}", step.step_kebab_name());
			let summary = step_summary(&step);
			CommandListItem { name, summary }
		})
		.collect()
}

fn command_summary(command: &CliCommandDefinition) -> String {
	command.help_text.clone().unwrap_or_else(|| {
		let steps = command
			.steps
			.iter()
			.map(CliStepDefinition::kind_name)
			.collect::<Vec<_>>()
			.join(" → ");
		if steps.is_empty() {
			"Run a monochange workflow command from monochange.toml".to_string()
		} else {
			format!("Run configured workflow steps: {steps}")
		}
	})
}

fn configured_command_help(command: &CliCommandDefinition) -> OwnedCommandHelp {
	let summary = command_summary(command);
	let step_names = command
		.steps
		.iter()
		.map(|step| format!("{} ({})", step.display_name(), step.kind_name()))
		.collect::<Vec<_>>();
	let description = if step_names.is_empty() {
		"This user-defined command is loaded from `[cli.*]` in monochange.toml. \
		Edit that table to change its inputs, help text, or execution steps."
			.to_string()
	} else {
		format!(
			"This user-defined command is loaded from `[cli.{}]` in monochange.toml. \
			It executes these workflow steps in order:\n\n{}\n\n\
			Edit monochange.toml to change its inputs, help text, or execution steps.",
			command.name,
			step_names
				.iter()
				.map(|step| format!("- {step}"))
				.collect::<Vec<_>>()
				.join("\n")
		)
	};

	OwnedCommandHelp {
		name: command.name.clone(),
		summary,
		description,
		usage: command_usage(&command.name, &command.inputs),
		options: input_options(&command.inputs),
		examples: vec![
			(
				"Run this configured workflow:".to_string(),
				format!("mc {}", command.name),
			),
			(
				"Show help for this workflow:".to_string(),
				format!("mc help {}", command.name),
			),
		],
		tips: vec![
			"User-defined commands come from monochange.toml, not from the binary.".to_string(),
			"Use `mc step:*` commands when you need an immutable built-in step directly."
				.to_string(),
		],
		see_also: command
			.steps
			.iter()
			.map(|step| format!("step:{}", step.step_kebab_name()))
			.collect(),
	}
}

fn step_command_help(command_name: &str) -> Option<OwnedCommandHelp> {
	let kebab = command_name.strip_prefix("step:")?;
	let step = monochange_core::all_step_variants()
		.into_iter()
		.find(|step| step.step_kebab_name() == kebab)?;
	let details = step_details(kebab);
	Some(OwnedCommandHelp {
		name: command_name.to_string(),
		summary: step_summary(&step),
		description: details.description.to_string(),
		usage: command_usage(command_name, &step.step_inputs_schema()),
		options: input_options(&step.step_inputs_schema()),
		examples: details
			.examples
			.iter()
			.map(|(description, command)| ((*description).to_string(), (*command).to_string()))
			.collect(),
		tips: details.tips.iter().map(|tip| (*tip).to_string()).collect(),
		see_also: details
			.see_also
			.iter()
			.map(|command| (*command).to_string())
			.collect(),
	})
}

fn command_usage(command_name: &str, inputs: &[CliInputDefinition]) -> String {
	if inputs.is_empty() {
		format!("mc {command_name}")
	} else {
		format!("mc {command_name} [OPTIONS]")
	}
}

fn input_options(inputs: &[CliInputDefinition]) -> Vec<(String, String, String)> {
	inputs
		.iter()
		.map(|input| {
			let flag = format!("--{}", input.name.replace('_', "-"));
			let type_name = input_type_name(input);
			let description = input
				.help_text
				.clone()
				.unwrap_or_else(|| input_description(input));
			(flag, type_name, description)
		})
		.collect()
}

fn input_type_name(input: &CliInputDefinition) -> String {
	match input.kind {
		CliInputKind::Boolean => String::new(),
		CliInputKind::Path => "<PATH>".to_string(),
		CliInputKind::Choice | CliInputKind::String | CliInputKind::StringList => {
			"<VALUE>".to_string()
		}
	}
}

fn input_description(input: &CliInputDefinition) -> String {
	let mut description = match input.name.as_str() {
		"format" => "Output format".to_string(),
		"package" => "Limit the command to one or more package ids".to_string(),
		"from" | "from-ref" => "Git ref, branch, tag, or commit used as input".to_string(),
		"target" => "Target commit for the operation".to_string(),
		"force" => "Allow an otherwise unsafe operation".to_string(),
		"verify" => "Fail when policy requirements are not satisfied".to_string(),
		"changed_paths" => "Changed paths to evaluate".to_string(),
		"label" => "Pull request label influencing policy evaluation".to_string(),
		"draft" => "Create provider releases as drafts when supported".to_string(),
		"output" => "Path for the generated artifact".to_string(),
		"readiness" => "Path to a publish-readiness artifact".to_string(),
		"resume" => "Path to an existing publish result artifact to resume".to_string(),
		"mode" => "Rate-limit planning mode".to_string(),
		"ci" => "CI provider context used for trust metadata".to_string(),
		"interactive" => "Prompt interactively when supported".to_string(),
		"bump" => "Requested semver bump".to_string(),
		"version" => "Explicit version to request".to_string(),
		"reason" => "Human-readable reason for the change".to_string(),
		"type" => "Change category".to_string(),
		"details" => "Additional markdown body for the changeset".to_string(),
		"changeset" => "Changeset file to inspect".to_string(),
		"fix" => "Apply safe automatic fixes while validating".to_string(),
		"no_verify" => "Skip verification where the workflow explicitly allows it".to_string(),
		// patch-coverage:ignore-start -- static help text is covered by rendered command help snapshots.
		"stage_all" => {
			"Stage every non-ignored working-tree change before the release commit".to_string()
		}
		// patch-coverage:ignore-end
		"auto-close-issues" => "Close linked issues after commenting when supported".to_string(),
		_ => format!("Value for `{}`", input.name.replace('_', "-")),
	};
	if !input.choices.is_empty() {
		description.push_str(&format!(" ({})", input.choices.join(", ")));
	}
	description
}

struct StepDetails {
	description: &'static str,
	examples: &'static [(&'static str, &'static str)],
	tips: &'static [&'static str],
	see_also: &'static [&'static str],
}

fn step_summary(step: &CliStepDefinition) -> String {
	step_summary_for_kind(step.kind_name())
}

fn step_summary_for_kind(kind_name: &str) -> String {
	match kind_name {
		"Config" => "Render resolved monochange configuration and workspace metadata".to_string(),
		"Validate" => "Validate configuration, package manifests, and changesets".to_string(),
		"Discover" => "Discover packages across supported ecosystems".to_string(),
		"DisplayVersions" => "Preview planned versions without modifying files".to_string(),
		"CreateChangeFile" => "Create a structured changeset file".to_string(),
		"AffectedPackages" => "Evaluate affected packages and changeset coverage".to_string(),
		"DiagnoseChangesets" => "Inspect changeset provenance and metadata".to_string(),
		"ReleaseRecord" => "Inspect the monochange release record for a tag or commit".to_string(),
		"PublishReadiness" => {
			"Check package registry publishing readiness without publishing packages".to_string()
		}
		"TagRelease" => "Create and push release tags from an embedded release record".to_string(),
		"RetargetRelease" => "Repair release tags by retargeting a release".to_string(),
		"PrepareRelease" => "Plan version bumps, changelogs, and release artifacts".to_string(),
		"CommitRelease" => "Create a release commit with an embedded release record".to_string(),
		"VerifyReleaseBranch" => {
			"Verify that a release branch still targets a valid base".to_string()
		}
		"PlanPublishRateLimits" => {
			"Plan package publish batches around registry rate limits".to_string()
		}
		"PublishRelease" => "Create or update hosted source-provider releases".to_string(),
		"OpenReleaseRequest" => "Open or update a hosted release pull request".to_string(),
		"CommentReleasedIssues" => {
			"Comment on issues referenced by released changesets".to_string()
		}
		"PlaceholderPublish" => {
			"Publish missing first-time placeholder package versions".to_string()
		}
		"PublishPackages" => "Publish package versions from a publish plan".to_string(),
		"Command" => "Run an arbitrary configured shell command step".to_string(),
		kind_name => format!("Run the built-in {kind_name} step"),
	}
}

fn step_details(kebab: &str) -> StepDetails {
	match kebab {
		"publish-release" => {
			StepDetails {
				description: "PublishRelease converts a prepared release into hosted provider release operations.\n\nFor example, with a configured source provider it can create or update the outward release objects that correspond to monochange's prepared release targets. It does not publish package artifacts to registries; package publishing lives in `mc step:publish-readiness`, `mc publish --readiness <path>`, and `mc placeholder-publish`.\n\nUse it when you want monochange to handle provider-aware publication rather than stitching together release API calls manually. It needs a previous PrepareRelease step in the same workflow and `[source]` configuration.",
				examples: &[
					(
						"Preview provider release payloads:",
						"mc step:publish-release --format json --from-ref HEAD",
					),
					(
						"Compose it after PrepareRelease in monochange.toml:",
						"[[cli.publish-release.steps]]\ntype = \"PrepareRelease\"\n\n[[cli.publish-release.steps]]\ntype = \"PublishRelease\"",
					),
				],
				tips: &[
					"PublishRelease handles hosted/source-provider releases such as GitHub releases, not package registries.",
					"Use `mc step:publish-readiness --from HEAD --output <path>` followed by `mc publish --readiness <path>` for crates.io, npm, JSR, or pub.dev packages.",
					"Dry-run output stays aligned with the prepared release state and release target model.",
				],
				see_also: &["step:prepare-release", "step:publish-readiness", "publish"],
			}
		}
		"prepare-release" => {
			StepDetails {
				description: "PrepareRelease reads pending changesets, computes version bumps, updates manifests and changelogs, and refreshes the cached release manifest used by later stateful steps.",
				examples: &[(
					"Preview release planning:",
					"mc step:prepare-release --format json",
				)],
				tips: &[
					"Use this before CommitRelease, PublishRelease, OpenReleaseRequest, or CommentReleasedIssues in one workflow.",
				],
				see_also: &[
					"step:commit-release",
					"step:publish-release",
					"step:open-release-request",
				],
			}
		}
		"affected-packages" => {
			StepDetails {
				description: "AffectedPackages compares changed paths with workspace package ownership and changeset coverage. In CI it can enforce that pull requests touching published packages include appropriate changesets.",
				examples: &[(
					"Verify changed files in CI:",
					"mc step:affected-packages --format json --verify --changed-paths crates/monochange/src/lib.rs",
				)],
				tips: &[
					"Pass each changed file with `--changed-paths` when your CI provider already computed the diff.",
				],
				see_also: &["change", "check"],
			}
		}
		"create-change-file" => {
			StepDetails {
				description: "CreateChangeFile writes a structured markdown changeset under .changeset/ for one or more package targets, requested bumps, and release-note content.",
				examples: &[(
					"Create a patch changeset:",
					"mc step:create-change-file --package monochange --bump patch --reason \"improve CLI help\"",
				)],
				tips: &["Use package ids rather than legacy manifest paths whenever possible."],
				see_also: &["change", "step:affected-packages"],
			}
		}
		_ => {
			StepDetails {
				description: "This immutable `step:*` command runs one built-in monochange workflow step directly. Step commands are generated by the binary, derive flags from the step schema, and do not require a `[cli.*]` entry in monochange.toml.\n\nUse direct step commands for CI jobs, debugging, or one-off automation; use user-defined commands from monochange.toml when you want to chain multiple steps or expose repository-specific inputs.",
				examples: &[("Run the step directly:", "mc step:discover --format json")],
				tips: &[
					"All CLI steps support an optional `when = \"...\"` condition when composed inside monochange.toml.",
					"The `Command` step is intentionally not exposed as a direct step command because it needs repository configuration.",
				],
				see_also: &["help"],
			}
		}
	}
}

fn render_single_command_help(bin_name: &str, help: &CommandHelp) -> String {
	render_owned_command_help(bin_name, &OwnedCommandHelp::from(help))
}

fn option_label_width(flag: &str, type_name: &str) -> usize {
	flag.len() + 1 + type_name.len()
}

fn render_owned_command_help(bin_name: &str, help: &OwnedCommandHelp) -> String {
	let mut out = String::new();

	// Bordered header
	out.push_str(&bordered_header(
		&format!("{} {}", bin_name, help.name),
		&help.summary,
		60,
	));
	out.push_str("\n\n");

	// Description
	out.push_str(&section_heading("Description"));
	out.push_str("\n\n");
	for line in help.description.lines() {
		if line.is_empty() {
			out.push('\n');
		} else {
			out.push_str(&format!("  {line}\n"));
		}
	}
	out.push('\n');

	// Usage
	out.push_str(&section_heading("Usage"));
	out.push_str("\n\n");
	out.push_str(&format!("  {}\n\n", paint(&help.usage, accent())));

	// Options
	if !help.options.is_empty() {
		out.push_str(&section_heading("Options"));
		out.push_str("\n\n");
		let flag_width = help
			.options
			.iter()
			.map(|(flag, type_name, _)| option_label_width(flag, type_name))
			.max()
			.unwrap_or(20);
		for (flag, type_name, desc) in &help.options {
			let flag_part = paint(flag, flag_style());
			let label_width = option_label_width(flag, type_name);
			let padding = flag_width.saturating_sub(label_width);

			out.push_str("  ");
			out.push_str(&flag_part);
			if !type_name.is_empty() {
				out.push(' ');
				out.push_str(&paint(type_name, value_style()));
			}
			for _ in 0..padding {
				out.push(' ');
			}
			out.push_str("  ");
			out.push_str(&paint(desc, muted()));
			out.push('\n');
		}
		out.push('\n');
	}

	// Examples
	if !help.examples.is_empty() {
		out.push_str(&section_heading("Examples"));
		out.push_str("\n\n");
		for (desc, cmd) in &help.examples {
			out.push_str(&example_block(desc, cmd));
			out.push_str("\n\n");
		}
	}

	// Tips
	if !help.tips.is_empty() {
		out.push_str(&section_heading("Tips"));
		out.push_str("\n\n");
		for tip in &help.tips {
			out.push_str(&format!(
				"  {} {}\n",
				paint("•", accent()),
				multiline_indent(tip, 4),
			));
		}
		out.push('\n');
	}

	// See also
	if !help.see_also.is_empty() {
		out.push_str(&section_heading("See Also"));
		out.push_str("\n\n");
		out.push_str("  ");
		for (index, name) in help.see_also.iter().enumerate() {
			if index > 0 {
				out.push_str("  ");
			}
			out.push_str(&paint(&format!("mc help {name}"), accent()));
		}
		out.push('\n');
	}

	out
}

fn render_unknown_command_help(
	bin_name: &str,
	command_name: &str,
	helps: &[CommandListItem],
) -> String {
	let mut out = String::new();
	out.push_str(&format!(
		"{} Unknown command `{}`\n\n",
		paint("error:", error_style()),
		paint(command_name, flag_style()),
	));
	out.push_str(&format!(
		"  Run {} to see available commands.\n",
		paint(&format!("{bin_name} help"), accent()),
	));

	let name_width = helps.iter().map(|h| h.name.len()).max().unwrap_or(20);
	for help in helps {
		if help.name == command_name {
			continue;
		}
		let padded = format!("{:width$}", help.name, width = name_width);
		out.push_str(&format!(
			"  {}  {}\n",
			paint(&padded, flag_style()),
			paint(&help.summary, muted()),
		));
	}

	out
}

/// Indent continuation lines for a multi-line tip string.
fn multiline_indent(text: &str, indent: usize) -> String {
	let prefix = " ".repeat(indent);
	text.lines()
		.enumerate()
		.map(|(i, line)| {
			if i == 0 {
				line.to_string()
			} else {
				format!("{prefix}{line}")
			}
		})
		.collect::<Vec<_>>()
		.join("\n")
}

#[cfg(test)]
#[path = "__tests__/cli_help_tests.rs"]
mod tests;