rs-rich-cli 0.0.19

The `rich` executable: a Rust port of Python `rich-cli` 1.8.1
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
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
//! The `rich` command line described once, as a [`rich_ext::cli_doc`]
//! [`CommandSpec`], and rendered from there: `--help`, `rich <command>
//! --help`, shell completions, Markdown and man pages, and the configuration
//! reference.
//!
//! This is a binary-boundary convenience (see `docs/PORTING.md`): upstream
//! `rich-cli` prints click's help. The spec composes public `rich-ext` APIs
//! only, and the drift test below keeps it and the hand-written parser in
//! step.

use rich::{Console, Renderable};
use rich_ext::cli_doc::{
    ArgSpec, Choice, CommandSpec, ConfigEntry, ConfigReference, HelpView, Layer, ValueHint,
};

const ARGUMENTS: &str = "Arguments";
const RENDER_MODE: &str = "Render mode";
const INPUT: &str = "Input";
const LAYOUT: &str = "Layout";
const MODE_OPTIONS: &str = "Mode options";
const LOG: &str = "Log";
const SOURCE: &str = "Source";
const IMAGE: &str = "Image";
const INSPECT: &str = "Inspect";
const DIFF: &str = "Diff & ANSI";
const VIEWERS: &str = "Viewers";
const SOURCES: &str = "Diagram sources";
const CHART: &str = "Chart";
const PROFILE: &str = "Profile";
const EXPORT: &str = "Export";
const PAGING: &str = "Paging";
const WATCH: &str = "Watch";
const BATCH: &str = "Batch";
const CONFIG: &str = "Config & theme";
const OUTPUT: &str = "Output & reports";
const DEMO: &str = "Demo";
const GENERAL: &str = "General";

/// The render modes a `mode` config value (and the command word) may name.
pub(crate) const MODES: &[&str] = &[
    "print", "markdown", "rst", "org", "asciidoc", "man", "json", "syntax", "csv", "ipynb",
    "jsonl", "log", "rule", "image", "gif", "diff", "inspect", "ansi",
];

fn flag(long: &str, heading: &str, help: &str) -> ArgSpec {
    ArgSpec::flag(long).heading(heading).help(help)
}

fn option(long: &str, metavar: &str, heading: &str, help: &str) -> ArgSpec {
    ArgSpec::option(long)
        .value_name(metavar)
        .heading(heading)
        .help(help)
}

/// A boolean config flag with its `--no-…` inverse as an alias.
fn switch(long: &str, key: &str, heading: &str, help: &str) -> ArgSpec {
    flag(long, heading, help)
        .alias(format!("no-{long}"))
        .config_key(key)
}

fn mode_command(name: &str, aliases: &[&str], about: &str) -> CommandSpec {
    let mut command =
        CommandSpec::new(name)
            .about(about)
            .usage(format!("{name} [OPTIONS] [RESOURCE]"))
            .arg(ArgSpec::positional("resource").value(ValueHint::Path).help(
                "A file path, an http(s) URL, or `-` for stdin; every `rich` option applies",
            ));
    for alias in aliases {
        command = command.alias(*alias);
    }
    command.args(command_options(name))
}

/// The options `rich <command> --help` lists for the commands that have
/// their own, as copies of the root options (the root keeps their config
/// keys). Every other `rich` option still applies.
fn command_options(name: &str) -> Vec<ArgSpec> {
    let names: &[&str] = match name {
        "view" => &[
            "search",
            "no-line-numbers",
            "sanitize",
            "pager",
            "highlighter",
            "code-theme",
            "width",
        ],
        "hex" => &[
            "search",
            "offset",
            "length",
            "bytes-per-line",
            "group",
            "width",
        ],
        "unicode" => &["limit", "width"],
        "mermaid" => &["mermaid-backend", "width"],
        "dot" => &["dot-backend", "export-svg", "width"],
        "deps" => &[
            "metadata",
            "why",
            "graph",
            "depth",
            "duplicates",
            "no-dev",
            "features",
            "package",
            "timings",
            "audit",
            "licenses",
            "sizes",
            "build-profile",
            "source-sizes",
            "top",
            "baseline",
            "threshold",
            "save",
            "policy",
            "sbom",
            "width",
        ],
        "schema" => &["er", "width"],
        "chart" => &["kind", "x", "y", "width"],
        "profile" => &[
            "sample",
            "columns",
            "top",
            "depth",
            "max-paths",
            "flat",
            "style-rule",
            "rules",
            "report",
            "width",
        ],
        "env" => &["show-secrets", "width"],
        "capture" => &[
            "cast",
            "redact",
            "redact-pattern",
            "redact-preview",
            "sanitize",
            "width",
        ],
        "log" => &[
            "log-presentation",
            "level",
            "where",
            "follow",
            "collapse",
            "group",
            "fields",
            "parser",
            "time",
            "output",
            "summary",
            "interactive",
            "redact",
            "redact-pattern",
            "redact-preview",
            "sanitize",
            "width",
        ],
        "inspect" => &[
            "format",
            "select",
            "filter",
            "highlight",
            "transform",
            "find",
            "flatten",
            "table",
            "max-depth",
            "max-length",
            "show-paths",
            "redact",
            "compare",
        ],
        "ansi" => &["ansi-inline", "escapes-only"],
        "diff" => &[
            "side-by-side",
            "conflicts",
            "context",
            "language",
            "threshold",
            "sanitize",
        ],
        _ => &[],
    };
    let all = root_args();
    names
        .iter()
        .map(|long| {
            let mut arg = all
                .iter()
                .find(|arg| arg.long.as_deref() == Some(*long))
                .unwrap_or_else(|| panic!("no --{long} option"))
                .clone();
            arg.config_key = None;
            // `rich deps` borrows `--top` and `--threshold` from other
            // commands; its help lists them with its own options.
            if name == "deps" && long != &"width" {
                arg.heading = Some(SOURCES.into());
            }
            // `--depth` is `rich deps`'s too; in `rich profile --help` it
            // sits with the profile's own options.
            if name == "profile" && *long == "depth" {
                arg = arg.heading(PROFILE).help(
                    "With `rich profile` of JSON Lines, profile paths at most N steps deep \
                     (default 4), a key or [] each; an object or array at the last step \
                     counts as its JSON text",
                );
            }
            arg
        })
        .collect()
}

fn render_modes() -> Vec<ArgSpec> {
    let mode = |long: &str, short: Option<char>, help: &str| {
        let arg = flag(long, RENDER_MODE, help);
        match short {
            Some(short) => arg.short(short),
            None => arg,
        }
    };
    vec![
        mode(
            "print",
            Some('p'),
            "Treat RESOURCE as literal markup TEXT, not a file path",
        ),
        mode("markdown", Some('m'), "Render RESOURCE as Markdown"),
        mode("rst", None, "Render RESOURCE as reStructuredText"),
        mode(
            "org",
            None,
            "Render RESOURCE as an Org document (headlines, TODO keywords, lists, blocks, \
             tables, drawers)",
        ),
        mode(
            "asciidoc",
            None,
            "Render RESOURCE as AsciiDoc (sections, attributes, lists, source blocks with \
             callouts, tables, admonitions)",
        )
        .alias("adoc"),
        mode(
            "man",
            None,
            "Render RESOURCE as a man page from its roff source (man and mdoc macros, tbl \
             tables)",
        )
        .alias("roff"),
        mode("json", Some('j'), "Pretty-print RESOURCE as JSON").short_alias('J'),
        mode(
            "syntax",
            Some('x'),
            "Syntax-highlight RESOURCE (language from its extension)",
        ),
        mode("csv", None, "Render RESOURCE as a CSV/TSV table"),
        mode("ipynb", None, "Render RESOURCE as a Jupyter notebook"),
        mode("jsonl", None, "Stream JSON Lines / NDJSON records").alias("ndjson"),
        mode(
            "log",
            None,
            "Stream log records: JSON Lines, logfmt and common text formats, filtered, followed \
             and grouped",
        ),
        mode(
            "gif",
            None,
            "Animate GIFs side by side; pipes receive the first frame",
        ),
        mode(
            "rule",
            Some('u'),
            "Draw a horizontal rule (RESOURCE is its title)",
        ),
        mode(
            "diff",
            None,
            "Compare two images perceptually, two text files as a diff (syntax-aware; ANSI \
             captures by visible text and style), or render one patch such as `git diff` output",
        ),
        mode(
            "image",
            None,
            "Render RESOURCE as a still image (ASCII/Braille/blocks/quadrants/Sixel)",
        ),
        mode(
            "inspect",
            None,
            "Explore structured data (JSON, YAML, TOML, XML, INI, dotenv) as a tree",
        ),
        mode(
            "ansi-explain",
            None,
            "Decode every escape sequence in RESOURCE and show the visible text",
        ),
    ]
}

fn diff_options() -> Vec<ArgSpec> {
    vec![
        flag(
            "side-by-side",
            DIFF,
            "With a text --diff, show old and new in two columns",
        ),
        flag(
            "conflicts",
            DIFF,
            "With --diff, show the merge conflicts left in one FILE: numbered, ours, base \
             and theirs side by side when the width allows, with context; exits 0 either way \
             and 4 when the markers do not parse",
        ),
        option(
            "context",
            "N",
            DIFF,
            "With a text --diff, unchanged lines around each change",
        )
        .default_value("3"),
        option(
            "language",
            "NAME",
            DIFF,
            "With a text --diff, highlight as this language instead of guessing from the \
             file name",
        ),
        flag(
            "ansi-inline",
            DIFF,
            "With --ansi-explain, mark escapes inline in the text instead of a table",
        ),
        flag(
            "escapes-only",
            DIFF,
            "With --ansi-explain, list only escape sequences, not text runs",
        ),
    ]
}

