usage-argv 6.11.1

Zero-allocation argv parser for usage specs
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
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
//! What a user reads when a command line does not parse.
//!
//! Held to clap's shape on purpose. mise's users read clap's errors today, and the help output on
//! either side of this is already byte-identical to usage-lib's — so the error text is the last
//! thing an adopter's users would notice changing, and the aim is that they do not.
//!
//! The skeleton, measured from clap 4 rather than remembered:
//!
//! ```text
//! error: unexpected argument '--fore' found
//!
//!   tip: a similar argument exists: '--force'
//!
//! Usage: mise use [OPTIONS] [TOOL@VERSION]…
//!
//! For more information, try '--help'.
//! ```
//!
//! Two deliberate departures. The usage line is *ours* — the same one `--help` prints, rendered
//! from the spec — because an error that disagrees with the help about how a command is spelled is
//! worse than one that disagrees with clap. And which errors carry a usage block follows clap:
//! the ones about the shape of the command line do, the ones about a single value do not.

use crate::spec::{CommandMeta, FlagMeta, Spec, ViewMeta};
use crate::{Command, DoubleDash, Error};

/// A stable machine-readable category for one parse outcome.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Code {
    UnknownFlag,
    MissingFlagValue,
    UnexpectedArg,
    ArgRequiresDoubleDash,
    SubcommandConflict,
    TooDeep,
    MissingRequired,
    DuplicateFlag,
    InvalidChoice,
    VarTooFew,
    VarTooMany,
    ConflictingFlags,
    InvalidValue,
    MissingGroup,
    MissingSubcommand,
    Help,
    MissingArgsHelp,
    HelpAll,
    Version,
}

impl Code {
    /// The stable snake-case spelling intended for JSON, logs, and editor protocols.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::UnknownFlag => "unknown_flag",
            Self::MissingFlagValue => "missing_flag_value",
            Self::UnexpectedArg => "unexpected_arg",
            Self::ArgRequiresDoubleDash => "arg_requires_double_dash",
            Self::SubcommandConflict => "subcommand_conflict",
            Self::TooDeep => "too_deep",
            Self::MissingRequired => "missing_required",
            Self::DuplicateFlag => "duplicate_flag",
            Self::InvalidChoice => "invalid_choice",
            Self::VarTooFew => "var_too_few",
            Self::VarTooMany => "var_too_many",
            Self::ConflictingFlags => "conflicting_flags",
            Self::InvalidValue => "invalid_value",
            Self::MissingGroup => "missing_group",
            Self::MissingSubcommand => "missing_subcommand",
            Self::Help => "help",
            Self::MissingArgsHelp => "missing_args_help",
            Self::HelpAll => "help_all",
            Self::Version => "version",
        }
    }
}

/// The bytes within one argv word responsible for a diagnostic.
///
/// `index` addresses the argv slice passed to [`report`]. `start` and `end` are byte offsets in
/// that [`std::ffi::OsStr`], so the location remains lossless for non-UTF-8 command lines.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ArgvSpan {
    pub index: usize,
    pub start: usize,
    pub end: usize,
}

/// A parse outcome ready for a JSON reporter, editor, or another diagnostic framework.
///
/// The parser's original [`Error`] remains allocation-free on ordinary grammar failures. This
/// cold-path value owns its human rendering and optional subject so an embedding does not need
/// to reverse-engineer enum variants or scrape terminal text.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Report {
    pub code: Code,
    pub subject: Option<String>,
    pub location: Option<ArgvSpan>,
    pub rendered: String,
}

/// Whether to colour, and what with.
///
/// The codes are clap's, so that a terminal shows the same thing: bold red for `error:`, yellow
/// for the offending text, green for a suggestion and for what was expected, bold underline for
/// `Usage:`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Style {
    coloured: bool,
}

impl Style {
    /// Plain text, for a pipe or a test.
    pub const PLAIN: Style = Style { coloured: false };
    /// Coloured, whatever the terminal is.
    pub const COLOURED: Style = Style { coloured: true };

    /// Colour when stderr is a terminal and the environment has not asked otherwise.
    ///
    /// `NO_COLOR` wins over everything, per the convention: a user who sets it has said once, for
    /// every program, that they do not want this. `CLICOLOR_FORCE` is the other direction, for a
    /// pipe that ends up somewhere that does render colour.
    pub fn auto() -> Style {
        use std::io::IsTerminal as _;
        let forced = std::env::var_os("CLICOLOR_FORCE").is_some_and(|v| v != "0");
        let refused = std::env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty());
        if refused {
            return Style::PLAIN;
        }
        if forced || std::io::stderr().is_terminal() {
            Style::COLOURED
        } else {
            Style::PLAIN
        }
    }

    /// Append `text` in `ink`, followed by a reset when colouring.
    ///
    /// Written into the message rather than returned as a `String` to be formatted into it: a
    /// message is a list of these pieces, and every `format!` built from returned pieces compiles
    /// its own copy of the formatting.
    fn put(self, out: &mut String, ink: Ink, text: &str) {
        self.put_joined(out, ink, &[text]);
    }

    /// Append several texts as one coloured run, such as `--` and a flag's name.
    #[inline(never)]
    fn put_joined(self, out: &mut String, ink: Ink, texts: &[&str]) {
        let code = match ink {
            Ink::Plain => None,
            Ink::Error => Some("1m\u{1b}[31"),
            Ink::Warning => Some("1m\u{1b}[33"),
            Ink::Invalid => Some("33"),
            Ink::Valid => Some("32"),
            Ink::Heading => Some("1m\u{1b}[4"),
            Ink::Literal => Some("1"),
        };
        match code.filter(|_| self.coloured) {
            Some(code) => {
                push_all(out, &["\u{1b}[", code, "m"]);
                push_all(out, texts);
                push_all(out, &["\u{1b}[0m"]);
            }
            None => push_all(out, texts),
        }
    }

    /// Append each piece in turn; a message given as data rather than as a `format!`.
    #[inline(never)]
    fn put_all(self, out: &mut String, parts: &[(Ink, &str)]) {
        for (ink, text) in parts {
            self.put(out, *ink, text);
        }
    }
}

/// Append each of `texts`.
///
/// One out-of-line loop instead of a `push_str` per piece at every call site, each of which
/// inlines its own capacity check and copy.
#[inline(never)]
fn push_all(out: &mut String, texts: &[&str]) {
    for text in texts {
        out.push_str(text);
    }
}

/// What a piece of a message is, which decides its colour.
#[derive(Clone, Copy)]
enum Ink {
    /// Uncoloured prose.
    Plain,
    /// `error:`, and anything else that is the failure itself.
    Error,
    /// `warning:` — something that worked, and should not have been asked for.
    Warning,
    /// What the user typed that did not work.
    Invalid,
    /// What would have worked: a suggestion, a possible value, a missing argument.
    Valid,
    /// A heading, such as `Usage:`.
    Heading,
    /// Something to be typed as it is written.
    Literal,
}

/// A flag as the user named it, without a value they attached to it.
///
/// `--jobs=4` names `--jobs`; the parser splits on the `=` before looking the name up, so an
/// error about the whole token is about something nobody typed. Both halves of the message
/// depend on it: clap prints `'--fore'` for `--fore=1`, and it scores `fore` — with the value
/// left on, `fore=1` falls under the 0.7 bar and the tip disappears exactly where a mistyped
/// value-taking flag is most likely to be written.
///
/// Long flags only. A short cluster is refused whole, so `-xy` is not `-x` with something
/// attached, and `-j=4` is a value clap keeps.
fn flag_named(token: &str) -> &str {
    match token.strip_prefix("--") {
        Some(body) => match body.find('=') {
            Some(i) => &token[..i + 2],
            None => token,
        },
        None => token,
    }
}

/// Every long spelling a word at this command could have named: its own flags', then any
/// ancestor's globals — negations included.
///
/// The same set the parser would have accepted, which is what makes a suggestion one that works.
/// The parser takes `--no-color` through `find_negation`, and the completions offer it, so a
/// suggestion that leaves it out is the odd one — a near miss of a name that works gets silence.
/// clap has no separate notion of a negation, so the two forms are two arguments there and it
/// suggests either; matching that is the point.
fn long_names_in_scope<'a>(chain: &[&'a CommandMeta<'a>]) -> Vec<&'a str> {
    // The command's own flags, and from each ancestor only what it declared global — the rule
    // the parser follows on the way down. The chain and not the tree: an earlier version
    // collected globals from every branch it walked through, so a global declared on one command
    // was suggested under an unrelated one — a tip naming a flag the parser would refuse, which
    // is worse than no tip.
    let mut names = Vec::new();
    for (i, meta) in chain.iter().enumerate() {
        let own = i + 1 == chain.len();
        for f in meta.flags {
            if !f.hide && (own || f.flag.global) {
                names.extend_from_slice(f.flag.longs);
                names.extend(f.flag.negate);
            }
        }
    }
    names
}

/// How alike two words are, from 0 (nothing in common) to 1 (the same word).
///
/// Jaro, and deliberately not Jaro-Winkler. clap decides whether to suggest something with
/// `strsim::jaro`, and says why in its own source:
///
/// ```text
/// // GH #4660: using `jaro` because `jaro_winkler` implementation in `strsim-rs` is wrong
/// ```
///
/// Winkler's variant adds a bonus for a shared prefix, which sounds right for a mistyped flag and
/// would move the bar: some words clear 0.7 with it and not without, and the ranking changes too.
/// Since the point of this module is that an adopter's users see the same tips they saw under
/// clap, the algorithm has to be the same one. The bonus is three lines away if it is ever wanted
/// on both sides.
///
/// Written out rather than depended on, because this crate takes no dependencies.
fn jaro(a: &str, b: &str) -> f64 {
    let (a, b): (Vec<char>, Vec<char>) = (a.chars().collect(), b.chars().collect());
    if a.is_empty() && b.is_empty() {
        return 1.0;
    }
    if a.is_empty() || b.is_empty() {
        return 0.0;
    }

    // Two characters count as matching if they are the same and no further apart than this.
    let window = (a.len().max(b.len()) / 2).saturating_sub(1);
    let mut a_matched = vec![false; a.len()];
    let mut b_matched = vec![false; b.len()];
    let mut matches = 0usize;

    for (i, ch) in a.iter().enumerate() {
        let start = i.saturating_sub(window);
        let end = (i + window + 1).min(b.len());
        for j in start..end {
            if !b_matched[j] && b[j] == *ch {
                a_matched[i] = true;
                b_matched[j] = true;
                matches += 1;
                break;
            }
        }
    }
    if matches == 0 {
        return 0.0;
    }

    // Matching characters that arrive in a different order are half a transposition each.
    let mut transpositions = 0usize;
    let mut k = 0usize;
    for (i, matched) in a_matched.iter().enumerate() {
        if !matched {
            continue;
        }
        while !b_matched[k] {
            k += 1;
        }
        if a[i] != b[k] {
            transpositions += 1;
        }
        k += 1;
    }

    let matches = matches as f64;
    (matches / a.len() as f64
        + matches / b.len() as f64
        + (matches - transpositions as f64 / 2.0) / matches)
        / 3.0
}

/// Everything close enough to `typed` to be worth saying, in the order clap says them.
///
/// The threshold is clap's — a score above 0.7 — so the two suggest in the same cases. Below it a
/// suggestion is noise: offering `--quiet` for `--zzz` is worse than offering nothing, because a
/// user reads it as the CLI having understood them.
///
/// All of them, not the best one: clap lists every candidate over the bar, and `mise config lss`
/// really is close to both `ls` and `list`. Sorted *ascending* by score, which is what clap does —
/// so the closest match comes last. That reads oddly, and it is preserved here because the point
/// of this module is that an adopter's users see no change; it is a difference worth undoing on
/// both sides rather than on one.
fn nearest<'a>(typed: &str, candidates: &[&'a str]) -> Vec<&'a str> {
    let mut scored: Vec<(f64, &str)> = Vec::new();
    for candidate in candidates {
        let score = jaro(typed, candidate);
        if score > 0.7 {
            scored.push((score, candidate));
        }
    }
    crate::order::sort_by(&mut scored, &mut |a, b| {
        a.0.total_cmp(&b.0).then_with(|| a.1.cmp(b.1))
    });
    scored.dedup_by(|a, b| a.1 == b.1);
    scored.into_iter().map(|(_, candidate)| candidate).collect()
}