fn viewer_options() -> Vec<ArgSpec> {
    vec![
        option(
            "search",
            "TEXT",
            VIEWERS,
            "With `rich view`, highlight every case-insensitive match and mark its line; with \
             `rich hex`, highlight a byte string: hex such as `de ad` or `0xDEAD`, or quoted \
             text such as '\"PNG\"'",
        ),
        flag(
            "no-line-numbers",
            VIEWERS,
            "With `rich view`, leave out the line-number gutter",
        ),
        option(
            "offset",
            "N",
            VIEWERS,
            "With `rich hex`, start N bytes in (decimal or 0x hex)",
        ),
        option(
            "length",
            "N",
            VIEWERS,
            "With `rich hex`, show at most N bytes",
        ),
        option(
            "bytes-per-line",
            "N",
            VIEWERS,
            "With `rich hex`, bytes on each line (default: fit the width, up to 16)",
        ),
        option(
            "group",
            "N|BY",
            VIEWERS,
            "With `rich hex`, bytes per group (default 8); with `rich log`, group records by \
             trace, span or correlation id (trace_id, span_id, correlation_id, request_id, ...) \
             under a header each, with a summary at the end",
        ),
        option(
            "limit",
            "N",
            VIEWERS,
            "With `rich unicode`, show at most N graphemes",
        ),
        flag(
            "show-secrets",
            VIEWERS,
            "With `rich env`, show values whose names look secret instead of masking them",
        ),
        option(
            "cast",
            "FILE",
            VIEWERS,
            "With `rich capture`, also write an asciicast v2 recording (`asciinema play FILE`)",
        ),
        option(
            "redact-pattern",
            "REGEX",
            VIEWERS,
            "With `rich capture` and `rich log`, also mask matches of REGEX (only its `secret` \
             group when it has one); repeatable. Experimental: check the output before sharing \
             it",
        )
        .multiple(true),
        flag(
            "redact-preview",
            VIEWERS,
            "With `rich capture` or `rich log` and --redact or --redact-pattern, show each \
             match on its line, highlighted, with its rule and replacement, and a summary, \
             instead of the output; write nothing (no cast, export or log output) and name on \
             stderr what would have been written. The preview shows the secrets unmasked. With \
             --report json, the findings as one JSON document on stdout",
        ),
    ]
}

fn input_options() -> Vec<ArgSpec> {
    let mut args = vec![option(
        "format",
        "F",
        INPUT,
        "Input format. With --inspect, the parser (default auto-detect); otherwise `auto` \
         detects piped or extensionless input and routes it (JSON to --json, other formats \
         to highlighting, likely Markdown, reStructuredText, Org, AsciiDoc and man pages to \
         their renderers, anything else to plain text), and a named format overrides the \
         extension. A document format (markdown, rst, org, asciidoc, man) is the same as its \
         mode flag",
    )
    .choices([
        "auto", "json", "yaml", "toml", "xml", "ini", "env", "markdown", "rst", "org", "asciidoc",
        "man",
    ])
    .default_value("auto")
    .config_key("format")];
    args.extend(
        rich_ext::cli::arg_specs()
            .into_iter()
            .filter(|arg| arg.id == "encoding")
            .map(|arg| arg.heading(INPUT)),
    );
    args
}

fn layout_options() -> Vec<ArgSpec> {
    vec![
        option(
            "width",
            "N",
            LAYOUT,
            "Render the output N columns wide (the console keeps its own width, so \
             --left/--center/--right still use it)",
        )
        .short('w')
        .config_key("width"),
        option(
            "max-width",
            "SIZE",
            LAYOUT,
            "Print at most SIZE columns wide (upstream's `console.print(width=…)`)",
        )
        .short('W'),
        flag("left", LAYOUT, "Left-justify output").short('l'),
        flag("center", LAYOUT, "Center output").short('c'),
        flag("right", LAYOUT, "Right-justify output").short('r'),
        flag(
            "text-left",
            LAYOUT,
            "Justify --print and --rule text to the left",
        )
        .short('L'),
        flag(
            "text-center",
            LAYOUT,
            "Justify --print and --rule text to the center",
        )
        .short('C'),
        flag(
            "text-right",
            LAYOUT,
            "Justify --print and --rule text to the right",
        )
        .short('R'),
        flag(
            "text-full",
            LAYOUT,
            "Justify --print text to both edges (a rule centres its title)",
        )
        .short('F'),
        flag(
            "soft",
            LAYOUT,
            "Soft wrap: no line is wrapped or cut, so the terminal wraps it",
        ),
        flag(
            "emoji",
            LAYOUT,
            "Replace :emoji: codes, e.g. :sparkles: (off by default, as upstream)",
        ),
        option(
            "panel",
            "BOX",
            LAYOUT,
            "Wrap output in a panel, shrunk to fit its content (none = no panel)",
        )
        .choices(
            super::PANEL_BOXES
                .iter()
                .map(|(name, _)| *name)
                .chain(["none"]),
        )
        .short('a')
        .config_key("panel"),
        option(
            "padding",
            "P",
            LAYOUT,
            "Wrap output in padding (1, 2, or 4 comma-separated ints)",
        )
        .short('d')
        .config_key("padding"),
        flag(
            "expand",
            LAYOUT,
            "Make --panel/--padding fill the width instead of fitting (implied by --width)",
        )
        .short('e'),
        option(
            "title",
            "T",
            LAYOUT,
            "Panel title; also the CSV table's title",
        ),
        option(
            "caption",
            "T",
            LAYOUT,
            "Panel subtitle; also the CSV table's caption",
        ),
        option(
            "style",
            "S",
            LAYOUT,
            "Style laid under the whole output, e.g. \"bold red\"",
        )
        .short('s'),
        option(
            "panel-style",
            "S",
            LAYOUT,
            "Panel border style, e.g. \"dim\" (with --panel)",
        )
        .short('S'),
        flag(
            "hyperlinks",
            LAYOUT,
            "Render a Markdown link as a clickable OSC 8 hyperlink. Off by default, which \
             shows the URL as `text (url)`",
        )
        .short('y'),
    ]
}

/// Upstream's options for source: `--syntax`, automatic mode, and the code
/// cells of `--ipynb`; `--head`/`--tail` also cut `--csv` rows.
fn source_options() -> Vec<ArgSpec> {
    vec![
        option(
            "head",
            "LINES",
            SOURCE,
            "Show the first LINES of a source file or CSV (as upstream, `-h` is --head, \
             not help)",
        )
        .short('h'),
        option(
            "tail",
            "LINES",
            SOURCE,
            "Show the end of a source file or CSV (as upstream, --tail N shows the last N-1 \
             source lines)",
        )
        .short('t'),
        flag("line-numbers", SOURCE, "Number the lines of source").short('n'),
        flag("guides", SOURCE, "Draw indentation guides in source").short('g'),
        option(
            "lexer",
            "LEXER",
            SOURCE,
            "Highlight source as LEXER instead of guessing from the file name; with --rst, \
             --org, --asciidoc or --man, highlight code blocks that name no language as LEXER",
        ),
        flag(
            "no-wrap",
            SOURCE,
            "Crop long lines of source instead of wrapping them; with --print, don't wrap text",
        ),
        flag(
            "infer",
            SOURCE,
            "With --csv (or a .csv/.tsv file), infer each column's type (integer, float, \
             boolean, date, timestamp, text) and show it under the heading; number columns \
             right-align even with null tokens such as NA. Without it the table is rich-cli's",
        ),
    ]
}

fn log_options() -> Vec<ArgSpec> {
    vec![
        option(
            "level",
            "LEVEL",
            LOG,
            "With `rich log`, keep only records at LEVEL (trace, debug, info, warn, error or \
             fatal), or at it and above with a `+` (`warn+`); several with commas or by \
             repeating it",
        )
        .multiple(true),
        option(
            "where",
            "EXPR",
            LOG,
            "With `rich log`, keep only records whose fields pass EXPR: FIELD=VALUE, \
             FIELD!=VALUE, FIELD>N (and >=, <, <=), FIELD~REGEX, FIELD!~REGEX, FIELD (it is \
             there) or !FIELD (it is not); numbers, levels and times compare as such, dots \
             reach nested fields; repeatable, and every one must pass",
        )
        .multiple(true),
        flag(
            "follow",
            LOG,
            "With `rich log`, read FILE, then what is appended to it, starting again when it \
             is truncated or replaced (log rotation), until Ctrl+C",
        )
        .short('f'),
        flag(
            "collapse",
            LOG,
            "With `rich log`, fold a run of identical records (their times aside) into the \
             first and a `× N` count; on by default with --follow and --interactive",
        )
        .alias("no-collapse"),
        flag(
            "fields",
            LOG,
            "With `rich log`, list each record's fields under it, one a line, instead of on \
             its line",
        ),
        option(
            "parser",
            "NAME",
            LOG,
            "With `rich log`, how lines that are not JSON are read: auto tries each format; \
             logfmt, level (`LEVEL message`), timestamp (a line that starts with a time) and \
             access (nginx and Apache) read one; text reads none. A line no parser reads \
             joins the record before it, or is shown as text",
        )
        .choices(["auto", "logfmt", "level", "timestamp", "access", "text"])
        .default_value("auto"),
        option(
            "time",
            "MODE",
            LOG,
            "With `rich log`, show each time as written (absolute), since the first record \
             shown (relative), or not at all (none)",
        )
        .choices(["absolute", "relative", "none"])
        .default_value("absolute"),
        option(
            "output",
            "FORMAT",
            LOG,
            "With `rich log`, write the records kept as text, or as one JSON object a line \
             (jsonl) for another program",
        )
        .choices(["text", "jsonl"])
        .default_value("text"),
        flag(
            "summary",
            LOG,
            "With `rich log`, end with a summary: records shown, counts by level and, with \
             --group, the groups with errors; on by default with --group (on standard error \
             with --output jsonl)",
        )
        .alias("no-summary"),
        flag(
            "interactive",
            LOG,
            "With `rich log` on a terminal, show the records in a live view that follows the \
             end: / filters, Space pauses, Enter shows a record's fields, q quits; without a \
             terminal the records stream as usual",
        )
        .short('i'),
    ]
}