/// A tip naming what was probably meant, or nothing when nothing was close.
///
/// `noun` is the singular — clap writes "a similar argument exists" for one and "some similar
/// arguments exist" for several, and the plural is the singular with an `s`. `prefix` goes in
/// front of each candidate, inside its colour: the dashes a flag is scored without.
fn tip(out: &mut String, style: Style, noun: &str, prefix: &str, near: &[&str]) {
    if near.is_empty() {
        return;
    }
    let one = near.len() == 1;
    style.put_all(
        out,
        &[
            (Ink::Plain, "\n  "),
            (Ink::Valid, "tip:"),
            (
                Ink::Plain,
                if one { " a similar " } else { " some similar " },
            ),
            (Ink::Plain, noun),
            (Ink::Plain, if one { " exists: " } else { "s exist: " }),
        ],
    );
    for (i, candidate) in near.iter().enumerate() {
        // Each separator closes the quote the previous candidate opened.
        push_all(out, &[if i == 0 { "'" } else { "', '" }]);
        style.put_joined(out, Ink::Valid, &[prefix, candidate]);
    }
    push_all(out, &["'\n"]);
}

/// Whether a bare `--` stands in argv ahead of `token`.
///
/// The parser knows this as `separator_seen`, but the error it hands back does not carry the
/// flag, and argv answers the same question without widening a public enum. A `--` that a
/// `DoubleDash::Preserve` argument kept as a value counts here when it should not; that only
/// ever withholds the tip below, which is the harmless direction to be wrong in.
fn separator_before(argv: &[&std::ffi::OsStr], token: &[u8]) -> bool {
    // `span_of_slice` matches on storage, which a token the parser cut out of argv shares and
    // one an embedding built for itself does not -- and `render` and `report` are public and
    // take both. Matching the word covers the second kind.
    //
    // Taking the first occurrence is what makes the answer "a separator stands ahead of *every*
    // occurrence of this word", since one ahead of the first is ahead of all of them. Where the
    // word appears on both sides of a `--` that reads as no separator given, and the tip is
    // offered: an embedding's error need not say which occurrence it means, and repeating advice
    // the user may already have taken is the smaller fault of the two available.
    let Some(limit) = span_of_slice(argv, token)
        .map(|span| span.index)
        .or_else(|| {
            argv.iter()
                .position(|word| word.as_encoded_bytes() == token)
        })
    else {
        // A token that appears nowhere in argv belongs to a command line this argv does not
        // describe, so nothing here places a separator ahead of it. Answering "yes" on the
        // strength of a `--` standing somewhere in an unrelated argv is the one reading that
        // is certainly wrong.
        return false;
    };
    argv.iter()
        .take(limit)
        .any(|word| word.as_encoded_bytes() == b"--")
}

/// What a command can still do with a word it refused as a flag.
///
/// Gathered at the call site, where argv and the resolved command are both in hand, and read by
/// [`unexpected_flag`] once it knows whether it has a spelling to suggest.
#[derive(Clone, Copy, Default)]
struct ValueCapture {
    /// The command takes positional values at all.
    takes_positionals: bool,
    /// One of those positionals collects the rest of the line — a `--`-only argument, or an
    /// `automatic` one that turns into the separator as soon as it takes a value. clap's
    /// `last`/`trailing_var_arg`.
    captures_trailing: bool,
    /// A bare `--` already stands ahead of the refused word, so the user knows about the
    /// separator and the word is a genuine mistake.
    separator_seen: bool,
}

fn value_capture(
    argv: &[&std::ffi::OsStr],
    token: &[u8],
    here: Option<&CommandMeta<'_>>,
) -> ValueCapture {
    let Some(meta) = here else {
        return ValueCapture::default();
    };
    let clause_args = meta.cmd.clause.map(|clause| clause.args).unwrap_or(&[]);
    let positionals = || meta.cmd.args.iter().chain(clause_args.iter());
    ValueCapture {
        takes_positionals: positionals().next().is_some(),
        captures_trailing: positionals().any(|arg| {
            matches!(
                arg.double_dash,
                DoubleDash::Required | DoubleDash::Automatic
            )
        }),
        separator_seen: separator_before(argv, token),
    }
}

// Both parser errors use the same spelling suggestions and output layout.
#[inline(never)]
fn unexpected_flag(
    out: &mut String,
    style: Style,
    typed: &str,
    chain: &[&CommandMeta<'_>],
    capture: ValueCapture,
) {
    use Ink::{Invalid, Plain, Valid};
    error_line(
        out,
        style,
        &[
            (Plain, "unexpected argument '"),
            (Invalid, typed),
            (Plain, "' found"),
        ],
    );
    // Scored without the dashes, and only then written back with them. Every flag
    // starts `--`, and the prefix bonus in Jaro-Winkler counts that agreement — so
    // `--fore` came out similar to `--quiet`, which it is not. clap compares the bare
    // names for the same reason.
    let bare = typed.trim_start_matches('-');
    let near = nearest(bare, &long_names_in_scope(chain));
    tip(out, style, "argument", "--", &near);
    // clap's rule, transcribed rather than invented, down to the exception. Its comment:
    // "`did_you_mean` is a lot more likely and should cause us to skip the `--` suggestion
    // with the one exception being that the CLI is trying to capture arguments". So a
    // misspelling wins on an ordinary command — `--fore` against `--force` is a typo, not a
    // value somebody wanted forwarded — but a command that exists to collect a trailing
    // command line says both, because there the flag really may have been meant for what runs
    // next. `mise exec -- pnpm --version` is that shape.
    let as_value = capture.takes_positionals
        && !capture.separator_seen
        && (near.is_empty() || capture.captures_trailing);
    // `tip` opens with the blank line separating the tips from the error above; when there was
    // no spelling to suggest, this supplies it instead.
    if as_value {
        style.put_all(
            out,
            &[
                (Plain, if near.is_empty() { "\n  " } else { "  " }),
                (Valid, "tip:"),
                (Plain, " to pass '"),
                (Invalid, typed),
                (Plain, "' as a value, use '"),
            ],
        );
        style.put_joined(out, Valid, &["-- ", typed]);
        push_all(out, &["'\n"]);
    }
}

/// A name as a usage line writes it: `<TOOL>`, `[TOOL]…`, `--jobs`.
///
/// The error carries the spec's name for a thing; a user reads the form the help shows. Both come
/// from `help` rather than being decided again here — an error and the page above it describing
/// one argument differently is the confusing kind of inconsistency, and rewriting the rule is how
/// that happens. A flag is spelled with its dashes, which is the whole of what `--jobs` versus
/// `jobs` is about.
fn shown<'a>(meta: Option<&'a CommandMeta<'a>>, name: &str) -> String {
    let Some(meta) = meta else {
        return name.to_string();
    };
    if let Some(arg) = meta.args.iter().find(|a| a.arg.name == name) {
        return crate::help::arg_usage(arg);
    }
    // A flag can be named by an error too — a missing required one, or a value that is not among
    // its choices — and the spec's name for it has no dashes.
    if let Some(flag) = meta
        .flags
        .iter()
        .find(|f| f.flag.name == name || f.value_name == Some(name))
    {
        return crate::help::flag_spelling(flag);
    }
    name.to_string()
}

/// A group member is stored as a selector (`--file` or `-f`), not a field name.
fn group_member_shown(meta: Option<&CommandMeta<'_>>, selector: &str) -> String {
    let Some(meta) = meta else {
        return selector.to_string();
    };
    // Compared in place rather than by spelling each candidate out, which allocated per flag.
    let long = selector.strip_prefix("--");
    // A short is a byte, and it matches the one character after the dash that is that byte
    // read as a `char`.
    let short = selector.strip_prefix('-').and_then(|rest| {
        let mut chars = rest.chars();
        let only = u8::try_from(chars.next()?).ok()?;
        chars.next().is_none().then_some(only)
    });
    let found = meta.flags.iter().find(|flag| {
        long.is_some_and(|long| flag.flag.longs.contains(&long) || flag.flag.negate == Some(long))
            || short.is_some_and(|short| flag.flag.shorts.contains(&short))
    });
    let Some(flag) = found else {
        return selector.to_string();
    };
    let mut shown = crate::help::flag_spelling(flag);
    if flag.flag.takes_value {
        push_all(
            &mut shown,
            &[
                " <",
                flag.value_name.unwrap_or(flag.flag.name),
                if flag.flag.variadic { ">…" } else { ">" },
            ],
        );
    }
    shown
}

/// The word that was bound to a named argument, recovered from argv.
///
/// The parse itself does not carry it: an error that owned the offending text would allocate on
/// the one path this crate promises not to, so [`Error::InvalidChoice`] names the argument and
/// stops. Recovering it here is what that promise assumes — the diagnostics are a layer that may
/// do the work, and by the time one is being written the parse has already failed.
fn value_bound_to(
    root: &Command<'_>,
    argv: &[&std::ffi::OsStr],
    name: &str,
    refused: &[&str],
    view: Option<&ViewMeta<'_>>,
) -> Option<String> {
    let mut parser = crate::Parser::new(root, argv);
    if let Some(view) = view {
        parser = parser.with_view(view);
    }
    let mut last = None;
    while let Some(event) = parser.next_event() {
        let value = match event {
            Ok(crate::Event::Arg { arg, value, .. }) if arg.name == name => value,
            Ok(crate::Event::Flag {
                flag,
                value: Some(value),
                ..
            }) if flag.name == name => value,
            Ok(_) => continue,
            Err(_) => break,
        };
        let value = String::from_utf8_lossy(value).into_owned();
        // The *offending* one, not the last. A repeatable flag or a variadic argument may be
        // given several values, and the check refuses the first that is not allowed — reporting
        // whichever came last would name a value that is perfectly good and leave the wrong one
        // unmentioned.
        if !refused.is_empty() && !refused.contains(&value.as_str()) {
            return Some(value);
        }
        last = Some(value);
    }
    last
}

/// The commands the words went through, root first, ending at the one an error is about.
///
/// Walked rather than carried on the error: only some variants know their command, and a caller
/// that has just been handed an error has the argv it came from. The walk stops where the parse
/// stopped, which is the command whose usage line belongs in the message.
///
/// The whole path and not just its end, because the end does not identify itself. One
/// `Subcommands` type mounted under two parents is one `Command` in both — the same address —
/// so a search of the metadata tree for that address finds whichever mount comes first. That is
/// how `ex beta shared --betaglobl` came back describing `ex alpha shared`, suggesting alpha's
/// globals and not beta's. The route is the only thing that tells the two apart, so the route is
/// what gets carried.
fn path_taken<'t>(
    root: &'t Command<'t>,
    argv: &[&std::ffi::OsStr],
    view: Option<&'t ViewMeta<'t>>,
) -> Vec<&'t Command<'t>> {
    let mut path = vec![root];
    let mut parser = crate::Parser::new(root, argv);
    if let Some(view) = view {
        parser = parser.with_view(view);
    }
    while let Some(event) = parser.next_event() {
        match event {
            Ok(crate::Event::Command(cmd)) => path.push(cmd),
            Ok(_) => {}
            Err(_) => break,
        }
    }
    path
}

/// The metadata for each command along a path, and the path as a user would type it.
///
/// Each step is matched among *that command's* children, which is what makes it unambiguous:
/// two mounts of one `Subcommands` type share an address, but a parent's own child list is its
/// own. Returns nothing if the path leaves this spec, which cannot happen for a path this module
/// produced and is not worth a panic if it ever does.
fn resolve<'a>(
    spec: &'a Spec<'a>,
    path: &[&Command<'_>],
) -> Option<(Vec<&'a str>, Vec<&'a CommandMeta<'a>>)> {
    let mut names = vec![spec.bin.unwrap_or(spec.name)];
    let mut chain = vec![spec.root];
    for cmd in path.iter().skip(1) {
        let here = chain.last()?;
        let next = here
            .subcommands
            .iter()
            .find(|sub| core::ptr::eq(sub.cmd, *cmd))?;
        names.push(next.cmd.name);
        chain.push(next);
    }
    Some((names, chain))
}

fn span_of_slice(argv: &[&std::ffi::OsStr], slice: &[u8]) -> Option<ArgvSpan> {
    let start = slice.as_ptr() as usize;
    let end = start.checked_add(slice.len())?;
    argv.iter().enumerate().find_map(|(index, word)| {
        let bytes = word.as_encoded_bytes();
        let word_start = bytes.as_ptr() as usize;
        let word_end = word_start.checked_add(bytes.len())?;
        (start >= word_start && end <= word_end).then(|| ArgvSpan {
            index,
            start: start - word_start,
            end: end - word_start,
        })
    })
}