fn mode_options() -> Vec<ArgSpec> {
    let mut args = vec![
        option(
            "rule-style",
            "STYLE",
            MODE_OPTIONS,
            "With --rule, the line's style",
        )
        .default_value("bright_green"),
        option(
            "rule-char",
            "CHARACTER",
            MODE_OPTIONS,
            "With --rule, the character(s) the line is drawn with",
        )
        .default_value("─"),
        option(
            "log-presentation",
            "MODE",
            MODE_OPTIONS,
            "With --log, select log presentation",
        )
        .choices(["plain", "rich"])
        .default_value("plain")
        .config_key("log_presentation"),
        option(
            "mermaid-backend",
            "BACKEND",
            MODE_OPTIONS,
            "How Mermaid diagrams (`rich mermaid`, ```mermaid fences in Markdown) are drawn: text \
             draws flowcharts as text, mmdc uses Mermaid's own CLI (a build with the mmdc \
             feature; `rich mermaid` tries it first there), off leaves fences as code",
        )
        .choices(["text", "mmdc", "off"])
        .config_key("mermaid_backend"),
        option(
            "dot-backend",
            "BACKEND",
            MODE_OPTIONS,
            "How DOT graphs (`rich dot`, ```dot fences in Markdown) are drawn: text draws them \
             natively, graphviz also makes --export-svg write Graphviz's own SVG (the `dot` \
             program, installed separately), off leaves fences as code",
        )
        .choices(["text", "graphviz", "off"])
        .config_key("dot_backend"),
        option(
            "metadata",
            "FILE",
            SOURCES,
            "With `rich deps`, read `cargo metadata --format-version 1` JSON from FILE (`-` for \
             stdin) instead of running cargo",
        ),
        option(
            "why",
            "CRATE",
            SOURCES,
            "With `rich deps`, show what pulls CRATE (`name` or `name@version`) in: every path \
             from the workspace to it",
        ),
        flag(
            "graph",
            SOURCES,
            "With `rich deps`, draw the dependencies (or --why's paths) as a diagram instead of \
             a tree",
        ),
        option(
            "depth",
            "N",
            SOURCES,
            "With `rich deps`, show at most N levels below each workspace member; with `rich \
             profile`, profile JSON paths at most N steps deep (default 4), an object or array \
             at the last step counting as its JSON text",
        ),
        flag(
            "duplicates",
            SOURCES,
            "With `rich deps`, keep only the branches that lead to a crate resolved at more than \
             one version, and add a consolidation summary: who pulls each version, and which \
             version most of them already use",
        ),
        flag(
            "no-dev",
            SOURCES,
            "With `rich deps`, leave dev-dependencies out",
        ),
        flag(
            "features",
            SOURCES,
            "With `rich deps`, show the features Cargo resolved for each crate: what each turns \
             on, and which dependents asked for them; every crate with a feature, or only \
             --package's",
        ),
        option(
            "package",
            "CRATE",
            SOURCES,
            "With `rich deps --features`, show only CRATE, as `name` or `name@version`",
        ),
        option(
            "timings",
            "FILE",
            SOURCES,
            "With `rich deps`, show a `cargo build --timings` report (the HTML file, or \
             timing-info JSON lines; `-` for stdin): the slowest units as bars and a table; \
             runs nothing",
        ),
        option(
            "audit",
            "FILE",
            SOURCES,
            "With `rich deps`, show `cargo audit --json` output from FILE (`-` for stdin) grouped \
             by severity; exits 5 when it lists a vulnerability. Runs no scanner",
        ),
        flag(
            "licenses",
            SOURCES,
            "With `rich deps`, group the crates by licence, marking copyleft, unknown and missing \
             licences",
        ),
        flag(
            "sizes",
            SOURCES,
            "With `rich deps`, show artifact sizes per crate as bars and a table: from the target \
             directory given next (default: the one `cargo metadata` names), matched to packages \
             through `cargo metadata`, or from a file given next: `cargo bloat --crates \
             --message-format json` output or a report saved with --save (`-` for stdin)",
        ),
        option(
            "build-profile",
            "NAME",
            SOURCES,
            "With `rich deps --sizes`, read this profile's artifacts: debug (dev), release, or a \
             custom profile (default: debug if it was built, else release)",
        ),
        flag(
            "source-sizes",
            SOURCES,
            "With `rich deps --sizes`, also add up each package's source files (its manifest \
             directory, without target/, .git/ and nested packages)",
        ),
        option(
            "baseline",
            "FILE",
            SOURCES,
            "With `rich deps --timings` or `--sizes`, compare with an older report, unit by unit \
             (a report saved with --save, or the original format: a timings report, cargo-bloat \
             JSON or a target directory); exits 5 when a unit or the total regressed past \
             --threshold",
        ),
        option(
            "save",
            "FILE",
            SOURCES,
            "With `rich deps --timings` or `--sizes`, also write the report as JSON to FILE, to \
             keep as a --baseline",
        ),
        option(
            "policy",
            "FILE",
            SOURCES,
            "With `rich deps`, check every crate's licence against a policy: a TOML allow and deny \
             list, or cargo-deny's deny.toml; conflicts with the root crate's licence are shown \
             too (a heuristic, not legal advice). Exits 5 on a violation",
        ),
        option(
            "sbom",
            "FILE",
            SOURCES,
            "With `rich deps --licenses` or `--policy`, read the packages from an SPDX (2.2, 2.3) \
             or CycloneDX (1.4 to 1.6) JSON document instead of `cargo metadata`",
        ),
        flag(
            "er",
            SOURCES,
            "With `rich schema`, draw the schema as an ER diagram: a box per table with its \
             columns and keys, an edge per foreign key",
        ),
        option(
            "kind",
            "KIND",
            CHART,
            "With `rich chart`, what to draw: spark (a sparkline per series), bar (a bar per \
             row, labelled by --x), line (the default) and scatter (each series against --x, or \
             the row number), or heatmap (rows by series, each value a shade)",
        )
        .choices(["spark", "bar", "line", "scatter", "heatmap"]),
        option(
            "x",
            "COLUMN",
            CHART,
            "With `rich chart`, the column of x positions (line, scatter) or labels (bar, \
             heatmap), by header or 1-based number (default: the row number; for labels, the \
             first column that is not all numbers)",
        ),
        option(
            "y",
            "COLUMN",
            CHART,
            "With `rich chart`, a column to draw, by header or 1-based number; repeatable, one \
             series each (default: every numeric column but --x)",
        )
        .multiple(true),
        option(
            "sample",
            "N",
            PROFILE,
            "With `rich profile`, the most rows kept for types, statistics and distributions \
             (default 10000): a longer input is sampled uniformly, the sample size is printed, \
             and nulls still count every row",
        ),
        option(
            "columns",
            "COLUMN[,COLUMN...]",
            PROFILE,
            "With `rich profile`, profile only these columns, by header, in this order; \
             repeatable",
        )
        .multiple(true),
        option(
            "top",
            "N",
            PROFILE,
            "With `rich profile`, the most common values shown for a column that is not \
             numeric (default 5); with `rich deps --sizes`, `--timings` or `--baseline`, the \
             units shown (default 20)",
        ),
        option(
            "max-paths",
            "N",
            PROFILE,
            "With `rich profile` of JSON Lines, profile at most N paths (default 200), in the \
             order first seen; the rest are noted",
        ),
        flag(
            "flat",
            PROFILE,
            "With `rich profile` of JSON Lines, profile rows of the top-level keys (nested \
             values as JSON text) instead of every nested path",
        ),
        option(
            "style-rule",
            "RULE",
            PROFILE,
            "Style the cells, rows or columns of a data table by value: COLUMN OP VALUE:STYLE \
             or COLUMN OP VALUE:row:STYLE (e.g. 'latency>500:bold red', \
             'status=failed:row:on red'); OP is == != < <= > >= *= ^= $= or a word such as \
             contains or empty. Repeatable; applies to --csv, `rich profile`'s table and \
             `rich inspect --table`, and has no effect elsewhere",
        )
        .multiple(true)
        .config_key("rules"),
        option(
            "rules",
            "FILE",
            PROFILE,
            "Read style rules from FILE's [[rules]] tables (column, op, value, style, target), \
             after the rules before it",
        )
        .value(ValueHint::Path)
        .multiple(true),
        option(
            "highlighter",
            "NAME",
            MODE_OPTIONS,
            "The code highlighter for source, Markdown code, view and diff: syntect (the \
             default), or lumis in a build with the lumis feature",
        )
        .config_key("highlighter"),
        option(
            "code-theme",
            "NAME",
            MODE_OPTIONS,
            "A theme of the chosen code highlighter, e.g. ansi_dark (`rich doctor --report json` \
             lists them)",
        )
        .config_key("code_theme"),
        option(
            "loop",
            "N",
            MODE_OPTIONS,
            "With --gif, repeat N times (default 1; 0 = forever)",
        ),
        option(
            "speed",
            "X",
            MODE_OPTIONS,
            "With --gif, play X times as fast (0.5 = half speed; default 1)",
        ),
        option(
            "fps",
            "N",
            MODE_OPTIONS,
            "With --gif, draw at most N frames a second (default 30)",
        ),
        option(
            "max-frames",
            "N",
            MODE_OPTIONS,
            "With --gif, stop each animation after N frames, even with --loop 0",
        ),
        option(
            "max-duration",
            "SECS",
            MODE_OPTIONS,
            "With --gif, stop after SECS seconds, whatever is still playing",
        ),
    ];
    args.extend(
        rich_ext::cli::arg_specs()
            .into_iter()
            .filter(|arg| arg.id == "gif-mode")
            .map(|arg| arg.heading(MODE_OPTIONS)),
    );
    args
}

fn image_options() -> Vec<ArgSpec> {
    let tone = "Tone adjustment (1.0 = unchanged)";
    vec![
        option(
            "image-mode",
            "M",
            IMAGE,
            "With --diff/--image, how to draw the picture: sixel draws real pixels; blocks, \
             quadrants, braille and ascii draw characters (--image rejects none: there would \
             be nothing to draw)",
        )
        .choices([
            "auto",
            "sixel",
            "blocks",
            "quadrants",
            "braille",
            "ascii",
            "none",
        ])
        .default_value("auto"),
        option(
            "height",
            "N",
            IMAGE,
            "With --image, render this many rows instead of the backend's default",
        )
        .config_key("height"),
        option(
            "image-fit",
            "M",
            IMAGE,
            "With --image and --height: contain letterboxes, cover crops at --image-anchor, \
             stretch fills ignoring aspect. native needs no --height: it renders the image at \
             its own pixel size (never enlarged), capped by --width, the terminal and \
             --image-max-width/--image-max-height",
        )
        .choices(["contain", "cover", "stretch", "native"])
        .config_key("image_fit"),
        option("image-anchor", "A", IMAGE, "Cover crop anchor")
            .choices([
                "center",
                "top",
                "bottom",
                "left",
                "right",
                "top-left",
                "top-right",
                "bottom-left",
                "bottom-right",
            ])
            .default_value("center")
            .config_key("image_anchor"),
        option(
            "image-max-width",
            "N",
            IMAGE,
            "With --image, never exceed N columns (aspect kept)",
        )
        .config_key("image_max_width"),
        option(
            "image-max-height",
            "N",
            IMAGE,
            "With --image, never exceed N rows (aspect kept)",
        )
        .config_key("image_max_height"),
        option(
            "image-background",
            "BG",
            IMAGE,
            "With --image: flatten transparency onto #RRGGBB (also colours contain padding; \
             quote the # in your shell), leave it to the terminal's background (default), or \
             show it on a gray checkerboard (checkerboard)",
        )
        .config_key("image_background"),
        option(
            "image-color",
            "M",
            IMAGE,
            "Colour depth for --image (not Braille, which is monochrome) and --gif frames",
        )
        .choices(["truecolor", "ansi256", "ansi16", "grayscale"])
        .default_value("truecolor")
        .config_key("image_color"),
        option(
            "image-dither",
            "M",
            IMAGE,
            "Dithering (needs a non-truecolor --image-color)",
        )
        .choices(["none", "floyd-steinberg", "bayer4x4", "atkinson"])
        .default_value("none")
        .config_key("image_dither"),
        option(
            "image-color-distance",
            "M",
            IMAGE,
            "How the nearest palette colour is measured: encoded RGB, or perceptual OKLab \
             (needs a non-truecolor --image-color)",
        )
        .choices(["rgb", "oklab"])
        .default_value("rgb")
        .config_key("image_color_distance"),
        option(
            "image-brightness",
            "F",
            IMAGE,
            "Tone adjustment (1.0 = unchanged). Brightness, contrast and gamma apply in that \
             order, after rotation/flips and before grayscale and colour",
        )
        .default_value("1.0")
        .config_key("image_brightness"),
        option("image-contrast", "F", IMAGE, tone)
            .default_value("1.0")
            .config_key("image_contrast"),
        option("image-gamma", "F", IMAGE, tone)
            .default_value("1.0")
            .config_key("image_gamma"),
        option("image-rotate", "N", IMAGE, "Rotate still images clockwise")
            .choices(["0", "90", "180", "270"])
            .default_value("0")
            .config_key("image_rotate"),
        switch(
            "image-flip-horizontal",
            "image_flip_horizontal",
            IMAGE,
            "Flip still images horizontally, after rotation",
        ),
        switch(
            "image-flip-vertical",
            "image_flip_vertical",
            IMAGE,
            "Flip still images vertically, after rotation",
        ),
        switch(
            "image-grayscale",
            "image_grayscale",
            IMAGE,
            "Composite and convert still images to grayscale",
        ),
        option(
            "threshold",
            "PCT",
            IMAGE,
            "With --diff, exit non-zero above PCT% changed (pixels for images, lines for \
             text). Also sets the exit code: 0 within, 5 over. With `rich deps --baseline`, what \
             counts as a regression: a percentage (default 10%) and a floor the change must \
             also pass, a time (`0.5s`, the default for --timings) or a size (`64KiB`, the \
             default for --sizes); give it twice for both",
        ),
    ]
}

fn inspect_options() -> Vec<ArgSpec> {
    vec![
        option(
            "select",
            "EXPR",
            INSPECT,
            "Show only what a JSONPath expression selects, e.g. `$.servers[*].name`",
        ),
        option(
            "filter",
            "PATTERN",
            INSPECT,
            "Keep only what matches: with --inspect, the values a JSONPath selects and the \
             containers above them; for text, --print and --syntax, the lines a regular \
             expression matches",
        ),
        option(
            "highlight",
            "PATTERN",
            INSPECT,
            "Highlight what matches, in reverse video: with --inspect, the tree lines a \
             JSONPath selects; for text, --print and --syntax, the matches of a regular \
             expression",
        ),
        option(
            "transform",
            "NAME",
            INSPECT,
            "Apply a registered text transform, such as one a --plugin adds, to text, \
             --print and --syntax; repeatable, applied in the order given, after --filter and \
             before --highlight. `rich plugins list` shows the names",
        )
        .multiple(true),
        option(
            "find",
            "TEXT",
            INSPECT,
            "Search keys and values (case-insensitive), highlighting matches",
        ),
        flag(
            "flatten",
            INSPECT,
            "Show `path = value` rows instead of a tree",
        ),
        flag(
            "table",
            INSPECT,
            "Show records, or a path/value table, instead of a tree",
        ),
        option(
            "max-depth",
            "N",
            INSPECT,
            "Fold containers deeper than N levels",
        ),
        option(
            "max-length",
            "N",
            INSPECT,
            "Show at most N items per container",
        ),
        flag("show-paths", INSPECT, "Append each value's path"),
        flag(
            "redact",
            INSPECT,
            "Mask secret-looking keys such as password, token and api_key. With `rich \
             capture`, mask secrets in the output before it is shown, exported or recorded: \
             secret-named `key=value` values, bearer tokens, GitHub/GitLab/Slack/Stripe/npm/`sk-` \
             tokens, AWS key ids, JWTs and URL passwords; with `rich log`, the same in each \
             record's fields and lines. Capture redaction is experimental and best effort: \
             check the output before sharing it",
        ),
        option(
            "compare",
            "PATH",
            INSPECT,
            "Show added, removed and changed values against another document",
        )
        .value(ValueHint::File),
    ]
}

fn export_options() -> Vec<ArgSpec> {
    vec![
        option(
            "export-html",
            "PATH",
            EXPORT,
            "Also write a self-contained HTML document to PATH",
        )
        .short('o')
        .value(ValueHint::File)
        .config_key("export_html"),
        option(
            "export-svg",
            "PATH",
            EXPORT,
            "Also write an SVG document to PATH. Unlike the HTML, it references its font \
             from a CDN, so it is not self-contained offline.",
        )
        .value(ValueHint::File)
        .config_key("export_svg"),
        option(
            "export-png",
            "PATH",
            EXPORT,
            "Also write a PNG image of the output to PATH: the terminal's width, rich's SVG \
             export theme, in a window titled like the SVG's, drawn with an embedded DejaVu \
             Sans Mono so it is the same on every machine (the png feature; not with --batch)",
        )
        .value(ValueHint::File),
    ]
}