fn value_span(
    root: &Command<'_>,
    argv: &[&std::ffi::OsStr],
    name: &str,
    wanted: impl Fn(&str) -> bool,
    view: Option<&ViewMeta<'_>>,
) -> Option<ArgvSpan> {
    let mut parser = crate::Parser::new(root, argv);
    if let Some(view) = view {
        parser = parser.with_view(view);
    }
    while let Some(event) = parser.next_event() {
        let value = match event {
            Ok(crate::Event::Arg { arg, value, .. }) if arg.name == name => value,
            Ok(crate::Event::Flag {
                flag,
                value: Some(value),
                ..
            }) if flag.name == name => value,
            Ok(_) => continue,
            Err(_) => break,
        };
        if wanted(&String::from_utf8_lossy(value)) {
            return span_of_slice(argv, value);
        }
    }
    None
}

fn flag_span(
    root: &Command<'_>,
    argv: &[&std::ffi::OsStr],
    flag: &crate::Flag<'_>,
    view: Option<&ViewMeta<'_>>,
) -> Option<ArgvSpan> {
    let mut parser = crate::Parser::new(root, argv);
    if let Some(view) = view {
        parser = parser.with_view(view);
    }
    while let Some(event) = parser.next_event() {
        let Err(Error::MissingFlagValue { flag: missing }) = event else {
            continue;
        };
        if !core::ptr::eq(missing, flag) {
            return None;
        }
        let index = parser.pos.checked_sub(1)?;
        let bytes = argv.get(index)?.as_encoded_bytes();
        if let Some(long) = bytes.strip_prefix(b"--") {
            let name = long.split(|byte| *byte == b'=').next().unwrap_or(long);
            return flag.longs.iter().find_map(|candidate| {
                (name == candidate.as_bytes()).then_some(ArgvSpan {
                    index,
                    start: 0,
                    end: candidate.len() + 2,
                })
            });
        }
        let shorts = bytes.strip_prefix(b"-")?;
        return shorts.iter().enumerate().find_map(|(offset, short)| {
            flag.shorts.contains(short).then_some(ArgvSpan {
                index,
                start: offset + 1,
                end: offset + 2,
            })
        });
    }
    None
}

fn code(error: &Error<'_, '_>) -> Code {
    match error {
        Error::UnknownFlag { .. } => Code::UnknownFlag,
        Error::MissingFlagValue { .. } => Code::MissingFlagValue,
        Error::UnexpectedArg { .. } => Code::UnexpectedArg,
        Error::ArgRequiresDoubleDash { .. } => Code::ArgRequiresDoubleDash,
        Error::SubcommandConflict { .. } => Code::SubcommandConflict,
        Error::TooDeep => Code::TooDeep,
        Error::MissingRequired { .. } => Code::MissingRequired,
        Error::DuplicateFlag { .. } => Code::DuplicateFlag,
        Error::InvalidChoice { .. } => Code::InvalidChoice,
        Error::VarTooFew { .. } => Code::VarTooFew,
        Error::VarTooMany { .. } => Code::VarTooMany,
        Error::ConflictingFlags { .. } => Code::ConflictingFlags,
        Error::InvalidValue(_) => Code::InvalidValue,
        Error::MissingGroup { .. } => Code::MissingGroup,
        Error::MissingSubcommand => Code::MissingSubcommand,
        Error::Help { .. } => Code::Help,
        Error::MissingArgsHelp { .. } => Code::MissingArgsHelp,
        Error::HelpAll { .. } => Code::HelpAll,
        Error::Version { .. } => Code::Version,
    }
}

fn subject(error: &Error<'_, '_>) -> Option<String> {
    match error {
        Error::UnknownFlag { token } | Error::UnexpectedArg { token } => {
            Some(String::from_utf8_lossy(token).into_owned())
        }
        Error::MissingFlagValue { flag } => Some(flag.name.to_string()),
        Error::ArgRequiresDoubleDash { arg } => Some(arg.name.to_string()),
        Error::SubcommandConflict { subcommand } => Some(subcommand.name.to_string()),
        Error::MissingRequired { name }
        | Error::DuplicateFlag { name }
        | Error::InvalidChoice { name, .. }
        | Error::VarTooFew { name, .. }
        | Error::VarTooMany { name, .. }
        | Error::ConflictingFlags { name, .. } => Some((*name).to_string()),
        Error::InvalidValue(invalid) => Some(invalid.name.to_string()),
        Error::MissingGroup { group, .. } => Some((*group).to_string()),
        Error::TooDeep
        | Error::MissingSubcommand
        | Error::Help { .. }
        | Error::MissingArgsHelp { .. }
        | Error::HelpAll { .. }
        | Error::Version { .. } => None,
    }
}

fn location(
    spec: &Spec<'_>,
    argv: &[&std::ffi::OsStr],
    error: &Error<'_, '_>,
    view: Option<&ViewMeta<'_>>,
) -> Option<ArgvSpan> {
    match error {
        Error::UnknownFlag { token } | Error::UnexpectedArg { token } => span_of_slice(argv, token),
        Error::MissingFlagValue { flag } => flag_span(spec.root.cmd, argv, flag, view),
        Error::InvalidChoice { name, choices } => value_span(
            spec.root.cmd,
            argv,
            name,
            |value| !choices.contains(&value),
            view,
        ),
        Error::InvalidValue(invalid) => value_span(
            spec.root.cmd,
            argv,
            invalid.name,
            |value| value == invalid.value,
            view,
        ),
        _ => None,
    }
}

/// Describe a parse outcome as stable fields plus the ordinary plain-text rendering.
///
/// Locations are present when one argv word (or bytes within it) caused the failure. They are
/// absent for omissions and command-wide relationship failures, which have no honest single
/// token to underline.
pub fn report(spec: &Spec<'_>, argv: &[&std::ffi::OsStr], error: &Error<'_, '_>) -> Report {
    Report {
        code: code(error),
        subject: subject(error),
        location: location(spec, argv, error, None),
        rendered: render(spec, argv, error, Style::PLAIN),
    }
}

fn view_argv<'a>(
    argv: &'a [&'a std::ffi::OsStr],
    view: &'a ViewMeta<'_>,
) -> (Vec<&'a std::ffi::OsStr>, usize) {
    let words = argv.get(1..).unwrap_or_default();
    let root_depth = view.root.split_ascii_whitespace().count();
    let mut rewritten = Vec::with_capacity(words.len() + root_depth);
    rewritten.extend(view.root.split_ascii_whitespace().map(std::ffi::OsStr::new));
    rewritten.extend_from_slice(words);
    (rewritten, root_depth)
}

/// Describe a parse outcome through a spec-declared executable view.
///
/// `argv` is the original full argv, including the view executable as argv0. Locations address
/// that original slice; synthetic words from the view's canonical root are never exposed.
pub fn report_view<'a>(
    spec: &'a Spec<'a>,
    argv: &[&std::ffi::OsStr],
    error: &Error<'_, '_>,
    view: &'a ViewMeta<'a>,
) -> Report {
    let (rewritten, root_depth) = view_argv(argv, view);
    let location = location(spec, &rewritten, error, Some(view)).and_then(|span| {
        (span.index >= root_depth).then_some(ArgvSpan {
            index: span.index - root_depth + 1,
            start: span.start,
            end: span.end,
        })
    });
    Report {
        code: code(error),
        subject: subject(error),
        location,
        rendered: render_projected(spec, &rewritten, error, Style::PLAIN, view),
    }
}

/// Render `error` the way a user should read it.
///
/// `argv` is what was being parsed, which is how the message finds the command to show a usage
/// line for. [`Error::Help`] and [`Error::Version`] render as nothing: neither is a failure, and a
/// caller that has not
/// handled it before reaching here has a bug this cannot paper over.
pub fn render(
    spec: &Spec<'_>,
    argv: &[&std::ffi::OsStr],
    error: &Error<'_, '_>,
    style: Style,
) -> String {
    render_plain(spec, argv, error, style)
}

/// Render the deprecations a command line used, the way a user should read them.
///
/// One line each, in the order they were collected, with the same colour vocabulary as a failure:
/// what the user typed in yellow, what to use instead in green. The wording itself comes from
/// [`crate::warn`], so the plain and coloured halves cannot drift apart.
pub fn render_warnings(warnings: &[crate::warn::Warning<'_>], style: Style) -> String {
    let mut out = String::new();
    for warning in warnings {
        style.put_all(
            &mut out,
            &[
                (Ink::Warning, "warning:"),
                (Ink::Plain, " "),
                (Ink::Invalid, &crate::warn::subject(warning)),
                (Ink::Plain, " is deprecated"),
            ],
        );
        if let Some(at) = warning.remove_at {
            style.put_all(
                &mut out,
                &[(Ink::Plain, ", removed at "), (Ink::Literal, at)],
            );
        }
        // A replacement is something to type, so it is coloured like anything else that would
        // have worked; an author's own message is prose, and colouring a sentence green would
        // claim more about it than is known.
        match crate::warn::tail(warning) {
            Some(crate::warn::Tail::Message(message)) => push_all(&mut out, &[": ", message]),
            Some(crate::warn::Tail::Replacement(replacement)) => style.put_all(
                &mut out,
                &[(Ink::Plain, ": use "), (Ink::Valid, replacement)],
            ),
            None => {}
        }
        push_all(&mut out, &["\n"]);
    }
    out
}

/// Render a parse failure through a spec-declared executable view.
pub fn render_view<'a>(
    spec: &'a Spec<'a>,
    argv: &[&std::ffi::OsStr],
    error: &Error<'_, '_>,
    style: Style,
    view: &'a ViewMeta<'a>,
) -> String {
    let (rewritten, _) = view_argv(argv, view);
    render_projected(spec, &rewritten, error, style, view)
}

/// The route a view-less render takes: resolve the command the words reached, then describe it.
fn render_plain(
    spec: &Spec<'_>,
    argv: &[&std::ffi::OsStr],
    error: &Error<'_, '_>,
    style: Style,
) -> String {
    let taken = path_taken(spec.root.cmd, argv, None);
    let cmd = *taken.last().expect("the root is always on the path");
    let resolved = resolve(spec, &taken);
    let resolved = resolved
        .as_ref()
        .map(|(names, chain)| (&names[..], &chain[..]));
    describe(spec, spec.root.cmd, argv, error, style, None, cmd, resolved)
}

/// The same through a view, which first projects the promoted command into the root it is shown
/// as.
///
/// Kept apart from [`render_plain`] so that a CLI declaring no views, whose generated code only
/// ever reaches that one, carries none of the projection.
fn render_projected<'a>(
    spec: &'a Spec<'a>,
    argv: &[&std::ffi::OsStr],
    error: &Error<'_, '_>,
    style: Style,
    view: &'a ViewMeta<'a>,
) -> String {
    let canonical_spec = spec;
    let taken = path_taken(spec.root.cmd, argv, Some(view));
    let cmd = *taken.last().expect("the root is always on the path");
    let mut resolved = resolve(spec, &taken);
    let mut projected_spec = None;
    let mut projected_flags: Vec<FlagMeta<'_>> = Vec::new();
    let mut projected_groups = Vec::new();
    let depth = view.root.split_ascii_whitespace().count();
    let promoted = resolved
        .as_ref()
        .and_then(|(_, chain)| chain.get(depth).copied());
    if let Some(promoted) = promoted {
        let (flags, groups) = crate::help::view_root_fields(spec, promoted, view);
        projected_flags = flags;
        projected_groups = groups;
    }
    let projected_root = promoted.map(|promoted| CommandMeta {
        flags: &projected_flags,
        groups: &projected_groups,
        ..*promoted
    });
    if let (Some(promoted), Some(root), Some((names, chain))) =
        (promoted, projected_root.as_ref(), resolved.as_mut())
    {
        chain[0] = root;
        chain.drain(1..=depth.min(chain.len().saturating_sub(1)));
        names.drain(1..=depth.min(names.len().saturating_sub(1)));
        names[0] = view.bin;
        projected_spec = Some(Spec {
            name: view.name,
            bin: Some(view.bin),
            about: promoted.about,
            long_about: promoted.long_about,
            usage: None,
            default_subcommand: None,
            default_subcommand_help: false,
            multicall: false,
            root,
            ..*spec
        });
    }
    let spec = projected_spec.as_ref().unwrap_or(spec);
    let resolved = resolved
        .as_ref()
        .map(|(names, chain)| (&names[..], &chain[..]));
    describe(
        spec,
        canonical_spec.root.cmd,
        argv,
        error,
        style,
        Some(view),
        cmd,
        resolved,
    )
}