fn paging_options() -> Vec<ArgSpec> {
    vec![
        flag(
            "pager",
            PAGING,
            "Page terminal output via MANPAGER, PAGER, then less/more.com",
        )
        .config_key("pager"),
        flag("no-pager", PAGING, "Disable explicit and automatic paging"),
        flag(
            "auto-pager",
            PAGING,
            "Page only terminal output taller than the viewport",
        )
        .config_key("auto_pager"),
        flag("no-auto-pager", PAGING, "Disable automatic paging"),
    ]
}

fn watch_options() -> Vec<ArgSpec> {
    vec![
        switch(
            "watch",
            "watch",
            WATCH,
            "Re-render changing files (several allowed) or one URL while stdout is a \
             terminal; each file gets its own live region",
        ),
        option("watch-interval", "SEC", WATCH, "Poll interval in seconds")
            .alias("interval")
            .default_value("1")
            .config_key("watch_interval"),
        option(
            "watch-debounce",
            "SEC",
            WATCH,
            "Quiet period collapsing a burst of file events",
        )
        .default_value("0.1")
        .config_key("watch_debounce"),
        switch(
            "watch-poll",
            "watch_poll",
            WATCH,
            "Poll local files at --watch-interval instead of file events",
        ),
        switch(
            "watch-exit-on-error",
            "watch_exit_on_error",
            WATCH,
            "End the watch with a non-zero exit when a render fails",
        ),
        switch(
            "watch-cache",
            "watch_cache",
            WATCH,
            "With URLs, render only when the response body changes",
        ),
    ]
}

fn batch_options() -> Vec<ArgSpec> {
    vec![
        switch(
            "batch",
            "batch",
            BATCH,
            "Convert explicit files, directories, or globs deterministically",
        ),
        option(
            "batch-input-root",
            "PATH",
            BATCH,
            "The directory --batch-preserve-dirs keeps paths relative to",
        )
        .value(ValueHint::Dir)
        .config_key("batch_input_root"),
        switch(
            "batch-preserve-dirs",
            "batch_preserve_dirs",
            BATCH,
            "Preserve paths under --batch-input-root PATH",
        ),
        option(
            "batch-name-template",
            "TEMPLATE",
            BATCH,
            "Name export leaves; export paths become directories",
        )
        .config_key("batch_name_template"),
        option(
            "jobs",
            "N",
            BATCH,
            "Parallel file-export workers; requires --batch. Terminal output stays in input \
             order; active jobs finish on error.",
        )
        .default_value("1")
        .config_key("jobs"),
        switch(
            "progress",
            "progress",
            BATCH,
            "Enable/disable batch counts on terminal stderr; hidden for redirected stderr, \
             JSON reports and dry runs",
        )
        .default_value("true"),
        flag(
            "dry-run",
            BATCH,
            "Validate and show the batch plan without writing files",
        ),
        switch(
            "continue-on-error",
            "continue_on_error",
            BATCH,
            "Process all planned inputs and aggregate failures",
        ),
        switch(
            "overwrite",
            "overwrite",
            BATCH,
            "Allow existing batch export destinations",
        ),
        option(
            "collision",
            "P",
            BATCH,
            "Batch policy for existing destinations",
        )
        .choices(["error", "overwrite", "suffix"])
        .default_value("error")
        .config_key("collision"),
    ]
}

fn config_options() -> Vec<ArgSpec> {
    vec![
        option(
            "config",
            "PATH",
            CONFIG,
            "Read versioned TOML defaults from PATH",
        )
        .value(ValueHint::File),
        option("profile", "NAME", CONFIG, "Select a config profile").default_value("default"),
        flag("no-config", CONFIG, "Disable config discovery"),
        option("theme", "NAME", CONFIG, "Select a named theme from config").config_key("theme"),
        option(
            "theme-style",
            "NAME=STYLE",
            CONFIG,
            "Override a theme binding; repeatable and worker-safe",
        )
        .multiple(true),
        option(
            "theme-file",
            "PATH",
            CONFIG,
            "Load styles from an upstream rich theme file ([styles] section); --theme and \
             --theme-style override it",
        )
        .config_key("theme_file"),
        option(
            "plugin",
            "PATH",
            CONFIG,
            "Load a runtime plugin: a WASM module (.wasm, sandboxed) or a native library \
             (.so, .dylib, .dll, which runs its own code), in a build with the wasm-plugins \
             or dylib-plugins feature; repeatable. `rich plugins list` shows what is loaded",
        )
        .value(ValueHint::File)
        .multiple(true)
        .config_key("plugins"),
        switch(
            "micro-project",
            "micro_project",
            CONFIG,
            "Load micro assets from the project's .rich/micro/ (off by default: a project's \
             assets load only when you trust it; ./rich.toml cannot turn this on)",
        ),
    ]
}

fn output_options() -> Vec<ArgSpec> {
    vec![
        flag(
            "no-color",
            OUTPUT,
            "Disable colored output (as does a non-empty NO_COLOR)",
        )
        .env("NO_COLOR")
        .config_key("no_color"),
        flag(
            "color",
            OUTPUT,
            "Override a config no_color setting (pipes remain plain)",
        ),
        switch(
            "sanitize",
            "sanitize",
            OUTPUT,
            "Replace input terminal controls, JSON/notebook strings, titles and captions \
             with visible inert text. On by default for `rich view`, `rich schema`, `rich log` \
             and text `rich diff`; --no-sanitize turns it off there",
        ),
        option(
            "report",
            "F",
            OUTPUT,
            "Emit a result/error envelope on stderr",
        )
        .choices(["human", "json"])
        .default_value("human"),
        flag("machine-json", OUTPUT, "Alias for --report json"),
    ]
}

fn demo_options() -> Vec<ArgSpec> {
    vec![
        flag(
            "demo",
            DEMO,
            "Guided suite tour; pauses 3 seconds between sections on a TTY",
        ),
        flag(
            "demo-list",
            DEMO,
            "List stable tour sections: core, workflows, art",
        ),
        option(
            "demo-section",
            "NAME",
            DEMO,
            "With --demo, play one section only",
        )
        .choices(["core", "workflows", "art"]),
        option(
            "demo-delay",
            "SECONDS",
            DEMO,
            "Tour pause (0–60); no pauses when redirected; Ctrl+C stops",
        )
        .default_value("3"),
    ]
}

fn general_options() -> Vec<ArgSpec> {
    vec![
        flag("help", GENERAL, "Show this help"),
        flag("version", GENERAL, "Show the rs-rich-cli package version")
            .short('V')
            .short_alias('v'),
        flag(
            "force-terminal",
            GENERAL,
            "Write colour and styles even when output is not a terminal",
        ),
    ]
}

/// Every config key, for `config explain` completion and suggestions.
fn config_keys() -> Vec<String> {
    let mut keys = vec!["mode".to_string()];
    keys.extend(
        root_args()
            .into_iter()
            .filter_map(|arg| arg.config_key)
            .filter(|key| key != "mode"),
    );
    keys
}

fn root_args() -> Vec<ArgSpec> {
    let mut args = vec![ArgSpec::positional("resource")
        .value(ValueHint::Path)
        .multiple(true)
        .heading(ARGUMENTS)
        .help(
            "A file path, an http(s) URL, or `-` for stdin (several with --batch, --watch \
             or --gif)",
        )];
    for group in [
        render_modes(),
        input_options(),
        layout_options(),
        source_options(),
        mode_options(),
        log_options(),
        image_options(),
        inspect_options(),
        diff_options(),
        viewer_options(),
        export_options(),
        paging_options(),
        watch_options(),
        batch_options(),
        config_options(),
        output_options(),
        demo_options(),
        general_options(),
    ] {
        args.extend(group);
    }
    args
}

fn config_command() -> CommandSpec {
    let same = "[OPTIONS]";
    CommandSpec::new("config")
        .about("Show, validate, explain or document configuration")
        .long_about(
            "Show, validate, explain or document configuration.\n\n\
             Each subcommand accepts --config PATH, --profile NAME, --no-config, --theme NAME \
             and the setting flags (--width, --pager, ...) that `rich` itself accepts, as \
             command-line overrides.",
        )
        .subcommand_required(true)
        .subcommand(
            CommandSpec::new("show")
                .about("Show configured settings with CLI overrides as JSON")
                .usage(format!("show {same}")),
        )
        .subcommand(
            CommandSpec::new("validate")
                .about("Validate TOML, all profiles and explicit setting values")
                .usage(format!("validate {same}")),
        )
        .subcommand(
            CommandSpec::new("explain")
                .about(
                    "Show which layer sets each setting — defaults, environment, config file, \
                     profile, command line — and what it overrides",
                )
                .usage(format!("explain {same} [KEY]"))
                .arg(
                    ArgSpec::positional("key")
                        .help("Explain one setting instead of all of them")
                        .value(ValueHint::Choices(
                            config_keys().into_iter().map(Choice::new).collect(),
                        )),
                ),
        )
        .subcommand(
            CommandSpec::new("reference")
                .about("List every configuration source and key")
                .usage(format!("reference {same}")),
        )
}

fn authoring_commands() -> Vec<CommandSpec> {
    vec![
        CommandSpec::new("completions")
            .about("Print a shell completion script")
            .arg(
                ArgSpec::positional("shell")
                    .required(true)
                    .help("The shell to complete for (pwsh is accepted for powershell)")
                    .choices(["bash", "zsh", "fish", "powershell"]),
            )
            .example(
                "rich completions bash > ~/.local/share/bash-completion/completions/rich",
                "Bash",
            )
            .example("rich completions zsh > \"${fpath[1]}/_rich\"", "Zsh")
            .example(
                "rich completions fish > ~/.config/fish/completions/rich.fish",
                "Fish",
            )
            .example(
                "rich completions powershell | Out-String | Invoke-Expression",
                "PowerShell",
            ),
        CommandSpec::new("docs")
            .about("Print reference documentation generated from this help")
            .subcommand_required(true)
            .subcommand(
                CommandSpec::new("markdown").about("Print the command reference as Markdown"),
            )
            .subcommand(
                CommandSpec::new("man")
                    .about("Print the rich(1) man page, or write every page to a directory")
                    .arg(
                        ArgSpec::option("output")
                            .value_name("DIR")
                            .value(ValueHint::Dir)
                            .help(
                                "Write rich.1 and one page per subcommand into DIR (created \
                                 if missing) and list them",
                            ),
                    ),
            )
            .subcommand(
                CommandSpec::new("config").about("Print the configuration reference as Markdown"),
            ),
        CommandSpec::new("bench")
            .about("Compare benchmark runs")
            .subcommand_required(true)
            .subcommand(
                CommandSpec::new("compare")
                    .about(
                        "Compare a candidate benchmark run with a baseline; exits 5 when any \
                         benchmark regressed",
                    )
                    .arg(
                        ArgSpec::positional("baseline")
                            .required(true)
                            .value(ValueHint::Path)
                            .help("A rich_ext::qa::bench JSON run, or a criterion directory"),
                    )
                    .arg(
                        ArgSpec::positional("candidate")
                            .required(true)
                            .value(ValueHint::Path)
                            .help("The run to judge, in the same form"),
                    )
                    .arg(
                        ArgSpec::option("threshold")
                            .value_name("PCT")
                            .default_value("5")
                            .help("Changes within ±PCT% (beyond noise) count as unchanged"),
                    )
                    .example(
                        "rich bench compare baseline.json candidate.json --threshold 10",
                        "Gate CI on a 10% slowdown",
                    ),
            ),
    ]
}

/// The whole `rich` command line.
pub(crate) fn spec() -> CommandSpec {
    let mut spec = CommandSpec::new("rich")
        .version(super::VERSION)
        .about(format!(
            "rich {} — Rust port of the rich-cli terminal toolbox\n\n\
             RESOURCE is a file path, an http(s) URL, or `-` for stdin. Everything after a \
             bare `--` is a RESOURCE, however much it looks like an option. Input modes with \
             no RESOURCE read stdin until EOF; `-p -` reads markup from stdin too. Terminal \
             stdin shows an input hint. Repeated scalar options use their last value.\n\n\
             A `--no-…` spelling (such as --no-watch) disables the corresponding \
             config/default boolean.",
            super::VERSION
        ))
        .usage("rich [OPTIONS] [RESOURCE]")
        .usage("rich [OPTIONS] <COMMAND> [RESOURCE]")
        .usage("rich --batch [OPTIONS] RESOURCE...")
        .usage("rich --watch [OPTIONS] FILE...")
        .args(root_args())
        .heading_note(
            RENDER_MODE,
            "Choose at most one; the default auto-detects .md/.rst/.json/.csv/.tsv/.ipynb, \
             .mmd/.mermaid and .dot/.gv by extension, and .org, .adoc/.asciidoc and \
             .man/.roff/.mdoc documents; a man page's section extension (.1 to .9) or .asc \
             counts when the content agrees, and a file with no extension that is plainly \
             Org, AsciiDoc or roff renders as one — anything else with a file extension is \
             syntax-highlighted.",
        )
        .heading_note(
            DEMO,
            "Self-contained examples; ignores config; accepts --no-color.",
        );
    for (name, aliases, about) in [
        (
            "print",
            &[][..],
            "Treat RESOURCE as literal markup TEXT (`--print`)",
        ),
        ("markdown", &["md"][..], "Render Markdown (`--markdown`)"),
        ("rst", &[][..], "Render reStructuredText (`--rst`)"),
        ("org", &[][..], "Render an Org document (`--org`)"),
        ("asciidoc", &["adoc"][..], "Render AsciiDoc (`--asciidoc`)"),
        (
            "man",
            &["roff"][..],
            "Render a man page from its roff source (`--man`)",
        ),
        (
            "syntax",
            &["code"][..],
            "Syntax-highlight source (`--syntax`)",
        ),
        ("json", &[][..], "Pretty-print JSON (`--json`)"),
        ("csv", &["tsv"][..], "Render CSV/TSV as a table (`--csv`)"),
        (
            "ipynb",
            &["notebook"][..],
            "Render a Jupyter notebook (`--ipynb`)",
        ),
        (
            "jsonl",
            &["ndjson"][..],
            "Stream JSON Lines / NDJSON records (`--jsonl`)",
        ),
        (
            "log",
            &["logs"][..],
            "Stream log records (`--log`): JSON Lines, logfmt and common text formats; filter \
             with --level and --where, follow a file with -f, group with --group",
        ),
        ("gif", &[][..], "Animate GIFs (`--gif`)"),
        (
            "diff",
            &[][..],
            "Compare two images, two text files, or render one patch (`--diff`)",
        ),
        (
            "image",
            &[][..],
            "Render a still image as ASCII/Braille/blocks/quadrants/Sixel (`--image`)",
        ),
        ("rule", &[][..], "Draw a horizontal rule (`--rule`)"),
        (
            "inspect",
            &[][..],
            "Explore structured data (JSON, YAML, TOML, XML, INI, dotenv) as a tree \
             (`--inspect`)",
        ),
        (
            "ansi",
            &["ansi-explain"][..],
            "Decode escape sequences: `rich ansi explain FILE` (`--ansi-explain`)",
        ),
        (
            "view",
            &[][..],
            "Show any file: Markdown, CSV, notebooks, images and patches rendered, source and \
             data numbered and highlighted, binary as hex; paged, searchable with --search",
        ),
        (
            "hex",
            &["hexdump"][..],
            "Hex dump with offsets, byte groups and an ASCII panel (--search, --offset, --length)",
        ),
        (
            "unicode",
            &[][..],
            "Show graphemes, code points, UTF-8 bytes, widths and invalid sequences",
        ),
        (
            "env",
            &[][..],
            "List environment variables, secrets masked; `rich env PATH` checks each PATH entry",
        ),
        (
            "capture",
            &[][..],
            "Run `rich capture -- COMMAND ARGS…` and show or export its output (--cast FILE \
             records it, --redact masks secrets), then exit with the command's status",
        ),
        (
            "mermaid",
            &["mmd"][..],
            "Draw a Mermaid diagram: flowcharts as text, every type through mmdc where built in \
             (.mmd and .mermaid files are detected)",
        ),
        (
            "dot",
            &["graphviz"][..],
            "Draw a DOT (Graphviz) graph as text (.dot and .gv files are detected); what the \
             native parser does not support is refused with its line",
        ),
    ] {
        spec = spec.subcommand(mode_command(name, aliases, about));
    }
    spec = spec.subcommand(
        CommandSpec::new("deps")
            .about(
                "A Cargo dependency tree from `cargo metadata`, crates resolved at several \
                 versions marked; --why CRATE for what pulls a crate in, --graph to draw it; \
                 --features, --licenses, --policy FILE, --timings FILE, --sizes and --audit FILE \
                 for supply-chain reports, --baseline FILE to compare timings or sizes",
            )
            .usage("deps [OPTIONS] [MANIFEST]")
            .arg(ArgSpec::positional("manifest").value(ValueHint::Path).help(
                "A Cargo.toml, or the directory holding one (default: the working directory)",
            ))
            .args(command_options("deps")),
    );
    spec = spec.subcommand(
        CommandSpec::new("schema")
            .about(
                "A JSON Schema, SQL DDL or Arrow schema as a tree: types, required markers, \
                 constraints, keys, $refs resolved; with two schemas (of any of these formats), \
                 what changed between them and which changes break; --er draws an ER diagram",
            )
            .usage("schema [OPTIONS] SCHEMA [NEW_SCHEMA]")
            .arg(ArgSpec::positional("schema").value(ValueHint::Path).help(
                "A JSON Schema, SQL DDL (.sql, .ddl, or text starting with CREATE) or \
                         Arrow IPC file (.arrow, .feather, .arrows, .ipc; the `arrow` feature), \
                         an http(s) URL, or `-` for stdin",
            ))
            .arg(
                ArgSpec::positional("new_schema")
                    .value(ValueHint::Path)
                    .help("A second version: show what changed from SCHEMA to it"),
            )
            .args(command_options("schema")),
    );
    spec = spec.subcommand(
        CommandSpec::new("chart")
            .about(
                "A chart from CSV, JSON or stdin: a sparkline, bars, lines, points or a heatmap; \
                 --x and --y pick the columns, a missing column or a value that is not a number \
                 is refused with its row and column",
            )
            .usage("chart [OPTIONS] [FILE]")
            .arg(ArgSpec::positional("file").value(ValueHint::Path).help(
                "CSV, TSV, JSON, JSON Lines or whitespace-separated numbers, an http(s) URL, or \
                 `-` for stdin (the default)",
            ))
            .args(command_options("chart")),
    );
    spec = spec.subcommand(
        CommandSpec::new("profile")
            .about(
                "A profile of CSV, TSV or JSON Lines: each column's type, nulls, distinct values, \
                 statistics and distribution, and a map of where the nulls are; a long input is \
                 sampled. --report json writes the profile as JSON to stdout",
            )
            .usage("profile [OPTIONS] [FILE]")
            .arg(ArgSpec::positional("file").value(ValueHint::Path).help(
                "CSV, TSV or JSON Lines (by extension, else by the first character), an http(s) \
                 URL, or `-` for stdin (the default)",
            ))
            .args(command_options("profile")),
    );
    spec = spec.subcommand(config_command());
    #[cfg(feature = "interact")]
    for command in super::interactive::commands() {
        spec = spec.subcommand(command);
    }
    #[cfg(feature = "interact")]
    for command in super::approve_jump::commands() {
        spec = spec.subcommand(command);
    }
    for command in authoring_commands() {
        spec = spec.subcommand(command);
    }
    #[cfg(feature = "art")]
    {
        spec = spec.subcommand(super::micro::command());
        spec = spec.subcommand(super::banner::command());
    }
    spec = spec.subcommand(super::shell::command());
    #[cfg(feature = "serve")]
    {
        spec = spec.subcommand(super::serve::command());
    }
    spec.subcommand(
        CommandSpec::new("record")
            .about(
                "Run tapes (scripted terminal sessions: type, press keys, wait for screen text, \
                 take screenshots) in a real terminal and write PNG, SVG and text screenshots, an \
                 asciinema cast, a GIF, an MP4 (with FFmpeg) and an HTML page with a player; \
                 --check compares screenshots with committed ones instead",
            )
            .usage(
                "record [--check] [--output DIR] [--bin-dir DIR] \
                 [--format png,svg,cast,gif,mp4,html] [--no-video] [--font FILE] \
                 [--window-frame on|off] [--caption TEXT] [--key-overlay on|off] TAPE...",
            ),
    )
    .subcommand(
        CommandSpec::new("plugins")
            .about(
                "List the plugins this build has — built in, linked, and any --plugin PATH \
                 loads — with their source, version, ABI and capabilities",
            )
            .usage(
                "plugins [list | info NAME] [--plugin PATH]... [--report json] [--config PATH] \
                 [--profile NAME] [--no-config] [--no-color]",
            )
            .subcommand(
                CommandSpec::new("list")
                    .about("A table of every plugin (the default)")
                    .usage("list [--plugin PATH]... [--report json]"),
            )
            .subcommand(
                CommandSpec::new("info")
                    .about("One plugin: its path, ABI, capabilities and description")
                    .usage("info NAME [--plugin PATH]... [--report json]")
                    .arg(ArgSpec::positional("name").help("The plugin's id, as `list` shows it")),
            ),
    )
    .subcommand(
        CommandSpec::new("doctor")
            .about(
                "Read-only build, terminal, config and pager diagnostics; --report json writes \
                 diagnostic data to stdout",
            )
            .usage(
                "doctor [--report json] [--config PATH] [--profile NAME] [--no-config] \
                 [--no-color]",
            ),
    )
    .section(
        "Environment",
        "- NO_COLOR: any non-empty value disables colour\n\
         - COLUMNS: console width (default 80 when unavailable)\n\
         - MANPAGER, PAGER: pager command; fallback is less (Unix), more.com (Windows)\n\
         - FORCE_COLOR: not supported; redirected stdout stays plain\n\
         - RICH_GRAPHICS: sixel forces Sixel on; none (or kitty, iterm) rules it out\n\
         - RICH_SIXEL: 0/1 overrides Sixel detection when RICH_GRAPHICS is unset\n\
         - RICH_MICRO: kitty, iterm, sixel, blocks or text: how micro assets are drawn on a \
         terminal (never off one)\n\
         - RICH_CELL_PIXELS: WxH, the cell size in pixels micro assets are fitted to",
    )
    .section(
        "",
        "With no RESOURCE and no mode flag, a capability demo is shown. Layout, style, \
         paging, hyperlinks and export options require a resource or render mode.",
    )
    .section(
        "Exit codes",
        "- 0: success\n\
         - 1: cancelled, or answered no (choose, filter, input, confirm, write, file, color, \
           asset, date)\n\
         - 2: usage/config error\n\
         - 3: input/read/write error\n\
         - 4: parse/render data error\n\
         - 5: threshold/gate failure\n\
         - 130: interrupted with Ctrl+C (choose, filter, input, confirm, pager, write, file, \
           color, asset, date)",
    )
}