/// One `error: …` line, assembled from its pieces.
///
/// Every failure opens with one. Given as data rather than each through its own `writeln!`, the
/// messages share this routine instead of each compiling a copy of the formatting.
#[inline(never)]
fn error_line(out: &mut String, style: Style, parts: &[(Ink, &str)]) {
    style.put_all(out, &[(Ink::Error, "error:"), (Ink::Plain, " ")]);
    style.put_all(out, parts);
    push_all(out, &["\n"]);
}

/// An indented line under the error, such as a missing argument's name.
#[inline(never)]
fn listed_line(out: &mut String, style: Style, text: &str) {
    style.put_all(
        out,
        &[(Ink::Plain, "  "), (Ink::Valid, text), (Ink::Plain, "\n")],
    );
}

/// The message for an error at the command `resolved` ends on.
///
/// `spec` and `resolved` are what the user is shown — projected, for a view — while
/// `canonical_root` and `view` are what a fresh parse of `argv` needs to recover a value.
#[allow(clippy::too_many_arguments)]
fn describe(
    spec: &Spec<'_>,
    canonical_root: &Command<'_>,
    argv: &[&std::ffi::OsStr],
    error: &Error<'_, '_>,
    style: Style,
    view: Option<&ViewMeta<'_>>,
    cmd: &Command<'_>,
    resolved: Option<(&[&str], &[&CommandMeta<'_>])>,
) -> String {
    use Ink::{Invalid, Literal, Plain};

    let chain: &[&CommandMeta<'_>] = resolved.map(|(_, c)| c).unwrap_or(&[]);
    let here = chain.last().copied();
    // The command as a user would type it, for the two places that need it.
    let path = || {
        resolved
            .map(|(names, _)| names.join(" "))
            .unwrap_or_else(|| spec.bin.unwrap_or(spec.name).to_string())
    };

    // The argument or flag most errors are about, spelled the way the help spells it. Worked out
    // once here rather than in each arm, which is one call and one `String` to drop.
    let named = match error {
        Error::MissingRequired { name }
        | Error::DuplicateFlag { name }
        | Error::InvalidChoice { name, .. }
        | Error::ConflictingFlags { name, .. }
        | Error::VarTooFew { name, .. }
        | Error::VarTooMany { name, .. } => shown(here, name),
        Error::InvalidValue(invalid) => shown(here, invalid.name),
        Error::ArgRequiresDoubleDash { arg } => shown(here, arg.name),
        _ => String::new(),
    };

    let mut out = String::new();
    // The errors about the shape of the command line carry a usage block, as clap's do; the ones
    // about a single value clear this, on the grounds that the shape was right.
    let mut with_usage = true;

    match error {
        Error::UnknownFlag { token } | Error::UnexpectedArg { token } => {
            let word = String::from_utf8_lossy(token);
            // For an unexpected argument, what the word *looks like* decides, before anything
            // about the command does. A dash-prefixed token is a flag the user got wrong —
            // telling them `--forc` is an unrecognized subcommand is answering a question they
            // did not ask, and it happens on exactly the commands where the mistake is easiest
            // to make: the ones with subcommands, where a bare word would have been one.
            if matches!(error, Error::UnknownFlag { .. }) || (word.starts_with('-') && word != "-")
            {
                // A value attached with `=` is not part of the name, and an unexpected
                // argument reaches here by the same spelling mistake as a refused flag.
                let typed = flag_named(&word);
                let capture = value_capture(argv, token, here);
                unexpected_flag(&mut out, style, typed, chain, capture);
            } else if cmd.subcommands.is_empty() {
                error_line(
                    &mut out,
                    style,
                    &[
                        (Plain, "unexpected argument '"),
                        (Invalid, &word),
                        (Plain, "' found"),
                    ],
                );
            } else {
                error_line(
                    &mut out,
                    style,
                    &[
                        (Plain, "unrecognized subcommand '"),
                        (Invalid, &word),
                        (Plain, "'"),
                    ],
                );
                // Every name a subcommand answers to, hidden ones included: a user who typed a
                // near miss of an old alias should be told the name it still works under.
                let mut names: Vec<&str> = Vec::new();
                for sub in cmd.subcommands {
                    names.push(sub.name);
                    names.extend_from_slice(sub.aliases);
                }
                tip(&mut out, style, "subcommand", "", &nearest(&word, &names));
            }
        }
        Error::SubcommandConflict { subcommand } => {
            error_line(
                &mut out,
                style,
                &[
                    (Plain, "the subcommand '"),
                    (Invalid, subcommand.name),
                    (
                        Plain,
                        "' cannot be used with arguments on its parent command",
                    ),
                ],
            );
        }
        Error::MissingRequired { .. } => {
            error_line(
                &mut out,
                style,
                &[(Plain, "the following required arguments were not provided:")],
            );
            listed_line(&mut out, style, &named);
        }
        Error::DuplicateFlag { .. } => {
            error_line(
                &mut out,
                style,
                &[
                    (Plain, "the argument '"),
                    (Invalid, &named),
                    (Plain, "' cannot be used multiple times"),
                ],
            );
        }
        Error::MissingSubcommand => {
            // A bare command that can do nothing on its own is a request for orientation.
            // clap prints the command's help page here, including the available subcommands,
            // while keeping exit 2; an error plus only `<SUBCOMMAND>` tells the reader what is
            // missing and withholds the list they need to fix it.
            let help_style = crate::help::Style::coloured_if(style.coloured);
            // `spec`, `chain`, and this route have already been projected for a view. Feeding
            // the canonical host route through the view renderer a second time makes it look
            // for host-only ancestors under the promoted root and loses the useful full help
            // page.
            let presented_route: Vec<&Command<'_>> = chain.iter().map(|meta| meta.cmd).collect();
            let help = crate::help::render_at_styled(spec, &presented_route, false, help_style);
            if let Some(help) = help {
                return help;
            }
            error_line(
                &mut out,
                style,
                &[
                    (Plain, "'"),
                    (Invalid, &path()),
                    (Plain, "' requires a subcommand but one was not provided"),
                ],
            );
        }
        Error::MissingFlagValue { flag } => {
            with_usage = false;
            let mut short = [0; 4];
            let (dashes, spelled): (&str, &str) = if let Some(long) = flag.longs.first() {
                ("--", long)
            } else if let Some(first) = flag.shorts.first() {
                ("-", (*first as char).encode_utf8(&mut short))
            } else {
                ("", flag.name)
            };
            let value = here
                .and_then(|meta| {
                    meta.flags
                        .iter()
                        .find(|m| core::ptr::eq(m.flag, *flag))
                        .and_then(|m| m.value_name)
                })
                .unwrap_or(flag.name);
            let (before, joint, after) = if flag.require_equals {
                ("equal sign is needed when assigning values to '", "=<", "'")
            } else {
                ("a value is required for '", " <", "' but none was supplied")
            };
            let mut wanted = String::new();
            push_all(&mut wanted, &[dashes, spelled, joint, value, ">"]);
            error_line(
                &mut out,
                style,
                &[(Plain, before), (Invalid, &wanted), (Plain, after)],
            );
        }
        Error::InvalidChoice { name, choices } => {
            with_usage = false;
            let typed = value_bound_to(canonical_root, argv, name, choices, view);
            match typed.as_deref() {
                Some(value) => error_line(
                    &mut out,
                    style,
                    &[
                        (Plain, "invalid value '"),
                        (Invalid, value),
                        (Plain, "' for '"),
                        (Literal, &named),
                        (Plain, "'"),
                    ],
                ),
                // Nothing in argv bound to it, which means the value came from somewhere else —
                // an environment variable, or a default the spec declared.
                None => error_line(
                    &mut out,
                    style,
                    &[
                        (Plain, "invalid value for '"),
                        (Literal, &named),
                        (Plain, "'"),
                    ],
                ),
            }
            push_all(&mut out, &["  [possible values: "]);
            for (i, choice) in choices.iter().enumerate() {
                style.put_all(
                    &mut out,
                    &[
                        (Plain, if i == 0 { "" } else { ", " }),
                        (Ink::Valid, choice),
                    ],
                );
            }
            push_all(&mut out, &["]\n"]);
            if let Some(typed) = typed {
                tip(&mut out, style, "value", "", &nearest(&typed, choices));
            }
        }
        Error::InvalidValue(invalid) => {
            with_usage = false;
            error_line(
                &mut out,
                style,
                &[
                    (Plain, "invalid value '"),
                    (Invalid, &invalid.value),
                    (Plain, "' for '"),
                    (Literal, &named),
                    (Plain, "': "),
                    (Plain, &invalid.reason),
                ],
            );
        }
        Error::MissingGroup { group, members } => {
            // clap's own shape for a required group, which is the required-arguments
            // message with the members listed under it. The group's name goes on the
            // first line rather than into the list, since it is not something to type.
            error_line(
                &mut out,
                style,
                &[
                    (
                        Plain,
                        "one of the following required arguments was not provided (",
                    ),
                    (Plain, group),
                    (Plain, "):"),
                ],
            );
            for member in *members {
                listed_line(&mut out, style, &group_member_shown(here, member));
            }
        }
        Error::ConflictingFlags { other, .. } => {
            // Spelled by `help`, like every other name in this module — and like clap, which
            // writes `the argument '--force' cannot be used with '--jobs <JOBS>'`.
            error_line(
                &mut out,
                style,
                &[
                    (Plain, "the argument '"),
                    (Invalid, &named),
                    (Plain, "' cannot be used with '"),
                    (Invalid, &shown(here, other)),
                    (Plain, "'"),
                ],
            );
        }
        Error::VarTooFew {
            min: count, got, ..
        }
        | Error::VarTooMany {
            max: count, got, ..
        } => {
            with_usage = false;
            let bound = if matches!(error, Error::VarTooFew { .. }) {
                " values required for '"
            } else {
                " values allowed for '"
            };
            error_line(
                &mut out,
                style,
                &[
                    (Plain, &count.to_string()),
                    (Plain, bound),
                    (Literal, &named),
                    (Plain, "' but "),
                    (Plain, &got.to_string()),
                    (Plain, " were provided"),
                ],
            );
        }
        Error::ArgRequiresDoubleDash { .. } => {
            error_line(
                &mut out,
                style,
                &[
                    (Plain, "'"),
                    (Literal, &named),
                    (Plain, "' can only be given after '"),
                    (Literal, "--"),
                    (Plain, "'"),
                ],
            );
        }
        Error::TooDeep => {
            with_usage = false;
            error_line(
                &mut out,
                style,
                &[(Plain, "this command line nests deeper than the parser goes")],
            );
        }
        // Neither is a failure, and a caller that has not handled them before reaching here
        // has a bug this cannot paper over.
        Error::Help { .. }
        | Error::MissingArgsHelp { .. }
        | Error::HelpAll { .. }
        | Error::Version { .. } => {
            return String::new();
        }
    }

    if with_usage {
        let usage = match (resolved, here) {
            (Some((names, _)), Some(meta)) => crate::help::usage_line(names, meta),
            _ => path(),
        };
        style.put_all(
            &mut out,
            &[
                (Plain, "\n"),
                (Ink::Heading, "Usage:"),
                (Plain, " "),
                (Literal, &usage),
                (Plain, "\n"),
            ],
        );
    }
    style.put_all(
        &mut out,
        &[
            (Plain, "\nFor more information, try '"),
            (Literal, "--help"),
            (Plain, "'.\n"),
        ],
    );
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::spec::{ArgMeta, FlagMeta};
    use crate::{Arg, Flag};

    static FORCE: Flag = Flag {
        key: 1,
        name: "force",
        longs: &["force"],
        shorts: b"f",
        // A negation, because a flag's spellings are not only its `longs` and the parser takes
        // this one — so a suggestion that cannot offer it is offering less than the CLI accepts.
        negate: Some("no-force"),
        ..Flag::BOOL
    };
    static JOBS: Flag = Flag {
        key: 2,
        name: "jobs",
        longs: &["jobs"],
        ..Flag::VALUE
    };
    static TOOL: Arg = Arg {
        key: 3,
        name: "TOOL",
        ..Arg::REQUIRED
    };
    /// Variadic and choice-bearing, so "which value was refused" has a wrong answer available.
    static SHELLS: Arg = Arg {
        key: 7,
        name: "SHELLS",
        ..Arg::VAR
    };
    static USE: Command = Command {
        name: "use",
        flags: &[&FORCE, &JOBS],
        args: &[&TOOL, &SHELLS],
        ..Command::EMPTY
    };
    static QUIET: Flag = Flag {
        key: 4,
        name: "quiet",
        longs: &["quiet"],
        global: true,
        ..Flag::BOOL
    };
    /// A second command close to the same typo, so the plural wording is reachable.
    static LOCAL: Flag = Flag {
        key: 5,
        name: "local",
        longs: &["local"],
        global: true,
        ..Flag::BOOL
    };
    static USER: Command = Command {
        name: "user",
        flags: &[&LOCAL],
        ..Command::EMPTY
    };
    /// Declared on the root and *not* global, so it belongs to the root alone.
    static SETUP: Flag = Flag {
        key: 6,
        name: "setup",
        longs: &["setup"],
        ..Flag::BOOL
    };
    /// `--`-only, the shape of `mise exec -- <command>`. A command whose job is to collect a
    /// command line is the one case where a flag it does not know may still have been meant as
    /// a value, which is what makes the separator tip worth printing next to a spelling.
    static CMDLINE: Arg = Arg {
        key: 8,
        name: "COMMAND",
        double_dash: DoubleDash::Required,
        ..Arg::VAR
    };
    static EXEC: Command = Command {
        name: "exec",
        flags: &[&FORCE],
        args: &[&CMDLINE],
        ..Command::EMPTY
    };
    static ROOT: Command = Command {
        name: "ex",
        flags: &[&QUIET, &SETUP],
        // `user` first, so the walk to `use` passes through it: a sibling that is visited on the
        // way is exactly what leaked into scope before.
        subcommands: &[&USER, &USE, &EXEC],
        ..Command::EMPTY
    };
    static USE_META: CommandMeta = CommandMeta {
        cmd: &USE,
        about: Some("Use a tool"),
        flags: &[
            FlagMeta {
                flag: &FORCE,
                help: Some("Force it"),
                ..FlagMeta::EMPTY
            },
            FlagMeta {
                flag: &JOBS,
                help: Some("How many"),
                value_name: Some("JOBS"),
                ..FlagMeta::EMPTY
            },
        ],
        args: &[
            ArgMeta {
                arg: &TOOL,
                help: Some("Which tool"),
                required: true,
                ..ArgMeta::EMPTY
            },
            ArgMeta {
                arg: &SHELLS,
                help: Some("Which shells"),
                choices: &["bash", "zsh"],
                required: false,
                ..ArgMeta::EMPTY
            },
        ],
        ..CommandMeta::EMPTY
    };
    static USER_META: CommandMeta = CommandMeta {
        cmd: &USER,
        about: Some("Manage users"),
        flags: &[FlagMeta {
            flag: &LOCAL,
            help: Some("Only this checkout"),
            ..FlagMeta::EMPTY
        }],
        ..CommandMeta::EMPTY
    };
    static EXEC_META: CommandMeta = CommandMeta {
        cmd: &EXEC,
        about: Some("Run a command"),
        flags: &[FlagMeta {
            flag: &FORCE,
            help: Some("Force it"),
            ..FlagMeta::EMPTY
        }],
        args: &[ArgMeta {
            arg: &CMDLINE,
            help: Some("What to run"),
            required: true,
            ..ArgMeta::EMPTY
        }],
        ..CommandMeta::EMPTY
    };
    static ROOT_META: CommandMeta = CommandMeta {
        cmd: &ROOT,
        flags: &[
            FlagMeta {
                flag: &QUIET,
                help: Some("Say less"),
                ..FlagMeta::EMPTY
            },
            FlagMeta {
                flag: &SETUP,
                help: Some("Set things up"),
                ..FlagMeta::EMPTY
            },
        ],
        subcommands: &[&USER_META, &USE_META, &EXEC_META],
        ..CommandMeta::EMPTY
    };
    static SPEC: Spec = Spec {
        name: "ex",
        bin: Some("ex"),
        root: &ROOT_META,
        ..Spec::EMPTY
    };

    fn rendered(words: &[&str], error: Error<'static, 'static>) -> String {
        let owned: Vec<std::ffi::OsString> = words.iter().map(std::ffi::OsString::from).collect();
        let argv: Vec<&std::ffi::OsStr> = owned.iter().map(|o| o.as_os_str()).collect();
        render(&SPEC, &argv, &error, Style::PLAIN)
    }

    #[test]
    fn a_structured_report_locates_the_exact_unknown_word() {
        let owned = [
            std::ffi::OsString::from("use"),
            std::ffi::OsString::from("--fore"),
        ];
        let argv = owned
            .iter()
            .map(|word| word.as_os_str())
            .collect::<Vec<_>>();
        let error = Error::UnknownFlag {
            token: argv[1].as_encoded_bytes(),
        };
        let report = report(&SPEC, &argv, &error);
        assert_eq!(report.code, Code::UnknownFlag);
        assert_eq!(report.code.as_str(), "unknown_flag");
        assert_eq!(report.subject.as_deref(), Some("--fore"));
        assert_eq!(
            report.location,
            Some(ArgvSpan {
                index: 1,
                start: 0,
                end: 6,
            })
        );
        assert!(report.rendered.starts_with("error: unexpected argument"));
    }

    #[test]
    fn a_structured_report_locates_an_attached_invalid_value() {
        let owned = [
            std::ffi::OsString::from("use"),
            std::ffi::OsString::from("--jobs=wat"),
            std::ffi::OsString::from("tool"),
        ];
        let argv = owned
            .iter()
            .map(|word| word.as_os_str())
            .collect::<Vec<_>>();
        let error = Error::InvalidValue(Box::new(crate::InvalidValue {
            name: "jobs",
            value: "wat".to_string(),
            reason: "invalid digit found in string".to_string(),
        }));
        let report = report(&SPEC, &argv, &error);
        assert_eq!(report.code, Code::InvalidValue);
        assert_eq!(
            report.location,
            Some(ArgvSpan {
                index: 1,
                start: 7,
                end: 10,
            })
        );
    }

    #[test]
    fn a_structured_report_finds_the_refused_value_in_a_variadic() {
        let owned = [
            std::ffi::OsString::from("use"),
            std::ffi::OsString::from("tool"),
            std::ffi::OsString::from("bash"),
            std::ffi::OsString::from("fish"),
        ];
        let argv = owned
            .iter()
            .map(|word| word.as_os_str())
            .collect::<Vec<_>>();
        let error = Error::InvalidChoice {
            name: "SHELLS",
            choices: &["bash", "zsh"],
        };
        assert_eq!(
            report(&SPEC, &argv, &error).location,
            Some(ArgvSpan {
                index: 3,
                start: 0,
                end: 4,
            })
        );
    }

    #[test]
    fn a_missing_flag_value_points_at_the_flag() {
        let owned = [
            std::ffi::OsString::from("use"),
            std::ffi::OsString::from("--jobs"),
        ];
        let argv = owned
            .iter()
            .map(|word| word.as_os_str())
            .collect::<Vec<_>>();
        let error = Error::MissingFlagValue { flag: &JOBS };
        assert_eq!(
            report(&SPEC, &argv, &error).location,
            Some(ArgvSpan {
                index: 1,
                start: 0,
                end: 6,
            })
        );
    }

    #[test]
    fn a_repeated_missing_flag_points_at_the_occurrence_the_parser_refused() {
        let owned = [
            std::ffi::OsString::from("use"),
            std::ffi::OsString::from("--jobs"),
            std::ffi::OsString::from("4"),
            std::ffi::OsString::from("--jobs"),
        ];
        let argv = owned
            .iter()
            .map(|word| word.as_os_str())
            .collect::<Vec<_>>();
        let error = Error::MissingFlagValue { flag: &JOBS };
        assert_eq!(
            report(&SPEC, &argv, &error).location,
            Some(ArgvSpan {
                index: 3,
                start: 0,
                end: 6,
            })
        );
    }

    #[test]
    fn view_reports_map_invalid_values_back_to_the_original_argv() {
        static VIEW: ViewMeta = ViewMeta {
            id: "runner",
            name: "runner",
            bin: "runner",
            root: "use",
            all_globals: false,
            globals: &[],
        };

        let invalid_owned = [
            std::ffi::OsString::from("runner"),
            std::ffi::OsString::from("--jobs=wat"),
        ];
        let invalid_argv = invalid_owned
            .iter()
            .map(|word| word.as_os_str())
            .collect::<Vec<_>>();
        let invalid = Error::InvalidValue(Box::new(crate::InvalidValue {
            name: "jobs",
            value: "wat".to_string(),
            reason: "invalid digit found in string".to_string(),
        }));
        assert_eq!(
            report_view(&SPEC, &invalid_argv, &invalid, &VIEW).location,
            Some(ArgvSpan {
                index: 1,
                start: 7,
                end: 10,
            })
        );

        let choice_owned = [
            std::ffi::OsString::from("runner"),
            std::ffi::OsString::from("tool"),
            std::ffi::OsString::from("fish"),
        ];
        let choice_argv = choice_owned
            .iter()
            .map(|word| word.as_os_str())
            .collect::<Vec<_>>();
        let choice = Error::InvalidChoice {
            name: "SHELLS",
            choices: &["bash", "zsh"],
        };
        assert_eq!(
            report_view(&SPEC, &choice_argv, &choice, &VIEW).location,
            Some(ArgvSpan {
                index: 2,
                start: 0,
                end: 4,
            })
        );
    }

    #[test]
    fn the_usage_line_is_the_one_the_help_prints() {
        // Not clap's, which spells a usage line its own way. An error that disagrees with the
        // help about how a command is written is worse than one that disagrees with clap.
        let message = rendered(&["use"], Error::UnknownFlag { token: b"--fore" });
        let line = message
            .lines()
            .find_map(|l| l.strip_prefix("Usage: "))
            .expect("a usage line");
        assert_eq!(line, crate::help::usage_line(&["ex", "use"], &USE_META));
    }

    #[test]
    fn a_missing_group_lists_value_taking_members_completely() {
        static FILE: Flag = Flag {
            name: "file",
            longs: &["file"],
            takes_value: true,
            ..Flag::BOOL
        };
        static ROOT: Command = Command {
            name: "grouped",
            flags: &[&FILE],
            ..Command::EMPTY
        };
        static META: CommandMeta = CommandMeta {
            cmd: &ROOT,
            flags: &[FlagMeta {
                flag: &FILE,
                value_name: Some("PATH"),
                ..FlagMeta::EMPTY
            }],
            ..CommandMeta::EMPTY
        };
        static SPEC: Spec = Spec {
            name: "grouped",
            bin: Some("grouped"),
            root: &META,
            ..Spec::EMPTY
        };
        let message = render(
            &SPEC,
            &[],
            &Error::MissingGroup {
                group: "input",
                members: &["--file"],
            },
            Style::PLAIN,
        );
        assert!(message.contains("  --file <PATH>"), "{message}");
    }

    #[test]
    fn a_missing_subcommand_prints_the_choices() {
        let message = rendered(&[], Error::MissingSubcommand);
        assert!(message.contains("Commands:"), "{message}");
        assert!(message.contains("use"), "{message}");
        assert!(message.contains("user"), "{message}");
        assert!(!message.contains("requires a subcommand"), "{message}");

        let coloured = render(&SPEC, &[], &Error::MissingSubcommand, Style::COLOURED);
        assert!(
            coloured.contains("\u{1b}[1;33mCommands:\u{1b}[0m"),
            "{coloured}"
        );
    }

    #[test]
    fn a_view_missing_subcommand_prints_the_promoted_choices() {
        static CHILD: Command = Command {
            name: "child",
            ..Command::EMPTY
        };
        static RUN: Command = Command {
            name: "run",
            subcommands: &[&CHILD],
            ..Command::EMPTY
        };
        static HOST: Command = Command {
            name: "host",
            subcommands: &[&RUN],
            ..Command::EMPTY
        };
        static CHILD_META: CommandMeta = CommandMeta {
            cmd: &CHILD,
            about: Some("Do the child thing"),
            ..CommandMeta::EMPTY
        };
        static RUN_META: CommandMeta = CommandMeta {
            cmd: &RUN,
            subcommands: &[&CHILD_META],
            ..CommandMeta::EMPTY
        };
        static HOST_META: CommandMeta = CommandMeta {
            cmd: &HOST,
            subcommands: &[&RUN_META],
            ..CommandMeta::EMPTY
        };
        static VIEW: ViewMeta = ViewMeta {
            id: "runner",
            name: "runner",
            bin: "runner",
            root: "run",
            all_globals: false,
            globals: &[],
        };
        static VIEW_SPEC: Spec = Spec {
            name: "host",
            bin: Some("host"),
            root: &HOST_META,
            views: &[VIEW],
            ..Spec::EMPTY
        };

        let message = render_view(
            &VIEW_SPEC,
            &[std::ffi::OsStr::new("runner")],
            &Error::MissingSubcommand,
            Style::PLAIN,
            &VIEW,
        );
        assert!(message.contains("Usage: runner"), "{message}");
        assert!(message.contains("Commands:"), "{message}");
        assert!(message.contains("child"), "{message}");
        assert!(!message.contains("requires a subcommand"), "{message}");
    }

    #[test]
    fn a_missing_value_names_what_it_wanted() {
        // No usage block: the shape of the command line was right, one value was missing — which
        // is the distinction clap draws too.
        assert_eq!(
            rendered(&["use"], Error::MissingFlagValue { flag: &JOBS }),
            "error: a value is required for '--jobs <JOBS>' but none was supplied\n\
             \n\
             For more information, try '--help'.\n"
        );
    }

    #[test]
    fn a_word_where_a_subcommand_was_expected_says_so() {
        // The root has subcommands, so an unexpected word there is an unrecognized subcommand;
        // inside `use`, which has none, the same error is an unexpected argument.
        let at_root = rendered(&[], Error::UnexpectedArg { token: b"nonesuch" });
        assert!(
            at_root.starts_with("error: unrecognized subcommand 'nonesuch'"),
            "{at_root}"
        );
        let in_use = rendered(&["use"], Error::UnexpectedArg { token: b"extra" });
        assert!(
            in_use.starts_with("error: unexpected argument 'extra' found"),
            "{in_use}"
        );
    }

    #[test]
    fn a_required_argument_is_listed_the_way_clap_lists_it() {
        assert_eq!(
            rendered(&["use"], Error::MissingRequired { name: "<TOOL>" }),
            "error: the following required arguments were not provided:\n  \
             <TOOL>\n\
             \n\
             Usage: ex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…\n\
             \n\
             For more information, try '--help'.\n"
        );
    }

    #[test]
    fn colour_is_the_same_codes_clap_uses() {
        // Measured from clap 4 rather than remembered: bold red for `error:`, yellow for what was
        // typed, bold underline for `Usage:`, bold for what to type.
        let owned = [std::ffi::OsString::from("use")];
        let argv: Vec<&std::ffi::OsStr> = owned.iter().map(|o| o.as_os_str()).collect();
        let message = render(
            &SPEC,
            &argv,
            &Error::UnknownFlag { token: b"--fore" },
            Style::COLOURED,
        );
        assert!(
            message.starts_with("\u{1b}[1m\u{1b}[31merror:\u{1b}[0m"),
            "{message:?}"
        );
        assert!(message.contains("\u{1b}[33m--fore\u{1b}[0m"), "{message:?}");
        assert!(
            message.contains("\u{1b}[1m\u{1b}[4mUsage:\u{1b}[0m"),
            "{message:?}"
        );
        // And nothing at all when plain, which is what a pipe or a test gets.
        assert!(!rendered(&["use"], Error::UnknownFlag { token: b"--fore" }).contains('\u{1b}'));
    }

    #[test]
    fn a_help_request_renders_nothing() {
        // It is not a failure, and a caller that reaches here with one has skipped handling it —
        // which a message would hide rather than help.
        assert_eq!(
            rendered(
                &["use"],
                Error::Help {
                    cmd: &USE,
                    long: true
                }
            ),
            ""
        );
    }
    #[test]
    fn a_name_is_spelled_the_way_the_help_spells_it() {
        // One rule, taken from `help` rather than decided again here: an error and the page above
        // it describing the same argument differently is the confusing kind of inconsistency.
        let message = rendered(&["use"], Error::MissingRequired { name: "TOOL" });
        assert!(message.contains("  <TOOL>"), "{message}");

        // A variadic keeps its ellipsis, exactly as the usage line writes it.
        let message = rendered(
            &["use"],
            Error::VarTooFew {
                name: "SHELLS",
                min: 2,
                got: 1,
            },
        );
        assert!(message.contains("'[SHELLS]…'"), "{message}");

        // And a *flag* is spelled with its dashes: the spec calls it `jobs`, a user reads
        // `--jobs`.
        let message = rendered(&["use"], Error::MissingRequired { name: "jobs" });
        assert!(message.contains("  --jobs"), "{message}");

        let message = rendered(&["use"], Error::DuplicateFlag { name: "jobs" });
        assert!(
            message.contains("the argument '--jobs' cannot be used multiple times"),
            "{message}"
        );

        // Every variant, not most of them. These two printed the spec's name while the ones
        // directly above and below them did not, so one argument could appear two ways in two
        // messages from the same command — and clap writes the dashes here too:
        //
        //     error: the argument '--force' cannot be used with '--jobs <JOBS>'
        let message = rendered(
            &["use"],
            Error::ConflictingFlags {
                name: "force",
                other: "jobs",
            },
        );
        assert!(
            message.contains("the argument '--force' cannot be used with '--jobs'"),
            "{message}"
        );

        let message = rendered(&["use"], Error::ArgRequiresDoubleDash { arg: &SHELLS });
        assert!(
            message.contains("'[SHELLS]…' can only be given"),
            "{message}"
        );
    }

    #[test]
    fn the_value_named_is_the_one_that_was_refused() {
        // A variadic given several values: the check refuses the first that is not allowed, so
        // naming whichever came last would name a value that is perfectly good and leave the
        // wrong one unmentioned.
        let owned = [
            std::ffi::OsString::from("use"),
            std::ffi::OsString::from("node"),
            std::ffi::OsString::from("fsh"),
            std::ffi::OsString::from("zsh"),
        ];
        let argv: Vec<&std::ffi::OsStr> = owned.iter().map(|o| o.as_os_str()).collect();
        let message = render(
            &SPEC,
            &argv,
            &Error::InvalidChoice {
                name: "SHELLS",
                choices: &["bash", "zsh"],
            },
            Style::PLAIN,
        );
        // The first line names the value, so assert on that line alone: the tip below it lists
        // every choice, `zsh` among them, and a whole-message search cannot tell the two apart.
        assert!(
            message.starts_with("error: invalid value 'fsh' for '[SHELLS]…'"),
            "named a value that was fine: {message}"
        );
    }

    #[test]
    fn a_view_recovers_the_choice_value_from_the_canonical_parse() {
        static VIEW: ViewMeta = ViewMeta {
            id: "runner",
            name: "runner",
            bin: "runner",
            root: "use",
            all_globals: false,
            globals: &[],
        };
        static VIEW_SPEC: Spec = Spec {
            views: &[VIEW],
            ..SPEC
        };
        let argv = [std::ffi::OsStr::new("runner"), std::ffi::OsStr::new("nod")];
        let message = render_view(
            &VIEW_SPEC,
            &argv,
            &Error::InvalidChoice {
                name: "TOOL",
                choices: &["node", "python"],
            },
            Style::PLAIN,
            &VIEW,
        );
        assert!(
            message.contains("invalid value 'nod' for '<TOOL>'"),
            "{message}"
        );
        assert!(
            message.contains("tip: a similar value exists: 'node'"),
            "{message}"
        );
    }

    #[test]
    fn a_near_miss_is_suggested_the_way_clap_suggests_one() {
        let message = rendered(&["use"], Error::UnknownFlag { token: b"--fore" });
        assert_eq!(
            message,
            "error: unexpected argument '--fore' found\n\
             \n\
             \x20 tip: a similar argument exists: '--force'\n\
             \n\
             Usage: ex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…\n\
             \n\
             For more information, try '--help'.\n"
        );
    }

    #[test]
    fn a_value_attached_to_a_flag_is_not_part_of_its_name() {
        // `--fore=1`. The parser splits on the `=` before looking the name up, so the flag the
        // user named is `--fore` and an error about `--fore=1` is about something nobody typed.
        //
        // Both halves matter, and clap 4 was run to check both rather than remembered:
        //
        //     error: unexpected argument '--fore' found
        //       tip: a similar argument exists: '--force'
        //
        // The tip is the half that would have gone quietly: `fore=1` against `force` falls under
        // the 0.7 bar, so leaving the value on loses the suggestion exactly where a mistyped
        // value-taking flag is most likely to be written.
        assert_eq!(
            rendered(&["use"], Error::UnknownFlag { token: b"--fore=1" }),
            "error: unexpected argument '--fore' found\n\
             \n\
             \x20 tip: a similar argument exists: '--force'\n\
             \n\
             Usage: ex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…\n\
             \n\
             For more information, try '--help'.\n"
        );

        // A short cluster is refused whole — `-xy` is not `-x` with a `y` attached — and clap
        // keeps the `=` in a short flag's value, so the rule is for long flags only.
        let message = rendered(&["use"], Error::UnknownFlag { token: b"-j=4" });
        assert!(
            message.starts_with("error: unexpected argument '-j=4' found"),
            "{message}"
        );
    }

    #[test]
    fn a_negation_is_suggested_like_any_other_spelling() {
        // `--no-force` is a name the parser accepts, through `find_negation`, and one the
        // completions already offer. Scoring only `longs` left it out, so a near miss of a name
        // that works got silence — and clap, which has no separate notion of a negation and
        // sees two arguments, suggests it. Measured:
        //
        //     error: unexpected argument '--no-colr' found
        //       tip: a similar argument exists: '--no-color'
        let message = rendered(
            &["use"],
            Error::UnknownFlag {
                token: b"--no-forc",
            },
        );
        assert!(
            message.contains("tip: a similar argument exists: '--no-force'"),
            "{message}"
        );

        // And the plain form is still found, which is the half that already worked.
        let message = rendered(&["use"], Error::UnknownFlag { token: b"--fore" });
        assert!(
            message.contains("tip: a similar argument exists: '--force'"),
            "{message}"
        );
    }

    #[test]
    fn nothing_is_suggested_when_nothing_is_close() {
        // Offering `--force` for `--zzz` is worse than offering nothing: a user reads a tip as
        // the CLI having understood them. clap's threshold, so clap's silence about spelling.
        // What is left is the separator tip, for the same reason: with no flag close to it, a
        // word this command refused is as likely to have been a value as a mistake. clap 4 was
        // run to check, not remembered.
        assert_eq!(
            rendered(&["use"], Error::UnknownFlag { token: b"--zzz" }),
            "error: unexpected argument '--zzz' found\n\
             \n\
             \x20 tip: to pass '--zzz' as a value, use '-- --zzz'\n\
             \n\
             Usage: ex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…\n\
             \n\
             For more information, try '--help'.\n"
        );
    }

    /// <https://github.com/jdx/mise/discussions/13196> is what this is for: a shell that eats the
    /// separator leaves `mise exec pnpm --version`, and the message a user got named `--verbose`
    /// and nothing else. The flag they typed was never meant for mise.
    #[test]
    fn a_command_that_collects_a_command_line_offers_the_separator_too() {
        // `exec` takes its values only after `--`, so both readings are live at once: a near
        // miss on one of its own flags, and a flag meant for whatever it runs. clap prints both
        // here and only the spelling on an ordinary command; this is that rule, either way.
        assert_eq!(
            rendered(&["exec"], Error::UnknownFlag { token: b"--fore" }),
            "error: unexpected argument '--fore' found\n\
             \n\
             \x20 tip: a similar argument exists: '--force'\n\
             \x20 tip: to pass '--fore' as a value, use '-- --fore'\n\
             \n\
             Usage: ex exec [-f --force] <-- COMMAND>…\n\
             \n\
             For more information, try '--help'.\n"
        );

        // An ordinary command keeps the spelling alone. Without this the pair above says only
        // that a tip appeared, not that it appeared where clap puts it.
        let ordinary = rendered(&["use"], Error::UnknownFlag { token: b"--fore" });
        assert!(
            ordinary.contains("tip: a similar argument exists: '--force'"),
            "{ordinary}"
        );
        assert!(!ordinary.contains("to pass"), "{ordinary}");
    }

    /// A user who has already typed one separator knows about them; a second refusal past it is
    /// a real mistake, and repeating the advice they just took reads as the CLI not listening.
    #[test]
    fn the_separator_is_not_suggested_once_it_has_been_given() {
        let owned = [
            std::ffi::OsString::from("exec"),
            std::ffi::OsString::from("--"),
            std::ffi::OsString::from("--zzz"),
        ];
        let argv: Vec<&std::ffi::OsStr> = owned.iter().map(|word| word.as_os_str()).collect();
        let message = render(
            &SPEC,
            &argv,
            &Error::UnknownFlag {
                token: argv[2].as_encoded_bytes(),
            },
            Style::PLAIN,
        );
        assert!(!message.contains("to pass"), "{message}");
    }

    /// `render` and `report` are public and accept an error an embedding built itself, whose
    /// token shares no storage with argv. Locating it by storage alone then found nothing, the
    /// scan fell back to the whole of argv, and a `--` standing *after* the refused flag was
    /// read as standing before it — withholding the tip on the strength of a separator the user
    /// had not reached yet.
    #[test]
    fn a_separator_later_on_the_line_does_not_count_as_already_given() {
        let owned = [
            std::ffi::OsString::from("use"),
            std::ffi::OsString::from("--zzz"),
            std::ffi::OsString::from("--"),
            std::ffi::OsString::from("tail"),
        ];
        let argv: Vec<&std::ffi::OsStr> = owned.iter().map(|word| word.as_os_str()).collect();
        // A standalone literal, the way an embedding constructing its own error would have it,
        // rather than the slice of argv the parser hands back.
        let message = render(
            &SPEC,
            &argv,
            &Error::UnknownFlag { token: b"--zzz" },
            Style::PLAIN,
        );
        assert!(
            message.contains("to pass '--zzz' as a value, use '-- --zzz'"),
            "{message}"
        );

        // And the other direction through the same path: a separator that really does stand
        // ahead of the refused word still counts, so matching the word is what makes both
        // answers right rather than one of them merely defaulting.
        let owned = [
            std::ffi::OsString::from("use"),
            std::ffi::OsString::from("--"),
            std::ffi::OsString::from("--zzz"),
        ];
        let argv: Vec<&std::ffi::OsStr> = owned.iter().map(|word| word.as_os_str()).collect();
        let message = render(
            &SPEC,
            &argv,
            &Error::UnknownFlag { token: b"--zzz" },
            Style::PLAIN,
        );
        assert!(!message.contains("to pass"), "{message}");
    }

    /// An embedding's error carries the word, not which occurrence of it. Answering for the
    /// first is what makes the question "does a separator stand ahead of every one of them",
    /// which has a defensible answer where "which one did they mean" does not.
    #[test]
    fn a_repeated_word_is_answered_for_every_occurrence_of_it() {
        let render_literal = |words: &[&str]| {
            let owned: Vec<std::ffi::OsString> =
                words.iter().map(std::ffi::OsString::from).collect();
            let argv: Vec<&std::ffi::OsStr> = owned.iter().map(|word| word.as_os_str()).collect();
            render(
                &SPEC,
                &argv,
                &Error::UnknownFlag { token: b"--zzz" },
                Style::PLAIN,
            )
        };

        // Split across the separator: the first occurrence has none ahead of it, so the tip
        // stands. The user may have meant the later one and already know, which costs them a
        // line they can ignore -- against withholding the answer from someone who does not.
        let split = render_literal(&["use", "--zzz", "--", "--zzz"]);
        assert!(split.contains("to pass '--zzz' as a value"), "{split}");

        // All of them past the separator: now it really is ahead of every candidate, whichever
        // one the error meant, and the tip would be telling the user what they already did.
        let after = render_literal(&["use", "--", "--zzz", "--zzz"]);
        assert!(!after.contains("to pass"), "{after}");
    }

    /// Nowhere to put a value means no value to suggest. `user` takes no positionals at all.
    #[test]
    fn the_separator_is_not_suggested_where_nothing_takes_a_value() {
        let message = rendered(&["user"], Error::UnknownFlag { token: b"--zzz" });
        assert!(!message.contains("to pass"), "{message}");
    }

    #[test]
    fn the_scores_are_the_ones_clap_would_compute() {
        // Spot values for the algorithm itself, so a rewrite cannot quietly change which words
        // count as similar. `fore` against `force` is the ordinary case: five of six characters,
        // in order.
        assert!(
            (jaro("fore", "force") - 0.933).abs() < 0.001,
            "{}",
            jaro("fore", "force")
        );
        assert_eq!(jaro("same", "same"), 1.0);
        assert_eq!(jaro("", ""), 1.0);
        assert_eq!(jaro("abc", ""), 0.0);
        // No characters in common at all.
        assert_eq!(jaro("abc", "xyz"), 0.0);

        // And *no* prefix bonus, which is the whole difference from Jaro-Winkler: Jaro counts
        // matching characters and their order, not where the agreement falls, so dropping a
        // word's last letter and dropping its first score alike. Under Winkler the first would
        // win, and a different set of words would clear the bar than clap's.
        assert_eq!(jaro("forc", "force"), jaro("orce", "force"));
    }

    #[test]
    fn a_subcommand_and_a_value_get_the_same_treatment() {
        // Two are close, so the plural — and in clap's order, which is ascending by score, so
        // the *closest* comes last. That reads oddly and is what clap does.
        let message = rendered(&[], Error::UnexpectedArg { token: b"usse" });
        assert!(
            message.contains("tip: some similar subcommands exist: 'user', 'use'"),
            "{message}"
        );

        // The singular is covered by the flag and value cases in this module, which name one
        // each — here both commands begin `us`, so anything close to one is close to both.

        let owned = [
            std::ffi::OsString::from("use"),
            std::ffi::OsString::from("nod"),
        ];
        let argv: Vec<&std::ffi::OsStr> = owned.iter().map(|o| o.as_os_str()).collect();
        let message = render(
            &SPEC,
            &argv,
            &Error::InvalidChoice {
                name: "TOOL",
                choices: &["node", "python"],
            },
            Style::PLAIN,
        );
        assert!(
            message.contains("invalid value 'nod' for '<TOOL>'"),
            "{message}"
        );
        assert!(
            message.contains("tip: a similar value exists: 'node'"),
            "{message}"
        );
    }

    #[test]
    fn a_global_flag_is_suggested_inside_a_subcommand() {
        // What the parser would have accepted there is what should be suggested there — the same
        // rule the completions follow, for the same reason.
        let message = rendered(&["use"], Error::UnknownFlag { token: b"--quie" });
        assert!(
            message.contains("tip: a similar argument exists: '--quiet'"),
            "{message}"
        );
    }
    #[test]
    fn a_dash_prefixed_word_is_a_flag_even_where_subcommands_exist() {
        // The root has subcommands, so a bare word there is a subcommand — but `--forc` is not a
        // subcommand anybody could have meant, and saying "unrecognized subcommand" answers a
        // question the user did not ask. It happens on exactly the commands where the mistake is
        // easiest to make.
        let message = rendered(&[], Error::UnexpectedArg { token: b"--quie" });
        assert!(
            message.starts_with("error: unexpected argument '--quie' found"),
            "{message}"
        );
        assert!(
            message.contains("tip: a similar argument exists: '--quiet'"),
            "{message}"
        );
        assert!(!message.contains("subcommand"), "{message}");

        // A bare word is still a subcommand, which is the other half of the same rule.
        let message = rendered(&[], Error::UnexpectedArg { token: b"usse" });
        assert!(
            message.starts_with("error: unrecognized subcommand 'usse'"),
            "{message}"
        );

        // A lone `-` is a word, not a flag: it is what several tools spell "standard input".
        let message = rendered(&[], Error::UnexpectedArg { token: b"-" });
        assert!(
            message.starts_with("error: unrecognized subcommand '-'"),
            "{message}"
        );
    }
    #[test]
    fn a_siblings_global_is_not_offered_here() {
        // `user` declares a global; it is a sibling of `use`, never an ancestor, so the parser
        // would refuse `--local` inside `use`. A tip naming a flag that does not work is worse
        // than no tip — and the first version of this walked the whole tree, collecting globals
        // from every branch it passed through.
        let message = rendered(&["use"], Error::UnknownFlag { token: b"--locl" });
        assert!(!message.contains("a similar argument exists"), "{message}");

        // Inside `user` itself it is offered, which is what makes the absence above a rule
        // rather than an oversight.
        let message = rendered(&["user"], Error::UnknownFlag { token: b"--locl" });
        assert!(
            message.contains("tip: a similar argument exists: '--local'"),
            "{message}"
        );

        // And the root's global still reaches a subcommand, which is the case globals exist for.
        let message = rendered(&["use"], Error::UnknownFlag { token: b"--quie" });
        assert!(
            message.contains("tip: a similar argument exists: '--quiet'"),
            "{message}"
        );

        // An ancestor's *non*-global flag does not: the root declares `--setup` for itself, and
        // the parser would refuse it inside `use` exactly as it refuses a sibling's.
        let message = rendered(&["use"], Error::UnknownFlag { token: b"--setu" });
        assert!(!message.contains("a similar argument exists"), "{message}");
        let message = rendered(&[], Error::UnknownFlag { token: b"--setu" });
        assert!(
            message.contains("tip: a similar argument exists: '--setup'"),
            "{message}"
        );
    }

    /// Every message shape, plain and coloured, pinned byte for byte.
    ///
    /// The other tests here look for the part of a message they are about; this one holds the
    /// whole of each, so a change to how the text is assembled cannot move a space, a newline, or
    /// an escape code without failing. Escapes are shown as `^[` to keep the expectation readable.
    fn every_shape(style: Style) -> String {
        static EQUALS: Flag = Flag {
            key: 9,
            name: "mode",
            longs: &["mode"],
            require_equals: true,
            ..Flag::VALUE
        };
        let cases: Vec<(&[&str], Error<'static, 'static>)> = vec![
            (&["use"], Error::UnknownFlag { token: b"--fore" }),
            (&["use"], Error::UnknownFlag { token: b"--zzz" }),
            (&["exec"], Error::UnknownFlag { token: b"--fore" }),
            (&["user"], Error::UnknownFlag { token: b"--zzz" }),
            (&[], Error::UnexpectedArg { token: b"usse" }),
            (&[], Error::UnexpectedArg { token: b"usr" }),
            (&[], Error::UnexpectedArg { token: b"zzz" }),
            (&[], Error::UnexpectedArg { token: b"--quie" }),
            (&["use"], Error::UnexpectedArg { token: b"extra" }),
            (&["use"], Error::SubcommandConflict { subcommand: &USE }),
            (&["use"], Error::MissingRequired { name: "TOOL" }),
            (&["use"], Error::DuplicateFlag { name: "jobs" }),
            (&["use"], Error::MissingFlagValue { flag: &JOBS }),
            (&["use"], Error::MissingFlagValue { flag: &FORCE }),
            (&["use"], Error::MissingFlagValue { flag: &EQUALS }),
            (
                &["use", "tool", "bash", "zssh"],
                Error::InvalidChoice {
                    name: "SHELLS",
                    choices: &["bash", "zsh"],
                },
            ),
            (
                &["use"],
                Error::InvalidChoice {
                    name: "SHELLS",
                    choices: &["bash", "zsh"],
                },
            ),
            (
                &["use", "--jobs", "wat"],
                Error::InvalidValue(Box::new(crate::InvalidValue {
                    name: "jobs",
                    value: "wat".to_string(),
                    reason: "invalid digit found in string".to_string(),
                })),
            ),
            (
                &["use"],
                Error::MissingGroup {
                    group: "input",
                    members: &["--jobs", "-f", "--no-force", "--elsewhere"],
                },
            ),
            (
                &["use"],
                Error::ConflictingFlags {
                    name: "force",
                    other: "jobs",
                },
            ),
            (
                &["use"],
                Error::VarTooFew {
                    name: "SHELLS",
                    min: 2,
                    got: 1,
                },
            ),
            (
                &["use"],
                Error::VarTooMany {
                    name: "SHELLS",
                    max: 1,
                    got: 3,
                },
            ),
            (&["exec"], Error::ArgRequiresDoubleDash { arg: &CMDLINE }),
            (&["use"], Error::TooDeep),
        ];
        let mut all = String::new();
        for (words, error) in cases {
            let owned: Vec<std::ffi::OsString> =
                words.iter().map(std::ffi::OsString::from).collect();
            let argv: Vec<&std::ffi::OsStr> = owned.iter().map(|o| o.as_os_str()).collect();
            all.push_str(&render(&SPEC, &argv, &error, style));
            all.push_str("----\n");
        }
        // The same shapes through a view, where the usage line and the names are the promoted
        // command's rather than the host's.
        static VIEW: ViewMeta = ViewMeta {
            id: "runner",
            name: "runner",
            bin: "runner",
            root: "use",
            all_globals: true,
            globals: &[],
        };
        static VIEW_SPEC: Spec = Spec {
            views: &[VIEW],
            ..SPEC
        };
        let view_cases: Vec<(&[&str], Error<'static, 'static>)> = vec![
            (
                &["runner", "--fore"],
                Error::UnknownFlag { token: b"--fore" },
            ),
            (
                &["runner", "--quie"],
                Error::UnknownFlag { token: b"--quie" },
            ),
            (&["runner"], Error::MissingRequired { name: "TOOL" }),
            (
                &["runner", "tool", "zssh"],
                Error::InvalidChoice {
                    name: "SHELLS",
                    choices: &["bash", "zsh"],
                },
            ),
        ];
        for (words, error) in view_cases {
            let argv: Vec<&std::ffi::OsStr> = words.iter().map(std::ffi::OsStr::new).collect();
            all.push_str(&render_view(&VIEW_SPEC, &argv, &error, style, &VIEW));
            all.push_str("----\n");
        }
        all.replace('\u{1b}', "^[")
    }

    #[test]
    fn a_warning_is_coloured_like_a_failure() {
        use crate::warn::Warning;
        let warnings = [
            Warning::flag("--old", None, None, Some("2.0.0")),
            Warning::flag("--old", Some("use --new"), None, None),
            Warning::env("OLD_TOKEN", Some("APP_TOKEN")),
        ];
        assert_eq!(
            render_warnings(&warnings, Style::PLAIN),
            "warning: --old is deprecated, removed at 2.0.0\n\
             warning: --old is deprecated: use --new\n\
             warning: OLD_TOKEN is deprecated: use APP_TOKEN\n",
        );
        assert_eq!(
            render_warnings(&warnings, Style::COLOURED).replace('\u{1b}', "^["),
            "^[[1m^[[33mwarning:^[[0m ^[[33m--old^[[0m is deprecated, removed at ^[[1m2.0.0^[[0m\n\
             ^[[1m^[[33mwarning:^[[0m ^[[33m--old^[[0m is deprecated: use --new\n\
             ^[[1m^[[33mwarning:^[[0m ^[[33mOLD_TOKEN^[[0m is deprecated: use ^[[32mAPP_TOKEN^[[0m\n",
        );
    }

    #[test]
    fn every_message_is_unchanged_plain() {
        let got = every_shape(Style::PLAIN);
        assert_eq!(got, EVERY_SHAPE_PLAIN, "\n{got}");
    }

    #[test]
    fn every_message_is_unchanged_coloured() {
        let got = every_shape(Style::COLOURED);
        assert_eq!(got, EVERY_SHAPE_COLOURED, "\n{got}");
    }

    const EVERY_SHAPE_PLAIN: &str = r#"error: unexpected argument '--fore' found

  tip: a similar argument exists: '--force'

Usage: ex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…

For more information, try '--help'.
----
error: unexpected argument '--zzz' found

  tip: to pass '--zzz' as a value, use '-- --zzz'

Usage: ex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…

For more information, try '--help'.
----
error: unexpected argument '--fore' found

  tip: a similar argument exists: '--force'
  tip: to pass '--fore' as a value, use '-- --fore'

Usage: ex exec [-f --force] <-- COMMAND>…

For more information, try '--help'.
----
error: unexpected argument '--zzz' found

Usage: ex user [--local]

For more information, try '--help'.
----
error: unrecognized subcommand 'usse'

  tip: some similar subcommands exist: 'user', 'use'

Usage: ex [--quiet] [--setup] [SUBCOMMAND]

For more information, try '--help'.
----
error: unrecognized subcommand 'usr'

  tip: some similar subcommands exist: 'use', 'user'

Usage: ex [--quiet] [--setup] [SUBCOMMAND]

For more information, try '--help'.
----
error: unrecognized subcommand 'zzz'

Usage: ex [--quiet] [--setup] [SUBCOMMAND]

For more information, try '--help'.
----
error: unexpected argument '--quie' found

  tip: a similar argument exists: '--quiet'

Usage: ex [--quiet] [--setup] [SUBCOMMAND]

For more information, try '--help'.
----
error: unexpected argument 'extra' found

Usage: ex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…

For more information, try '--help'.
----
error: the subcommand 'use' cannot be used with arguments on its parent command

Usage: ex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…

For more information, try '--help'.
----
error: the following required arguments were not provided:
  <TOOL>

Usage: ex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…

For more information, try '--help'.
----
error: the argument '--jobs' cannot be used multiple times

Usage: ex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…

For more information, try '--help'.
----
error: a value is required for '--jobs <JOBS>' but none was supplied

For more information, try '--help'.
----
error: a value is required for '--force <force>' but none was supplied

For more information, try '--help'.
----
error: equal sign is needed when assigning values to '--mode=<mode>'

For more information, try '--help'.
----
error: invalid value 'zssh' for '[SHELLS]…'
  [possible values: bash, zsh]

  tip: a similar value exists: 'zsh'

For more information, try '--help'.
----
error: invalid value for '[SHELLS]…'
  [possible values: bash, zsh]

For more information, try '--help'.
----
error: invalid value 'wat' for '--jobs': invalid digit found in string

For more information, try '--help'.
----
error: one of the following required arguments was not provided (input):
  --jobs <JOBS>
  --force
  --force
  --elsewhere

Usage: ex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…

For more information, try '--help'.
----
error: the argument '--force' cannot be used with '--jobs'

Usage: ex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…

For more information, try '--help'.
----
error: 2 values required for '[SHELLS]…' but 1 were provided

For more information, try '--help'.
----
error: 1 values allowed for '[SHELLS]…' but 3 were provided

For more information, try '--help'.
----
error: '<-- COMMAND>…' can only be given after '--'

Usage: ex exec [-f --force] <-- COMMAND>…

For more information, try '--help'.
----
error: this command line nests deeper than the parser goes

For more information, try '--help'.
----
error: unexpected argument '--fore' found

  tip: a similar argument exists: '--force'

Usage: runner [FLAGS] <TOOL> [SHELLS]…

For more information, try '--help'.
----
error: unexpected argument '--quie' found

  tip: a similar argument exists: '--quiet'

Usage: runner [FLAGS] <TOOL> [SHELLS]…

For more information, try '--help'.
----
error: the following required arguments were not provided:
  <TOOL>

Usage: runner [FLAGS] <TOOL> [SHELLS]…

For more information, try '--help'.
----
error: invalid value 'zssh' for '[SHELLS]…'
  [possible values: bash, zsh]

  tip: a similar value exists: 'zsh'

For more information, try '--help'.
----
"#;

    const EVERY_SHAPE_COLOURED: &str = r#"^[[1m^[[31merror:^[[0m unexpected argument '^[[33m--fore^[[0m' found

  ^[[32mtip:^[[0m a similar argument exists: '^[[32m--force^[[0m'

^[[1m^[[4mUsage:^[[0m ^[[1mex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m unexpected argument '^[[33m--zzz^[[0m' found

  ^[[32mtip:^[[0m to pass '^[[33m--zzz^[[0m' as a value, use '^[[32m-- --zzz^[[0m'

^[[1m^[[4mUsage:^[[0m ^[[1mex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m unexpected argument '^[[33m--fore^[[0m' found

  ^[[32mtip:^[[0m a similar argument exists: '^[[32m--force^[[0m'
  ^[[32mtip:^[[0m to pass '^[[33m--fore^[[0m' as a value, use '^[[32m-- --fore^[[0m'

^[[1m^[[4mUsage:^[[0m ^[[1mex exec [-f --force] <-- COMMAND>…^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m unexpected argument '^[[33m--zzz^[[0m' found

^[[1m^[[4mUsage:^[[0m ^[[1mex user [--local]^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m unrecognized subcommand '^[[33musse^[[0m'

  ^[[32mtip:^[[0m some similar subcommands exist: '^[[32muser^[[0m', '^[[32muse^[[0m'

^[[1m^[[4mUsage:^[[0m ^[[1mex [--quiet] [--setup] [SUBCOMMAND]^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m unrecognized subcommand '^[[33musr^[[0m'

  ^[[32mtip:^[[0m some similar subcommands exist: '^[[32muse^[[0m', '^[[32muser^[[0m'

^[[1m^[[4mUsage:^[[0m ^[[1mex [--quiet] [--setup] [SUBCOMMAND]^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m unrecognized subcommand '^[[33mzzz^[[0m'

^[[1m^[[4mUsage:^[[0m ^[[1mex [--quiet] [--setup] [SUBCOMMAND]^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m unexpected argument '^[[33m--quie^[[0m' found

  ^[[32mtip:^[[0m a similar argument exists: '^[[32m--quiet^[[0m'

^[[1m^[[4mUsage:^[[0m ^[[1mex [--quiet] [--setup] [SUBCOMMAND]^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m unexpected argument '^[[33mextra^[[0m' found

^[[1m^[[4mUsage:^[[0m ^[[1mex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m the subcommand '^[[33muse^[[0m' cannot be used with arguments on its parent command

^[[1m^[[4mUsage:^[[0m ^[[1mex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m the following required arguments were not provided:
  ^[[32m<TOOL>^[[0m

^[[1m^[[4mUsage:^[[0m ^[[1mex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m the argument '^[[33m--jobs^[[0m' cannot be used multiple times

^[[1m^[[4mUsage:^[[0m ^[[1mex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m a value is required for '^[[33m--jobs <JOBS>^[[0m' but none was supplied

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m a value is required for '^[[33m--force <force>^[[0m' but none was supplied

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m equal sign is needed when assigning values to '^[[33m--mode=<mode>^[[0m'

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m invalid value '^[[33mzssh^[[0m' for '^[[1m[SHELLS]…^[[0m'
  [possible values: ^[[32mbash^[[0m, ^[[32mzsh^[[0m]

  ^[[32mtip:^[[0m a similar value exists: '^[[32mzsh^[[0m'

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m invalid value for '^[[1m[SHELLS]…^[[0m'
  [possible values: ^[[32mbash^[[0m, ^[[32mzsh^[[0m]

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m invalid value '^[[33mwat^[[0m' for '^[[1m--jobs^[[0m': invalid digit found in string

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m one of the following required arguments was not provided (input):
  ^[[32m--jobs <JOBS>^[[0m
  ^[[32m--force^[[0m
  ^[[32m--force^[[0m
  ^[[32m--elsewhere^[[0m

^[[1m^[[4mUsage:^[[0m ^[[1mex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m the argument '^[[33m--force^[[0m' cannot be used with '^[[33m--jobs^[[0m'

^[[1m^[[4mUsage:^[[0m ^[[1mex use [-f --force] [--jobs <JOBS>] <TOOL> [SHELLS]…^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m 2 values required for '^[[1m[SHELLS]…^[[0m' but 1 were provided

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m 1 values allowed for '^[[1m[SHELLS]…^[[0m' but 3 were provided

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m '^[[1m<-- COMMAND>…^[[0m' can only be given after '^[[1m--^[[0m'

^[[1m^[[4mUsage:^[[0m ^[[1mex exec [-f --force] <-- COMMAND>…^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m this command line nests deeper than the parser goes

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m unexpected argument '^[[33m--fore^[[0m' found

  ^[[32mtip:^[[0m a similar argument exists: '^[[32m--force^[[0m'

^[[1m^[[4mUsage:^[[0m ^[[1mrunner [FLAGS] <TOOL> [SHELLS]…^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m unexpected argument '^[[33m--quie^[[0m' found

  ^[[32mtip:^[[0m a similar argument exists: '^[[32m--quiet^[[0m'

^[[1m^[[4mUsage:^[[0m ^[[1mrunner [FLAGS] <TOOL> [SHELLS]…^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m the following required arguments were not provided:
  ^[[32m<TOOL>^[[0m

^[[1m^[[4mUsage:^[[0m ^[[1mrunner [FLAGS] <TOOL> [SHELLS]…^[[0m

For more information, try '^[[1m--help^[[0m'.
----
^[[1m^[[31merror:^[[0m invalid value '^[[33mzssh^[[0m' for '^[[1m[SHELLS]…^[[0m'
  [possible values: ^[[32mbash^[[0m, ^[[32mzsh^[[0m]

  ^[[32mtip:^[[0m a similar value exists: '^[[32mzsh^[[0m'

For more information, try '^[[1m--help^[[0m'.
----
"#;
}