/// The configuration reference: every source, lowest precedence first, and
/// every key.
pub(crate) fn config_reference() -> ConfigReference {
    let mut reference = ConfigReference::from_spec(&spec())
        .description(
            "Settings are TOML: `version = 1`, a `[defaults]` table, optional \
             `[profile.NAME]` tables and `[themes.NAME]` style tables. `rich config explain` \
             shows where each effective value comes from.",
        )
        .source("defaults", "", "Built-in defaults")
        .source(
            "environment",
            "NO_COLOR",
            "A non-empty value sets no_color = true. A no_color = false in ./rich.toml \
             cannot undo it; ~/.config/rich/config.toml, --config PATH and --color can",
        )
        .source(
            "config file",
            "./rich.toml, else ~/.config/rich/config.toml",
            "The [defaults] table. Only the first file found is read; --config PATH reads \
             that file instead and --no-config reads none",
        )
        .source(
            "profile",
            "[profile.NAME]",
            "The profile selected with --profile (default: default) overrides the [defaults] \
             table",
        )
        .source(
            "command line",
            "",
            "Explicit flags override every other source",
        );
    // The spec knows values only as text; these are what `validate_value`
    // accepts.
    for entry in &mut reference.entries {
        let kind = match entry.key.as_str() {
            "width" | "height" | "jobs" | "image_max_width" | "image_max_height" => {
                "positive integer"
            }
            "watch_interval" | "watch_debounce" | "image_brightness" | "image_contrast"
            | "image_gamma" => "number",
            "plugins" => "list of paths",
            "rules" => "array of rule tables",
            _ => continue,
        };
        entry.kind = kind.to_string();
    }
    reference.entries.insert(
        0,
        ConfigEntry::new("mode", "enum")
            .choices(MODES.iter().copied())
            .default_value("auto")
            .flag("--print, --markdown, ... or a command word")
            .description("Render mode, as the flag or command of the same name"),
    );
    reference
}

/// The `defaults` precedence layer: each config key's built-in value. A flag
/// with no stated default is off.
pub(crate) fn default_layer() -> Layer {
    let mut layer = Layer::new("defaults").value("mode", "auto");
    for arg in root_args() {
        let Some(key) = &arg.config_key else { continue };
        match (&arg.default, arg.takes_value()) {
            (Some(value), _) => layer = layer.value(key.clone(), value.clone()),
            (None, false) => layer = layer.value(key.clone(), "false"),
            (None, true) => {}
        }
    }
    layer
}

/// Every known config key.
pub(crate) fn known_config_keys() -> Vec<String> {
    config_keys()
}

/// A console for stdout: colour only on a terminal, and never with
/// `no_color`.
pub(crate) fn console(no_color: bool) -> Console {
    Console::builder().no_color(no_color).build()
}

/// Render through a stdout console to a string (no trailing newline).
pub(crate) fn render(renderable: &dyn Renderable, no_color: bool) -> String {
    console(no_color).render_to_string(renderable)
}

/// `rich --help`.
pub(crate) fn print_help(no_color: bool) {
    console(no_color).print(&HelpView::new(&spec()));
}

/// Help for the subcommand at `path`, such as `["config", "explain"]`.
pub(crate) fn subcommand_help(path: &[&str], no_color: bool) -> Option<String> {
    let spec = spec();
    HelpView::for_path(&spec, path).map(|view| render(&view, no_color))
}

/// Colour off for NO_COLOR or `--no-color` (the last of `--no-color` and
/// `--color` wins), as the rest of the binary decides it before config.
pub(crate) fn no_color_requested(args: &[String]) -> bool {
    let mut no_color = std::env::var_os("NO_COLOR").is_some_and(|value| !value.is_empty());
    for arg in args.iter().take_while(|arg| *arg != "--") {
        match arg.as_str() {
            "--no-color" => no_color = true,
            "--color" => no_color = false,
            _ => {}
        }
    }
    no_color
}

#[cfg(test)]
mod tests {
    //! Drift between this spec and the hand-written parser.
    //!
    //! Heuristic for "what the parser recognises": every string literal that
    //! is exactly an option spelling (`"--name"` or `"-x"`) inside the parser
    //! functions — `parse_inner`, `VALUE_OPTIONS`, `mode_flag_alias` in
    //! `lib.rs`, `arguments` and `boolean_flags` in `config.rs`, `options`
    //! in `demo.rs`, and the extension options in `rich_ext::cli`. Error
    //! messages and table labels such as `"--diff/--image"` never match a
    //! whole literal, so no exclusions are needed. The spec side is every
    //! short, long and alias of every argument, including subcommands.
    use super::*;
    use crate::config::{config_args, ConfigRoots};
    use std::collections::BTreeSet;

    const MAIN: &str = include_str!("lib.rs");
    const CONFIG: &str = include_str!("config.rs");
    const DEMO: &str = include_str!("demo.rs");
    const INSPECT: &str = include_str!("inspect.rs");
    const TOOLS: &str = include_str!("tools.rs");
    const VIEWERS: &str = include_str!("viewers.rs");
    const SOURCES_RS: &str = include_str!("sources.rs");
    const CHART_RS: &str = include_str!("chart.rs");
    const PROFILE_RS: &str = include_str!("profile.rs");
    const LOGS_RS: &str = include_str!("logs.rs");
    const STYLE_RULES_RS: &str = include_str!("style_rules.rs");
    #[cfg(feature = "interact")]
    const INTERACTIVE: &str = include_str!("interactive.rs");
    #[cfg(feature = "interact")]
    const APPROVE_JUMP: &str = include_str!("approve_jump.rs");
    #[cfg(feature = "art")]
    const MICRO: &str = include_str!("micro.rs");
    #[cfg(feature = "serve")]
    const SERVE: &str = include_str!("serve.rs");
    #[cfg(feature = "art")]
    const BANNER: &str = include_str!("banner.rs");
    const SHELL: &str = include_str!("shell.rs");

    /// The source of the item that starts with `start`, up to the next
    /// top-level item.
    fn item<'a>(source: &'a str, start: &str) -> &'a str {
        let from = source
            .find(start)
            .unwrap_or_else(|| panic!("{start} not found"));
        let rest = &source[from..];
        let end = rest[1..]
            .find("\nfn ")
            .into_iter()
            .chain(rest[1..].find("\nconst "))
            .chain(rest[1..].find("\npub(crate) fn "))
            .chain(rest[1..].find("\npub(super) fn "))
            .min()
            .map_or(rest.len(), |i| i + 1);
        &rest[..end]
    }

    /// Whole string literals that are option spellings.
    fn option_literals(source: &str) -> BTreeSet<String> {
        let mut out = BTreeSet::new();
        for piece in source.split('"').skip(1).step_by(2) {
            let long = piece.len() > 2
                && piece.starts_with("--")
                && piece[2..].starts_with(|c: char| c.is_ascii_lowercase())
                && piece[2..]
                    .chars()
                    .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-');
            let short = piece.len() == 2
                && piece.starts_with('-')
                && piece[1..].starts_with(|c: char| c.is_ascii_alphabetic());
            if long || short {
                out.insert(piece.to_string());
            }
        }
        out
    }

    fn parser_options() -> BTreeSet<String> {
        let mut out = BTreeSet::new();
        for source in [
            item(MAIN, "fn parse_inner("),
            item(MAIN, "const VALUE_OPTIONS"),
            item(MAIN, "fn mode_flag_alias("),
            item(CONFIG, "fn arguments("),
            item(CONFIG, "fn boolean_flags("),
            item(DEMO, "fn options("),
            item(INSPECT, "impl DataOptions {"),
            item(TOOLS, "impl ToolOptions {"),
            item(VIEWERS, "impl ViewerOptions {"),
            item(SOURCES_RS, "impl GraphSourceOptions {"),
            item(SOURCES_RS, "impl SchemaOptions {"),
            item(CHART_RS, "impl ChartOptions {"),
            item(PROFILE_RS, "impl ProfileOptions {"),
            item(LOGS_RS, "impl LogOptions {"),
            item(STYLE_RULES_RS, "impl RuleOptions {"),
        ] {
            out.extend(option_literals(source));
        }
        #[cfg(feature = "interact")]
        out.extend(option_literals(item(INTERACTIVE, "fn parse_args(")));
        #[cfg(feature = "interact")]
        out.extend(option_literals(item(APPROVE_JUMP, "fn parse_args(")));
        #[cfg(feature = "serve")]
        out.extend(option_literals(item(SERVE, "fn parse_args(")));
        #[cfg(feature = "art")]
        out.extend(option_literals(item(BANNER, "fn parse_args(")));
        out.extend(option_literals(item(SHELL, "fn parse_args(")));
        #[cfg(feature = "art")]
        for start in ["const CREATE_VALUES", "const CREATE_FLAGS", "fn request("] {
            out.extend(option_literals(item(MICRO, start)));
        }
        for arg in rich_ext::cli::arg_specs() {
            out.extend(arg.switches());
        }
        out
    }

    fn all_args(spec: &CommandSpec) -> Vec<ArgSpec> {
        let mut out: Vec<ArgSpec> = spec.args.clone();
        for command in &spec.subcommands {
            out.extend(all_args(command));
        }
        out
    }

    fn spec_switches() -> BTreeSet<String> {
        all_args(&spec())
            .iter()
            .filter(|arg| !arg.positional)
            .flat_map(ArgSpec::switches)
            .collect()
    }

    #[test]
    fn the_heuristic_sees_the_parser() {
        let found = parser_options();
        for known in [
            "--width",
            "-w",
            "--no-watch",
            "--config",
            "--demo-delay",
            "--gif-mode",
        ] {
            assert!(found.contains(known), "{known} not found: {found:?}");
        }
        assert!(!found.iter().any(|o| o.contains('/')));
        assert!(
            found.len() > 90,
            "the heuristic found too little: {found:?}"
        );
    }

    #[test]
    fn every_parser_option_is_in_the_spec() {
        let documented = spec_switches();
        let missing: Vec<_> = parser_options()
            .into_iter()
            .filter(|option| !documented.contains(option))
            .collect();
        assert!(missing.is_empty(), "undocumented options: {missing:?}");
    }

    #[test]
    fn every_spec_option_has_a_parser() {
        let parser = parser_options();
        let unknown: Vec<_> = spec_switches()
            .into_iter()
            .filter(|s| !parser.contains(s))
            // Subcommand-local options are parsed by their subcommand.
            .filter(|s| s != "--output")
            .collect();
        assert!(
            unknown.is_empty(),
            "spec options with no parser: {unknown:?}"
        );
    }

    /// A value the option accepts, or at least one that reaches its own
    /// validation rather than the unknown-option arm.
    fn sample(arg: &ArgSpec) -> String {
        arg.choice_list()
            .first()
            .map_or_else(|| "1".to_string(), |choice| choice.value.clone())
    }

    #[test]
    fn every_spec_switch_is_accepted_by_the_parser() {
        let root = tempfile::tempdir().unwrap();
        let roots = ConfigRoots {
            home: None,
            cwd: root.path().into(),
            no_color_env: false,
        };
        let demo = demo_options();
        for arg in root_args().iter().filter(|arg| !arg.positional) {
            for switch in arg.switches() {
                if demo.iter().any(|d| d.id == arg.id) {
                    // The tour has its own parser; it is covered by
                    // `every_parser_option_is_in_the_spec` via `demo.rs`.
                    assert!(DEMO.contains(&format!("\"{switch}\" =>")), "{switch}");
                    continue;
                }
                let mut args = vec![switch.clone()];
                if arg.takes_value() {
                    args.push(sample(arg));
                }
                args.push("input.txt".into());
                let result = config_args(&args, &roots).and_then(|merged| {
                    // `--help`/`--version` print; that is acceptance too.
                    super::super::parse_inner(&merged).map(|_| ())
                });
                if let Err(message) = result {
                    assert!(
                        !message.starts_with("unknown option"),
                        "{switch} rejected: {message}"
                    );
                }
            }
        }
    }

    #[test]
    fn value_taking_agrees_with_the_parser() {
        for arg in root_args().iter().filter(|arg| !arg.positional) {
            for switch in arg.switches() {
                let consumes = super::super::VALUE_OPTIONS.contains(&switch.as_str());
                assert_eq!(
                    arg.takes_value(),
                    consumes,
                    "{switch}: spec and VALUE_OPTIONS disagree about taking a value"
                );
            }
        }
    }

    #[test]
    fn config_keys_match_the_validator() {
        let keys = config_keys();
        for key in &keys {
            // Every documented key is one `validate_value` knows.
            let probe = toml::Value::Boolean(true);
            if let Err(message) = crate::config::validate_value(key, &probe) {
                assert!(!message.starts_with("unknown key"), "{key}: {message}");
            }
        }
        for key in crate::config::all_keys() {
            assert!(
                keys.iter().any(|k| k == key),
                "{key} has no documented flag"
            );
        }
    }

    #[test]
    fn subcommands_cover_every_mode_word() {
        let spec = spec();
        for mode in super::super::MODE_SPECS.iter().skip(1) {
            for alias in mode.aliases {
                assert!(spec.find_subcommand(alias).is_some(), "{alias}");
            }
        }
        for name in [
            "config",
            "completions",
            "docs",
            "doctor",
            "inspect",
            "shell",
        ] {
            assert!(spec.find_subcommand(name).is_some(), "{name}");
        }
    }

    #[test]
    fn help_keeps_the_documented_phrases() {
        let out = Console::builder()
            .width(100)
            .build()
            .render_to_string(&HelpView::new(&spec()));
        let flat = out.split_whitespace().collect::<Vec<_>>().join(" ");
        for phrase in [
            "Usage: rich [OPTIONS] [RESOURCE]",
            "rich --watch [OPTIONS] FILE...",
            "stdin until EOF",
            "Choose at most one",
            "-y, --hyperlinks",
            "default 1; 0 = forever",
            "--jobs <N>",
            "[config: watch_interval]",
            "[env: NO_COLOR]",
            "RICH_SIXEL",
            "5: threshold/gate failure",
            "a capability demo is shown",
        ] {
            assert!(flat.contains(phrase), "missing {phrase:?}:\n{out}");
        }
    }
}