usage-lib 6.0.0

Library for working with 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
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
use heck::ToSnakeCase;
use indexmap::IndexMap;
use itertools::Itertools;
use log::trace;
use miette::bail;
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
use std::fmt::{Debug, Display, Formatter};
use std::sync::Arc;
use strum::EnumTryAs;

#[cfg(feature = "docs")]
use crate::docs;
use crate::error::UsageErr;
use crate::spec::arg::SpecDoubleDashChoices;
use crate::spec::unknown_flags::UnknownFlags;
use crate::warn::Warning;
use crate::{Spec, SpecArg, SpecChoices, SpecCommand, SpecFlag};

/// Merge a subcommand's flags into the currently available flags when descending
/// into that subcommand.
///
/// On descent we drop the parent's non-global flags (they are scoped to the parent)
/// but keep its global flags so they remain recognized further down. A subcommand may
/// re-declare a flag that the parent exposed as global (e.g. `-C/--cd`) but mark its own
/// copy as non-global. In that case we must NOT let the non-global re-declaration shadow
/// the inherited global flag, otherwise the next descent's `retain(global)` would drop it
/// entirely and later parsing would treat the already-consumed global token as an
/// unexpected positional/flag value.
///
/// Descending into a *mounted* subcommand (`crossing_mount`) is different: the mounted
/// command describes another program, which does not accept the mounting CLI's globals.
/// Those globals stay recognized (they may appear before the mounted command, and Phase 2
/// re-parses them), but the mounted command's own flags take precedence over them, so its
/// choices/completions are not replaced by a global's. Which flags a completion may offer
/// there is a separate question, answered by [`ParseOutput::completion_flags`].
fn merge_subcommand_flags(
    available: &mut BTreeMap<String, Arc<SpecFlag>>,
    new_flags: BTreeMap<String, Arc<SpecFlag>>,
    crossing_mount: bool,
) {
    // Keep only inherited global flags from the parent.
    available.retain(|_, f| f.global);

    if crossing_mount {
        // A mounted command owns its flags outright, including names an inherited global also
        // uses: a word after the mounted command belongs to the mounted program. Words before
        // it keep resolving to the global they were read as, via `Token::binding`. Aliases the
        // mounted command does not declare (e.g. a global's short) stay inherited.
        for (key, flag) in new_flags {
            available.insert(key, flag);
        }
        return;
    }

    // Cache the merged (global ∪ orphan-alias) flag per re-declared child so every alias key of
    // that flag ends up sharing one `Arc`. Keyed by the child `Arc`'s identity.
    let mut merged_cache: HashMap<usize, Arc<SpecFlag>> = HashMap::new();
    // Maps each merged flag produced below back to the inherited global it was merged from, so
    // the collision check can compare *origins*: a flag this loop already merged is not a
    // different global, even though it is a different `Arc`.
    let mut merged_origin: HashMap<usize, usize> = HashMap::new();
    // The inherited global a flag stands for: itself, or — for a merged flag — its source global.
    fn origin_of(merged_origin: &HashMap<usize, usize>, flag: &Arc<SpecFlag>) -> usize {
        let ptr = Arc::as_ptr(flag) as usize;
        *merged_origin.get(&ptr).unwrap_or(&ptr)
    }

    // Iterate the *flattened* child map directly (one entry per alias key). This preserves the
    // map's existing intra-subcommand collision resolution: when two flags in the same command
    // share an alias (e.g. `-x --alpha` then `-x --beta`), the BTreeMap already collapsed `-x`
    // to its last-declared owner, and we must not change which flag owns it.
    for (key, flag) in new_flags {
        if flag.global {
            // A child that re-declares (or adds) a global flag stays recognized everywhere.
            available.insert(key, flag);
            continue;
        }

        // A non-global re-declaration that shares a LONG name with an inherited global flag is
        // the SAME logical flag (e.g. mise's `-r --raw` re-declaring the long-only `--raw`
        // global). Keep the global flag (global precedence, so it survives the next descent's
        // `retain`), but union in any short/long aliases that exist only on the re-declaration,
        // otherwise those orphan aliases would be silently dropped. Matching on a shared long is
        // deliberate: a re-declaration sharing only a short letter with an unrelated global
        // (`-q --quiet` vs `-q --quoting`) is a genuine collision, not an alias addition, and is
        // handled by the `contains_key` skip below instead.
        let inherited_global = flag.long.iter().find_map(|l| {
            available
                .get(&format!("--{l}"))
                .filter(|f| f.global)
                .cloned()
        });
        if let Some(global_flag) = inherited_global {
            // Never clobber a *different* inherited global's alias. If this re-declaration's
            // orphan alias (e.g. `-r`) is already owned by some other global (e.g. an unrelated
            // `-r --restrict`), that is a genuine collision: keep the existing global, as global
            // precedence dictates, instead of stealing the alias for the merged flag.
            //
            // Compare origins, not `Arc`s: when the global has several aliases of its own, an
            // earlier key of this same child already replaced some of them with the merged flag,
            // which the lookups above may now resolve to. That is the same logical flag, so it
            // must not read as a collision and leave this key on the pre-merge global.
            let global_origin = origin_of(&merged_origin, &global_flag);
            if available.get(&key).is_some_and(|existing| {
                existing.global && origin_of(&merged_origin, existing) != global_origin
            }) {
                continue;
            }
            let merged = match merged_cache.get(&(Arc::as_ptr(&flag) as usize)) {
                Some(merged) => merged.clone(),
                None => {
                    let mut merged = (*global_flag).clone();
                    // `exclusive` is deliberately *not* reconciled here, in either direction.
                    // One object now answers to two alias sets that may disagree: the child
                    // owns the spellings it declared, the ancestor keeps the ones only it
                    // declared. A single bool cannot hold both, so the merged flag carries the
                    // ancestor's and validation resolves the occurrence by the spelling that
                    // was typed — the ledger it already consults to decide whether selecting
                    // the child is company.
                    for s in &flag.short {
                        if !merged.short.contains(s) {
                            merged.short.push(*s);
                        }
                    }
                    for l in &flag.long {
                        if !merged.long.contains(l) {
                            merged.long.push(l.clone());
                        }
                    }
                    // A child may deliberately promote one of the ancestor's hidden aliases.
                    // Hidden lists are subsets of the accepted spellings, so a spelling present
                    // on the child but absent from its hidden subset is visible at this level.
                    merged.hidden_short_aliases.retain(|alias| {
                        !flag.short.contains(alias) || flag.hidden_short_aliases.contains(alias)
                    });
                    merged.hidden_aliases.retain(|alias| {
                        !flag.long.contains(alias) || flag.hidden_aliases.contains(alias)
                    });
                    for s in &flag.hidden_short_aliases {
                        if !merged.hidden_short_aliases.contains(s) {
                            merged.hidden_short_aliases.push(*s);
                        }
                    }
                    for l in &flag.hidden_aliases {
                        if !merged.hidden_aliases.contains(l) {
                            merged.hidden_aliases.push(l.clone());
                        }
                    }
                    let merged = Arc::new(merged);
                    merged_cache.insert(Arc::as_ptr(&flag) as usize, Arc::clone(&merged));
                    merged_origin.insert(Arc::as_ptr(&merged) as usize, global_origin);
                    // Rebind the global's *other* aliases onto the merged flag. The loop only
                    // visits keys the child declared, so an alias the child left out (the `-y` of
                    // a `-y --yes` global re-declared as just `--yes`) would otherwise keep
                    // pointing at the pre-merge flag and miss the aliases just unioned in. One
                    // logical flag must be one object under every key it answers to.
                    for existing in available.values_mut() {
                        if origin_of(&merged_origin, existing) == global_origin {
                            *existing = Arc::clone(&merged);
                        }
                    }
                    merged
                }
            };
            available.insert(key, merged);
            continue;
        }

        // Purely-local flag (shares nothing with an inherited global), or one that collides only
        // on a short with an unrelated global. Insert this alias but never shadow an inherited
        // global flag. Such non-global flags are dropped by the next descent's `retain`.
        if available.contains_key(&key) {
            continue;
        }
        available.insert(key, flag);
    }
}

/// Build the lookup keys a flag is registered under in `available_flags`:
/// `--<long>` for each long name, `-<short>` for each short char, plus the `negate` token.
fn flag_keys(flag: &SpecFlag) -> Vec<String> {
    let mut keys: Vec<String> = flag
        .long
        .iter()
        .map(|l| format!("--{l}"))
        .chain(flag.short.iter().map(|s| format!("-{s}")))
        .collect();
    if let Some(negate) = &flag.negate {
        keys.push(negate.clone());
    }
    keys
}

/// The flags a command declares, keyed by each of their aliases.
fn gather_flags(cmd: &SpecCommand) -> BTreeMap<String, Arc<SpecFlag>> {
    cmd.flags
        .iter()
        .flat_map(|f| {
            let f = Arc::new(f.clone()); // One clone per flag, then cheap Arc refs
            flag_keys(&f)
                .into_iter()
                .map(|key| (key, Arc::clone(&f)))
                .collect::<Vec<_>>()
        })
        .collect()
}

fn unique_flags<'a>(
    flags: impl IntoIterator<Item = &'a Arc<SpecFlag>>,
) -> impl Iterator<Item = &'a Arc<SpecFlag>> {
    let mut seen = HashSet::new();
    flags
        .into_iter()
        .filter(move |flag| seen.insert(Arc::as_ptr(flag) as usize))
}

/// Every flag a command accepts, resolved the way parsing an invocation of it
/// resolves them.
///
/// `chain` runs from the root command (`spec.cmd`) down to the command in
/// question; an empty chain yields no flags.
///
/// This is not "the command's flags plus its ancestors' globals". A subcommand
/// that re-declares a global's long name is describing the *same* flag rather
/// than a new one, so the global's help, argument and effect survive and only
/// the re-declaration's extra aliases are added — see
/// [`merge_subcommand_flags`]. Anything that reports a command's flags without
/// going through this will disagree with what the parser actually accepts.
pub fn available_flags(chain: &[&SpecCommand]) -> Vec<Arc<SpecFlag>> {
    let Some((root, rest)) = chain.split_first() else {
        return vec![];
    };
    let mut available = gather_flags(root);
    for cmd in rest {
        merge_subcommand_flags(&mut available, gather_flags(cmd), false);
    }

    // Deduplicating by `Arc` identity is not enough. When a child re-declares a
    // global that has both a short and a long, the merged flag is written under
    // the long key while the short key keeps pointing at the pre-merge `Arc` —
    // two objects for one logical flag. That is harmless for parsing, which
    // looks flags up by key, but a caller listing flags would see it twice.
    //
    // Names break the tie because a long key always sorts before a short one
    // (`--x` < `-y` at the second byte), so the merged declaration is the one
    // reached first. Two genuinely distinct flags sharing a name is a spec bug
    // that `usage lint` reports as a duplicate flag.
    let mut seen_names = HashSet::new();
    unique_flags(available.values())
        .filter(|f| seen_names.insert(f.name.clone()))
        .cloned()
        .collect()
}

/// Extract the flag key from a flag word for lookup in available_flags map
/// Handles both long flags (--flag, --flag=value) and short flags (-f)
fn get_flag_key(word: &str) -> &str {
    if word.starts_with("--") {
        // Long flag: strip =value if present
        word.split_once('=').map(|(k, _)| k).unwrap_or(word)
    } else if let Some((end, _)) = word.char_indices().nth(2) {
        // Short flag: the dash and one letter, which is one character and not
        // necessarily one byte.
        &word[0..end]
    } else {
        word
    }
}

/// Where a value came from, when it did not come from the command line.
///
/// About the *value*, not the flag. `--color` typed bare with `default_missing` has a
/// token for the flag and none for the value, and that distinction is the whole question
/// a spec author is asking when they ask why `--color` came out `always`. Values that were
/// typed are attributed to the token that carried them instead — see [`TokenRole::Value`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ValueOrigin {
    /// A flag that takes a value was given without one, so the declaration supplied it:
    /// `default_missing`, or the empty tri-state a bare `value_optional` flag records.
    /// One variant for both, because from argv's side the same thing happened — the flag
    /// was typed and the value was not.
    DefaultMissing,
    /// An environment variable, named.
    ///
    /// Named because a flag may list several — `env`, `env_fallback` and `deprecated_env`,
    /// folded together by [`SpecFlag::env_names`] — and "it came from the environment" does
    /// not say which declaration fired or which one to delete.
    Env(String),
    /// A declared `default`, on the flag or on the flag's argument.
    ///
    /// Not two variants: the precedence between them is a spec-authoring oddity rather than
    /// a fact about the value, and `usage lint` is the place to complain about declaring
    /// both.
    Default,
    /// A `default_if` whose condition matched, with the condition that decided it. The
    /// selector alone is ambiguous — several conditions may name it with different `when`
    /// values.
    DefaultIf {
        selector: String,
        when: Option<String>,
    },
}

/// What one word of the command line became.
///
/// Several because a single token can do more than one thing: `-abc` sets three flags,
/// `-j8` is a flag and its value.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum TokenRole {
    /// argv[0]. Also a `Command` when a multicall symlink makes the basename a word.
    Program,
    /// Selected a subcommand.
    Command { name: String },
    /// Named a flag, in this spelling. `negated` for the `negate` form.
    Flag {
        flag: Arc<SpecFlag>,
        spelling: String,
        negated: bool,
    },
    /// Supplied a flag's value. Several values when a `delimiter` split the word.
    Value {
        flag: Arc<SpecFlag>,
        values: Vec<String>,
        /// Whether the value rode along on the flag's own token (`--env=prod`, `-j8`)
        /// rather than following it as its own word.
        attached: bool,
    },
    /// Filled a positional argument. Several values when a `delimiter` split the word.
    Arg {
        arg: Arc<SpecArg>,
        values: Vec<String>,
    },
    /// An explicit `--`, consumed as a separator.
    Separator,
    /// A word the parser answers itself rather than binding: `--help`, `-h`, `--version`,
    /// `-V`. The parse stops here and the answer travels as an error carrying the text, so
    /// without a role the word reads as having done nothing while a whole help page arrives
    /// in the error list.
    Builtin { spelling: String },
    /// A declared `value_terminator`, consumed to end a run of values. `ends` names the
    /// declaration whose run it closed — the word is not one of that run's values, which is
    /// the whole reason it was declared.
    ValueTerminator { ends: String },
    /// A declared `restart_token`: the positional cursor and the values it had filled start
    /// over here. Recorded because the words before it are still in the report, and without
    /// this row they look like they filled arguments that then came back empty.
    Restart,
    /// A flag-like word no declaration matched. `bound_as` is the positional that took it
    /// under `unknown_flags="value"`, and `None` when the word was refused.
    UnknownFlag { bound_as: Option<Arc<SpecArg>> },
    /// The word reached a declaration that would not take it, and was dropped. Without this
    /// the token reads as having done nothing, which is the one thing it did not do.
    Refused { reason: String },
    /// Forwarded to an external subcommand.
    External,
    /// The parser stopped before this word — a help request, a refused value.
    Unread,
}

/// One word of the command line, and what it became.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct TokenBinding {
    /// Position in the argv slice the parse was given, argv[0] included.
    pub index: usize,
    pub word: String,
    /// Roles a word the parser made up contributed, folded onto the token it was derived
    /// from: the tail of a short bundle onto the bundle, a multicall applet name onto
    /// argv[0]. `word` is what the caller wrote, not what the parser read.
    pub synthesized: bool,
    pub roles: Vec<TokenRole>,
}

#[non_exhaustive]
pub struct ParseOutput {
    pub cmd: SpecCommand,
    pub cmds: Vec<SpecCommand>,
    pub args: IndexMap<Arc<SpecArg>, ParseValue>,
    pub flags: IndexMap<Arc<SpecFlag>, ParseValue>,
    /// What each word of the command line became, in argv order, one entry per word.
    ///
    /// The token half of provenance; [`ParseOutput::flag_origins`] and
    /// [`ParseOutput::arg_origins`] are the other half. A table keyed by token cannot show
    /// a value that came from nowhere in argv, and a table keyed by declaration cannot show
    /// a token that bound to nothing, so both exist.
    pub tokens: Vec<TokenBinding>,
    /// Where a flag's value came from when it did not come from argv, in the order the
    /// fallbacks fired. Keyed as [`ParseOutput::flags`] is.
    ///
    /// A list rather than one origin: repeated bare occurrences of a `var` flag each take a
    /// `default_missing` value, so one flag can have several.
    pub flag_origins: IndexMap<Arc<SpecFlag>, Vec<ValueOrigin>>,
    /// Where an argument's value came from when it did not come from argv. Keyed as
    /// [`ParseOutput::args`] is.
    pub arg_origins: IndexMap<Arc<SpecArg>, Vec<ValueOrigin>>,
    /// Flags a later occurrence removed, and the flag that removed them.
    ///
    /// The overriding name is the half a caller needs: the fallback phase silently declines
    /// to fill an overridden flag, so "why is `--quiet` unset when its default says
    /// otherwise" has no answer without it.
    pub overridden_flags: BTreeMap<String, String>,
    /// Every flag the parser recognizes at this point, keyed by each of its aliases
    /// (`--long`, `-s`, negations).
    ///
    /// This includes flags that only remain recognized because they may appear *before* a
    /// mounted command — see [`ParseOutput::completion_flags`] for the set a completion
    /// should offer.
    pub available_flags: BTreeMap<String, Arc<SpecFlag>>,
    pub flag_awaiting_value: Vec<Arc<SpecFlag>>,
    pub errors: Vec<UsageErr>,
    /// Deprecated declarations this command line used, for the caller to render when its
    /// logging is up. Empty from [`parse_partial`]: a half-typed line being completed has
    /// not used anything yet.
    pub warnings: Vec<Warning>,
    /// The positional argument the next word would have filled, i.e. where the parser's
    /// cursor stopped. `None` once every argument is satisfied.
    ///
    /// Completions need exactly this: the parser already accounts for `var_max`, for
    /// `restart_token` rewinds, and for the jump an explicit `--` performs onto a
    /// `double_dash="required"` argument, so re-deriving it from `args` would disagree.
    pub next_arg: Option<Arc<SpecArg>>,
    /// Whether an explicit `--` was consumed *as a separator*.
    ///
    /// A `--` that `double_dash="preserve"` keeps as a value does not count: it is a value
    /// of the variadic argument collecting it, not a separator, so it does not unlock a
    /// `double_dash="required"` argument.
    pub double_dash_seen: bool,
    /// Remaining argv captured when an unmatched word was forwarded as an external
    /// subcommand: the command name first, then every token after it.
    ///
    /// Absent when no external command was selected. See [`SpecCommand::external_subcommand`].
    pub external: Option<Vec<String>>,
}

impl ParseOutput {
    /// The flags a completion should offer for the parsed command.
    ///
    /// Usually every recognized flag, i.e. [`ParseOutput::available_flags`]. Once a mounted
    /// command has been reached, though, the commands above it belong to the mounting CLI and
    /// their flags are not accepted there — mise, for example, forwards everything after a task
    /// name to the task itself — so only the flags declared from the mount boundary down are
    /// offered. Those globals stay in `available_flags` because they may legitimately appear
    /// *before* the mounted command.
    pub fn completion_flags(&self) -> BTreeMap<String, Arc<SpecFlag>> {
        let Some(boundary) = self.cmds.iter().position(|cmd| cmd.mounted) else {
            return self.available_flags.clone();
        };
        // A mount can also merge flags from its spec's root into the command it is mounted on
        // (`SpecCommand::flags_from_mount`). Those describe the mounted program too, so the
        // replay starts one level up to inherit its globals.
        let start = match boundary.checked_sub(1) {
            Some(prev) if self.cmds[prev].flags_from_mount => prev,
            _ => boundary,
        };
        // Re-run the descent from there, which starts with no inherited flags. Below the
        // boundary the mounted program's commands are ordinary commands, so the descents use
        // the same merge as the real parse.
        let mut offered = gather_flags(&self.cmds[start]);
        for cmd in &self.cmds[start + 1..] {
            merge_subcommand_flags(&mut offered, gather_flags(cmd), false);
        }
        offered
    }
}

#[derive(Debug, EnumTryAs, Clone)]
pub enum ParseValue {
    Bool(bool),
    String(String),
    MultiBool(Vec<bool>),
    MultiString(Vec<String>),
}

/// The deprecated declarations argv itself named: the commands it descended through, and the
/// flags it bound.
///
/// Called before the environment and defaults have filled anything, because afterwards nothing
/// distinguishes a flag the user typed from one a variable supplied — and the two are reported
/// differently, at the point where each is applied.
///
/// The root is skipped. A `deprecated` root would otherwise warn on every invocation of the CLI,
/// including `--help`, and the compiled parser reports selected commands rather than the one the
/// process already is.
fn collect_deprecations(out: &mut ParseOutput) {
    for cmd in out.cmds.iter().skip(1) {
        if cmd.deprecated.is_none()
            && cmd.deprecated_warn_at.is_none()
            && cmd.deprecated_remove_at.is_none()
        {
            continue;
        }
        out.warnings.push(Warning::command(
            cmd.name.clone(),
            cmd.deprecated.clone(),
            cmd.deprecated_warn_at.clone(),
            cmd.deprecated_remove_at.clone(),
        ));
    }
    for flag in out.flags.keys() {
        if let Some(warning) = flag_deprecation(flag) {
            out.warnings.push(warning);
        }
    }
}

/// A warning for a flag that was used, if its declaration is deprecated at all.
fn flag_deprecation(flag: &SpecFlag) -> Option<Warning> {
    if flag.deprecated.is_none()
        && flag.deprecated_warn_at.is_none()
        && flag.deprecated_remove_at.is_none()
    {
        return None;
    }
    Some(Warning::flag(
        flag_spelling(flag),
        flag.deprecated.clone(),
        flag.deprecated_warn_at.clone(),
        flag.deprecated_remove_at.clone(),
    ))
}

/// A flag named the way the user names it. The spec's name for it has no dashes, and a warning
/// about `old-flag` would be about a word nobody typed.
fn flag_spelling(flag: &SpecFlag) -> String {
    flag.long
        .first()
        .map(|long| format!("--{long}"))
        .or_else(|| flag.short.first().map(|short| format!("-{short}")))
        .unwrap_or_else(|| flag.name.clone())
}

/// The name this flag reads first, which is what to use instead of a deprecated alias.
fn flag_current_env(flag: &SpecFlag) -> Option<String> {
    flag.env
        .clone()
        .or_else(|| flag.env_fallback.first().cloned())
}

fn flag_env_is_deprecated(flag: &SpecFlag, name: &str) -> bool {
    flag.deprecated_env.iter().any(|declared| declared == name)
}

/// The same two questions for a positional, which has aliases but no `deprecated` of its own.
fn arg_current_env(arg: &SpecArg) -> Option<String> {
    arg.env
        .clone()
        .or_else(|| arg.env_fallback.first().cloned())
}

fn arg_env_is_deprecated(arg: &SpecArg, name: &str) -> bool {
    arg.deprecated_env.iter().any(|declared| declared == name)
}

/// The first of `names` that is set, and which one it was.
///
/// `env_names()` yields the current name, then the declared fallbacks, then the deprecated
/// aliases, so the winner's identity is what says whether a value arrived through an alias.
/// Deciding that a second time, from the outside, would be a copy of this precedence rule free to
/// disagree with it.
fn first_set_env<'a>(
    mut names: impl Iterator<Item = &'a str>,
    get_env: &impl Fn(&str) -> Option<String>,
) -> Option<(&'a str, String)> {
    names.find_map(|name| get_env(name).map(|value| (name, value)))
}

/// Builder for parsing command-line arguments with custom options.
///
/// Use this when you need to customize parsing behavior, such as providing
/// a custom environment variable map instead of using the process environment.
///
/// # Example
/// ```
/// use std::collections::HashMap;
/// use usage::Spec;
/// use usage::parse::Parser;
///
/// let spec: Spec = r#"flag "--name <name>" env="NAME""#.parse().unwrap();
/// let env: HashMap<String, String> = [("NAME".into(), "john".into())].into();
///
/// let result = Parser::new(&spec)
///     .with_env(env)
///     .parse(&["cmd".into()])
///     .unwrap();
/// ```
#[non_exhaustive]
pub struct Parser<'a> {
    spec: &'a Spec,
    env: Option<HashMap<String, String>>,
    mount_outputs: Option<HashMap<String, String>>,
}

impl<'a> Parser<'a> {
    /// Create a new parser for the given spec.
    pub fn new(spec: &'a Spec) -> Self {
        Self {
            spec,
            env: None,
            mount_outputs: None,
        }
    }

    /// Use a custom environment variable map instead of the process environment.
    ///
    /// This is useful when parsing for tasks in a monorepo where the env vars
    /// come from a child config file rather than the current process environment.
    pub fn with_env(mut self, env: HashMap<String, String>) -> Self {
        self.env = Some(env);
        self
    }

    /// Inject deterministic outputs for mount commands instead of executing them.
    ///
    /// Keys are the exact `run` strings declared by mount nodes and values are the
    /// usage specs those commands would print. When this is set, every encountered
    /// mount must have an entry. Production parsing remains process-backed unless a
    /// caller explicitly opts into injection.
    pub fn with_mount_outputs(mut self, outputs: HashMap<String, String>) -> Self {
        self.mount_outputs = Some(outputs);
        self
    }

    /// Parse the input arguments.
    ///
    /// Returns the parsed arguments and flags, with defaults and env vars applied.
    pub fn parse(self, input: &[String]) -> Result<ParseOutput, miette::Error> {
        let out = self.parse_collecting(input)?;
        if let Some(err) = out
            .errors
            .iter()
            .find(|e| matches!(e, UsageErr::Help(_) | UsageErr::Version(_)))
        {
            bail!("{err}");
        }
        if !out.errors.is_empty() {
            bail!("{}", out.errors.iter().map(|e| e.to_string()).join("\n"));
        }
        Ok(out)
    }

    /// Everything the parse learned, whether or not it succeeded.
    ///
    /// [`Parser::parse`] wants the first error and nothing else, which is right for a
    /// caller about to act on a command line. A caller that wants to *explain* one wants
    /// the opposite: the bindings that worked and every complaint about the rest, since a
    /// report saying only "missing required <src>" is the report you already had.
    ///
    /// Failures that stop the parse dead — a mount that will not run, a word no
    /// declaration can take — still come back as `Err`. There is no output to describe in
    /// those cases; see [`Parser::explain`] for what to do about it.
    pub fn explain(self, input: &[String]) -> Result<ParseOutput, miette::Error> {
        self.parse_collecting(input)
    }

    /// The binding phase's own answer for a line [`Parser::explain`] refused.
    ///
    /// `Ok` when the binding phase finished and the failure came after it — a flag left
    /// waiting for a value, say. Everything argv supplied is there and only the
    /// environment-and-defaults phase is missing.
    ///
    /// `Err` when the binding phase is where it died, leaving the tokens it had attributed
    /// by then. Those words are most of what a report is for: "no declaration takes `bogus`"
    /// is more useful next to the three tokens that did bind than on its own. The word that
    /// caused the failure carries a role saying so, and everything still queued behind it is
    /// [`TokenRole::Unread`], for the two failures a command line reaches on its own — a
    /// word nothing declares, and a flag a strict spec refuses. A failure in the spec rather
    /// than in the line, such as a mount that will not run, stops the trace where it stopped
    /// and the words past it carry no role.
    pub fn explain_refused(self, input: &[String]) -> Result<ParseOutput, Vec<TokenBinding>> {
        let mut trace = Trace::new(input);
        match parse_partial_traced(
            self.spec,
            input,
            self.env.as_ref(),
            self.mount_outputs.as_ref(),
            MountTiming::WhenAWordIsUnknown,
            &mut trace,
        ) {
            // A parse that got as far as stopping normally already moved its tokens onto the
            // output, which is where a caller should read them from.
            Ok((out, _)) => Ok(out),
            Err(_) => Err(trace.tokens),
        }
    }

    fn parse_collecting(self, input: &[String]) -> Result<ParseOutput, miette::Error> {
        let custom_env = self.env.as_ref();
        let (mut out, overridden_flags) = parse_partial_with_env(
            self.spec,
            input,
            custom_env,
            self.mount_outputs.as_ref(),
            MountTiming::WhenAWordIsUnknown,
        )?;
        trace!("{out:?}");

        // A flag still waiting for a value never got one, so the command line ended
        // mid-flag. `parse_partial` leaves this for completions to look at — a
        // half-typed `--jobs ` is exactly what a completion is asked about — but a
        // full parse has nothing left to wait for, and dropping the flag silently
        // made a forgotten value look like a working command.
        while try_bind_default_missing(
            &mut out.flags,
            &mut out.flag_awaiting_value,
            custom_env,
            &mut out.flag_origins,
        )? {}
        if let Some(flag) = out.flag_awaiting_value.first() {
            let token = flag
                .long
                .first()
                .map(|l| format!("--{l}"))
                .or_else(|| flag.short.first().map(|s| format!("-{s}")))
                .unwrap_or_else(|| flag.name.clone());
            let rendered = input.join(" ");
            let span = rendered
                .rfind(&token)
                .map(|at| (at, token.len()))
                .unwrap_or((0, 0));
            return Err(UsageErr::InvalidFlag {
                token,
                reason: "requires an argument".to_string(),
                span: span.into(),
                input: rendered,
            }
            .into());
        }

        // Before the environment and defaults have their turn, because both mark a field as
        // filled and only argv can be reported as something the user typed. Env is reported
        // where it is applied, below; a default is nobody's request and reports nothing.
        collect_deprecations(&mut out);

        let get_env = |key: &str| -> Option<String> {
            if let Some(env_map) = custom_env {
                env_map.get(key).cloned()
            } else {
                std::env::var(key).ok()
            }
        };

        // Apply env vars and defaults for args
        //
        // Not `skip(out.args.len())`: an explicit `--` can jump the parser's cursor past an arg
        // that stayed empty, leaving a gap that makes the fill count a wrong starting offset.
        for arg in out.cmd.args.iter() {
            if out.args.contains_key(arg) {
                continue;
            }
            if let Some((env_name, env_value)) = first_set_env(arg.env_names(), &get_env) {
                if arg_env_is_deprecated(arg, env_name) {
                    out.warnings
                        .push(Warning::env(env_name, arg_current_env(arg)));
                }
                let values = split_fallback_values(std::slice::from_ref(&env_value), arg.delimiter);
                validate_choice_values(
                    ChoiceTarget::arg(arg),
                    &values,
                    arg.choices.as_ref(),
                    custom_env,
                )?;
                let parsed = if arg.var {
                    validate_arg_fallback_count(arg, values.len(), &mut out.errors);
                    ParseValue::MultiString(values)
                } else {
                    ParseValue::String(values.into_iter().next().unwrap_or_default())
                };
                out.args.insert(Arc::new(arg.clone()), parsed);
                out.arg_origins
                    .entry(Arc::new(arg.clone()))
                    .or_default()
                    .push(ValueOrigin::Env(env_name.to_string()));
                continue;
            }
            if !arg.default.is_empty() {
                // Consider var when deciding the type of default return value
                if arg.var {
                    let values = split_fallback_values(&arg.default, arg.delimiter);
                    validate_arg_fallback_count(arg, values.len(), &mut out.errors);
                    validate_choice_values(
                        ChoiceTarget::arg(arg),
                        &values,
                        arg.choices.as_ref(),
                        custom_env,
                    )?;
                    // For var=true, always return a vec (MultiString)
                    out.args
                        .insert(Arc::new(arg.clone()), ParseValue::MultiString(values));
                    out.arg_origins
                        .entry(Arc::new(arg.clone()))
                        .or_default()
                        .push(ValueOrigin::Default);
                } else {
                    validate_choice_value(
                        ChoiceTarget::arg(arg),
                        &arg.default[0],
                        arg.choices.as_ref(),
                        custom_env,
                    )?;
                    // For var=false, return the first default value as String
                    out.args.insert(
                        Arc::new(arg.clone()),
                        ParseValue::String(arg.default[0].clone()),
                    );
                    out.arg_origins
                        .entry(Arc::new(arg.clone()))
                        .or_default()
                        .push(ValueOrigin::Default);
                }
            }
        }

        // Environment first, for every flag, so a `default_if` can see a sibling
        // that was filled from env. Applying both in one pass would make the
        // answer depend on declaration order: `--bin-names` before `--json`
        // would miss `EX_JSON=1`.
        let flags: Vec<Arc<SpecFlag>> = out.available_flags.values().cloned().collect();
        for flag in &flags {
            if out.flags.contains_key(flag) || overridden_flags.contains(&flag.name) {
                continue;
            }
            if let Some((env_name, env_value)) = first_set_env(flag.env_names(), &get_env) {
                // The flag's own deprecation before the alias's, which is the order the
                // compiled parser reports them in: it walks a command's flags and then its
                // aliases. Using a deprecated flag through a variable is still using it.
                if let Some(warning) = flag_deprecation(flag) {
                    out.warnings.push(warning);
                }
                if flag_env_is_deprecated(flag, env_name) {
                    out.warnings
                        .push(Warning::env(env_name, flag_current_env(flag)));
                }
                if let Some(arg) = flag.arg.as_ref() {
                    let values =
                        split_fallback_values(std::slice::from_ref(&env_value), arg.delimiter);
                    validate_choice_values(
                        ChoiceTarget::option(flag),
                        &values,
                        arg.choices.as_ref(),
                        custom_env,
                    )?;
                    let parsed = if flag.var || arg.var {
                        if flag.var {
                            validate_flag_fallback_count(flag, values.len(), &mut out.errors);
                        }
                        if arg.var {
                            validate_flag_arg_fallback_count(
                                flag,
                                arg,
                                values.len(),
                                &mut out.errors,
                            );
                        }
                        ParseValue::MultiString(values)
                    } else {
                        ParseValue::String(values.into_iter().next().unwrap_or_default())
                    };
                    out.flags.insert(Arc::clone(flag), parsed);
                } else {
                    let is_true = matches!(env_value.as_str(), "1" | "true" | "True" | "TRUE");
                    out.flags
                        .insert(Arc::clone(flag), ParseValue::Bool(is_true));
                }
                out.flag_origins
                    .entry(Arc::clone(flag))
                    .or_default()
                    .push(ValueOrigin::Env(env_name.to_string()));
            }
        }
        // Decide every `default_if` against argv+env only. Binding as we go would put
        // a default into `out.flags` and make the next flag's condition treat it as
        // explicit — Go's `Given()` and the derive's `__given_*` both ignore defaults
        // here, so an unconditional `default` on `--json` must not fire
        // `default_if "--json"`.
        let mut from_default_if: Vec<(Arc<SpecFlag>, crate::SpecDefaultIf)> = Vec::new();
        for flag in &flags {
            if out.flags.contains_key(flag) || overridden_flags.contains(&flag.name) {
                continue;
            }
            if let Some(condition) = flag.default_if.iter().find(|condition| {
                default_if_condition_matches(condition, &out, &overridden_flags, custom_env)
            }) {
                from_default_if.push((Arc::clone(flag), condition.clone()));
            }
        }
        for (flag, condition) in &from_default_if {
            // The whole condition, not just the value: several conditions may name the same
            // selector with different `when` values, so the selector alone does not say
            // which one fired.
            bind_flag_fallback(
                flag,
                std::slice::from_ref(&condition.value),
                &mut out,
                custom_env,
                ValueOrigin::DefaultIf {
                    selector: condition.selector.clone(),
                    when: condition.when.clone(),
                },
            )?;
        }
        for flag in &flags {
            if out.flags.contains_key(flag) || overridden_flags.contains(&flag.name) {
                continue;
            }
            if !flag.default.is_empty() {
                bind_flag_fallback(
                    flag,
                    &flag.default,
                    &mut out,
                    custom_env,
                    ValueOrigin::Default,
                )?;
                continue;
            }
            if let Some(arg) = flag.arg.as_ref() {
                if !arg.default.is_empty() {
                    bind_flag_fallback(
                        flag,
                        &arg.default,
                        &mut out,
                        custom_env,
                        ValueOrigin::Default,
                    )?;
                }
            }
        }
        // Declarative value validation is deliberately post-binding. Defaults and
        // environment fallbacks have landed by here, and delimiters were already split
        // while binding. Like clap's value parsers, a declaration judges each resulting
        // raw value independently.
        for (arg, parsed) in &out.args {
            validate_expression(
                &arg.name,
                arg.validate.as_deref(),
                arg.validate_error.as_deref(),
                parsed,
                &mut out.errors,
            );
        }
        for (flag, parsed) in &out.flags {
            if let Some(arg) = &flag.arg {
                validate_expression(
                    &flag.name,
                    arg.validate.as_deref(),
                    arg.validate_error.as_deref(),
                    parsed,
                    &mut out.errors,
                );
            }
        }
        // Applied once, here, because this is where the CLI's own version is known: a
        // `deprecated_warn_at` the spec has not reached yet is an author saying *not yet*.
        crate::warn::retain_reached(&mut out.warnings, self.spec.version.as_deref());
        Ok(out)
    }
}

/// Parse command-line arguments according to a spec.
///
/// Returns the parsed arguments and flags, with defaults and env vars applied.
/// Uses `std::env::var` for environment variable lookups.
///
/// For custom environment variable handling, use [`Parser`] instead.
#[must_use = "parsing result should be used"]
pub fn parse(spec: &Spec, input: &[String]) -> Result<ParseOutput, miette::Error> {
    Parser::new(spec).parse(input)
}

/// Parse command-line arguments without applying defaults.
///
/// Use this for help text generation or when you need the raw parsed values.
#[must_use = "parsing result should be used"]
pub fn parse_partial(spec: &Spec, input: &[String]) -> Result<ParseOutput, miette::Error> {
    parse_partial_with_env(spec, input, None, None, MountTiming::Eager).map(|(out, _)| out)
}

/// Basename of argv[0] for a multicall CLI: last path component, with a trailing
/// `.exe` stripped so Windows and Unix agree.
pub fn multicall_basename(argv0: &str) -> &str {
    let name = argv0.rsplit(['/', '\\']).next().unwrap_or(argv0);
    match name.get(name.len().saturating_sub(4)..) {
        Some(ext) if ext.eq_ignore_ascii_case(".exe") => &name[..name.len() - 4],
        _ => name,
    }
}

/// The applet name to parse as the first word, when argv[0] is not the dispatcher.
///
/// `None` means a dispatcher invocation (`busybox ls`): skip argv[0] and parse the
/// rest. `Some` is a symlink invocation (`ls -l`): inject the basename.
pub fn multicall_applet<'a>(argv0: &'a str, name: &str, bin: Option<&str>) -> Option<&'a str> {
    let base = multicall_basename(argv0);
    if !name.is_empty() && base == multicall_basename(name) {
        return None;
    }
    if let Some(bin) = bin {
        if !bin.is_empty() && base == multicall_basename(bin) {
            return None;
        }
    }
    Some(base)
}

/// Internal version of parse_partial that accepts an optional custom env map.
/// When a command's own `mount` runs, for the root — which nothing descends into.
///
/// A completion has to know every command before it can offer one, even with
/// nothing typed yet, so it resolves up front. An execution knows the word it was
/// given, so it only pays for discovery when that word matches nothing declared —
/// and a CLI that declares its commands and mounts a few more does not spawn a
/// process on every invocation.
#[derive(Clone, Copy, PartialEq, Eq)]
enum MountTiming {
    Eager,
    WhenAWordIsUnknown,
}

/// One word on its way through the parser, with what the parser has learned about it.
///
/// This holds what a side queue used to: the flag Phase 1 read a word as, previously a
/// `VecDeque` popped in step with the words. Two queues staying aligned is an invariant
/// nothing checks, and it was delicate enough to need explaining at three call sites; on
/// the word itself there is nothing to keep aligned. The argv position is here for the
/// same reason: the queue is popped, re-queued, split on `=`, and has subcommand words
/// removed from the middle, so position in the queue stops meaning position in argv on the
/// first descent.
struct Token {
    word: String,
    /// Where in the caller's argv this word came from.
    ///
    /// A word the parser made up points at the token it was derived from — the tail of a
    /// short bundle at the bundle, a multicall applet name at argv[0] — because that is the
    /// token a reader would point at, and there is nothing else to point at.
    argv: usize,
    /// The flag Phase 1 read this word as, and the command level it read it at.
    ///
    /// `Some((flag, command_level))` for a flag word, `None` for its value, for anything
    /// unresolved, and for every word Phase 1 never reached. The words stay in the queue
    /// for Phase 2 to re-parse — that is how they reach `out.flags` and `as_env()` — but by
    /// then the recognized flags have changed, because each descent drops the parent's
    /// non-global flags and a mounted command may declare the same name as a global seen
    /// here. Recording the owner keeps a word bound to the flag it was read as.
    ///
    /// The level matters to strict parsing: clap permits an inherited global once on each
    /// side of a subcommand boundary.
    binding: Option<(Arc<SpecFlag>, usize)>,
}

impl Token {
    fn new(word: String, argv: usize) -> Self {
        Self {
            word,
            argv,
            binding: None,
        }
    }
}

/// The token trace, while it is being built.
///
/// One row per word of the caller's argv, so a role can be recorded against a position
/// without the recorder having to know how many words came before it. Words the parser
/// made up have no row of their own and fold onto the row they were derived from.
struct Trace {
    tokens: Vec<TokenBinding>,
}

impl Trace {
    fn new(input: &[String]) -> Self {
        Self {
            tokens: input
                .iter()
                .enumerate()
                .map(|(index, word)| TokenBinding {
                    index,
                    word: word.clone(),
                    synthesized: false,
                    roles: vec![],
                })
                .collect(),
        }
    }

    fn record(&mut self, argv: usize, role: TokenRole) {
        if let Some(token) = self.tokens.get_mut(argv) {
            token.roles.push(role);
        }
    }

    /// Note that what was read at this position is not what the caller wrote there.
    fn note_synthesized(&mut self, argv: usize) {
        if let Some(token) = self.tokens.get_mut(argv) {
            token.synthesized = true;
        }
    }

    /// Every word the parse never reached, once it has stopped.
    fn close(&mut self, unread: &VecDeque<Token>) {
        for token in unread {
            self.record(token.argv, TokenRole::Unread);
        }
    }
}

fn parse_partial_with_env(
    spec: &Spec,
    input: &[String],
    custom_env: Option<&HashMap<String, String>>,
    mount_outputs: Option<&HashMap<String, String>>,
    mount_timing: MountTiming,
) -> Result<(ParseOutput, HashSet<String>), miette::Error> {
    let mut trace = Trace::new(input);
    parse_partial_traced(
        spec,
        input,
        custom_env,
        mount_outputs,
        mount_timing,
        &mut trace,
    )
}

/// The binding phase, with the trace left somewhere the caller can still read it.
///
/// A failure this phase cannot continue past — a word no declaration can take, a flag a
/// strict spec refuses — leaves through `?`, and a trace owned by the loop goes with it. The
/// words read before the failure are most of what a report wants, so the caller owns the
/// trace instead and keeps them. See [`Parser::explain_refused`].
fn parse_partial_traced(
    spec: &Spec,
    input: &[String],
    custom_env: Option<&HashMap<String, String>>,
    mount_outputs: Option<&HashMap<String, String>>,
    mount_timing: MountTiming,
    trace: &mut Trace,
) -> Result<(ParseOutput, HashSet<String>), miette::Error> {
    if let Some(view) = input.first().and_then(|argv0| spec.view_for_program(argv0)) {
        let viewed = spec.for_view(view)?;
        return parse_partial_traced(
            &viewed,
            input,
            custom_env,
            mount_outputs,
            mount_timing,
            trace,
        );
    }
    trace!("parse_partial: {input:?}");
    let mut input = input
        .iter()
        .enumerate()
        .map(|(argv, word)| Token::new(word.clone(), argv))
        .collect::<VecDeque<_>>();
    let argv0 = input.pop_front();
    if let Some(argv0) = argv0.as_ref() {
        trace.record(argv0.argv, TokenRole::Program);
    }
    if spec.multicall {
        if let Some(raw) = argv0 {
            if let Some(applet) = multicall_applet(&raw.word, &spec.name, Some(spec.bin.as_str())) {
                // A symlink invocation reads a word the caller never typed — the basename of
                // the program itself — so argv[0] is both the program and, below, whatever
                // that word selects.
                trace.note_synthesized(raw.argv);
                input.push_front(Token::new(applet.to_string(), raw.argv));
            }
        }
    }
    // The policy observes the selected command's own argv, not values eventually filled from
    // env/default. Start at the root, then reset on every explicit descent. A default
    // subcommand receives the unmatched word that selected it, so it is necessarily non-bare.
    let mut command_has_argv = !input.is_empty();

    let mut out = ParseOutput {
        cmd: spec.cmd.clone(),
        cmds: vec![spec.cmd.clone()],
        args: IndexMap::new(),
        flags: IndexMap::new(),
        tokens: vec![],
        flag_origins: IndexMap::new(),
        arg_origins: IndexMap::new(),
        overridden_flags: BTreeMap::new(),
        available_flags: gather_flags(&spec.cmd),
        flag_awaiting_value: vec![],
        errors: vec![],
        warnings: vec![],
        next_arg: None,
        double_dash_seen: false,
        external: None,
    };
    // Keep this internal so adding relationship support remains semver-compatible. The full
    // parser uses it to prevent defaults and environment values from restoring overridden flags.
    let mut overridden_flags = HashSet::new();
    // Which spelling supplied each parsed flag. A child may re-declare one long form of an
    // inherited global while the merge keeps the ancestor's other aliases on the same `Arc`.
    // The declaration object alone then cannot answer whether `--clean` belonged to the child
    // or an inherited `-c` belonged to the ancestor.
    let mut parsed_flag_spellings: HashMap<usize, HashSet<String>> = HashMap::new();

    // Phase 1: Scan for subcommands and collect global flags
    //
    // This phase identifies subcommands early because they may have mount points
    // that need to be executed with the global flags that appeared before them.
    //
    // Example: "usage --verbose run task"
    //   -> finds "run" subcommand, passes ["--verbose"] to its mount command
    //   -> then finds "task" as a subcommand of "run" (if it exists)
    //
    // We only collect global flags for mounts because:
    // - Non-global flags are specific to the current command, not subcommands
    // - Global flags affect all commands and should be passed to mount points
    let mut prefix_flags: Vec<(Arc<SpecFlag>, Vec<String>)> = vec![];
    // Which flag each word skipped here belongs to is recorded on the word — see
    // `Token::binding`.
    let mut command_arg_found = false;
    let mut variadic_flag_active = false;
    let mut idx = 0;
    // Track whether we've already applied the default_subcommand to prevent
    // multiple switches (e.g., if default is "run" and there's a task named "run")
    let mut used_default_subcommand = false;
    // Whether the command in scope has had its own mounts run. A mount on the root
    // is the case that needs this: a subcommand's mounts are run when the parser
    // descends into it, but nothing descends into the root.
    let mut mounts_resolved = false;
    // A completion needs the whole command list before it can offer anything, and
    // `mycli <tab>` has no word to trigger discovery with — so waiting for one would
    // mean a root mount never contributed to the very thing it exists for.
    //
    // The default-subcommand gate applies here too, and has to: offering a discovered
    // command that a real parse would hand to the default instead would be worse than
    // not offering it. A root mount under a `default_subcommand` that does not say
    // `overrides_default` therefore contributes nothing anywhere, which is what
    // "the default outranks discovery" means.
    let default_outranks_mounts =
        spec.default_subcommand.is_some() && !out.cmd.mounts.iter().any(|m| m.overrides_default);
    if mount_timing == MountTiming::Eager && !default_outranks_mounts && !out.cmd.mounts.is_empty()
    {
        mounts_resolved = true;
        let mut mounted = out.cmd.clone();
        mounted.mount(&[], mount_outputs)?;
        merge_subcommand_flags(&mut out.available_flags, gather_flags(&mounted), false);
        if let Some(last) = out.cmds.last_mut() {
            *last = mounted.clone();
        }
        out.cmd = mounted;
    }

    while idx < input.len() {
        // Only for a word that could name a command, and only when it matches
        // nothing already declared. A CLI that declares its commands and mounts more
        // does not spawn a process for every invocation, and a flag — `--help`, or
        // anything unrecognized — never triggers discovery at all, which it would
        // otherwise do simply by not being a subcommand.
        // A declared `default_subcommand` already says what an unmatched word means,
        // and it costs nothing — so discovery waits behind it unless a mount asks to
        // outrank it. Without this, a task runner would spawn its discovery process
        // once per task invocation.
        let default_catches_it = spec.default_subcommand.as_deref().is_some_and(|name| {
            default_accepts_word(&out.cmd, name, &input[idx].word)
                && !out.cmd.mounts.iter().any(|m| m.overrides_default)
        });
        if !mounts_resolved
            && !out.cmd.mounts.is_empty()
            && !default_catches_it
            && is_command_word(&input[idx].word)
            && !is_negative_number(&input[idx].word)
            && out.cmd.find_subcommand(&input[idx].word).is_none()
        {
            mounts_resolved = true;
            let mut mounted = out.cmd.clone();
            mounted.mount(&mount_prefix_words(&prefix_flags), mount_outputs)?;
            merge_subcommand_flags(&mut out.available_flags, gather_flags(&mounted), false);
            if let Some(last) = out.cmds.last_mut() {
                *last = mounted.clone();
            }
            out.cmd = mounted;
        }
        if variadic_flag_active
            && out.cmd.find_subcommand(&input[idx].word).is_some()
            && !out.cmd.subcommand_precedence_over_arg
        {
            break;
        }
        if let Some(subcommand) = out.cmd.find_subcommand(&input[idx].word) {
            if out.cmd.args_conflicts_with_subcommands && command_arg_found {
                bail!(
                    "subcommand '{}' cannot be used with arguments on its parent command",
                    input[idx].word
                );
            }
            let mut subcommand = subcommand.clone();
            // Pass prefix words (global flags before this subcommand) to mount
            subcommand.mount(&mount_prefix_words(&prefix_flags), mount_outputs)?;
            // Only the *boundary* is a mount crossing: below it, the mounted program's own
            // commands are ordinary commands relative to each other.
            let crossing_mount = subcommand.mounted && !out.cmd.mounted;
            merge_subcommand_flags(
                &mut out.available_flags,
                gather_flags(&subcommand),
                crossing_mount,
            );
            // Remove subcommand from input
            let selected = input.remove(idx);
            if let Some(selected) = selected {
                trace.record(
                    selected.argv,
                    TokenRole::Command {
                        name: subcommand.name.clone(),
                    },
                );
            }
            command_has_argv = idx < input.len();
            out.cmds.push(subcommand.clone());
            out.cmd = subcommand.clone();
            // A descent already ran the new command's mounts, above.
            mounts_resolved = true;
            prefix_flags.clear();
            command_arg_found = false;
            variadic_flag_active = false;
            // Continue from current position (don't reset to 0)
            // After remove(), idx now points to the next element
        } else if !is_command_word(&input[idx].word)
            || declared_numeric_short(&out.available_flags, &input[idx].word)
        {
            // Check if this is a known flag
            let word = input[idx].word.clone();
            let flag_key = get_flag_key(&word);

            // A short token keys on its first letter, so `-az` would be recorded as
            // `-a` and its tail left over. Check the whole token here, where it is
            // first read: a token containing an unrecognized letter is not a bundle,
            // and recording it as one is what let `-a` be applied from a token that
            // never named it.
            let is_bundle = word.starts_with("--")
                || short_bundle_is_known(spec, &out.cmds, &out.available_flags, &word);
            if let Some(f) = out
                .available_flags
                .get(flag_key)
                .cloned()
                .filter(|_| is_bundle)
            {
                command_arg_found = true;
                variadic_flag_active = f.arg.as_ref().is_some_and(|arg| arg.var);
                // Skip the flag and keep scanning. Both global and non-global flags may precede
                // a subcommand (`mycli --verbose run task`, `mycli run --force task`), and
                // stopping at one would hide the subcommand — and any mount on it — from the
                // parse, leaving the subcommand name to be mis-read as a positional argument.
                //
                // Only globals are forwarded to mounts: a non-global flag belongs to the
                // command that declared it, not to what is mounted below it.
                input[idx].binding = Some((Arc::clone(&f), out.cmds.len() - 1));
                let mut forwarded = f.global.then(|| vec![word.clone()]);
                idx += 1;

                // Only consume next word if flag takes an argument AND value isn't embedded
                // Example: "--dir foo" consumes "foo", but "--dir=foo" or "--verbose" do not
                if f.arg.is_some()
                    && !word.contains('=')
                    && idx < input.len()
                    && (!is_flag_like(&input[idx].word)
                        || (f.arg.as_ref().is_some_and(|arg| arg.allow_negative_numbers)
                            && is_negative_number(&input[idx].word)))
                {
                    if let Some(words) = forwarded.as_mut() {
                        words.push(input[idx].word.clone());
                    }
                    idx += 1;
                }
                if let Some(words) = forwarded {
                    apply_prefix_flag_overrides(&mut prefix_flags, Arc::clone(&f));
                    prefix_flags.push((f, words));
                }
            } else {
                // Unknown flag - stop looking for subcommands
                // Let the main parsing phase handle the error
                break;
            }
        } else {
            if variadic_flag_active && out.cmd.subcommand_precedence_over_arg {
                idx += 1;
                continue;
            }
            // Found a word that's not a flag or subcommand
            // Check if we should use the default_subcommand (only once, and only at the
            // root, which is the only place a spec can declare one — `out.cmds` holds just
            // the root until something descends). Without that second condition the one
            // declared name is looked up wherever the parser happens to be standing, so an
            // unrelated command acquires a default because a name matched one level down:
            // with `default_subcommand "ls"` at the top, `ex config zzz` descended into
            // `config ls`.
            if !used_default_subcommand && out.cmds.len() == 1 {
                if let Some(default_name) = &spec.default_subcommand {
                    if let Some(subcommand) = out
                        .cmd
                        .find_subcommand(default_name)
                        .filter(|_| default_accepts_word(&out.cmd, default_name, &input[idx].word))
                    {
                        if out.cmd.args_conflicts_with_subcommands && command_arg_found {
                            bail!(
                                "subcommand '{}' cannot be used with arguments on its parent command",
                                subcommand.name
                            );
                        }
                        let mut subcommand = subcommand.clone();
                        // Pass prefix words (global flags before this) to mount
                        subcommand.mount(&mount_prefix_words(&prefix_flags), mount_outputs)?;
                        let crossing_mount = subcommand.mounted && !out.cmd.mounted;
                        merge_subcommand_flags(
                            &mut out.available_flags,
                            gather_flags(&subcommand),
                            crossing_mount,
                        );
                        out.cmds.push(subcommand.clone());
                        out.cmd = subcommand.clone();
                        command_has_argv = true;
                        prefix_flags.clear();
                        command_arg_found = false;
                        variadic_flag_active = false;
                        // This descent ran the new command's mounts, so lazy
                        // discovery must not run them a second time.
                        mounts_resolved = true;
                        used_default_subcommand = true;
                        // Continue the loop to check if this word is a subcommand of the
                        // default subcommand (e.g., a task name added via mount).
                        // If it's not a subcommand, the next iteration will break and
                        // Phase 2 will handle it as a positional arg.
                        continue;
                    }
                }
            }
            // An unmatched word that names no subcommand is forwarded as an external
            // command: this word, then every token after it, including flags. Known
            // subcommands already won above, and a default_subcommand already caught.
            // clap's `allow_external_subcommands` is this, not `unknown_flags=value`.
            if out.cmd.external_subcommand {
                let rest: Vec<Token> = input.drain(idx..).collect();
                for token in &rest {
                    trace.record(token.argv, TokenRole::External);
                }
                out.external = Some(rest.into_iter().map(|t| t.word).collect());
                break;
            }
            // This could be a positional argument, so stop subcommand search
            break;
        }
    }

    // Phase 2: Main argument and flag parsing
    //
    // Now that we've identified all subcommands and executed their mounts,
    // we can parse the remaining arguments, flags, and their values.

    // The cursor into `out.cmd.args`, kept as an index rather than a reference because an
    // explicit `--` may jump it *past* arguments that stay empty (see the `w == "--"` arm).
    // With such a gap `out.args.len()` no longer equals the cursor, so anything asking "is this
    // argument filled?" has to consult `out.args` by key instead of counting.
    let mut next_arg_idx: usize = 0;
    let mut enable_flags = true;
    let mut grouped_flag = false;
    // Whether an explicit `--` has been consumed *as a separator* (as opposed to being kept as a
    // value by `double_dash="preserve"`). Args declared `double_dash="required"` only accept
    // words that come after it — see `report_double_dash_violation`.
    let mut seen_double_dash = false;
    // Args already reported as having been offered a word before the `--` they require, so a
    // variadic one does not report the same violation for every word it is offered.
    let mut double_dash_violations: HashSet<String> = HashSet::new();
    // Scalar occurrences are scoped to the command level where they were written. Inherited
    // globals may therefore appear once before and once after a subcommand under clap's strict
    // `args_override_self(false)` policy. The bitset also keeps both forms of a negatable flag:
    // opposite forms may override one another, while repeating either spelling is an error.
    let mut scalar_occurrences: HashMap<(usize, usize), u8> = HashMap::new();

    while !input.is_empty() {
        let token = input.pop_front().unwrap();
        // The flag this word was read as in Phase 1, if it skipped it (see `Token::binding`).
        let binding = token.binding;
        let argv = token.argv;
        let mut w = token.word;
        // A short's attached value is re-queued with `grouped_flag` set, and that
        // continuation is not a following word. `require_equals` refuses only the
        // following word; `-i9229` and `-i=9229` still bind. `default_missing` binds
        // only when the value is actually missing, so `-cnever` is still `never`.
        let attached_continuation = grouped_flag;

        // Check for restart_token - resets argument parsing for multiple command invocations
        // e.g., `mise run lint ::: test ::: check` with restart_token=":::"
        if let Some(ref restart_token) = out.cmd.restart_token {
            if w == *restart_token {
                // Reset argument parsing state for a fresh command invocation, keeping the
                // flags. `double_dash_violations` is deliberately *not* cleared: `out.errors`
                // is not cleared here either, so clearing it would let one arg report the same
                // violation once per invocation.
                out.args.clear();
                // With the values gone, so is where they came from — otherwise the second
                // invocation of `run lint ::: test` reports the first one's provenance. The
                // token trace is *not* cleared: those words were read, and a report that
                // dropped them would show a command line with a hole in it.
                out.arg_origins.clear();
                trace.record(argv, TokenRole::Restart);
                next_arg_idx = 0;
                out.flag_awaiting_value.clear(); // Clear any pending flag values
                enable_flags = true; // Reset -- separator effect
                seen_double_dash = false; // The next invocation needs its own `--`
                continue;
            }
        }

        // A flag declared `allow_hyphen_values` takes the next token whatever it looks
        // like, and that has to be asked before the separator arm below rather than
        // after it. Asked after, a `--` was consumed as a separator while the flag
        // stayed hungry, and the flag then ate the word past it: `ex -a -- -x` bound
        // `-x` and the separator was simply gone. Asked here, the flag takes the `--`
        // itself, which is what clap does with the same declaration — and no flag can
        // still be waiting once the separator has done its job, so the starvation rule
        // below has no path around it.
        if enable_flags
            && w.starts_with('-')
            && out.flag_awaiting_value.last().is_some_and(|flag| {
                !flag.require_equals
                    && (flag.allow_hyphen_values()
                        || (flag
                            .arg
                            .as_ref()
                            .is_some_and(|arg| arg.allow_negative_numbers)
                            && is_negative_number(&w)))
            })
        {
            // A variadic argument collects here too: which token supplied its first
            // value says nothing about how many it takes.
            let should_return = bind_pending_flag_value(
                spec,
                &out.cmd,
                &mut out.errors,
                &mut out.flags,
                &mut out.flag_awaiting_value,
                &mut w,
                &mut input,
                custom_env,
                trace,
                argv,
                // The token a hyphen-valued flag takes is the following word, never attached.
                false,
            )?;
            if should_return {
                record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);
                return Ok((out, overridden_flags));
            }
            continue;
        }

        // A flag whose value may be omitted that cannot take this token as a detached
        // value finishes bare and leaves the token for whatever comes next:
        // `--color --verbose` colours with the missing value and still sets verbose,
        // and `--inspect 9229` with `require_equals` binds the missing value rather
        // than treating 9229 as the port.
        if enable_flags
            && !attached_continuation
            && !out.flag_awaiting_value.is_empty()
            && out.flag_awaiting_value.last().is_some_and(|flag| {
                (flag.default_missing.is_some() || flag.value_optional)
                    && (flag.require_equals
                        || (is_flag_like(&w)
                            && !flag.allow_hyphen_values()
                            && !(flag
                                .arg
                                .as_ref()
                                .is_some_and(|arg| arg.allow_negative_numbers)
                                && is_negative_number(&w))))
            })
        {
            try_bind_default_missing(
                &mut out.flags,
                &mut out.flag_awaiting_value,
                custom_env,
                &mut out.flag_origins,
            )?;
        }

        // The first explicit `--` is still a separator after an `automatic` argument has
        // stopped flag parsing. Once an explicit separator has done its job, a second one is
        // an ordinary value: every parser worth comparing against keeps it (POSIX getopt,
        // argparse, clap, commander, yargs), and jdx/usage#229 was a user reporting the old
        // behavior as the bug it is.
        if w == "--" && !seen_double_dash {
            enable_flags = false;

            // Only preserve the double dash token if we're collecting values for a variadic arg
            // in double_dash == `preserve` mode
            let should_preserve = out
                .cmd
                .args
                .get(next_arg_idx)
                .map(|arg| arg.var && arg.double_dash == SpecDoubleDashChoices::Preserve)
                .unwrap_or(false);

            if should_preserve {
                // Fall through to arg parsing. This `--` is a *value*, not a separator, so it
                // neither counts as one nor unlocks a `double_dash="required"` arg.
            } else {
                seen_double_dash = true;
                trace.record(argv, TokenRole::Separator);

                // Everything after an explicit `--` belongs to the arg that requires one, so
                // jump the cursor there — past any earlier arg, including a greedy variadic
                // that would otherwise swallow the rest. This mirrors clap's `Arg::last(true)`,
                // which is what `double_dash="required"` is generated from. Specs without such
                // an arg find nothing and keep the cursor where it was.
                let target = out.cmd.args.iter().position(|arg| {
                    arg.double_dash == SpecDoubleDashChoices::Required
                        && !out.args.contains_key(arg)
                });
                if let Some(target) = target {
                    // Forward only. An unfilled required arg declared *before* the cursor is
                    // left where it is rather than rewound to — words already assigned to
                    // later args would have to be taken back for that to mean anything, and
                    // the arg keeps its `MissingArg`. `double_dash="required"` mirrors clap's
                    // `Arg::last(true)`, which is the final positional, so a spec that puts
                    // one ahead of others is already outside what this models.
                    if target > next_arg_idx {
                        next_arg_idx = target;
                    }
                }
                continue;
            }
        }

        // long flags
        if enable_flags && w.starts_with("--") {
            grouped_flag = false;
            // `Some` only when an `=` was actually written, so `--jobs=` can supply
            // an empty value while `--jobs` supplies none. Collapsing the two lost
            // the flag entirely.
            let split = w.split_once('=');
            let word = split.map(|(word, _)| word).unwrap_or(&w);
            let bound_flag = binding.as_ref().map(|(flag, _)| flag);
            if let Some(f) = bound_flag.or_else(|| out.available_flags.get(word)) {
                let command_level = binding
                    .as_ref()
                    .map(|(_, level)| *level)
                    .unwrap_or(out.cmds.len() - 1);
                parsed_flag_spellings
                    .entry(Arc::as_ptr(f) as usize)
                    .or_default()
                    .insert(word.to_string());
                // Recorded before the action check below: a token that named a flag named it
                // whether or not the parse can carry on afterwards.
                trace.record(
                    argv,
                    TokenRole::Flag {
                        flag: Arc::clone(f),
                        spelling: word.to_string(),
                        negated: f.negate.as_deref() == Some(word),
                    },
                );
                if f.action != crate::SpecFlagAction::Set {
                    out.errors.push(render_action_err(spec, &out.cmd, f, word));
                    record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);
                    return Ok((out, overridden_flags));
                }
                apply_flag_overrides(
                    f,
                    &out.available_flags,
                    &mut out.flags,
                    &mut out.flag_awaiting_value,
                    &mut overridden_flags,
                    &mut out.overridden_flags,
                );
                // An attached value only means something to a flag that takes one:
                // `--jobs=` is an empty string, while `--force=yes` has nothing to
                // give a flag that holds no value. Handing that leftover to the
                // positionals would re-split one token into two, so `ex --force=yes`
                // would fill an argument the caller never typed a word for.
                if f.arg.is_some() {
                    record_scalar_flag_occurrence(
                        &out.cmds,
                        f,
                        command_level,
                        None,
                        &mut scalar_occurrences,
                        &mut out.errors,
                    );
                    let f = Arc::clone(f);
                    out.flag_awaiting_value.push(Arc::clone(&f));
                    // The `=` has already settled that this text is the value, so it
                    // binds here rather than going back on the queue to be read as a
                    // token again — where `--jobs=--force` looked like a flag of its
                    // own and bound `force`, leaving `jobs` unset.
                    if let Some((_, val)) = split {
                        // The `=` settles where the *first* value came from and nothing
                        // more, so a variadic argument goes on collecting from the words
                        // after it exactly as the detached form does.
                        let mut val = val.to_string();
                        let should_return = bind_pending_flag_value(
                            spec,
                            &out.cmd,
                            &mut out.errors,
                            &mut out.flags,
                            &mut out.flag_awaiting_value,
                            &mut val,
                            &mut input,
                            custom_env,
                            trace,
                            argv,
                            // The `=` settled that this text is the value, so it rode in on
                            // the flag's own token.
                            true,
                        )?;
                        if should_return {
                            record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);
                            return Ok((out, overridden_flags));
                        }
                    }
                } else if f.count {
                    let arr = out
                        .flags
                        .entry(Arc::clone(f))
                        .or_insert_with(|| ParseValue::MultiBool(vec![]))
                        .try_as_multi_bool_mut()
                        .unwrap();
                    arr.push(true);
                } else {
                    let negate = f.negate.clone().unwrap_or_default();
                    let negated_form = word == negate;
                    let value = if f.bool_value {
                        match split.map(|(_, value)| value) {
                            Some("true") => !negated_form,
                            Some("false") => negated_form,
                            Some(value) => {
                                out.errors.push(UsageErr::InvalidValue {
                                    name: f.name.clone(),
                                    value: value.to_string(),
                                    reason: "expected `true` or `false`".to_string(),
                                });
                                continue;
                            }
                            None => !negated_form,
                        }
                    } else {
                        !negated_form
                    };
                    // Which form was typed is a question about the name, so it is
                    // asked of `word` rather than the whole token: the attached value
                    // is dropped just above, and comparing `--no-color=yes` against
                    // `--no-color` would take the negation down with it.
                    record_scalar_flag_occurrence(
                        &out.cmds,
                        f,
                        command_level,
                        Some(!negated_form),
                        &mut scalar_occurrences,
                        &mut out.errors,
                    );
                    out.flags.insert(Arc::clone(f), ParseValue::Bool(value));
                }
                continue;
            }
            if is_help_arg(spec, &out.cmd, &w) {
                out.errors
                    .push(render_help_err(spec, &out.cmd, w.len() > 2));
                trace.record(
                    argv,
                    TokenRole::Builtin {
                        spelling: w.clone(),
                    },
                );
                record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);
                return Ok((out, overridden_flags));
            }
            if is_version_arg(spec, &out.cmds, &w) {
                out.errors.push(render_version_err(spec, w.len() > 2));
                trace.record(
                    argv,
                    TokenRole::Builtin {
                        spelling: w.clone(),
                    },
                );
                record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);
                return Ok((out, overridden_flags));
            }
            if let Err(refused) = reject_unknown_flag_if_asked(spec, &out.cmds, &w) {
                trace.record(argv, TokenRole::UnknownFlag { bound_as: None });
                trace.close(&input);
                return Err(refused.into());
            }
        }

        // short flags
        //
        // A fresh token is checked whole before any of it is applied: `-az` with only
        // `-a` declared is not a bundle at all, so it must not set `a` on the way to
        // discovering that `z` names nothing. A grouped continuation is exempt — its
        // token was already checked when it arrived.
        let declared_numeric_short = declared_numeric_short(&out.available_flags, &w);
        let positional_negative_number = !declared_numeric_short
            && is_negative_number(&w)
            && out
                .cmd
                .args
                .get(next_arg_idx)
                .is_some_and(|arg| arg.allow_negative_numbers);
        if enable_flags
            && !grouped_flag
            // A word phase 1 already resolved to a flag needs no re-checking, and
            // the flags in scope have changed since, so re-checking would be wrong.
            && binding.is_none()
            && w.starts_with('-')
            && w.len() > 1
            && is_flag_like(&w)
            && !positional_negative_number
            && !short_bundle_is_known(spec, &out.cmds, &out.available_flags, &w)
        {
            // Refused if this command asked for that; otherwise it carries on below
            // as one word, with none of its letters applied.
            if let Err(refused) = reject_unknown_flag_if_asked(spec, &out.cmds, &w) {
                trace.record(argv, TokenRole::UnknownFlag { bound_as: None });
                trace.close(&input);
                return Err(refused.into());
            }
        } else if enable_flags && !positional_negative_number && w.starts_with('-') && w.len() > 1 {
            let short = w.chars().nth(1).unwrap();
            if let Some(f) = binding
                .as_ref()
                .map(|(flag, _)| flag)
                .or_else(|| out.available_flags.get(&format!("-{short}")))
            {
                let command_level = binding
                    .as_ref()
                    .map(|(_, level)| *level)
                    .unwrap_or(out.cmds.len() - 1);
                if f.action != crate::SpecFlagAction::Set {
                    out.errors
                        .push(render_action_err(spec, &out.cmd, f, &format!("-{short}")));
                    record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);
                    return Ok((out, overridden_flags));
                }
                parsed_flag_spellings
                    .entry(Arc::as_ptr(f) as usize)
                    .or_default()
                    .insert(format!("-{short}"));
                trace.record(
                    argv,
                    TokenRole::Flag {
                        flag: Arc::clone(f),
                        spelling: format!("-{short}"),
                        // A short spelling is never the negated form: `negate` is a long.
                        negated: false,
                    },
                );
                apply_flag_overrides(
                    f,
                    &out.available_flags,
                    &mut out.flags,
                    &mut out.flag_awaiting_value,
                    &mut overridden_flags,
                    &mut out.overridden_flags,
                );
                let rest = &w[1 + short.len_utf8()..];
                if !rest.is_empty() {
                    // `-abc` is one token that names three flags, so the tail is read at the
                    // bundle's own position rather than at one of its own.
                    input.push_front(Token::new(format!("-{rest}"), argv));
                }
                // A fully consumed short is no longer a grouped continuation.
                // Leaving this set after `-ai` made `-i` skip `require_equals`
                // and bind the following word.
                grouped_flag = !rest.is_empty();
                if f.arg.is_some() {
                    record_scalar_flag_occurrence(
                        &out.cmds,
                        f,
                        command_level,
                        None,
                        &mut scalar_occurrences,
                        &mut out.errors,
                    );
                    out.flag_awaiting_value.push(Arc::clone(f));
                } else if f.count {
                    let arr = out
                        .flags
                        .entry(Arc::clone(f))
                        .or_insert_with(|| ParseValue::MultiBool(vec![]))
                        .try_as_multi_bool_mut()
                        .unwrap();
                    arr.push(true);
                } else {
                    let negate = f.negate.clone().unwrap_or_default();
                    let value = w != negate;
                    record_scalar_flag_occurrence(
                        &out.cmds,
                        f,
                        command_level,
                        Some(value),
                        &mut scalar_occurrences,
                        &mut out.errors,
                    );
                    out.flags.insert(Arc::clone(f), ParseValue::Bool(value));
                }
                continue;
            }
            // The letter nothing declared may still be one the parser supplies, and it may
            // sit anywhere in the token: `-hv` asks for help as surely as `-vh` does, and
            // neither reaches the whole-token spellings below.
            if let Some(err) = supplied_short(spec, &out.cmds, short) {
                out.errors.push(err);
                trace.record(
                    argv,
                    TokenRole::Builtin {
                        spelling: format!("-{short}"),
                    },
                );
                record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);
                return Ok((out, overridden_flags));
            }
            if is_help_arg(spec, &out.cmd, &w) {
                out.errors
                    .push(render_help_err(spec, &out.cmd, w.len() > 2));
                trace.record(
                    argv,
                    TokenRole::Builtin {
                        spelling: w.clone(),
                    },
                );
                record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);
                return Ok((out, overridden_flags));
            }
            if is_version_arg(spec, &out.cmds, &w) {
                out.errors.push(render_version_err(spec, w.len() > 2));
                trace.record(
                    argv,
                    TokenRole::Builtin {
                        spelling: w.clone(),
                    },
                );
                record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);
                return Ok((out, overridden_flags));
            }
            if let Err(refused) = reject_unknown_flag_if_asked(spec, &out.cmds, &w) {
                trace.record(argv, TokenRole::UnknownFlag { bound_as: None });
                trace.close(&input);
                return Err(refused.into());
            }
            if grouped_flag {
                grouped_flag = false;
                w.remove(0);
                // What is left is a short flag's attached value, and one `=` between
                // the letter and the value is a separator: `-j=8` means 8. Only one,
                // so `-j==8` still means `=8`.
                if !out.flag_awaiting_value.is_empty() && w.starts_with('=') {
                    w.remove(0);
                }
            }
        }

        // Only while flags are still being read. A flag still waiting when the separator
        // was consumed is starved: its value would have to come from after the `--`,
        // where every token is data. Draining there gave `ex --jobs -- x` the word after
        // the separator, so the command line quietly meant `ex --jobs=x` and the `--`
        // was gone. Left waiting, it is reported as the missing value it is.
        // `require_equals` refuses a detached value: `--flag value` is a missing
        // value, not a flag of `"value"`. The attached form is still bound above.
        // Reported here rather than left waiting until the end of the line: falling
        // through would offer `value` to the positionals and call it an unexpected
        // word, which is the wrong error and a different one from usage-argv.
        if enable_flags
            && !attached_continuation
            && !out.flag_awaiting_value.is_empty()
            && out
                .flag_awaiting_value
                .last()
                .is_some_and(|flag| flag.require_equals)
        {
            let flag = out.flag_awaiting_value.last().unwrap();
            let token = flag
                .long
                .first()
                .map(|l| format!("--{l}"))
                .or_else(|| flag.short.first().map(|s| format!("-{s}")))
                .unwrap_or_else(|| flag.name.clone());
            out.errors.push(UsageErr::InvalidFlag {
                token: token.clone(),
                reason: "requires an argument".to_string(),
                span: (0, 0).into(),
                input: format!("{token} {w}"),
            });
            record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);
            return Ok((out, overridden_flags));
        }
        if enable_flags && !out.flag_awaiting_value.is_empty() {
            // Held before the drain pops it: a flag whose argument is variadic keeps
            // taking values after this first one.
            let should_return = bind_pending_flag_value(
                spec,
                &out.cmd,
                &mut out.errors,
                &mut out.flags,
                &mut out.flag_awaiting_value,
                &mut w,
                &mut input,
                custom_env,
                trace,
                argv,
                attached_continuation,
            )?;
            if should_return {
                record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);
                return Ok((out, overridden_flags));
            }
            continue;
        }

        if out.cmd.allow_missing_positional {
            while let Some(current) = out.cmd.args.get(next_arg_idx) {
                if current.required || out.args.contains_key(current) {
                    break;
                }
                let required_after = out.cmd.args[next_arg_idx + 1..]
                    .iter()
                    .filter(|arg| arg.required)
                    .count();
                if required_after == 0 {
                    break;
                }
                let remaining_values = 1 + input
                    .iter()
                    .filter(|token| !enable_flags || !is_flag_like(&token.word))
                    .count();
                if remaining_values > required_after {
                    break;
                }
                next_arg_idx += 1;
            }
        }

        if let Some(arg) = out.cmd.args.get(next_arg_idx) {
            if arg.var
                && out.args.contains_key(arg)
                && arg.value_terminator.as_deref() == Some(w.as_str())
            {
                trace.record(
                    argv,
                    TokenRole::ValueTerminator {
                        ends: arg.name.clone(),
                    },
                );
                next_arg_idx += 1;
                continue;
            }
            // Before anything else: an arg that requires `--` accepts nothing until one has been
            // seen. Checking ahead of `validate_choices` keeps a discarded word from also being
            // reported as an invalid choice, and from reaching that function's help escape.
            if arg.double_dash == SpecDoubleDashChoices::Required && !seen_double_dash {
                report_double_dash_violation(arg, &mut out.errors, &mut double_dash_violations);
                trace.record(
                    argv,
                    TokenRole::Refused {
                        reason: format!("{} only accepts words after `--`", arg.name),
                    },
                );
                // Drop the word without filling the arg or advancing the cursor: every later
                // word hits the same arg and is rejected the same way, so the parse still ends
                // in an error rather than in `unexpected word`.
                continue;
            }
            // Split before judging, as the flag path does: after the split the word is
            // no longer one value, and `choices` has to be asked about each. Judging
            // first rejects `src:docs` against a list that both halves are on, and
            // names the whole word rather than the half that was wrong.
            let trailing_value =
                seen_double_dash || arg.double_dash == SpecDoubleDashChoices::Automatic;
            let suppress_trailing_delimiter =
                out.cmds.iter().any(|cmd| cmd.dont_delimit_trailing_values);
            let delimiter = if suppress_trailing_delimiter && trailing_value {
                None
            } else {
                arg.delimiter
            };
            let parts: Vec<String> = match delimiter {
                Some(delimiter) => w.split(delimiter).map(str::to_string).collect(),
                None => vec![w.clone()],
            };
            let mut refused = false;
            for part in &parts {
                if validate_choices(
                    spec,
                    &out.cmd,
                    &mut out.errors,
                    ChoiceTarget::arg(arg),
                    part,
                    arg.choices.as_ref(),
                    custom_env,
                )? {
                    refused = true;
                    break;
                }
            }
            if refused {
                record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);
                return Ok((out, overridden_flags));
            }
            // `double_dash="automatic"` means the first value this arg takes is the last
            // token read as anything but data: a wrapper declaring it can forward flags
            // without its caller typing a `--`. Set before the value is stored, so the
            // rest of the command line is already past flag parsing.
            if arg.double_dash == SpecDoubleDashChoices::Automatic {
                enable_flags = false;
            }
            // A flag-like word reaching a positional while flags are still being read was
            // offered to every declaration and matched none: under the default
            // `unknown_flags="value"` it becomes data, and saying so is the difference
            // between "you have a typo" and "this argument took your typo".
            let unknown_flag = enable_flags
                && !positional_negative_number
                && is_flag_like(&w)
                && binding.is_none();
            trace.record(
                argv,
                if unknown_flag {
                    TokenRole::UnknownFlag {
                        bound_as: Some(Arc::new(arg.clone())),
                    }
                } else {
                    TokenRole::Arg {
                        arg: Arc::new(arg.clone()),
                        values: parts.clone(),
                    }
                },
            );
            if arg.var {
                let arr = out
                    .args
                    .entry(Arc::new(arg.clone()))
                    .or_insert_with(|| ParseValue::MultiString(vec![]))
                    .try_as_multi_string_mut()
                    .unwrap();
                // The values this word carried, split above so that everything
                // downstream — `choices`, `var_max` stopping the collection, `var_min` —
                // counts the values the user meant rather than the words they typed.
                arr.extend(parts.iter().cloned());
                if arr.len() >= arg.var_max.unwrap_or(usize::MAX) {
                    next_arg_idx += 1;
                }
            } else {
                out.args
                    .insert(Arc::new(arg.clone()), ParseValue::String(w));
                next_arg_idx += 1;
            }
            continue;
        }
        if is_help_arg(spec, &out.cmd, &w) {
            out.errors
                .push(render_help_err(spec, &out.cmd, w.len() > 2));
            trace.record(
                argv,
                TokenRole::Builtin {
                    spelling: w.clone(),
                },
            );
            record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);
            return Ok((out, overridden_flags));
        }
        if is_version_arg(spec, &out.cmds, &w) {
            out.errors.push(render_version_err(spec, w.len() > 2));
            trace.record(
                argv,
                TokenRole::Builtin {
                    spelling: w.clone(),
                },
            );
            record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);
            return Ok((out, overridden_flags));
        }
        trace.record(
            argv,
            TokenRole::Refused {
                reason: "no declaration takes this word".to_string(),
            },
        );
        trace.close(&input);
        bail!("unexpected word: {w}");
    }

    record_stop(&mut out, next_arg_idx, seen_double_dash, trace, &input);

    // `out.flags` is keyed by `SpecFlag`, whose equality is intentionally name-only. Two
    // declarations with the same canonical name therefore share one public value entry even
    // when both were typed. The spelling ledger is keyed by declaration identity and retains
    // both, which is what exclusivity needs.
    let flag_was_parsed =
        |flag: &Arc<SpecFlag>| parsed_flag_spellings.contains_key(&(Arc::as_ptr(flag) as usize));

    // The spellings the selected command's own declaration speaks for, on this object.
    //
    // Empty unless that declaration really is this object's: a parent and child may each
    // declare `--clean` without merging, leaving two flags that share a name, and the
    // ancestor's must not be read as the child's. The test is whether every spelling the child
    // declared resolves back here — true of a merged flag, and of a plain local one, but not of
    // an ancestor whose long form the child took over.
    let child_spellings = |flag: &Arc<SpecFlag>| -> HashSet<String> {
        let declared: HashSet<String> = out
            .cmd
            .flags
            .iter()
            .filter(|declared| declared.name == flag.name)
            .flat_map(flag_keys)
            .collect();
        // *Any* of them, not all. All was too strong: a child may declare a spelling that
        // some other inherited global already owns — `-c --clean` beside an inherited
        // `-c --config` — and that collision is resolved in the other global's favor, so the
        // child's `-c` resolves elsewhere. Requiring every spelling to land here let one
        // unrelated collision disown the child from the `--clean` it plainly does own.
        //
        // Still enough to tell the two-object case apart, which is what this guards: when a
        // child re-declares a global as global, the child's own spellings resolve to the
        // child's separate flag, so none of them lands on the ancestor's.
        let speaks_for_this_flag = declared.iter().any(|spelling| {
            out.available_flags
                .get(spelling)
                .is_some_and(|available| Arc::ptr_eq(available, flag))
        });
        if speaks_for_this_flag {
            declared
        } else {
            HashSet::new()
        }
    };

    // Whose `exclusive` an occurrence activates, as `(the child's, an ancestor's)`.
    //
    // A child that re-declares an inherited global merges into one object answering to two
    // alias sets whose declarations may disagree, so there is no single owner to name: the
    // child owns the spellings it declared and the ancestor keeps the ones only it declared.
    // Both sides can be in play at once — `run -c --clean` is the ancestor's alias and the
    // child's in one invocation — and each carries its own declaration's answer.
    let exclusivity_in_play = |flag: &Arc<SpecFlag>| -> (bool, bool) {
        let child = child_spellings(flag);
        let child_exclusive = !child.is_empty()
            && out
                .cmd
                .flags
                .iter()
                .any(|declared| declared.name == flag.name && declared.exclusive);
        match parsed_flag_spellings.get(&(Arc::as_ptr(flag) as usize)) {
            Some(spellings) => (
                child_exclusive && spellings.iter().any(|s| child.contains(s)),
                flag.exclusive && spellings.iter().any(|s| !child.contains(s)),
            ),
            // An environment value has no spelling to attribute it by. The declaration the
            // selected command has in scope is the one that answers — which is the child's
            // when it re-declared the flag, and the ancestor's when it did not.
            None => (child_exclusive, flag.exclusive && child.is_empty()),
        }
    };

    let exclusive_occurrence = |flag: &Arc<SpecFlag>| {
        let (child, ancestor) = exclusivity_in_play(flag);
        child || ancestor
    };

    // clap's `exclusive` is also an escape from requiredness: `--version` has to work on a
    // command that otherwise needs an input. Companions are still diagnosed below, but an
    // exclusive occurrence suppresses the missing-value checks that would make it unusable
    // whether it was alone or not.
    let exclusive_present =
        unique_flags(out.available_flags.values().chain(out.flags.keys())).any(|flag| {
            exclusive_occurrence(flag)
                && !overridden_flags.contains(&flag.name)
                && (flag_was_parsed(flag) || flag_has_env(flag, custom_env))
        });
    let requirements_apply = |command_index: usize| {
        command_index + 1 == out.cmds.len() || !out.cmds[command_index].subcommand_negates_reqs
    };

    if out.cmd.arg_required_else_help && !command_has_argv {
        out.errors.push(render_help_err(spec, &out.cmd, false));
    }

    // A command that says it needs a subcommand, given none. Checked on `out.cmd` and nowhere
    // else, because `out.cmd` *is* the command the words reached: had a subcommand been taken,
    // the child would be here instead. The spec has carried `subcommand_required` since it was
    // added for the derive, and this parser never read it — so `mise generate` parsed as a
    // complete invocation while usage-argv and clap both refused it.
    if out.cmd.subcommand_required && !out.cmd.subcommands.is_empty() && out.external.is_none() {
        let mut names: Vec<&str> = out
            .cmd
            .subcommands
            .iter()
            // Aliases share a map entry with the name they point at; listing both would offer
            // the same command twice under two spellings.
            .filter(|(name, sub)| sub.name == **name && !sub.hide)
            .map(|(name, _)| name.as_str())
            .collect();
        names.sort_unstable();
        out.errors.push(UsageErr::MissingSubcommand(
            out.cmd.name.clone(),
            names.join(", "),
        ));
    }

    // Not `skip(out.args.len())`: a `--` may have jumped the cursor past an arg that stayed
    // empty, so position and fill count can disagree. Ask `out.args` which args it holds.
    if !exclusive_present {
        for arg in out
            .cmds
            .iter()
            .enumerate()
            .filter(|(index, _)| requirements_apply(*index))
            .flat_map(|(_, cmd)| &cmd.args)
        {
            if out.args.contains_key(arg) {
                continue;
            }
            // Already reported as needing a `--`; one mistake should not yield two messages.
            if double_dash_violations.contains(&arg.name) {
                continue;
            }
            let required_if = arg.required_if.iter().any(|selector| {
                selector_is_explicit(selector, &out, &overridden_flags, custom_env)
            });
            let required_if_eq = arg.required_if_eq.iter().any(|condition| {
                selector_explicit_has_value(
                    &condition.selector,
                    &condition.value,
                    &out,
                    &overridden_flags,
                    custom_env,
                )
            });
            let required_if_eq_all = !arg.required_if_eq_all.is_empty()
                && arg.required_if_eq_all.iter().all(|condition| {
                    selector_explicit_has_value(
                        &condition.selector,
                        &condition.value,
                        &out,
                        &overridden_flags,
                        custom_env,
                    )
                });
            let unless_any = arg.required_unless.iter().any(|selector| {
                selector_is_explicit(selector, &out, &overridden_flags, custom_env)
            });
            let unless_all = !arg.required_unless_all.is_empty()
                && arg.required_unless_all.iter().all(|selector| {
                    selector_is_explicit(selector, &out, &overridden_flags, custom_env)
                });
            let required_unless = (!arg.required_unless.is_empty()
                || !arg.required_unless_all.is_empty())
                && !(unless_any || unless_all);
            if (arg.required
                || required_if
                || required_if_eq
                || required_if_eq_all
                || required_unless)
                && arg.default.is_empty()
            {
                // Check if there's an env var available (custom env map takes precedence)
                let has_env = arg
                    .env
                    .as_ref()
                    .is_some_and(|env_var| env_contains(custom_env, env_var));
                if !has_env {
                    out.errors.push(UsageErr::MissingArg(arg.name.clone()));
                }
            }
        }
    }

    // Conflicts are a question about the invocation as a whole rather than about any one
    // token, so they are checked here beside the requirement checks rather than at the
    // point a flag is matched — the flag it conflicts with may still be ahead of it.
    // Its own loop: the requirement loop below skips the flags that *were* given, which
    // is exactly the set this needs.
    //
    // A value from the environment counts on both sides, matching what
    // `selector_is_explicit` says about the other flag: the question is whether a flag
    // has a value, not how it got one. That is what clap does, and an asymmetric rule
    // would make the same pair of flags a conflict or not depending on which one
    // happened to be typed.
    for flag in unique_flags(out.available_flags.values()) {
        let given = out.flags.contains_key(flag) || flag_has_env(flag, custom_env);
        if !given || overridden_flags.contains(&flag.name) {
            continue;
        }
        for other in &flag.conflicts {
            if selector_is_explicit(other, &out, &overridden_flags, custom_env) {
                out.errors.push(UsageErr::InvalidFlag {
                    token: format!("--{}", flag.name),
                    reason: format!("conflicts with {other}"),
                    span: (0, 0).into(),
                    input: format!("--{} {other}", flag.name),
                });
            }
        }
        // The positive form, checked in the same pass and under the same rule: a value
        // from the environment satisfies a requirement, because the question is whether
        // the other flag has a value rather than how it got one. A flag that was
        // overridden away has not been given, so it cannot satisfy anything either —
        // which is what `selector_is_explicit` already accounts for.
        //
        // Reported as the missing flag rather than as something wrong with the flag that
        // named it, which is what clap says too: an unmet `requires` is a required
        // argument that was not provided. Named by its own name, resolved through the
        // same matcher, so a `requires="-f"` reports `--force` rather than the selector.
        let owner = out
            .cmds
            .iter()
            .rposition(|cmd| cmd.flags.iter().any(|declared| declared.name == flag.name))
            .unwrap_or(out.cmds.len() - 1);
        if !exclusive_present && requirements_apply(owner) {
            for other in &flag.requires {
                if !selector_is_satisfied(other, &out, &overridden_flags, custom_env) {
                    let name = selector_flag_name(other, &out).unwrap_or_else(|| other.clone());
                    if other.starts_with('-') {
                        out.errors.push(UsageErr::MissingFlag(name));
                    } else {
                        out.errors.push(UsageErr::MissingArg(name));
                    }
                }
            }
            for condition in &flag.requires_if {
                if explicit_flag_has_value(flag, &condition.value, &out, custom_env)
                    && !selector_is_satisfied(
                        &condition.requires,
                        &out,
                        &overridden_flags,
                        custom_env,
                    )
                {
                    let name = selector_flag_name(&condition.requires, &out)
                        .unwrap_or_else(|| condition.requires.clone());
                    out.errors.push(UsageErr::MissingFlag(name));
                }
            }
        }
    }

    // Positionals can declare the same pairwise conflict as flags. Their selector is
    // the bare argument name, while a flag keeps its dashed spelling.
    for (command_index, arg) in out
        .cmds
        .iter()
        .enumerate()
        .flat_map(|(index, cmd)| cmd.args.iter().map(move |arg| (index, arg)))
    {
        let given = arg_is_explicit(arg, &out, custom_env);
        if !given {
            continue;
        }
        for other in &arg.conflicts {
            if selector_is_explicit(other, &out, &overridden_flags, custom_env) {
                out.errors.push(UsageErr::InvalidFlag {
                    token: arg.name.clone(),
                    reason: format!("conflicts with {other}"),
                    span: (0, 0).into(),
                    input: format!("{} {other}", arg.name),
                });
            }
        }
        if !exclusive_present && requirements_apply(command_index) {
            for other in &arg.requires {
                if !selector_is_satisfied(other, &out, &overridden_flags, custom_env) {
                    let name = selector_flag_name(other, &out).unwrap_or_else(|| other.clone());
                    if other.starts_with('-') {
                        out.errors.push(UsageErr::MissingFlag(name));
                    } else {
                        out.errors.push(UsageErr::MissingArg(name));
                    }
                }
            }
        }
    }

    // An exclusive flag is the whole-command form of a conflict: `--version` means the
    // rest of the line has nothing to act on. Everything the invocation supplied counts,
    // positionals included, which is what distinguishes it from being in a group with
    // every other flag.
    //
    // Only what was *given*, as `conflicts` reads it: a defaulted flag standing beside an
    // exclusive one is nobody saying anything, and counting it would make the exclusive
    // flag unusable on any command that has a default. Environment values do count, also as
    // `conflicts` reads them, so the spec parser and the derive agree.
    for flag in unique_flags(out.available_flags.values().chain(out.flags.keys())) {
        // `SpecFlag` equality is intentionally name-only for the public parsed-value map,
        // but re-declared aliases can leave distinct declarations with that same name in
        // scope. Exclusivity is about the declaration the typed spelling resolved to, so
        // compare the parser's `Arc`s by identity here.
        let given = flag_was_parsed(flag) || flag_has_env(flag, custom_env);
        if !exclusive_occurrence(flag) || !given || overridden_flags.contains(&flag.name) {
            continue;
        }
        let other_flag = unique_flags(out.available_flags.values().chain(out.flags.keys()))
            .find(|other| {
                !Arc::ptr_eq(other, flag)
                    && !overridden_flags.contains(&other.name)
                    && (flag_was_parsed(other) || flag_has_env(other, custom_env))
            })
            .map(|other| format!("--{}", other.name));
        let other_arg = out.cmd.args.iter().find(|arg| {
            out.args.keys().any(|given| given.name == arg.name)
                || arg
                    .env
                    .as_ref()
                    .is_some_and(|env| env_contains(custom_env, env))
        });
        // Selecting a child is company for an exclusive flag declared by an ancestor. An
        // exclusive flag belonging to the child itself does not conflict with the command word
        // needed to reach that child — so the question is not who owns the flag but whose
        // exclusivity is the one being enforced, which is what `exclusivity_in_play` already
        // separated.
        let (_, ancestor_exclusivity) = exclusivity_in_play(flag);
        let selected_subcommand =
            (out.cmds.len() > 1 && ancestor_exclusivity).then(|| out.cmd.name.clone());
        let other = other_flag
            .or_else(|| other_arg.map(|arg| format!("<{}>", arg.name)))
            .or(selected_subcommand);
        if let Some(other) = other {
            out.errors.push(UsageErr::InvalidFlag {
                token: format!("--{}", flag.name),
                reason: format!("must be given on its own, and {other} was given too"),
                span: (0, 0).into(),
                input: format!("--{} {other}", flag.name),
            });
        }
    }

    // Groups, checked once per group rather than per flag: both questions a group asks —
    // how many members were given, and whether that is enough — are about the set, which
    // is the whole reason a group exists rather than a pile of pairwise conflicts.
    //
    // The same "given" rule as everything else here, so a member filled from the
    // environment or a default counts.
    // Every command in the chain, not only the selected one: a group may name global
    // flags, which belong to an ancestor and are declared there.
    let mut group_errors: Vec<UsageErr> = Vec::new();
    for (command_index, group) in out
        .cmds
        .iter()
        .enumerate()
        .flat_map(|(index, cmd)| cmd.groups.iter().map(move |group| (index, group)))
    {
        // Counted by the *flag* a selector resolves to, not by the selector. `-f` and
        // `--file` are two spellings of one flag, and a group naming both — or naming one
        // flag twice — would otherwise report that flag as conflicting with itself the
        // moment it was given. Deduplicated rather than refused where the group is
        // written, because listing both spellings is redundant, not wrong.
        let mut given: Vec<&str> = Vec::new();
        let mut seen: Vec<String> = Vec::new();
        for selector in &group.members {
            if !selector_is_explicit(selector, &out, &overridden_flags, custom_env) {
                continue;
            }
            let name = selector_flag_name(selector, &out).unwrap_or_else(|| selector.clone());
            if seen.contains(&name) {
                continue;
            }
            seen.push(name);
            given.push(selector.as_str());
        }
        if !group.multiple && given.len() > 1 {
            group_errors.push(UsageErr::InvalidFlag {
                token: given[1].to_string(),
                reason: format!("cannot be used with {} in group {}", given[0], group.name),
                span: (0, 0).into(),
                input: format!("{} {}", given[0], given[1]),
            });
        }
        // Requiredness is a *positive* rule, so it reads a default as filling a member —
        // the rule `requires` follows. That is also why it cannot reuse `given` above:
        // exclusivity must count only what was supplied, or a defaulted member would
        // collide with the sibling the user actually typed.
        let satisfied = group
            .members
            .iter()
            .any(|selector| selector_is_satisfied(selector, &out, &overridden_flags, custom_env));
        if group.required && requirements_apply(command_index) && !satisfied && !exclusive_present {
            // The members are what a user has to type, so they are in the message; the
            // group's name is there too, since a command with several groups would
            // otherwise report the same sentence twice with nothing to tell them apart.
            group_errors.push(UsageErr::MissingGroup {
                group: group.name.clone(),
                members: group.members.join(", "),
            });
        }
    }
    out.errors.extend(group_errors);

    if !exclusive_present {
        for flag in unique_flags(out.available_flags.values()) {
            let owner = out
                .cmds
                .iter()
                .rposition(|cmd| cmd.flags.iter().any(|declared| declared.name == flag.name))
                .unwrap_or(out.cmds.len() - 1);
            if !requirements_apply(owner) {
                continue;
            }
            if out.flags.contains_key(flag) || overridden_flags.contains(&flag.name) {
                continue;
            }
            let has_default =
                !flag.default.is_empty() || flag.arg.iter().any(|a| !a.default.is_empty());
            let has_env = flag_has_env(flag, custom_env);
            let required_if = flag.required_if.iter().any(|selector| {
                selector_is_explicit(selector, &out, &overridden_flags, custom_env)
            });
            let required_if_eq = flag.required_if_eq.iter().any(|condition| {
                selector_explicit_has_value(
                    &condition.selector,
                    &condition.value,
                    &out,
                    &overridden_flags,
                    custom_env,
                )
            });
            let required_if_eq_all = !flag.required_if_eq_all.is_empty()
                && flag.required_if_eq_all.iter().all(|condition| {
                    selector_explicit_has_value(
                        &condition.selector,
                        &condition.value,
                        &out,
                        &overridden_flags,
                        custom_env,
                    )
                });
            let unless_any = flag.required_unless.iter().any(|selector| {
                selector_is_explicit(selector, &out, &overridden_flags, custom_env)
            });
            let unless_all = !flag.required_unless_all.is_empty()
                && flag.required_unless_all.iter().all(|selector| {
                    selector_is_explicit(selector, &out, &overridden_flags, custom_env)
                });
            let required_unless = (!flag.required_unless.is_empty()
                || !flag.required_unless_all.is_empty())
                && !(unless_any || unless_all);
            if (flag.required
                || required_if
                || required_if_eq
                || required_if_eq_all
                || required_unless)
                && !has_default
                && !has_env
            {
                out.errors.push(UsageErr::MissingFlag(flag.name.clone()));
            }
        }
    }

    // Validate var_min/var_max constraints for variadic args
    for (arg, value) in &out.args {
        if arg.var {
            if let ParseValue::MultiString(values) = value {
                if let Some(min) = arg.var_min {
                    if values.len() < min {
                        out.errors.push(UsageErr::VarArgTooFew {
                            name: arg.name.clone(),
                            min,
                            got: values.len(),
                        });
                    }
                }
                if let Some(max) = arg.var_max {
                    if values.len() > max {
                        out.errors.push(UsageErr::VarArgTooMany {
                            name: arg.name.clone(),
                            max,
                            got: values.len(),
                        });
                    }
                }
            }
        }
    }

    // Validate var_min/var_max constraints for variadic flags. These are bounds on
    // repeated occurrences of the flag itself. Bounds on its nested argument are enforced
    // by binding once per occurrence, where the per-occurrence count is still available.
    for flag in unique_flags(out.available_flags.values()) {
        if flag.var {
            let bound = match out.flags.get(flag) {
                Some(ParseValue::MultiString(values)) => values.len(),
                Some(ParseValue::MultiBool(values)) => values.len(),
                Some(_) => 1,
                None => 0,
            };
            // A partial parse deliberately leaves the final value-optional flag pending so
            // completion can still answer for it. It is nevertheless a real occurrence for
            // the repeated flag's bounds; the full parser closes it just after this phase.
            let pending = out
                .flag_awaiting_value
                .iter()
                .filter(|pending| {
                    Arc::ptr_eq(pending, flag)
                        && (pending.value_optional || pending.default_missing.is_some())
                })
                .count();
            let count = bound + pending;
            if count == 0 {
                continue;
            }
            if let Some(min) = flag.var_min {
                if count < min {
                    out.errors.push(UsageErr::VarFlagTooFew {
                        name: flag.name.clone(),
                        min,
                        got: count,
                    });
                }
            }
            if let Some(max) = flag.var_max {
                if count > max {
                    out.errors.push(UsageErr::VarFlagTooMany {
                        name: flag.name.clone(),
                        max,
                        got: count,
                    });
                }
            }
        }
    }

    Ok((out, overridden_flags))
}

fn validate_expression(
    name: &str,
    expression: Option<&str>,
    message: Option<&str>,
    parsed: &ParseValue,
    errors: &mut Vec<UsageErr>,
) {
    let Some(expression) = expression else {
        return;
    };
    #[cfg(not(feature = "validation"))]
    let _ = expression;
    let values: &[String] = match parsed {
        ParseValue::String(value) => std::slice::from_ref(value),
        ParseValue::MultiString(values) => values,
        ParseValue::Bool(_) | ParseValue::MultiBool(_) => return,
    };
    #[cfg(feature = "validation")]
    for value in values {
        let reason = match usage_validation::validate(expression, value) {
            Ok(true) => continue,
            Ok(false) => message
                .unwrap_or("does not satisfy the validation expression")
                .to_string(),
            Err(error) => format!("validation expression failed: {error}"),
        };
        errors.push(UsageErr::InvalidValue {
            name: name.to_string(),
            value: value.clone(),
            reason,
        });
        break;
    }
    #[cfg(not(feature = "validation"))]
    if let Some(value) = values.first() {
        let _ = message;
        errors.push(UsageErr::InvalidValue {
            name: name.to_string(),
            value: value.clone(),
            reason: "expression validation requires the `validation` feature".to_string(),
        });
    }
}

#[cfg(all(test, not(feature = "validation")))]
mod optional_validation_tests {
    use crate::{parse, Spec};

    #[test]
    fn validation_declarations_require_the_opt_in_runtime_feature() {
        let spec: Spec = r#"
name "ex"
bin "ex"
arg "<port>" validate="int(value) > 0"
        "#
        .parse()
        .unwrap();
        let error = parse(&spec, &["ex".to_string(), "1".to_string()]).unwrap_err();
        assert!(
            error
                .to_string()
                .contains("requires the `validation` feature"),
            "{error:?}"
        );
    }
}

fn flag_matches_selector(flag: &SpecFlag, selector: &str) -> bool {
    flag.name == selector || flag_keys(flag).iter().any(|key| key == selector)
}

fn flags_override(overrider: &SpecFlag, overridden: &SpecFlag) -> bool {
    overrider
        .overrides
        .iter()
        .any(|selector| flag_matches_selector(overridden, selector))
}

fn apply_prefix_flag_overrides(
    prefix_flags: &mut Vec<(Arc<SpecFlag>, Vec<String>)>,
    flag: Arc<SpecFlag>,
) {
    prefix_flags
        .retain(|(other, _)| !(flags_override(&flag, other) || flags_override(other, &flag)));
}

fn mount_prefix_words(prefix_flags: &[(Arc<SpecFlag>, Vec<String>)]) -> Vec<String> {
    prefix_flags
        .iter()
        .flat_map(|(_, words)| words.iter().cloned())
        .collect()
}

fn env_contains(custom_env: Option<&HashMap<String, String>>, env_var: &str) -> bool {
    match custom_env {
        Some(env) => env.contains_key(env_var),
        None => std::env::var(env_var).is_ok(),
    }
}

fn flag_has_env(flag: &SpecFlag, custom_env: Option<&HashMap<String, String>>) -> bool {
    flag.env_names()
        .any(|env_var| env_contains(custom_env, env_var))
}

fn fallback_is_true(value: &str) -> bool {
    matches!(value, "1" | "true" | "True" | "TRUE")
}

fn split_fallback_values(values: &[String], delimiter: Option<char>) -> Vec<String> {
    match delimiter {
        Some(delimiter) => values
            .iter()
            .flat_map(|value| value.split(delimiter).map(str::to_string))
            .collect(),
        None => values.to_vec(),
    }
}

fn validate_arg_fallback_count(arg: &SpecArg, count: usize, errors: &mut Vec<UsageErr>) {
    if let Some(min) = arg.var_min {
        if count < min {
            errors.push(UsageErr::VarArgTooFew {
                name: arg.name.clone(),
                min,
                got: count,
            });
        }
    }
    if let Some(max) = arg.var_max {
        if count > max {
            errors.push(UsageErr::VarArgTooMany {
                name: arg.name.clone(),
                max,
                got: count,
            });
        }
    }
}

fn validate_flag_fallback_count(flag: &SpecFlag, count: usize, errors: &mut Vec<UsageErr>) {
    if let Some(min) = flag.var_min {
        if count < min {
            errors.push(UsageErr::VarFlagTooFew {
                name: flag.name.clone(),
                min,
                got: count,
            });
        }
    }
    if let Some(max) = flag.var_max {
        if count > max {
            errors.push(UsageErr::VarFlagTooMany {
                name: flag.name.clone(),
                max,
                got: count,
            });
        }
    }
}

fn validate_flag_arg_fallback_count(
    flag: &SpecFlag,
    arg: &SpecArg,
    count: usize,
    errors: &mut Vec<UsageErr>,
) {
    if let Some(min) = arg.var_min {
        if count < min {
            errors.push(UsageErr::VarFlagTooFew {
                name: flag.name.clone(),
                min,
                got: count,
            });
        }
    }
    if let Some(max) = arg.var_max {
        if count > max {
            errors.push(UsageErr::VarFlagTooMany {
                name: flag.name.clone(),
                max,
                got: count,
            });
        }
    }
}

/// Bind a fallback the way an unconditional `default` does: one value, or several
/// for `var`, and choices checked the same way.
fn bind_flag_fallback(
    flag: &Arc<SpecFlag>,
    values: &[String],
    out: &mut ParseOutput,
    custom_env: Option<&HashMap<String, String>>,
    origin: ValueOrigin,
) -> Result<(), miette::Error> {
    if values.is_empty() {
        return Ok(());
    }
    if let Some(arg) = flag.arg.as_ref() {
        let values = split_fallback_values(values, arg.delimiter);
        if flag.var || arg.var {
            if flag.var {
                validate_flag_fallback_count(flag, values.len(), &mut out.errors);
            }
            if arg.var {
                validate_flag_arg_fallback_count(flag, arg, values.len(), &mut out.errors);
            }
            validate_choice_values(
                ChoiceTarget::option(flag),
                &values,
                arg.choices.as_ref(),
                custom_env,
            )?;
            out.flags
                .insert(Arc::clone(flag), ParseValue::MultiString(values));
        } else {
            let value = values.into_iter().next().unwrap_or_default();
            validate_choice_value(
                ChoiceTarget::option(flag),
                &value,
                arg.choices.as_ref(),
                custom_env,
            )?;
            out.flags
                .insert(Arc::clone(flag), ParseValue::String(value));
        }
    } else if flag.var {
        validate_flag_fallback_count(flag, values.len(), &mut out.errors);
        let bools: Vec<bool> = values.iter().map(|s| fallback_is_true(s)).collect();
        out.flags
            .insert(Arc::clone(flag), ParseValue::MultiBool(bools));
    } else {
        out.flags.insert(
            Arc::clone(flag),
            ParseValue::Bool(fallback_is_true(&values[0])),
        );
    }
    out.flag_origins
        .entry(Arc::clone(flag))
        .or_default()
        .push(origin);
    Ok(())
}

fn default_if_condition_matches(
    condition: &crate::SpecDefaultIf,
    out: &ParseOutput,
    overridden_flags: &HashSet<String>,
    custom_env: Option<&HashMap<String, String>>,
) -> bool {
    match &condition.when {
        None => selector_is_explicit(&condition.selector, out, overridden_flags, custom_env),
        Some(when) => {
            let Some(flag) = out
                .available_flags
                .values()
                .chain(out.flags.keys())
                .find(|flag| flag_matches_selector(flag, &condition.selector))
            else {
                return false;
            };
            if overridden_flags.contains(&flag.name) {
                return false;
            }
            explicit_flag_has_value(flag, when, out, custom_env)
        }
    }
}

/// Whether an explicitly supplied value of `flag` equals `expected`.
///
/// clap treats command-line and environment values as explicit for `requires_if`, but
/// not defaults. Keep that source distinction here instead of consulting the flag's
/// defaults through `selector_is_satisfied`.
fn explicit_flag_has_value(
    flag: &SpecFlag,
    expected: &str,
    out: &ParseOutput,
    custom_env: Option<&HashMap<String, String>>,
) -> bool {
    let parsed_matches = out.flags.get(flag).is_some_and(|value| match value {
        ParseValue::Bool(value) => value.to_string() == expected,
        ParseValue::String(value) => value == expected,
        ParseValue::MultiBool(values) => values.iter().any(|value| value.to_string() == expected),
        ParseValue::MultiString(values) => values.iter().any(|value| value == expected),
    });
    if out.flags.contains_key(flag) {
        return parsed_matches;
    }

    let value = flag.env_names().find_map(|env| match custom_env {
        Some(values) => values.get(env).cloned(),
        None => std::env::var(env).ok(),
    });
    value.is_some_and(
        |value| match flag.arg.as_ref().and_then(|arg| arg.delimiter) {
            Some(delimiter) => value.split(delimiter).any(|value| value == expected),
            None if flag.arg.is_none() => {
                matches!(value.as_str(), "1" | "true" | "True" | "TRUE").to_string() == expected
            }
            None => value == expected,
        },
    )
}

fn selector_explicit_has_value(
    selector: &str,
    expected: &str,
    out: &ParseOutput,
    overridden_flags: &HashSet<String>,
    custom_env: Option<&HashMap<String, String>>,
) -> bool {
    if let Some(flag) = out
        .available_flags
        .values()
        .chain(out.flags.keys())
        .find(|flag| flag_matches_selector(flag, selector))
    {
        return !overridden_flags.contains(&flag.name)
            && explicit_flag_has_value(flag, expected, out, custom_env);
    }
    let Some(arg) = selector_arg(selector, out) else {
        return false;
    };
    let parsed = out
        .args
        .iter()
        .find(|(given, _)| given.name == arg.name)
        .map(|(_, value)| value);
    if let Some(value) = parsed {
        return match value {
            ParseValue::String(value) => value == expected,
            ParseValue::MultiString(values) => values.iter().any(|value| value == expected),
            ParseValue::Bool(value) => value.to_string() == expected,
            ParseValue::MultiBool(values) => {
                values.iter().any(|value| value.to_string() == expected)
            }
        };
    }
    let value = arg.env_names().find_map(|env| match custom_env {
        Some(values) => values.get(env).cloned(),
        None => std::env::var(env).ok(),
    });
    value.is_some_and(|value| match arg.delimiter {
        Some(delimiter) => value.split(delimiter).any(|value| value == expected),
        None => value == expected,
    })
}

fn selector_is_explicit(
    selector: &str,
    out: &ParseOutput,
    overridden_flags: &HashSet<String>,
    custom_env: Option<&HashMap<String, String>>,
) -> bool {
    let flag_is_explicit = out
        .available_flags
        .values()
        .chain(out.flags.keys())
        .any(|flag| {
            flag_matches_selector(flag, selector)
                && !overridden_flags.contains(&flag.name)
                && (out.flags.contains_key(flag) || flag_has_env(flag, custom_env))
        });
    flag_is_explicit
        || selector_arg(selector, out).is_some_and(|arg| arg_is_explicit(arg, out, custom_env))
}

/// The name of the flag a selector points at, for an error that has to name it.
///
/// `selector_is_explicit` only answers yes or no, which is all a check needs; a message
/// about a flag that is *missing* has to say which one, and the selector may be a short
/// form or an alias rather than the name.
fn selector_flag_name(selector: &str, out: &ParseOutput) -> Option<String> {
    out.available_flags
        .values()
        .chain(out.flags.keys())
        .find(|flag| flag_matches_selector(flag, selector))
        .map(|flag| flag.name.clone())
        .or_else(|| selector_arg(selector, out).map(|arg| arg.name.clone()))
}

/// Whether a selector's flag ended up with a value, however it got one.
///
/// The rule for a *positive* relationship, and the difference from
/// [`selector_is_explicit`] is deliberate. A negative rule — `conflicts`, or a group's
/// exclusivity — has to count only what was given, or a flag with a default would
/// conflict with everything and no command line would parse. A positive one asks whether
/// the flag it names has a value, and a default is a value: that is already how plain
/// `required`, `required_if` and `required_unless` read a default, and `requires` saying
/// otherwise would have made the same flag missing here and present ten lines below.
fn selector_is_satisfied(
    selector: &str,
    out: &ParseOutput,
    overridden_flags: &HashSet<String>,
    custom_env: Option<&HashMap<String, String>>,
) -> bool {
    if selector_is_explicit(selector, out, overridden_flags, custom_env) {
        return true;
    }
    let flag_is_satisfied = out
        .available_flags
        .values()
        .chain(out.flags.keys())
        .filter(|flag| flag_matches_selector(flag, selector))
        .any(|flag| {
            !overridden_flags.contains(&flag.name)
                && (!flag.default.is_empty()
                    || flag.arg.iter().any(|a| !a.default.is_empty())
                    || flag.default_if.iter().any(|condition| {
                        default_if_condition_matches(condition, out, overridden_flags, custom_env)
                    }))
        });
    flag_is_satisfied || selector_arg(selector, out).is_some_and(|arg| !arg.default.is_empty())
}

fn selector_arg<'a>(selector: &str, out: &'a ParseOutput) -> Option<&'a SpecArg> {
    // Bare words are positional selectors. Keep accepting a flag's internal name above
    // for existing specs; when both exist, the dashed flag spelling removes ambiguity.
    if selector.starts_with('-') {
        return None;
    }
    out.cmds
        .iter()
        .flat_map(|cmd| &cmd.args)
        .find(|arg| arg.name == selector)
}

fn arg_is_explicit(
    arg: &SpecArg,
    out: &ParseOutput,
    custom_env: Option<&HashMap<String, String>>,
) -> bool {
    out.args.keys().any(|given| given.name == arg.name)
        || arg
            .env
            .as_ref()
            .is_some_and(|env| env_contains(custom_env, env))
}

fn apply_flag_overrides(
    flag: &Arc<SpecFlag>,
    available_flags: &BTreeMap<String, Arc<SpecFlag>>,
    parsed_flags: &mut IndexMap<Arc<SpecFlag>, ParseValue>,
    pending_flags: &mut Vec<Arc<SpecFlag>>,
    overridden_flags: &mut HashSet<String>,
    // The reportable half of the same fact: which flag did the overriding. The set above
    // only stops a default or an environment value restoring what was overridden, and
    // "`--quiet` is unset despite its default" has no answer without the name.
    attributed: &mut BTreeMap<String, String>,
) {
    let overridden_names: HashSet<String> = available_flags
        .values()
        .chain(parsed_flags.keys())
        .filter(|other| flags_override(flag, other) || flags_override(other, flag))
        .map(|other| other.name.clone())
        .collect();

    parsed_flags.retain(|parsed, _| !overridden_names.contains(&parsed.name));
    pending_flags.retain(|pending| !overridden_names.contains(&pending.name));
    for name in &overridden_names {
        attributed.insert(name.clone(), flag.name.clone());
    }
    overridden_flags.extend(overridden_names);
    // An explicit occurrence always restores this flag, including self-overrides.
    overridden_flags.remove(&flag.name);
    attributed.remove(&flag.name);
}

#[cfg(feature = "docs")]
fn render_help_err(spec: &Spec, cmd: &SpecCommand, long: bool) -> UsageErr {
    UsageErr::Help(docs::cli::render_help(spec, cmd, long))
}

#[cfg(feature = "docs")]
fn render_help_all_err(spec: &Spec, cmd: &SpecCommand) -> UsageErr {
    fn append(out: &mut String, spec: &Spec, cmd: &SpecCommand) {
        if !out.is_empty() {
            out.push('\n');
        }
        out.push_str(&docs::cli::render_help(spec, cmd, true));
        let mut children: Vec<_> = cmd
            .subcommands
            .values()
            .filter(|child| !child.hide)
            .collect();
        children.sort_by_key(|child| (child.display_order.unwrap_or(999), child.name.as_str()));
        for child in children {
            append(out, spec, child);
        }
    }

    let mut out = String::new();
    append(&mut out, spec, cmd);
    UsageErr::Help(out)
}

#[cfg(not(feature = "docs"))]
fn render_help_err(_spec: &Spec, _cmd: &SpecCommand, _long: bool) -> UsageErr {
    UsageErr::Help("help".to_string())
}

#[cfg(not(feature = "docs"))]
fn render_help_all_err(_spec: &Spec, _cmd: &SpecCommand) -> UsageErr {
    UsageErr::Help("help".to_string())
}

/// The version to answer with. `--version` prefers the long text and `-V` the concise
/// one, each falling back to the other when only one is declared.
fn render_version_err(spec: &Spec, long: bool) -> UsageErr {
    let value = if long {
        spec.long_version.as_ref().or(spec.version.as_ref())
    } else {
        spec.version.as_ref().or(spec.long_version.as_ref())
    };
    UsageErr::Version(value.cloned().unwrap_or_default())
}

fn render_action_err(spec: &Spec, cmd: &SpecCommand, flag: &SpecFlag, spelling: &str) -> UsageErr {
    use crate::SpecFlagAction;
    match flag.action {
        SpecFlagAction::Help => render_help_err(spec, cmd, spelling.starts_with("--")),
        SpecFlagAction::HelpShort => render_help_err(spec, cmd, false),
        SpecFlagAction::HelpLong => render_help_err(spec, cmd, true),
        SpecFlagAction::HelpAll => render_help_all_err(spec, cmd),
        SpecFlagAction::Version => render_version_err(spec, spelling.starts_with("--")),
        SpecFlagAction::Set => unreachable!("binding actions are handled before this helper"),
    }
}

#[derive(Copy, Clone)]
struct ChoiceTarget<'a> {
    kind: &'a str,
    name: &'a str,
}

impl<'a> ChoiceTarget<'a> {
    fn arg(arg: &'a SpecArg) -> Self {
        Self {
            kind: "arg",
            name: &arg.name,
        }
    }

    fn option(flag: &'a SpecFlag) -> Self {
        Self {
            kind: "option",
            name: &flag.name,
        }
    }
}

/// Whether every letter of a short token names a flag in scope.
///
/// Scanning stops at the first letter whose flag takes a value, because everything
/// after it is that value rather than more letters.
fn short_bundle_is_known(
    spec: &Spec,
    cmds: &[SpecCommand],
    available: &BTreeMap<String, Arc<SpecFlag>>,
    token: &str,
) -> bool {
    for c in token.chars().skip(1) {
        match available.get(&format!("-{c}")) {
            // `-h` and `-V` are recognized letters even though no spec declares them, so a
            // bundle containing one is a bundle. Without this `-vh` was not read as one at
            // all and fell through to `unexpected word`, while usage-argv, usage-go and
            // clap all answer it with help.
            None if supplied_short(spec, cmds, c).is_some() => {}
            None => return false,
            Some(f) if f.arg.is_some() => return true,
            Some(_) => {}
        }
    }
    true
}

/// The response `-h` or `-V` produces where nothing declares that letter.
///
/// The letter form of the flags the parser supplies rather than a spec declaring them,
/// under exactly the conditions [`is_help_arg`] and [`is_version_arg`] state — asked
/// about here one letter at a time, because a bundle is read one letter at a time.
///
/// Always the short response: `-h` is short help however many letters share its token,
/// and `-V` the concise version. The long forms belong to the long spellings. `-?` is not
/// here — it is a whole-token spelling of `-h` rather than a letter anyone bundles.
fn supplied_short(spec: &Spec, cmds: &[SpecCommand], letter: char) -> Option<UsageErr> {
    let cmd = cmds.last()?;
    match letter {
        'h' if is_help_arg(spec, cmd, "-h") => Some(render_help_err(spec, cmd, false)),
        'V' if is_version_arg(spec, cmds, "-V") => Some(render_version_err(spec, false)),
        _ => None,
    }
}

/// Refuse a flag-like token that named nothing, if this command asked for that.
///
/// Called from the flag branches, where the lookup has just failed and nothing from
/// the token has been applied yet — so a bundle like `-az` is refused whole rather
/// than after setting `-a`.
fn reject_unknown_flag_if_asked(
    spec: &Spec,
    path: &[SpecCommand],
    token: &str,
) -> Result<(), UsageErr> {
    // A lone `-` is a value by convention. A negative number reaches this only
    // when no pending value opted into the narrower exception.
    if !is_flag_like(token) {
        return Ok(());
    }
    if effective_unknown_flags(spec, path) != UnknownFlags::Error {
        return Ok(());
    }
    Err(UsageErr::InvalidFlag {
        token: token.to_string(),
        reason: "no such flag".to_string(),
        span: (0, 0).into(),
        input: token.to_string(),
    })
}

/// Whether a flag-like token that matches nothing is a value or an error, here.
///
/// The nearest enclosing command that stated a preference wins, then the spec,
/// then the default. Inherited, unlike `effect`: it describes how a command line
/// is read, and a CLI that forwards options tends to forward them at every level.
fn effective_unknown_flags(spec: &Spec, path: &[SpecCommand]) -> UnknownFlags {
    path.iter()
        .rev()
        .find_map(|cmd| cmd.unknown_flags)
        .or(spec.unknown_flags)
        .unwrap_or_default()
}

/// Whether a token would be read as a flag, for the purpose of rejecting unknown
/// ones.
///
/// A lone `-` is a value by convention. Other dash-prefixed tokens are flag-like;
/// a field may make the narrower negative-number exception.
fn is_flag_like(token: &str) -> bool {
    match token.strip_prefix('-') {
        None | Some("") => false,
        Some(_) => true,
    }
}

fn is_negative_number(token: &str) -> bool {
    token.strip_prefix('-').is_some_and(is_number)
}

fn record_scalar_flag_occurrence(
    cmds: &[SpecCommand],
    flag: &Arc<SpecFlag>,
    command_level: usize,
    bool_value: Option<bool>,
    occurrences: &mut HashMap<(usize, usize), u8>,
    errors: &mut Vec<UsageErr>,
) {
    let strict = cmds
        .get(command_level)
        .is_some_and(|cmd| !cmd.args_override_self);
    let collects_values = flag.var || flag.arg.as_ref().is_some_and(|arg| arg.var);
    if !strict || flag.count || collects_values {
        return;
    }

    let bit = match bool_value {
        Some(false) if flag.negate.is_some() => 0b10,
        _ => 0b01,
    };
    let key = (Arc::as_ptr(flag) as usize, command_level);
    let seen = occurrences.entry(key).or_default();
    if *seen & bit != 0 {
        errors.push(UsageErr::DuplicateFlag(flag.name.clone()));
    }
    *seen |= bit;
}

/// A token that can select a subcommand, trigger a mount, or be forwarded as an
/// external command.
///
/// Flag-like tokens are not words. A lone `-` is a value — conventionally stdin —
/// so it was never a candidate to *select* anything either. usage-argv uses the
/// same rule; without it, `-1` skipped the external-subcommand path because Phase 1
/// treated every token that `starts_with('-')` as a flag.
fn is_command_word(token: &str) -> bool {
    (!is_flag_like(token) || is_negative_number(token)) && token != "-"
}

/// Whether an otherwise numeric-looking token is an exact declared short flag.
///
/// This check belongs in both parse phases: phase 1 must skip the flag while it
/// searches for a later subcommand, and phase 2 must bind it instead of offering
/// it to an `allow_negative_numbers` positional.
fn declared_numeric_short(available_flags: &BTreeMap<String, Arc<SpecFlag>>, token: &str) -> bool {
    token.len() == 2 && token.as_bytes()[1].is_ascii_digit() && available_flags.contains_key(token)
}

/// Whether an unmatched word belongs to the root's default command.
///
/// Ordinary words always do. A negative number only does when the default command's
/// first positional explicitly accepts one; otherwise it stays at the root, matching
/// usage-argv and generated Go.
fn default_accepts_word(cmd: &SpecCommand, default_name: &str, token: &str) -> bool {
    !is_negative_number(token)
        || cmd
            .find_subcommand(default_name)
            .and_then(|default| default.args.first())
            .is_some_and(|arg| arg.allow_negative_numbers)
}

/// Digits, at most one `.`, and an optional exponent.
///
/// Spelled out rather than deferred to `f64::from_str`, which also accepts `inf` and
/// `NaN`: `-inf` is far likelier to be a misspelled flag than a number somebody meant
/// to pass. usage-argv implements the same rule, and the corpus pins the edges — the
/// two disagreed about `-1e5` when one used a float parse and the other did not.
fn is_number(rest: &str) -> bool {
    let (mantissa, exponent) = match rest.find(['e', 'E']) {
        Some(at) => (&rest[..at], Some(&rest[at + 1..])),
        None => (rest, None),
    };

    let mut seen_digit = false;
    let mut seen_dot = false;
    for c in mantissa.chars() {
        match c {
            '0'..='9' => seen_digit = true,
            '.' if !seen_dot => seen_dot = true,
            _ => return false,
        }
    }
    if !seen_digit {
        return false;
    }

    match exponent {
        None => true,
        Some(exp) => {
            let digits = exp
                .strip_prefix('+')
                .or_else(|| exp.strip_prefix('-'))
                .unwrap_or(exp);
            !digits.is_empty() && digits.chars().all(|c| c.is_ascii_digit())
        }
    }
}

/// Bind one value to the flag waiting for it, and let a variadic argument go on
/// collecting from the words that follow.
///
/// Every route to a flag's value comes through here — the following word, the text
/// after an `=`, and the token a `allow_hyphen_values` flag takes whatever it looks
/// like — so that all three agree on how many values the flag ends up with.
#[allow(clippy::too_many_arguments)]
fn bind_pending_flag_value(
    spec: &Spec,
    cmd: &SpecCommand,
    errors: &mut Vec<UsageErr>,
    flags: &mut IndexMap<Arc<SpecFlag>, ParseValue>,
    flag_awaiting_value: &mut Vec<Arc<SpecFlag>>,
    word: &mut String,
    input: &mut VecDeque<Token>,
    custom_env: Option<&HashMap<String, String>>,
    trace: &mut Trace,
    // Which token supplied `word`, and whether it rode along on the flag's own token
    // (`--jobs=8`, `-j8`) rather than following it. A variadic run's later words carry
    // their own positions and are recorded where they are read.
    argv: usize,
    attached: bool,
) -> miette::Result<bool> {
    // Held before the drain pops it, along with what the flag is already carrying: a
    // `var_max` bounds the values this occurrence takes, not the list they are appended
    // to, so a second `--include` starts counting again.
    let collecting = flag_awaiting_value
        .last()
        .filter(|flag| flag.arg.as_ref().is_some_and(|arg| arg.var))
        .cloned()
        .map(|flag| {
            let carried = flags.get(&flag).map(value_count).unwrap_or(0);
            (flag, carried)
        });
    let mut bound = vec![];
    let refused = drain_pending_flag_values(
        spec,
        cmd,
        errors,
        flags,
        flag_awaiting_value,
        word,
        custom_env,
        &mut bound,
    )?;
    for (flag, values) in bound {
        trace.record(
            argv,
            TokenRole::Value {
                flag,
                values,
                attached,
            },
        );
    }
    if refused {
        return Ok(true);
    }
    let Some((flag, carried)) = collecting else {
        return Ok(false);
    };
    collect_variadic_flag_values(
        spec,
        cmd,
        errors,
        flags,
        flag_awaiting_value,
        &flag,
        carried,
        input,
        custom_env,
        trace,
    )
}

/// Keep feeding a flag whose argument is variadic from the words that follow it.
///
/// `--include <pattern>...` collects from a single occurrence, so it takes tokens until
/// one is flag-like, a `--` arrives, its `var_max` is reached, or the command line ends.
/// This is greedy by design — a command declaring both such a flag and positionals will
/// find the flag eating them, and `--` or a `var_max` is how the run is stopped.
///
/// `carried` is what the flag already held when this occurrence began, so the bound
/// counts this run rather than everything the flag has collected across the command
/// line. Each value goes through the same drain as the first, so choices are checked
/// and the value lands in the same list rather than by a second route that could
/// disagree.
#[allow(clippy::too_many_arguments)]
fn collect_variadic_flag_values(
    spec: &Spec,
    cmd: &SpecCommand,
    errors: &mut Vec<UsageErr>,
    flags: &mut IndexMap<Arc<SpecFlag>, ParseValue>,
    flag_awaiting_value: &mut Vec<Arc<SpecFlag>>,
    flag: &Arc<SpecFlag>,
    carried: usize,
    input: &mut VecDeque<Token>,
    custom_env: Option<&HashMap<String, String>>,
    trace: &mut Trace,
) -> miette::Result<bool> {
    let max = flag
        .arg
        .as_ref()
        .and_then(|arg| arg.var_max)
        .unwrap_or(usize::MAX);
    while flags
        .get(flag)
        .map(value_count)
        .unwrap_or(0)
        .saturating_sub(carried)
        < max
    {
        let Some(next) = input.front().map(|token| token.word.as_str()) else {
            break;
        };
        if flag
            .arg
            .as_ref()
            .and_then(|arg| arg.value_terminator.as_deref())
            == Some(next)
        {
            let terminator = input.pop_front().unwrap();
            trace.record(
                terminator.argv,
                TokenRole::ValueTerminator {
                    ends: flag.name.clone(),
                },
            );
            break;
        }
        // The separator is left where it is: stopping here hands it to the arm that
        // knows what it means, rather than reading it as one more value.
        if next == "--"
            || (is_flag_like(next)
                && !(flag
                    .arg
                    .as_ref()
                    .is_some_and(|arg| arg.allow_negative_numbers)
                    && is_negative_number(next)))
        {
            break;
        }
        let taken = input.pop_front().unwrap();
        let argv = taken.argv;
        let mut word = taken.word;
        flag_awaiting_value.push(Arc::clone(flag));
        let mut bound = vec![];
        let refused = drain_pending_flag_values(
            spec,
            cmd,
            errors,
            flags,
            flag_awaiting_value,
            &mut word,
            custom_env,
            &mut bound,
        )?;
        for (flag, values) in bound {
            // A later word of the same occurrence is its own token, and never attached:
            // only the first value can ride along on the flag.
            trace.record(
                argv,
                TokenRole::Value {
                    flag,
                    values,
                    attached: false,
                },
            );
        }
        if refused {
            return Ok(true);
        }
    }
    // The loop stops once the occurrence has reached its bound, which without a delimiter is
    // exactly when it has taken `max` words. A delimiter breaks that: one word can carry
    // several values, so the run can end up *past* the bound rather than on it, and stopping
    // is no longer the same as staying within it. `--include a,b,c` under `var_max=2` is the
    // case — three values out of the one word the loop was entitled to take.
    //
    // Counted against `carried` like the loop itself, so this stays a statement about the
    // occurrence rather than about the list the occurrences build up.
    let taken = flags
        .get(flag)
        .map(value_count)
        .unwrap_or(0)
        .saturating_sub(carried);
    if let Some(min) = flag.arg.as_ref().and_then(|arg| arg.var_min) {
        if taken < min {
            errors.push(UsageErr::VarFlagTooFew {
                name: flag.name.clone(),
                min,
                got: taken,
            });
        }
    }
    if taken > max {
        errors.push(UsageErr::VarFlagTooMany {
            name: flag.name.clone(),
            max,
            got: taken,
        });
    }
    Ok(false)
}

/// How many values a flag is holding, for a bound that counts them.
fn value_count(value: &ParseValue) -> usize {
    match value {
        ParseValue::MultiString(values) => values.len(),
        ParseValue::MultiBool(values) => values.len(),
        _ => 1,
    }
}

/// Finish a value-optional flag that was given with no value.
///
/// Returns whether anything was bound. Completions keep the flag waiting — a
/// half-typed `--color ` is a question about the value — so this is asked only
/// once a full parse has decided the value is not coming, or once the next token
/// has made that decision.
///
/// The missing string is a real value: if the flag names `choices`, it has to
/// be one of them, the same way an env var or a `default` is checked. Binding
/// first and failing later would leave the flag set to a value the spec forbids.
fn try_bind_default_missing(
    flags: &mut IndexMap<Arc<SpecFlag>, ParseValue>,
    flag_awaiting_value: &mut Vec<Arc<SpecFlag>>,
    custom_env: Option<&HashMap<String, String>>,
    origins: &mut IndexMap<Arc<SpecFlag>, Vec<ValueOrigin>>,
) -> miette::Result<bool> {
    let Some(flag) = flag_awaiting_value.last() else {
        return Ok(false);
    };
    let value = match flag.default_missing.clone() {
        Some(value) => value,
        None if flag.value_optional => {
            let flag = flag_awaiting_value.pop().unwrap();
            // Presence in the map distinguishes this from an absent flag; an
            // empty collection distinguishes it from an explicitly empty
            // `--flag=` string without inventing a sentinel value.
            let variadic_value = flag.arg.as_ref().is_some_and(|arg| arg.var);
            origins
                .entry(Arc::clone(&flag))
                .or_default()
                .push(ValueOrigin::DefaultMissing);
            if flag.var {
                // A repeated bare occurrence is still an occurrence. The string collection
                // uses an empty value for it, just as the concrete `default_missing` path
                // pushes one value per occurrence; otherwise bounds and consumers silently
                // lose every bare repeat after the first.
                flags
                    .entry(flag)
                    .or_insert_with(|| ParseValue::MultiString(Vec::new()))
                    .try_as_multi_string_mut()
                    .unwrap()
                    .push(String::new());
            } else if variadic_value {
                // A variadic occurrence stays pending after each value. Reaching the next
                // flag (or EOF) closes that same occurrence; it must not erase what it took.
                flags
                    .entry(flag)
                    .or_insert_with(|| ParseValue::MultiString(Vec::new()));
            } else {
                // A scalar pending here is a new bare occurrence. The normal permissive
                // repeat policy makes the later occurrence a correction, including a
                // correction from an explicit value back to the bare tri-state.
                flags.insert(flag, ParseValue::MultiString(Vec::new()));
            }
            return Ok(true);
        }
        None => return Ok(false),
    };
    if let Some(arg) = flag.arg.as_ref() {
        validate_choice_value(
            ChoiceTarget::option(flag),
            &value,
            arg.choices.as_ref(),
            custom_env,
        )?;
    }
    let flag = flag_awaiting_value.pop().unwrap();
    origins
        .entry(Arc::clone(&flag))
        .or_default()
        .push(ValueOrigin::DefaultMissing);
    let collecting = flag.var || flag.arg.as_ref().is_some_and(|arg| arg.var);
    if collecting {
        let arr = flags
            .entry(flag)
            .or_insert_with(|| ParseValue::MultiString(vec![]))
            .try_as_multi_string_mut()
            .unwrap();
        arr.push(value);
    } else {
        flags.insert(flag, ParseValue::String(value));
    }
    Ok(true)
}

/// `bound` collects what each drained flag took, in the order it took it. The values are
/// the word after any `delimiter` split, which is the only place that split is known: by the
/// time they are in `flags` a scalar and a one-element list are indistinguishable, and a
/// second occurrence has appended to the same list.
#[allow(clippy::too_many_arguments)]
fn drain_pending_flag_values(
    spec: &Spec,
    cmd: &SpecCommand,
    errors: &mut Vec<UsageErr>,
    flags: &mut IndexMap<Arc<SpecFlag>, ParseValue>,
    flag_awaiting_value: &mut Vec<Arc<SpecFlag>>,
    word: &mut String,
    custom_env: Option<&HashMap<String, String>>,
    bound: &mut Vec<(Arc<SpecFlag>, Vec<String>)>,
) -> miette::Result<bool> {
    while let Some(flag) = flag_awaiting_value.pop() {
        let arg = flag.arg.as_ref().unwrap();
        // Split before anything judges the word, because after the split it is no longer
        // one value: `--env dev,prod` is two, and `choices` has to be asked about each.
        // Judging first would reject the whole word against a list neither half is on.
        let parts: Vec<String> = match arg.delimiter {
            Some(delimiter) => word.split(delimiter).map(str::to_string).collect(),
            None => vec![std::mem::take(word)],
        };
        for part in &parts {
            if validate_choices(
                spec,
                cmd,
                errors,
                ChoiceTarget::option(&flag),
                part,
                arg.choices.as_ref(),
                custom_env,
            )? {
                return Ok(true);
            }
        }
        word.clear();
        bound.push((Arc::clone(&flag), parts.clone()));
        // Two ways to hold several values, and both record a list: a `var` flag
        // collects one per occurrence, a variadic argument collects several from one.
        if flag.var || arg.var {
            let arr = flags
                .entry(flag)
                .or_insert_with(|| ParseValue::MultiString(vec![]))
                .try_as_multi_string_mut()
                .unwrap();
            arr.extend(parts);
        } else {
            // Nowhere for a second value to go, so the word stands as it was typed. A
            // delimiter on a flag that takes one value is refused where it is written.
            flags.insert(
                flag,
                ParseValue::String(parts.into_iter().next().unwrap_or_default()),
            );
        }
    }
    Ok(false)
}

fn choice_error(
    target: ChoiceTarget<'_>,
    value: &str,
    choices: Option<&SpecChoices>,
    custom_env: Option<&HashMap<String, String>>,
) -> Option<String> {
    let choices = choices?;
    if !choices.strict {
        return None;
    }
    let values = choices.values_with_env(custom_env);
    if choices.matches_with_env(value, custom_env) {
        return None;
    }
    if let Some(env) = choices.env() {
        if values.is_empty() {
            return Some(format!(
                "Invalid choice for {} {}: {value}, no choices resolved from env {env}",
                target.kind, target.name,
            ));
        }
    }
    Some(format!(
        "Invalid choice for {} {}: {value}, expected one of {}",
        target.kind,
        target.name,
        values.join(", ")
    ))
}

fn validate_choices(
    spec: &Spec,
    cmd: &SpecCommand,
    errors: &mut Vec<UsageErr>,
    target: ChoiceTarget<'_>,
    value: &str,
    choices: Option<&SpecChoices>,
    custom_env: Option<&HashMap<String, String>>,
) -> miette::Result<bool> {
    if is_help_arg(spec, cmd, value)
        && choices
            .is_some_and(|choices| choices.strict && !choices.matches_with_env(value, custom_env))
    {
        errors.push(render_help_err(spec, cmd, value.len() > 2));
        return Ok(true);
    }

    if let Some(err) = choice_error(target, value, choices, custom_env) {
        bail!("{err}");
    }
    Ok(false)
}

fn validate_choice_value(
    target: ChoiceTarget<'_>,
    value: &str,
    choices: Option<&SpecChoices>,
    custom_env: Option<&HashMap<String, String>>,
) -> miette::Result<()> {
    if let Some(err) = choice_error(target, value, choices, custom_env) {
        bail!("{err}");
    }
    Ok(())
}

fn validate_choice_values(
    target: ChoiceTarget<'_>,
    values: &[String],
    choices: Option<&SpecChoices>,
    custom_env: Option<&HashMap<String, String>>,
) -> miette::Result<()> {
    for value in values {
        validate_choice_value(target, value, choices, custom_env)?;
    }
    Ok(())
}

/// Everything a parse records about where it stopped: the positional cursor, so callers that
/// do not re-run the parse — completions, above all — agree with it, and the token trace.
///
/// Every exit from the binding phase comes through here, which is what makes it the right
/// place to close the trace: whatever is still queued was never read, and saying so is more
/// useful than leaving those words out of the report entirely.
fn record_stop(
    out: &mut ParseOutput,
    next_arg_idx: usize,
    seen_double_dash: bool,
    trace: &mut Trace,
    unread: &VecDeque<Token>,
) {
    out.next_arg = out.cmd.args.get(next_arg_idx).cloned().map(Arc::new);
    out.double_dash_seen = seen_double_dash;
    trace.close(unread);
    out.tokens = std::mem::take(&mut trace.tokens);
}

/// Record that `arg` was handed a word before the `--` it requires.
///
/// A variadic arg would otherwise report the same mistake once per word it was offered, so the
/// message is emitted only the first time each arg is seen. The set is also what suppresses the
/// `MissingArg` that a `required` + `double_dash="required"` arg would otherwise collect at the
/// end of the parse.
fn report_double_dash_violation(
    arg: &SpecArg,
    errors: &mut Vec<UsageErr>,
    violations: &mut HashSet<String>,
) {
    if violations.insert(arg.name.clone()) {
        errors.push(UsageErr::ArgRequiresDoubleDash(arg.name.clone()));
    }
}

/// `--version` and `-V`, which the parser supplies where the spec declares a version.
///
/// The twin of [`is_help_arg`], and of the `version` bit in usage-argv's and usage-go's
/// command tables — both of which accepted these spellings while this parser called them
/// unknown words. The help page has always listed `-V, --version` under the same
/// condition, and said in as many words that it did so "only where a version is
/// declared, which is where a parser accepts one", so a spec with a `version` rendered a
/// page advertising a flag the parse refused.
///
/// The root only, because that is where the page lists it: `version` is a property of
/// the program, and a subcommand answering with the program's version is a claim no spec
/// made. A declared flag wins by arriving first — every scan consults this only after
/// nothing declared matched — so a CLI that spends `-V` on something else keeps it, and
/// keeps `--version` supplied beside it.
fn is_version_arg(spec: &Spec, cmds: &[SpecCommand], w: &str) -> bool {
    (spec.version.is_some() || spec.long_version.is_some())
        && cmds.len() == 1
        && !spec.cmd.disable_version_flag
        && (w == "--version" || w == "-V")
}

fn is_help_arg(spec: &Spec, cmd: &SpecCommand, w: &str) -> bool {
    spec.disable_help != Some(true)
        && (((w == "--help" || w == "-h" || w == "-?") && !cmd.disable_help_flag)
            || (w == "help" && !cmd.disable_help_subcommand && cmd.subcommands.is_empty()))
}

impl ParseOutput {
    pub fn as_env(&self) -> BTreeMap<String, String> {
        let mut env = BTreeMap::new();
        for (flag, val) in &self.flags {
            let key = format!("usage_{}", flag.name.to_snake_case());
            let val = match val {
                ParseValue::Bool(b) => if *b { "true" } else { "false" }.to_string(),
                ParseValue::String(s) => s.clone(),
                ParseValue::MultiBool(b) => b.iter().filter(|b| **b).count().to_string(),
                ParseValue::MultiString(s) => shell_words::join(s),
            };
            env.insert(key, val);
        }
        for (arg, val) in &self.args {
            let key = format!("usage_{}", arg.name.to_snake_case());
            env.insert(key, val.to_string());
        }
        env
    }
}

impl Display for ParseValue {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ParseValue::Bool(b) => write!(f, "{b}"),
            ParseValue::String(s) => write!(f, "{s}"),
            ParseValue::MultiBool(b) => write!(f, "{}", b.iter().join(" ")),
            ParseValue::MultiString(s) => write!(f, "{}", shell_words::join(s)),
        }
    }
}

/// One `tokens` line for [`Debug`]: the position, the word, and what it became.
fn render_token(token: &TokenBinding) -> String {
    let roles = token.roles.iter().map(render_role).join(", ");
    let synthesized = if token.synthesized { " (read as)" } else { "" };
    format!("[{}] {}{synthesized}: {roles}", token.index, token.word)
}

fn render_role(role: &TokenRole) -> String {
    match role {
        TokenRole::Program => "program".to_string(),
        TokenRole::Command { name } => format!("subcommand {name}"),
        TokenRole::Flag {
            flag,
            spelling,
            negated,
        } => {
            let negated = if *negated { ", negated" } else { "" };
            format!("flag {} as {spelling}{negated}", flag.name)
        }
        TokenRole::Value {
            flag,
            values,
            attached,
        } => {
            let attached = if *attached { ", attached" } else { "" };
            format!("value of {} = {values:?}{attached}", flag.name)
        }
        TokenRole::Arg { arg, values } => format!("arg {} = {values:?}", arg.name),
        TokenRole::Separator => "separator".to_string(),
        TokenRole::Builtin { spelling } => format!("built-in {spelling}"),
        TokenRole::ValueTerminator { ends } => format!("value terminator, ends {ends}"),
        TokenRole::Restart => "restart".to_string(),
        TokenRole::UnknownFlag { bound_as } => match bound_as {
            Some(arg) => format!("unknown flag, bound as {}", arg.name),
            None => "unknown flag".to_string(),
        },
        TokenRole::Refused { reason } => format!("refused: {reason}"),
        TokenRole::External => "external".to_string(),
        TokenRole::Unread => "unread".to_string(),
    }
}

impl Debug for ParseOutput {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ParseOutput")
            .field("cmds", &self.cmds.iter().map(|c| &c.name).join(" ").trim())
            .field(
                "args",
                &self
                    .args
                    .iter()
                    .map(|(a, w)| format!("{}: {w}", a.name))
                    .collect_vec(),
            )
            .field(
                "available_flags",
                &self
                    .available_flags
                    .iter()
                    .map(|(f, w)| format!("{f}: {w}"))
                    .collect_vec(),
            )
            .field(
                "flags",
                &self
                    .flags
                    .iter()
                    .map(|(f, w)| format!("{}: {w}", f.name))
                    .collect_vec(),
            )
            .field("flag_awaiting_value", &self.flag_awaiting_value)
            .field("errors", &self.errors)
            .field("external", &self.external)
            // Provenance, one line per token and one per fallback. This is the parser's
            // debug channel under `USAGE_LOG=trace`, so it is where a spec author looks
            // first — `usage explain` renders the same facts for a reader.
            .field(
                "tokens",
                &self.tokens.iter().map(render_token).collect_vec(),
            )
            .field(
                "origins",
                &self
                    .flag_origins
                    .iter()
                    .map(|(f, o)| format!("{}: {o:?}", f.name))
                    .chain(
                        self.arg_origins
                            .iter()
                            .map(|(a, o)| format!("{}: {o:?}", a.name)),
                    )
                    .collect_vec(),
            )
            .field("overridden_flags", &self.overridden_flags)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::SpecFlagAction;

    fn input(words: &[&str]) -> Vec<String> {
        words.iter().map(|word| (*word).to_string()).collect()
    }

    #[test]
    fn a_declared_version_supplies_the_flag_the_help_page_lists() {
        // The page has always listed `-V, --version` wherever a `version` is declared,
        // and usage-argv and usage-go have always accepted both. This parser called them
        // unknown words, so the one implementation the corpus measures the others against
        // was the one that disagreed.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nversion \"1.2.3\"\ncmd \"run\"\n"
            .parse()
            .unwrap();

        for spelling in ["--version", "-V"] {
            let err = parse(&spec, &input(&["ex", spelling]))
                .expect_err("answering with a version ends the parse");
            assert_eq!(err.to_string(), "1.2.3", "{spelling}");
        }
    }

    #[test]
    fn the_supplied_version_flag_is_the_roots_alone() {
        // `version` describes the program, and the page lists the entry on the program's
        // own page only. A subcommand answering with it would be a claim no spec made.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nversion \"1.2.3\"\ncmd \"run\"\n"
            .parse()
            .unwrap();

        let err = parse(&spec, &input(&["ex", "run", "--version"])).unwrap_err();
        assert_eq!(err.to_string(), "unexpected word: --version");
    }

    #[test]
    fn no_declared_version_supplies_nothing() {
        // A `--version` answering with nothing is worse than one that is not there, which
        // is why the entry is conditional on the page and the spelling on the parse.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\n".parse().unwrap();

        for spelling in ["--version", "-V"] {
            let err = parse(&spec, &input(&["ex", spelling])).unwrap_err();
            assert_eq!(err.to_string(), format!("unexpected word: {spelling}"));
        }
    }

    #[test]
    fn disable_version_flag_removes_the_supplied_spellings() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nversion \"1.2.3\"\ndisable_version_flag #true\n"
            .parse()
            .unwrap();

        for spelling in ["--version", "-V"] {
            let err = parse(&spec, &input(&["ex", spelling])).unwrap_err();
            assert_eq!(err.to_string(), format!("unexpected word: {spelling}"));
        }
    }

    #[test]
    fn a_spelling_the_spec_spends_elsewhere_keeps_its_meaning() {
        // The page drops each supplied spelling the CLI claimed and keeps the other; the
        // parse agrees without being told, because a declared flag is matched first.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nversion \"1.2.3\"\nflag \"-V --verbose\"\n"
            .parse()
            .unwrap();

        let out = parse(&spec, &input(&["ex", "-V"])).expect("-V is the CLI's own flag");
        assert_eq!(out.flags.len(), 1);

        let err = parse(&spec, &input(&["ex", "--version"])).unwrap_err();
        assert_eq!(err.to_string(), "1.2.3");
    }

    #[test]
    fn the_supplied_spellings_split_the_two_version_texts() {
        // The same split `render_action_err` gives a declared version flag: the long
        // spelling prefers `long_version`, the short prefers the concise one.
        let spec: Spec =
            "name \"ex\"\nbin \"ex\"\nversion \"1.2.3\"\nlong_version \"1.2.3 (abcdef)\"\n"
                .parse()
                .unwrap();

        assert_eq!(
            parse(&spec, &input(&["ex", "--version"]))
                .unwrap_err()
                .to_string(),
            "1.2.3 (abcdef)"
        );
        assert_eq!(
            parse(&spec, &input(&["ex", "-V"])).unwrap_err().to_string(),
            "1.2.3"
        );
    }

    #[test]
    fn a_supplied_short_is_a_letter_a_bundle_may_contain() {
        // `-h` and `-V` are recognized letters that no spec declares, so a token holding
        // one beside a declared letter is a bundle. usage-lib alone read `-vh` as a word
        // naming nothing: usage-argv and usage-go both resolve the letter through the
        // same lookup that finds a declared short, and clap prints help for it too.
        let spec: Spec =
            "name \"ex\"\nbin \"ex\"\nversion \"1.2.3\"\nflag \"-v --verbose\"\ncmd \"run\"\n"
                .parse()
                .unwrap();

        for token in ["-vh", "-hv"] {
            let err = parse(&spec, &input(&["ex", token])).expect_err("help ends the parse");
            assert!(err.to_string().starts_with("ex 1.2.3"), "{token}: {err}");
        }
        for token in ["-vV", "-Vv"] {
            let err = parse(&spec, &input(&["ex", token])).expect_err("a version ends it too");
            assert_eq!(err.to_string(), "1.2.3", "{token}");
        }
    }

    #[test]
    fn a_bundled_help_letter_asks_for_the_short_page() {
        // Whatever else shares the token: `-h` is the short spelling, and the letters
        // beside it say nothing about which page was asked for.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"-v --verbose\" help=\"Be loud\" {\n    long_help \"Be loud, and say so at length.\"\n}\n"
            .parse()
            .unwrap();

        let short = parse(&spec, &input(&["ex", "-vh"]))
            .unwrap_err()
            .to_string();
        let long = parse(&spec, &input(&["ex", "--help"]))
            .unwrap_err()
            .to_string();
        assert!(short.contains("Be loud"), "{short}");
        assert!(!short.contains("at length"), "{short}");
        assert!(long.contains("at length"), "{long}");
    }

    #[test]
    fn the_bundled_version_letter_is_the_roots_alone() {
        // The same rule the whole-token spelling follows, asked one letter at a time.
        let spec: Spec =
            "name \"ex\"\nbin \"ex\"\nversion \"1.2.3\"\nflag \"-v --verbose\" global=#true\ncmd \"run\"\n"
                .parse()
                .unwrap();

        let err = parse(&spec, &input(&["ex", "run", "-vV"])).unwrap_err();
        assert_eq!(err.to_string(), "unexpected word: -vV");
    }

    #[test]
    fn a_declared_letter_keeps_its_meaning_inside_a_bundle() {
        // Nothing is supplied where the CLI spent the letter itself, so `-vh local` is
        // this spec's own `-h`, taking its value from the rest of the token.
        let spec: Spec =
            "name \"ex\"\nbin \"ex\"\nversion \"1.2.3\"\nflag \"-v --verbose\"\nflag \"-h --host <host>\"\n"
                .parse()
                .unwrap();

        let out = parse(&spec, &input(&["ex", "-vhlocal"])).expect("a bundle and its value");
        assert_eq!(out.flags.len(), 2);
        assert!(out
            .flags
            .iter()
            .any(|(flag, value)| flag.name == "host" && value.to_string() == "local"));
    }

    #[test]
    fn disabling_help_takes_the_letter_back_out_of_the_bundle() {
        let spec: Spec =
            "name \"ex\"\nbin \"ex\"\ndisable_help_flag #true\nflag \"-v --verbose\"\n"
                .parse()
                .unwrap();

        let err = parse(&spec, &input(&["ex", "-vh"])).unwrap_err();
        assert_eq!(err.to_string(), "unexpected word: -vh");
    }

    #[test]
    fn a_letter_nothing_supplies_still_refuses_the_whole_bundle() {
        // The rule this must not weaken: `-az` is not a bundle at all, so `-a` is not set
        // on the way to discovering that `z` names nothing.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"-a --all\"\narg \"[file]\"\n"
            .parse()
            .unwrap();

        let out = parse(&spec, &input(&["ex", "-az"])).expect("it falls through to the argument");
        assert!(out.flags.is_empty(), "{:?}", out.flags);
        assert_eq!(out.args.len(), 1);
    }

    fn spec_with_arg(arg: SpecArg) -> Spec {
        let cmd = SpecCommand::builder().name("test").arg(arg).build();
        Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        }
    }

    fn spec_with_flag(flag: SpecFlag) -> Spec {
        let cmd = SpecCommand::builder().name("test").flag(flag).build();
        Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        }
    }

    fn parse_with_env(
        spec: &Spec,
        words: &[&str],
        env: &[(&str, &str)],
    ) -> Result<ParseOutput, miette::Error> {
        let env = env
            .iter()
            .map(|(k, v)| ((*k).to_string(), (*v).to_string()))
            .collect();
        Parser::new(spec).with_env(env).parse(&input(words))
    }

    fn first_string_value(parsed: &ParseOutput) -> &str {
        if let Some(ParseValue::String(value)) = parsed.args.values().next() {
            return value;
        }
        if let Some(ParseValue::String(value)) = parsed.flags.values().next() {
            return value;
        }
        panic!("expected first parsed value to be ParseValue::String");
    }

    #[test]
    fn custom_environment_parser_dispatches_executable_views() {
        let spec: Spec = r#"
bin "ex"
view "runner" root="run"
cmd "run" {
    flag "--token <token>" env="TOKEN"
}
        "#
        .parse()
        .unwrap();
        let parsed = Parser::new(&spec)
            .with_env([("TOKEN".to_string(), "secret".to_string())].into())
            .parse(&input(&["runner"]))
            .unwrap();

        assert_eq!(parsed.cmd.name, "runner");
        assert!(parsed.flags.iter().any(|(flag, value)| flag.name == "token"
            && matches!(value, ParseValue::String(value) if value == "secret")));
    }

    #[test]
    fn an_executable_view_keeps_the_hosts_version_action() {
        let spec: Spec = r#"
bin "ex"
version "1.2.3"
flag "-V --version" action="version"
flag "--verbose" global=#true
view "runner" root="run" globals=#true
cmd "run"
        "#
        .parse()
        .unwrap();

        let error = Parser::new(&spec)
            .parse(&input(&["runner", "--version"]))
            .expect_err("the host version action should answer before view projection");
        assert_eq!(error.to_string(), "1.2.3");

        let error = Parser::new(&spec)
            .parse(&input(&["runner", "--verbose", "--version"]))
            .expect_err("the host version action should remain after a carried global");
        assert_eq!(error.to_string(), "1.2.3");
    }

    fn flag_string_value<'a>(parsed: &'a ParseOutput, name: &str) -> &'a str {
        let flag = parsed
            .flags
            .keys()
            .find(|flag| flag.name == name)
            .unwrap_or_else(|| panic!("expected flag {name}"));
        let value = parsed
            .flags
            .get(flag)
            .unwrap_or_else(|| panic!("expected value for flag {name}"));
        match value {
            ParseValue::String(value) => value,
            _ => panic!("expected flag {name} to be ParseValue::String"),
        }
    }

    fn assert_parse_err(result: Result<ParseOutput, miette::Error>, expected: &str) {
        let err = result.expect_err("expected parser error");
        assert_eq!(format!("{err}"), expected);
    }

    #[test]
    fn a_short_version_action_falls_back_to_the_long_version() {
        let flag = SpecFlag::builder()
            .short('R')
            .action(SpecFlagAction::Version)
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            long_version: Some("1.2.3\ncommit abc123".to_string()),
            ..Default::default()
        };
        let UsageErr::Version(version) = render_action_err(&spec, &spec.cmd, &flag, "-R") else {
            panic!("expected version action")
        };
        assert_eq!(version, "1.2.3\ncommit abc123");
    }

    #[cfg(feature = "unstable_choices_env")]
    fn spec_arg_choices_env(key: &str) -> Spec {
        spec_with_arg(
            SpecArg::builder()
                .name("env")
                .choices_env(key)
                .required(false)
                .build(),
        )
    }

    #[cfg(feature = "unstable_choices_env")]
    fn spec_flag_choices_env(key: &str) -> Spec {
        spec_with_flag(
            SpecFlag::builder()
                .long("env")
                .arg(SpecArg::builder().name("env").choices_env(key).build())
                .build(),
        )
    }

    #[test]
    fn test_parse() {
        let cmd = SpecCommand::builder()
            .name("test")
            .arg(SpecArg::builder().name("arg").build())
            .flag(SpecFlag::builder().long("flag").build())
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };
        let input = vec!["test".to_string(), "arg1".to_string(), "--flag".to_string()];
        let parsed = parse(&spec, &input).unwrap();
        assert_eq!(parsed.cmds.len(), 1);
        assert_eq!(parsed.cmds[0].name, "test");
        assert_eq!(parsed.args.len(), 1);
        assert_eq!(parsed.flags.len(), 1);
        assert_eq!(parsed.available_flags.len(), 1);
    }

    #[test]
    fn test_flag_overrides_last_occurrence_wins() {
        let spec: Spec = r#"
flag "--stdin" default=#true
flag "--file <file>" overrides="--stdin"
        "#
        .parse()
        .unwrap();

        let file_wins = parse(&spec, &input(&["test", "--stdin", "--file", "input.txt"])).unwrap();
        assert_eq!(file_wins.flags.len(), 1);
        assert_eq!(flag_string_value(&file_wins, "file"), "input.txt");
        assert!(!file_wins.flags.keys().any(|flag| flag.name == "stdin"));

        let stdin_wins = parse(&spec, &input(&["test", "--file", "input.txt", "--stdin"])).unwrap();
        assert_eq!(stdin_wins.flags.len(), 1);
        assert!(stdin_wins.flags.keys().any(|flag| flag.name == "stdin"));
        assert!(!stdin_wins.flags.keys().any(|flag| flag.name == "file"));
    }

    #[test]
    fn test_flag_override_clears_pending_value() {
        let spec: Spec = r#"
flag "--file <file>" overrides="--stdin"
flag "--stdin"
arg "[input]"
        "#
        .parse()
        .unwrap();

        let parsed = parse(&spec, &input(&["test", "--file", "--stdin", "input.txt"])).unwrap();
        assert_eq!(parsed.flags.len(), 1);
        assert!(parsed.flags.keys().any(|flag| flag.name == "stdin"));
        assert_eq!(first_string_value(&parsed), "input.txt");
    }

    #[cfg(unix)]
    #[test]
    fn a_mount_on_the_root_discovers_subcommands() {
        // The root is a command like any other, so it can find its own subcommands
        // by running something. Uses `echo` rather than a fixture because resolving
        // a mount is what is being tested.
        let spec: Spec = r#"
name "ex"
bin "ex"
cmd "declared"
mount run="echo 'cmd \"discovered\"'"
"#
        .parse()
        .unwrap();

        let out = parse(&spec, &["ex".to_string(), "discovered".to_string()]).unwrap();
        assert_eq!(out.cmd.name, "discovered");
    }

    #[test]
    fn injected_mount_outputs_are_complete_and_never_fall_back_to_processes() {
        let spec: Spec = r#"
name "ex"
bin "ex"
mount run="this command must never run"
cmd "declared"
"#
        .parse()
        .unwrap();

        Parser::new(&spec)
            .with_mount_outputs(HashMap::new())
            .parse(&input(&["ex", "declared"]))
            .expect("a declared command does not resolve the mount");

        let error = Parser::new(&spec)
            .with_mount_outputs(HashMap::new())
            .parse(&input(&["ex", "discovered"]))
            .unwrap_err();
        assert!(
            error
                .to_string()
                .contains("No injected output was provided for mount command"),
            "{error}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn completion_sees_root_mounted_commands_with_nothing_typed() {
        // The case a root mount exists for. `mycli <tab>` has no word to trigger
        // discovery with, so a completion has to resolve up front or the mounted
        // commands are never offered.
        let spec: Spec = r#"
name "ex"
bin "ex"
cmd "declared"
mount run="echo 'cmd \"discovered\"'"
"#
        .parse()
        .unwrap();

        let out = parse_partial(&spec, &["ex".to_string()]).unwrap();
        assert!(
            out.cmd.subcommands.contains_key("discovered"),
            "a completion should see mounted commands; got {:?}",
            out.cmd.subcommands.keys().collect::<Vec<_>>()
        );
    }

    #[cfg(unix)]
    #[test]
    fn completion_and_execution_agree_about_discovery() {
        // Offering a command that a real parse would hand to the default instead is
        // worse than not offering it, so the gate applies to both paths. The mount
        // fails if it runs, which is how both halves are checked at once.
        let spec: Spec = r#"
name "ex"
bin "ex"
default_subcommand "run"
cmd "run" {
  arg "<task>"
}
mount run="exit 1"
"#
        .parse()
        .unwrap();

        let out = parse_partial(&spec, &["ex".to_string()]).unwrap();
        assert!(
            !out.cmd.subcommands.contains_key("discovered"),
            "a completion must not offer what execution will not route"
        );

        let out = parse(&spec, &["ex".to_string(), "mytask".to_string()]).unwrap();
        assert_eq!(out.cmd.name, "run");
    }

    #[cfg(unix)]
    #[test]
    fn a_default_subcommand_outranks_discovery() {
        // The default already says what an unmatched word means, and says it for
        // free. The mount fails if it runs, so parsing proves discovery was skipped.
        let spec: Spec = r#"
name "ex"
bin "ex"
default_subcommand "run"
cmd "run" {
  arg "<task>"
}
mount run="exit 1"
"#
        .parse()
        .unwrap();

        let out = parse(&spec, &["ex".to_string(), "mytask".to_string()]).unwrap();
        assert_eq!(out.cmd.name, "run");
    }

    #[cfg(unix)]
    #[test]
    fn a_mount_may_ask_to_outrank_the_default() {
        // Opting in, and paying for it: discovery runs first, so a discovered
        // command wins over the fallback.
        let spec: Spec = r#"
name "ex"
bin "ex"
default_subcommand "run"
cmd "run" {
  arg "<task>"
}
mount run="echo 'cmd \"discovered\"'" overrides_default=#true
"#
        .parse()
        .unwrap();

        let out = parse(&spec, &["ex".to_string(), "discovered".to_string()]).unwrap();
        assert_eq!(out.cmd.name, "discovered");

        // A word it does not know still reaches the default.
        let out = parse(&spec, &["ex".to_string(), "mytask".to_string()]).unwrap();
        assert_eq!(out.cmd.name, "run");
    }

    #[cfg(unix)]
    #[test]
    fn a_flag_does_not_run_the_mount() {
        // A flag matches no subcommand, which would have been enough to trigger
        // discovery — so `ex --help` spawned a process. The mount fails if it runs,
        // so parsing at all is the proof that it did not.
        let spec: Spec = r#"
name "ex"
bin "ex"
flag "--verbose"
cmd "declared"
mount run="exit 1"
"#
        .parse()
        .unwrap();

        let out = parse(&spec, &["ex".to_string(), "--verbose".to_string()]).unwrap();
        assert_eq!(out.cmd.name, "ex");
    }

    #[cfg(unix)]
    #[test]
    fn a_declared_subcommand_does_not_run_the_mount() {
        // The mount would fail if it ran, so this parsing at all is the proof that
        // discovery is skipped when the word is already known. Worth pinning: a root
        // mount that resolved eagerly would spawn a process on every invocation.
        let spec: Spec = r#"
name "ex"
bin "ex"
cmd "declared"
mount run="exit 1"
"#
        .parse()
        .unwrap();

        let out = parse(&spec, &["ex".to_string(), "declared".to_string()]).unwrap();
        assert_eq!(out.cmd.name, "declared");
    }

    #[test]
    fn a_root_mount_survives_being_written_out() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nmount run=\"ex plugins --usage\"\n"
            .parse()
            .unwrap();
        assert_eq!(spec.cmd.mounts.len(), 1);

        let reparsed: Spec = spec.to_string().parse().unwrap();
        assert_eq!(reparsed.cmd.mounts.len(), 1, "written:\n{spec}");
        assert_eq!(reparsed.cmd.mounts[0].run, "ex plugins --usage");
    }

    #[test]
    fn test_mount_prefix_applies_flag_overrides() {
        let stdin = Arc::new(
            SpecFlag::builder()
                .name("stdin")
                .long("stdin")
                .global(true)
                .build(),
        );
        let file = Arc::new(
            SpecFlag::builder()
                .name("file")
                .long("file")
                .arg(SpecArg::builder().name("file").build())
                .global(true)
                .overrides_with(vec!["--stdin".to_string()])
                .build(),
        );
        let mut prefix_flags = vec![(stdin, vec!["--stdin".to_string()])];

        apply_prefix_flag_overrides(&mut prefix_flags, Arc::clone(&file));
        prefix_flags.push((file, vec!["--file".to_string(), "input.txt".to_string()]));

        assert_eq!(mount_prefix_words(&prefix_flags), ["--file", "input.txt"]);
    }

    #[test]
    fn test_flag_override_suppresses_env_value() {
        let spec: Spec = r#"
flag "--stdin" env="USE_STDIN"
flag "--file <file>" overrides="--stdin"
        "#
        .parse()
        .unwrap();

        let parsed = parse_with_env(
            &spec,
            &["test", "--file", "input.txt"],
            &[("USE_STDIN", "true")],
        )
        .unwrap();
        assert_eq!(parsed.flags.len(), 1);
        assert_eq!(flag_string_value(&parsed, "file"), "input.txt");
    }

    #[test]
    fn test_flag_override_suppresses_required_check() {
        let spec: Spec = r#"
flag "--stdin" required=#true
flag "--file <file>" overrides="--stdin"
        "#
        .parse()
        .unwrap();

        let parsed = parse(&spec, &input(&["test", "--file", "input.txt"])).unwrap();
        assert_eq!(parsed.flags.len(), 1);
        assert_eq!(flag_string_value(&parsed, "file"), "input.txt");
    }

    #[test]
    fn test_flag_required_if() {
        let spec: Spec = r#"
flag "--dir <dir>"
flag "--file <file>" required_if="--dir"
        "#
        .parse()
        .unwrap();

        parse(&spec, &input(&["test"])).unwrap();
        assert_parse_err(
            parse(&spec, &input(&["test", "--dir", "src"])),
            "Missing required flag: --file <file>",
        );
        parse(
            &spec,
            &input(&["test", "--dir", "src", "--file", "input.txt"]),
        )
        .unwrap();
    }

    #[test]
    fn test_flag_required_unless() {
        let spec: Spec = r#"
flag "--stdin"
flag "--file <file>" required_unless="--stdin"
        "#
        .parse()
        .unwrap();

        assert_parse_err(
            parse(&spec, &input(&["test"])),
            "Missing required flag: --file <file>",
        );
        parse(&spec, &input(&["test", "--stdin"])).unwrap();
        parse(&spec, &input(&["test", "--file", "input.txt"])).unwrap();
    }

    #[test]
    fn complete_required_relationship_truth_tables() {
        let spec: Spec = r#"
name "ex"
bin "ex"
flag "--mode <mode>"
flag "--scope <scope>"
flag "--token <token>" {
    required_if_eq "--mode" "remote"
}
flag "--approval <approval>" {
    required_if_eq_all "--mode" "remote" "--scope" "global"
}
flag "--input <input>" {
    required_unless "--stdin" "--file"
}
flag "--checksum <checksum>" {
    required_unless_all "--stdin" "--file"
}
flag "--stdin"
flag "--file <file>"
arg "[request]" {
    requires "--mode" "--scope"
}
"#
        .parse()
        .unwrap();
        let parse_args = |args: &[&str]| {
            parse(
                &spec,
                &args
                    .iter()
                    .map(|arg| (*arg).to_string())
                    .collect::<Vec<_>>(),
            )
        };

        assert!(parse_args(&["ex", "--mode", "remote", "--stdin"]).is_err());
        assert!(parse_args(&[
            "ex", "--mode", "remote", "--token", "secret", "--scope", "global", "--stdin",
        ])
        .is_err());
        parse_args(&[
            "ex",
            "--mode",
            "remote",
            "--token",
            "secret",
            "--scope",
            "global",
            "--approval",
            "yes",
            "--stdin",
            "--file",
            "in",
        ])
        .unwrap();
        parse_args(&[
            "ex",
            "--mode",
            "local",
            "--scope",
            "project",
            "--stdin",
            "--checksum",
            "sum",
            "request.json",
        ])
        .unwrap();

        let reparsed: Spec = spec.to_string().parse().unwrap();
        assert_eq!(reparsed.cmd.flags[2].required_if_eq.len(), 1);
        assert_eq!(reparsed.cmd.flags[3].required_if_eq_all.len(), 2);
        assert_eq!(reparsed.cmd.flags[5].required_unless_all.len(), 2);
        assert_eq!(reparsed.cmd.args[0].requires.len(), 2);
    }

    #[test]
    fn test_conditional_requirements_treat_env_as_explicit() {
        let spec: Spec = r#"
flag "--dir <dir>" env="INPUT_DIR"
flag "--stdin" env="USE_STDIN"
flag "--file <file>" required_if="--dir" required_unless="--stdin"
        "#
        .parse()
        .unwrap();

        assert_parse_err(
            parse_with_env(&spec, &["test"], &[("INPUT_DIR", "src")]),
            "Missing required flag: --file <file>",
        );
        parse_with_env(&spec, &["test"], &[("USE_STDIN", "true")]).unwrap();
    }

    #[test]
    fn test_custom_env_does_not_fall_back_to_process_env() {
        assert!(std::env::var("PATH").is_ok());
        let spec: Spec = r#"flag "--file <file>" env="PATH" required=#true"#.parse().unwrap();

        assert_parse_err(
            parse_with_env(&spec, &["test"], &[]),
            "Missing required flag: --file <file>",
        );
    }

    #[test]
    fn test_conditional_requirements_ignore_defaults_on_condition_flags() {
        let spec: Spec = r#"
flag "--dir <dir>" default="src"
flag "--file <file>" required_if="--dir"
        "#
        .parse()
        .unwrap();

        parse(&spec, &input(&["test"])).unwrap();
    }

    #[test]
    fn test_conditional_requirements_see_overridden_flags_as_absent() {
        let spec: Spec = r#"
flag "--stdin"
flag "--dir <dir>" overrides="--stdin"
flag "--file <file>" required_unless="--stdin"
        "#
        .parse()
        .unwrap();

        assert_parse_err(
            parse(&spec, &input(&["test", "--stdin", "--dir", "src"])),
            "Missing required flag: --file <file>",
        );
    }

    #[test]
    fn short_flag_is_one_character_not_one_byte() {
        // A short is declared and read by character. Counting bytes instead either
        // refuses the declaration or slices the token inside the character, and clap
        // — which many specs are generated from — accepts shorts like this one.
        let spec = spec_with_flag(
            SpecFlag::builder()
                .short('')
                .long("polish")
                .arg(SpecArg::builder().name("opt").build())
                .build(),
        );
        let attached = Parser::new(&spec)
            .parse(&input(&["test", "-磨VALUE"]))
            .unwrap();
        assert_eq!(flag_string_value(&attached, "polish"), "VALUE");
        let detached = Parser::new(&spec)
            .parse(&input(&["test", "-磨", "V"]))
            .unwrap();
        assert_eq!(flag_string_value(&detached, "polish"), "V");
    }

    #[test]
    fn test_as_env() {
        let cmd = SpecCommand::builder()
            .name("test")
            .arg(SpecArg::builder().name("arg").build())
            .flag(SpecFlag::builder().long("flag").build())
            .flag(
                SpecFlag::builder()
                    .long("force")
                    .negate("--no-force")
                    .build(),
            )
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };
        let input = vec![
            "test".to_string(),
            "--flag".to_string(),
            "--no-force".to_string(),
        ];
        let parsed = parse(&spec, &input).unwrap();
        let env = parsed.as_env();
        assert_eq!(env.len(), 2);
        assert_eq!(env.get("usage_flag"), Some(&"true".to_string()));
        assert_eq!(env.get("usage_force"), Some(&"false".to_string()));
    }

    #[test]
    fn test_arg_env_var() {
        let cmd = SpecCommand::builder()
            .name("test")
            .arg(
                SpecArg::builder()
                    .name("input")
                    .env("TEST_ARG_INPUT")
                    .required(true)
                    .build(),
            )
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // Set env var
        std::env::set_var("TEST_ARG_INPUT", "test_file.txt");

        let input = vec!["test".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        assert_eq!(parsed.args.len(), 1);
        let arg = parsed.args.keys().next().unwrap();
        assert_eq!(arg.name, "input");
        let value = parsed.args.values().next().unwrap();
        assert_eq!(value.to_string(), "test_file.txt");

        // Clean up
        std::env::remove_var("TEST_ARG_INPUT");
    }

    #[test]
    fn test_flag_env_var_with_arg() {
        let cmd = SpecCommand::builder()
            .name("test")
            .flag(
                SpecFlag::builder()
                    .long("output")
                    .env("TEST_FLAG_OUTPUT")
                    .arg(SpecArg::builder().name("file").build())
                    .build(),
            )
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // Set env var
        std::env::set_var("TEST_FLAG_OUTPUT", "output.txt");

        let input = vec!["test".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        assert_eq!(parsed.flags.len(), 1);
        let flag = parsed.flags.keys().next().unwrap();
        assert_eq!(flag.name, "output");
        let value = parsed.flags.values().next().unwrap();
        assert_eq!(value.to_string(), "output.txt");

        // Clean up
        std::env::remove_var("TEST_FLAG_OUTPUT");
    }

    #[test]
    fn test_flag_env_var_boolean() {
        let cmd = SpecCommand::builder()
            .name("test")
            .flag(
                SpecFlag::builder()
                    .long("verbose")
                    .env("TEST_FLAG_VERBOSE")
                    .build(),
            )
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // Set env var to true
        std::env::set_var("TEST_FLAG_VERBOSE", "true");

        let input = vec!["test".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        assert_eq!(parsed.flags.len(), 1);
        let flag = parsed.flags.keys().next().unwrap();
        assert_eq!(flag.name, "verbose");
        let value = parsed.flags.values().next().unwrap();
        assert_eq!(value.to_string(), "true");

        // Clean up
        std::env::remove_var("TEST_FLAG_VERBOSE");
    }

    #[test]
    fn test_env_var_precedence() {
        // CLI args should take precedence over env vars
        let cmd = SpecCommand::builder()
            .name("test")
            .arg(
                SpecArg::builder()
                    .name("input")
                    .env("TEST_PRECEDENCE_INPUT")
                    .required(true)
                    .build(),
            )
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // Set env var
        std::env::set_var("TEST_PRECEDENCE_INPUT", "env_file.txt");

        let input = vec!["test".to_string(), "cli_file.txt".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        assert_eq!(parsed.args.len(), 1);
        let value = parsed.args.values().next().unwrap();
        // CLI arg should take precedence
        assert_eq!(value.to_string(), "cli_file.txt");

        // Clean up
        std::env::remove_var("TEST_PRECEDENCE_INPUT");
    }

    #[test]
    fn test_flag_var_true_with_single_default() {
        // When var=true and default="bar", the default should be MultiString(["bar"])
        let cmd = SpecCommand::builder()
            .name("test")
            .flag(
                SpecFlag::builder()
                    .long("foo")
                    .var(true)
                    .arg(SpecArg::builder().name("foo").build())
                    .default_value("bar")
                    .build(),
            )
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // User doesn't provide the flag
        let input = vec!["test".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        assert_eq!(parsed.flags.len(), 1);
        let flag = parsed.flags.keys().next().unwrap();
        assert_eq!(flag.name, "foo");
        let value = parsed.flags.values().next().unwrap();
        // Should be MultiString, not String
        match value {
            ParseValue::MultiString(v) => {
                assert_eq!(v.len(), 1);
                assert_eq!(v[0], "bar");
            }
            _ => panic!("Expected MultiString, got {:?}", value),
        }
    }

    #[test]
    fn test_flag_var_true_with_multiple_defaults() {
        // When var=true and multiple defaults, should return MultiString(["xyz", "bar"])
        let cmd = SpecCommand::builder()
            .name("test")
            .flag(
                SpecFlag::builder()
                    .long("foo")
                    .var(true)
                    .arg(SpecArg::builder().name("foo").build())
                    .default_values(["xyz", "bar"])
                    .build(),
            )
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // User doesn't provide the flag
        let input = vec!["test".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        assert_eq!(parsed.flags.len(), 1);
        let value = parsed.flags.values().next().unwrap();
        // Should be MultiString with both values
        match value {
            ParseValue::MultiString(v) => {
                assert_eq!(v.len(), 2);
                assert_eq!(v[0], "xyz");
                assert_eq!(v[1], "bar");
            }
            _ => panic!("Expected MultiString, got {:?}", value),
        }
    }

    #[test]
    fn test_flag_var_false_with_default_remains_string() {
        // When var=false (default), the default should still be String("bar")
        let cmd = SpecCommand::builder()
            .name("test")
            .flag(
                SpecFlag::builder()
                    .long("foo")
                    .var(false) // Default behavior
                    .arg(SpecArg::builder().name("foo").build())
                    .default_value("bar")
                    .build(),
            )
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // User doesn't provide the flag
        let input = vec!["test".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        assert_eq!(parsed.flags.len(), 1);
        let value = parsed.flags.values().next().unwrap();
        // Should be String, not MultiString
        match value {
            ParseValue::String(s) => {
                assert_eq!(s, "bar");
            }
            _ => panic!("Expected String, got {:?}", value),
        }
    }

    #[test]
    fn test_arg_var_true_with_single_default() {
        // When arg has var=true and default="bar", the default should be MultiString(["bar"])
        let cmd = SpecCommand::builder()
            .name("test")
            .arg(
                SpecArg::builder()
                    .name("files")
                    .var(true)
                    .default_value("default.txt")
                    .required(false)
                    .build(),
            )
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // User doesn't provide the arg
        let input = vec!["test".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        assert_eq!(parsed.args.len(), 1);
        let value = parsed.args.values().next().unwrap();
        // Should be MultiString, not String
        match value {
            ParseValue::MultiString(v) => {
                assert_eq!(v.len(), 1);
                assert_eq!(v[0], "default.txt");
            }
            _ => panic!("Expected MultiString, got {:?}", value),
        }
    }

    #[test]
    fn test_arg_var_true_with_multiple_defaults() {
        // When arg has var=true and multiple defaults
        let cmd = SpecCommand::builder()
            .name("test")
            .arg(
                SpecArg::builder()
                    .name("files")
                    .var(true)
                    .default_values(["file1.txt", "file2.txt"])
                    .required(false)
                    .build(),
            )
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // User doesn't provide the arg
        let input = vec!["test".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        assert_eq!(parsed.args.len(), 1);
        let value = parsed.args.values().next().unwrap();
        // Should be MultiString with both values
        match value {
            ParseValue::MultiString(v) => {
                assert_eq!(v.len(), 2);
                assert_eq!(v[0], "file1.txt");
                assert_eq!(v[1], "file2.txt");
            }
            _ => panic!("Expected MultiString, got {:?}", value),
        }
    }

    #[test]
    fn test_arg_var_false_with_default_remains_string() {
        // When arg has var=false (default), the default should still be String
        let cmd = SpecCommand::builder()
            .name("test")
            .arg(
                SpecArg::builder()
                    .name("file")
                    .var(false)
                    .default_value("default.txt")
                    .required(false)
                    .build(),
            )
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // User doesn't provide the arg
        let input = vec!["test".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        assert_eq!(parsed.args.len(), 1);
        let value = parsed.args.values().next().unwrap();
        // Should be String, not MultiString
        match value {
            ParseValue::String(s) => {
                assert_eq!(s, "default.txt");
            }
            _ => panic!("Expected String, got {:?}", value),
        }
    }

    #[test]
    fn test_scalar_defaults_validate_only_first_default_choice() {
        let specs = [
            spec_with_arg(
                SpecArg::builder()
                    .name("env")
                    .var(false)
                    .default_values(["dev", "prod"])
                    .choices(["dev"])
                    .required(false)
                    .build(),
            ),
            spec_with_flag(
                SpecFlag::builder()
                    .long("env")
                    .arg(
                        SpecArg::builder()
                            .name("env")
                            .default_values(["dev", "prod"])
                            .choices(["dev"])
                            .build(),
                    )
                    .build(),
            ),
        ];

        for spec in specs {
            let parsed = parse(&spec, &input(&["test"])).unwrap();
            assert_eq!(first_string_value(&parsed), "dev");
        }
    }

    #[test]
    fn a_delimiter_turns_one_word_into_several_values() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--tags <tag>\" var=#true delimiter=\",\"\narg \"[files]...\" var=#true delimiter=\":\"\n"
            .parse()
            .unwrap();

        let parsed = parse(&spec, &input(&["ex", "--tags", "a,b,c", "x:y"])).unwrap();
        let multi = |value: &ParseValue| match value {
            ParseValue::MultiString(values) => values.clone(),
            other => panic!("expected several values, got {other:?}"),
        };
        let tags = parsed
            .flags
            .iter()
            .find(|(f, _)| f.name == "tags")
            .map(|(_, v)| v)
            .unwrap();
        assert_eq!(multi(tags), vec!["a", "b", "c"]);
        assert_eq!(multi(parsed.args.values().next().unwrap()), vec!["x", "y"]);
    }

    #[test]
    fn a_positional_splits_before_its_choices_are_asked() {
        // The flag path did this and the positional path did not, so a word whose parts
        // were all choices was rejected as one value, and a bad half was reported as the
        // whole word.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\narg \"[paths]...\" var=#true delimiter=\":\" {\n  choices \"src\" \"docs\"\n}\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "src:docs"])).expect("both halves are choices");

        let err = parse(&spec, &input(&["ex", "src:nowhere"])).unwrap_err();
        let message = err.to_string();
        assert!(message.contains("nowhere"), "{message}");
        assert!(
            !message.contains("src:nowhere"),
            "the bad half should be named, not the whole word: {message}"
        );
    }

    #[test]
    fn a_split_value_is_counted_and_judged_as_values() {
        // Split during the parse rather than after it, so everything downstream sees the
        // values the user meant rather than the words they typed: `choices` judges each
        // one, and the bounds count them.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--env <e>\" var=#true delimiter=\",\" var_max=2 {\n  choices \"dev\" \"prod\"\n}\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "--env", "dev,prod"])).expect("two values, both allowed");
        let err = parse(&spec, &input(&["ex", "--env", "dev,staging"])).unwrap_err();
        assert!(err.to_string().contains("staging"), "{err}");
        assert!(
            parse(&spec, &input(&["ex", "--env", "dev,prod,dev"])).is_err(),
            "three values should breach var_max=2"
        );
    }

    #[test]
    fn a_split_bound_counts_one_occurrence_at_a_time() {
        // The bound on a variadic flag *argument* is what one occurrence may take. Without a
        // delimiter the collection simply stops at it, so it could never be exceeded; a word
        // carrying several values can carry an occurrence past it in one step, and that is
        // the only way this bound is ever breached.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--include <pattern>...\" delimiter=\",\" {\n  arg \"<pattern>...\" var=#true var_max=2\n}\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "--include", "a,b"])).expect("exactly the bound is fine");
        assert!(
            parse(&spec, &input(&["ex", "--include", "a,b,c"])).is_err(),
            "three values out of one word is still three values"
        );
        // The rule the corpus documents for plain words, on split ones: a second occurrence
        // starts counting again rather than adding to the first.
        parse(
            &spec,
            &input(&["ex", "--include", "a,b", "--include", "c,d"]),
        )
        .expect("two per occurrence, twice, is within the bound");
    }

    #[test]
    fn a_nested_minimum_is_checked_once_per_flag_occurrence() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--pair <value>...\" {\n  arg \"<value>...\" var=#true var_min=2 var_max=2\n}\n"
            .parse()
            .unwrap();

        parse(
            &spec,
            &input(&["ex", "--pair", "a", "b", "--pair", "c", "d"]),
        )
        .expect("each occurrence satisfies the bound independently");

        let error = parse(&spec, &input(&["ex", "--pair", "a", "--pair", "b", "c"])).unwrap_err();
        assert!(
            error
                .to_string()
                .contains("requires at least 2 value(s), got 1"),
            "{error:?}"
        );
    }

    #[test]
    fn an_exclusive_flag_has_to_be_alone() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--dump\" exclusive=#true\nflag \"--verbose\"\narg \"[target]\"\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "--dump"])).expect("alone is the point");

        // Any other flag.
        let err = parse(&spec, &input(&["ex", "--dump", "--verbose"])).unwrap_err();
        assert!(err.to_string().contains("on its own"), "{err}");

        // And a positional, which is what makes this more than a conflict with every
        // other flag.
        let err = parse(&spec, &input(&["ex", "--dump", "t"])).unwrap_err();
        assert!(err.to_string().contains("on its own"), "{err}");

        // Not given, so it imposes nothing.
        parse(&spec, &input(&["ex", "--verbose", "t"])).expect("without it, nothing changes");
    }

    #[test]
    fn an_exclusive_flag_is_not_disturbed_by_a_default() {
        // Only what was supplied counts, as `conflicts` reads it. A default counting as
        // company would make an exclusive flag unusable on any command that has one.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--dump\" exclusive=#true\nflag \"--jobs <n>\" default=\"4\"\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "--dump"])).expect("a default is nobody saying anything");
        assert!(parse(&spec, &input(&["ex", "--dump", "--jobs", "8"])).is_err());
    }

    #[test]
    fn an_exclusive_flag_bypasses_required_siblings() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--dump\" exclusive=#true\nflag \"--out <path>\" required=#true\narg \"<target>\"\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "--dump"]))
            .expect("exclusive is the command's requiredness escape");
        assert!(parse(
            &spec,
            &input(&["ex", "--dump", "--out", "somewhere", "target"])
        )
        .is_err());
    }

    #[test]
    fn an_environment_value_counts_for_an_exclusive_flag() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--dump\" exclusive=#true\nflag \"--out <path>\" env=\"EX_OUT\"\n"
            .parse()
            .unwrap();

        assert!(parse_with_env(&spec, &["ex", "--dump"], &[("EX_OUT", "somewhere")]).is_err());
        parse_with_env(&spec, &["ex", "--dump"], &[]).expect("without the value it is alone");
    }

    #[test]
    fn a_selected_subcommand_counts_for_an_ancestor_exclusive_flag() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--version\" global=#true exclusive=#true\ncmd \"run\"\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "--version"])).expect("alone is allowed");
        assert!(parse(&spec, &input(&["ex", "--version", "run"])).is_err());
    }

    #[test]
    fn a_child_exclusive_flag_is_not_mistaken_for_a_same_named_parent_flag() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--clean\" exclusive=#true\ncmd \"run\" {\n  flag \"--clean\" exclusive=#true\n}\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "run", "--clean"]))
            .expect("the child flag is alone within the child command");
        assert!(
            parse(&spec, &input(&["ex", "--clean", "run"])).is_err(),
            "the parent flag still conflicts with selecting the child"
        );
    }

    #[test]
    fn a_child_local_exclusive_redeclaration_belongs_to_the_child() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--clean\" global=#true exclusive=#true\ncmd \"run\" {\n  flag \"--clean\" exclusive=#true\n}\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "run", "--clean"]))
            .expect("the child-local exclusive flag is alone inside the child command");
        assert!(
            parse(&spec, &input(&["ex", "--clean", "run"])).is_err(),
            "the ancestor spelling still conflicts with selecting the child"
        );
    }

    #[test]
    fn a_same_named_parent_flag_is_company_for_a_child_exclusive_flag() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--clean\" global=#true\ncmd \"run\" {\n  flag \"--clean\" global=#true exclusive=#true\n}\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "run", "--clean"])).expect("the child exclusive flag is alone");
        assert!(
            parse(&spec, &input(&["ex", "--clean", "run", "--clean"])).is_err(),
            "the distinct parent declaration is still company despite sharing a name"
        );
    }

    #[test]
    fn a_local_child_redeclaration_keeps_its_exclusivity_when_merged() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--clean\" global=#true\ncmd \"run\" {\n  flag \"--clean\" exclusive=#true\n  flag \"--verbose\"\n}\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "run", "--clean"]))
            .expect("the child exclusive flag is valid alone");
        assert!(
            parse(&spec, &input(&["ex", "run", "--clean", "--verbose"])).is_err(),
            "merging with the inherited global must not discard child exclusivity"
        );
    }

    #[test]
    fn an_orphan_parent_alias_does_not_disown_a_child_local_exclusive_flag() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"-c --clean\" global=#true exclusive=#true\ncmd \"run\" {\n  flag \"--clean\" exclusive=#true\n}\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "run", "--clean"]))
            .expect("the typed long form belongs to the child declaration");
        assert!(
            parse(&spec, &input(&["ex", "run", "-c"])).is_err(),
            "the inherited short form still belongs to the ancestor"
        );
        assert!(
            parse(&spec, &input(&["ex", "run", "-c", "--clean"])).is_err(),
            "a child spelling cannot mask the ancestor-exclusive occurrence on the same merged flag"
        );
    }

    #[test]
    fn an_inherited_alias_keeps_its_ancestor_exclusivity() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"-c --clean\" global=#true exclusive=#true\ncmd \"run\" {\n  flag \"--clean\" global=#true\n}\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "run", "--clean"]))
            .expect("the child's spelling does not activate the orphan ancestor alias");
        assert!(
            parse(&spec, &input(&["ex", "run", "-c"])).is_err(),
            "the inherited short alias still belongs to the ancestor exclusive flag"
        );
    }

    #[test]
    fn an_inherited_negated_alias_keeps_its_ancestor_exclusivity() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"-c --clean\" negate=\"--no-clean\" global=#true exclusive=#true\ncmd \"run\" {\n  flag \"-c --clean\" global=#true\n}\n"
            .parse()
            .unwrap();

        assert!(
            parse(&spec, &input(&["ex", "run", "--no-clean"])).is_err(),
            "the inherited negated alias still belongs to the ancestor exclusive flag"
        );
    }

    #[test]
    fn a_colliding_alias_does_not_disown_the_child_from_the_rest() {
        // The child re-declares the inherited `--clean` as exclusive and gives it a `-c` that
        // an unrelated inherited global already owns. That collision is resolved in the other
        // global's favor, so the child's `-c` resolves elsewhere — but the child plainly owns
        // the `--clean` it declared, and its exclusivity holds.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--clean\" global=#true\nflag \"-c --config <f>\" global=#true\ncmd \"run\" {\n  flag \"-c --clean\" exclusive=#true\n  flag \"--verbose\"\n}\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "run", "--clean"])).expect("alone is allowed");
        assert!(
            parse(&spec, &input(&["ex", "run", "--clean", "--verbose"])).is_err(),
            "one unrelated alias collision cannot disown the child from its own flag"
        );
    }

    #[test]
    fn a_local_child_declaration_is_not_in_scope_before_the_subcommand() {
        // A child's *local* re-declaration describes the flag at the child. Typed ahead of the
        // subcommand word the flag can only be the ancestor's, because that is the only one in
        // scope there — so the ancestor's exclusivity is the one that answers, whichever way it
        // is set. The pair below differ in nothing else, which is what makes this one rule
        // rather than two behaviors.
        let quiet: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--clean\" global=#true\ncmd \"run\" {\n  flag \"--clean\" exclusive=#true\n  flag \"--verbose\"\n}\n"
            .parse()
            .unwrap();
        parse(&quiet, &input(&["ex", "--clean", "run", "--verbose"]))
            .expect("the ancestor owns this occurrence, and it is not exclusive");
        assert!(
            parse(&quiet, &input(&["ex", "run", "--clean", "--verbose"])).is_err(),
            "after the subcommand word the child's declaration is in scope, and it is exclusive"
        );

        let loud: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--clean\" global=#true exclusive=#true\ncmd \"run\" {\n  flag \"--clean\" exclusive=#true\n}\n"
            .parse()
            .unwrap();
        assert!(
            parse(&loud, &input(&["ex", "--clean", "run"])).is_err(),
            "the same rule, with an exclusive ancestor: selecting the child is company for it"
        );
    }

    #[test]
    fn an_orphan_ancestor_alias_keeps_its_exclusivity_past_a_plain_child_redeclaration() {
        // The mirror of `a_local_child_redeclaration_keeps_its_exclusivity_when_merged`: the
        // child owns `--clean` and says nothing about exclusivity, but `-c` is a spelling only
        // the ancestor ever declared, so the ancestor's answer still governs it.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"-c --clean\" global=#true exclusive=#true\ncmd \"run\" {\n  flag \"--clean\"\n  flag \"--verbose\"\n}\n"
            .parse()
            .unwrap();

        assert!(
            parse(&spec, &input(&["ex", "run", "-c"])).is_err(),
            "the orphan ancestor alias is still the ancestor's exclusive flag"
        );
        parse(&spec, &input(&["ex", "run", "--clean", "--verbose"]))
            .expect("the child's own spelling drops the exclusivity the child did not restate");
    }

    #[test]
    fn a_child_spelling_carries_its_exclusivity_even_beside_an_ancestor_spelling() {
        // Both spellings of one merged flag, typed together. The child's `--clean` is exclusive
        // whatever else was typed alongside it, so `--verbose` is company; attributing the whole
        // occurrence to the ancestor because `-c` appeared in it lost that.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"-c --clean\" global=#true\ncmd \"run\" {\n  flag \"--clean\" exclusive=#true\n  flag \"--verbose\"\n}\n"
            .parse()
            .unwrap();

        assert!(
            parse(&spec, &input(&["ex", "run", "-c", "--clean", "--verbose"])).is_err(),
            "the child spelling is exclusive whatever it was typed beside"
        );
        parse(&spec, &input(&["ex", "run", "-c", "--verbose"]))
            .expect("the ancestor's own spelling was never exclusive");
    }

    #[test]
    fn an_environment_value_takes_the_exclusivity_of_the_declaration_in_scope() {
        // An environment value has no spelling to attribute, so the declaration the selected
        // command has in scope answers — in both directions. Comparing whole alias sets asked
        // the ancestor instead, because the merged flag also carries its orphan `-c`.
        let added: Spec = "name \"ex\"\nbin \"ex\"\nflag \"-c --clean\" global=#true env=\"EX_CLEAN\"\ncmd \"run\" {\n  flag \"--clean\" exclusive=#true\n  flag \"--verbose\"\n}\n"
            .parse()
            .unwrap();

        assert!(
            parse_with_env(&added, &["ex", "run", "--verbose"], &[("EX_CLEAN", "1")]).is_err(),
            "the child added exclusivity the environment value has to honor"
        );

        let dropped: Spec = "name \"ex\"\nbin \"ex\"\nflag \"-c --clean\" global=#true exclusive=#true env=\"EX_CLEAN\"\ncmd \"run\" {\n  flag \"--clean\"\n  flag \"--verbose\"\n}\n"
            .parse()
            .unwrap();

        parse_with_env(&dropped, &["ex", "run", "--verbose"], &[("EX_CLEAN", "1")])
            .expect("the child dropped the exclusivity, and the environment value follows it");
    }

    #[test]
    fn a_merged_child_exclusive_flag_still_escapes_requiredness() {
        // Exclusivity suppresses missing-value checks, and that has to survive the merge for
        // the same reason the companion check does.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--clean\" global=#true\ncmd \"run\" {\n  flag \"--clean\" exclusive=#true\n  flag \"--out <path>\" required=#true\n}\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "run", "--clean"]))
            .expect("a merged child exclusive flag is still the command's requiredness escape");
    }

    #[test]
    fn a_group_allows_one_member_and_refuses_two() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--file <f>\"\nflag \"--url <u>\"\nflag \"--stdin\"\ngroup \"input\" \"--file\" \"--url\" \"--stdin\"\n"
            .parse()
            .unwrap();

        // One is fine, and so is none: a plain group says "at most one".
        parse(&spec, &input(&["ex", "--file", "a.txt"])).expect("one member is fine");
        parse(&spec, &input(&["ex"])).expect("a group that is not required asks for nothing");

        let err = parse(&spec, &input(&["ex", "--file", "a.txt", "--stdin"])).unwrap_err();
        assert!(err.to_string().contains("group input"), "{err}");
    }

    #[test]
    fn positional_selectors_work_in_conflicts_and_groups() {
        let conflicts: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--from-file <path>\" conflicts=\"value\"\narg \"[value]\"\n"
            .parse()
            .unwrap();
        parse(&conflicts, &input(&["ex", "--from-file", "vars.env"]))
            .expect("the flag alone is valid");
        parse(&conflicts, &input(&["ex", "literal"])).expect("the positional alone is valid");
        assert!(parse(
            &conflicts,
            &input(&["ex", "--from-file", "vars.env", "literal"])
        )
        .is_err());

        let positional_source: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--from-file <path>\"\narg \"[value]\" conflicts=\"--from-file\"\n"
            .parse()
            .unwrap();
        assert!(parse(
            &positional_source,
            &input(&["ex", "--from-file", "vars.env", "literal"])
        )
        .is_err());

        let group: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--file <path>\"\narg \"[target]\"\ngroup \"input\" \"--file\" \"target\" required=#true\n"
            .parse()
            .unwrap();
        assert!(parse(&group, &input(&["ex"])).is_err());
        parse(&group, &input(&["ex", "target-name"]))
            .expect("a positional satisfies a required group");
        assert!(parse(
            &group,
            &input(&["ex", "--file", "input.txt", "target-name"])
        )
        .is_err());
    }

    #[test]
    fn a_required_group_needs_one_of_its_members() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--file <f>\"\nflag \"--url <u>\"\ngroup \"input\" \"--file\" \"--url\" required=#true\n"
            .parse()
            .unwrap();

        let err = parse(&spec, &input(&["ex"])).unwrap_err();
        // The members, because that is what a user has to type; the name, because a
        // command with several groups would otherwise report the same sentence twice.
        assert!(err.to_string().contains("--file, --url"), "{err}");
        assert!(err.to_string().contains("input"), "{err}");

        parse(&spec, &input(&["ex", "--url", "u"])).expect("one member satisfies it");
    }

    #[test]
    fn a_multiple_group_only_polices_requiredness() {
        // `multiple` with `required` is "at least one of these", so two is fine and
        // none is not.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--a\"\nflag \"--b\"\ngroup \"any\" \"--a\" \"--b\" required=#true multiple=#true\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "--a", "--b"])).expect("multiple allows both");
        assert!(parse(&spec, &input(&["ex"])).is_err());
    }

    #[test]
    fn a_group_reads_a_default_for_requiredness_and_not_for_exclusivity() {
        // The two halves of a group are two kinds of rule, and they read a default
        // differently on purpose. Requiredness asks whether a member has a value, and a
        // default is a value — the rule `requires` follows. Exclusivity asks what the
        // user supplied, because a defaulted member counted as supplied would collide
        // with the sibling they actually typed and refuse a correct command line.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--file <f>\" default=\"a.txt\"\nflag \"--url <u>\"\ngroup \"input\" \"--file\" \"--url\" required=#true\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex"])).expect("the default fills the group");
        parse(&spec, &input(&["ex", "--url", "u"]))
            .expect("the default must not conflict with the flag the user typed");
    }

    #[test]
    fn a_group_naming_two_spellings_of_one_flag_is_not_a_conflict() {
        // `-f` and `--file` are one flag. Counted by selector, giving it once would
        // report it as conflicting with itself.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"-f --file <f>\"\nflag \"--url <u>\"\ngroup \"input\" \"-f\" \"--file\" \"--url\"\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "--file", "a.txt"])).expect("one flag is one member");
        parse(&spec, &input(&["ex", "-f", "a.txt"])).expect("either spelling, still one member");

        // A genuine collision is still one.
        let err = parse(&spec, &input(&["ex", "--file", "a.txt", "--url", "u"])).unwrap_err();
        assert!(err.to_string().contains("group input"), "{err}");
    }

    #[test]
    fn a_group_reads_the_environment_as_given() {
        // The environment does count, which is the same asymmetry `conflicts` has: an
        // env var is somebody saying something, a default is nobody saying anything.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--file <f>\" env=\"EX_FILE\"\nflag \"--url <u>\"\ngroup \"input\" \"--file\" \"--url\" required=#true\n"
            .parse()
            .unwrap();

        parse_with_env(&spec, &["ex"], &[("EX_FILE", "a.txt")]).expect("the environment fills it");
    }

    #[test]
    fn a_requirement_names_the_flag_that_is_missing() {
        // Reported as the missing flag rather than as something wrong with `--out`,
        // which is what clap says for an unmet `requires` and what a user can act on.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--out <p>\" requires=\"--format\"\nflag \"--format <f>\"\n"
            .parse()
            .unwrap();

        let err = parse(&spec, &input(&["ex", "--out", "a.txt"])).unwrap_err();
        assert!(
            err.to_string().contains("format"),
            "the missing flag should be named: {err}"
        );

        // Satisfied, in either order.
        for words in [
            &["ex", "--out", "a.txt", "--format", "json"][..],
            &["ex", "--format", "json", "--out", "a.txt"][..],
        ] {
            parse(&spec, &input(words)).unwrap_or_else(|e| panic!("{words:?}: {e}"));
        }

        // Nothing happens when the flag that imposes the rule is absent: a requirement
        // is a consequence of using the flag, not a rule about the command line.
        parse(&spec, &input(&["ex"])).expect("a bare invocation requires nothing");
    }

    #[test]
    fn a_value_activates_only_its_conditional_requirement() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--config <file>\" {\n  requires_if \"special.toml\" \"--key\"\n  requires_if \"remote.toml\" \"--token\"\n}\nflag \"--key <key>\"\nflag \"--token <token>\"\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "--config", "ordinary.toml"]))
            .expect("an unrelated value requires nothing");

        let key = parse(&spec, &input(&["ex", "--config", "special.toml"])).unwrap_err();
        assert!(key.to_string().contains("key"), "{key}");
        parse(
            &spec,
            &input(&["ex", "--config", "special.toml", "--key", "secret"]),
        )
        .expect("the matching requirement is satisfied");

        let token = parse(&spec, &input(&["ex", "--config", "remote.toml"])).unwrap_err();
        assert!(token.to_string().contains("token"), "{token}");
    }

    #[test]
    fn conditional_requirements_read_explicit_env_but_not_defaults() {
        let from_env: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--config <file>\" env=\"EX_CONFIG\" {\n  requires_if \"special.toml\" \"--key\"\n}\nflag \"--key <key>\"\n"
            .parse()
            .unwrap();
        let err = parse_with_env(&from_env, &["ex"], &[("EX_CONFIG", "special.toml")]).unwrap_err();
        assert!(err.to_string().contains("key"), "{err}");

        let from_default: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--config <file>\" default=\"special.toml\" {\n  requires_if \"special.toml\" \"--key\"\n}\nflag \"--key <key>\"\n"
            .parse()
            .unwrap();
        parse(&from_default, &input(&["ex"]))
            .expect("a default is not an explicit conditional value");
    }

    #[test]
    fn command_line_values_override_env_for_conditional_requirements() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--config <file>\" env=\"EX_CONFIG\" {\n  requires_if \"special.toml\" \"--key\"\n}\nflag \"--key <key>\"\n"
            .parse()
            .unwrap();

        parse_with_env(
            &spec,
            &["ex", "--config", "ordinary.toml"],
            &[("EX_CONFIG", "special.toml")],
        )
        .expect("the command-line value takes precedence over the environment");
    }

    #[test]
    fn conditional_requirements_normalize_boolean_env_values() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--feature\" env=\"EX_FEATURE\" {\n  requires_if \"true\" \"--key\"\n}\nflag \"--key <key>\"\n"
            .parse()
            .unwrap();

        for value in ["1", "true", "True", "TRUE"] {
            let err = parse_with_env(&spec, &["ex"], &[("EX_FEATURE", value)]).unwrap_err();
            assert!(err.to_string().contains("key"), "{value}: {err}");
        }
        parse_with_env(&spec, &["ex"], &[("EX_FEATURE", "false")])
            .expect("a false environment value does not activate a true condition");
    }

    #[test]
    fn a_default_satisfies_a_requirement() {
        // The flag it names has a value, which is the question a requirement asks. Read
        // any other way, `--format` would be missing here and present ten lines further
        // down, where plain required-ness reads the same default as filling it.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--out <p>\" requires=\"--format\"\nflag \"--format <f>\" default=\"json\"\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "--out", "a.txt"]))
            .expect("a defaulted flag is not a missing one");
    }

    #[test]
    fn a_present_flag_binds_a_conditional_default() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--bin-names\" {\n  default_if \"--json\" \"true\"\n}\nflag \"--json\"\n"
            .parse()
            .unwrap();

        let with = parse(&spec, &input(&["ex", "--json"])).unwrap();
        assert_eq!(
            with.as_env().get("usage_bin_names").map(String::as_str),
            Some("true")
        );

        let without = parse(&spec, &input(&["ex"])).unwrap();
        assert!(
            !without.as_env().contains_key("usage_bin_names"),
            "IsPresent does nothing when the selector is absent"
        );
    }

    #[test]
    fn an_equals_condition_binds_a_conditional_default() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--style <s>\" {\n  default_if \"--output\" \"json\" \"pretty\"\n}\nflag \"--output <fmt>\"\n"
            .parse()
            .unwrap();

        let json = parse(&spec, &input(&["ex", "--output", "json"])).unwrap();
        assert_eq!(
            json.as_env().get("usage_style").map(String::as_str),
            Some("pretty")
        );
        let yaml = parse(&spec, &input(&["ex", "--output", "yaml"])).unwrap();
        assert!(!yaml.as_env().contains_key("usage_style"));
    }

    #[test]
    fn an_equals_condition_reads_a_negated_flag() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--pretty\" {\n  default_if \"--json\" \"false\" \"true\"\n}\nflag \"--json\" negate=\"--no-json\"\n"
            .parse()
            .unwrap();

        let off = parse(&spec, &input(&["ex", "--no-json"])).unwrap();
        assert_eq!(
            off.as_env().get("usage_pretty").map(String::as_str),
            Some("true")
        );
        let on = parse(&spec, &input(&["ex", "--json"])).unwrap();
        assert!(
            !on.as_env().contains_key("usage_pretty"),
            "--json is true, so when=false should miss"
        );
    }

    #[test]
    fn the_first_matching_conditional_default_wins() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--style <s>\" {\n  default_if \"--json\" \"compact\"\n  default_if \"--pretty\" \"pretty\"\n}\nflag \"--json\"\nflag \"--pretty\"\n"
            .parse()
            .unwrap();

        let out = parse(&spec, &input(&["ex", "--json", "--pretty"])).unwrap();
        assert_eq!(
            out.as_env().get("usage_style").map(String::as_str),
            Some("compact")
        );
    }

    #[test]
    fn argv_and_env_suppress_a_conditional_default() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--bin-names\" env=\"EX_BIN\" {\n  default_if \"--json\" \"true\"\n}\nflag \"--json\"\n"
            .parse()
            .unwrap();

        let from_env = parse_with_env(&spec, &["ex", "--json"], &[("EX_BIN", "false")]).unwrap();
        assert_eq!(
            from_env.as_env().get("usage_bin_names").map(String::as_str),
            Some("false"),
            "the target's environment wins over default_if"
        );
    }

    #[test]
    fn a_sibling_env_activates_a_conditional_default() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--bin-names\" {\n  default_if \"--json\" \"true\"\n}\nflag \"--json\" env=\"EX_JSON\"\n"
            .parse()
            .unwrap();

        let out = parse_with_env(&spec, &["ex"], &[("EX_JSON", "1")]).unwrap();
        assert_eq!(
            out.as_env().get("usage_bin_names").map(String::as_str),
            Some("true")
        );
    }

    #[test]
    fn a_default_does_not_activate_a_conditional_default() {
        // `--json` sorts before `--pretty` in the available-flag map, so a one-pass
        // bind would put json's default into `out.flags` and then treat it as
        // explicit for pretty's `default_if`. Go and the derive ignore defaults.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--pretty\" {\n  default_if \"--json\" \"true\"\n}\nflag \"--json\" default=#true\n"
            .parse()
            .unwrap();

        let out = parse(&spec, &input(&["ex"])).unwrap();
        assert_eq!(
            out.as_env().get("usage_json").map(String::as_str),
            Some("true")
        );
        assert!(
            !out.as_env().contains_key("usage_pretty"),
            "a default is not an explicit value for default_if"
        );
    }

    #[test]
    fn a_conditional_default_does_not_activate_requires_if() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--format <f>\" {\n  default_if \"--json\" \"json\"\n  requires_if \"json\" \"--schema\"\n}\nflag \"--schema <s>\"\nflag \"--json\"\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "--json"]))
            .expect("a default_if value is not explicit for requires_if");
        assert!(parse(&spec, &input(&["ex", "--format", "json"])).is_err());
    }

    #[test]
    fn a_conditional_default_satisfies_a_requirement() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--out <p>\" requires=\"--format\"\nflag \"--format <f>\" {\n  default_if \"--json\" \"json\"\n}\nflag \"--json\"\n"
            .parse()
            .unwrap();

        parse(&spec, &input(&["ex", "--out", "a.txt", "--json"]))
            .expect("default_if fills the required flag");
        assert!(parse(&spec, &input(&["ex", "--out", "a.txt"])).is_err());
    }

    #[test]
    fn an_environment_value_satisfies_a_requirement() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--out <p>\" requires=\"--format\"\nflag \"--format <f>\" env=\"EX_FORMAT\"\n"
            .parse()
            .unwrap();

        assert!(parse(&spec, &input(&["ex", "--out", "a.txt"])).is_err());
        parse_with_env(&spec, &["ex", "--out", "a.txt"], &[("EX_FORMAT", "json")])
            .expect("the environment supplies it");
    }

    #[test]
    fn a_requirement_is_satisfied_by_a_short_form() {
        // The selector may spell the other flag any way it answers to, so the check
        // resolves it the way every other selector is resolved rather than matching
        // text. The error names the flag, not the selector.
        let spec: Spec =
            "name \"ex\"\nbin \"ex\"\nflag \"--sign\" requires=\"-k\"\nflag \"-k --key <k>\"\n"
                .parse()
                .unwrap();

        parse(&spec, &input(&["ex", "--sign", "--key", "x"])).expect("--key satisfies -k");

        let err = parse(&spec, &input(&["ex", "--sign"])).unwrap_err();
        assert!(err.to_string().contains("key"), "{err}");
    }

    #[test]
    fn conflicting_flags_are_rejected_in_either_order() {
        // Declared once, on `--file`, which is all clap exposes — so the check has to
        // be order-independent by looking at every flag that was given rather than at
        // the one that declared the conflict.
        let spec: Spec =
            "name \"ex\"\nbin \"ex\"\nflag \"--file <f>\" conflicts=\"--stdin\"\nflag \"--stdin\"\n"
                .parse()
                .unwrap();

        for words in [
            &["ex", "--file", "a.txt", "--stdin"][..],
            &["ex", "--stdin", "--file", "a.txt"][..],
        ] {
            let err = parse(&spec, &input(words)).unwrap_err();
            assert!(
                err.to_string().contains("conflicts with --stdin"),
                "{words:?} should be refused: {err}"
            );
        }

        // Either one alone is fine.
        parse(&spec, &input(&["ex", "--stdin"])).unwrap();
        parse(&spec, &input(&["ex", "--file", "a.txt"])).unwrap();
    }

    #[test]
    fn unknown_flags_are_values_by_default() {
        // The default, and the reason it is the default: a spec often parses a
        // command line whose flags belong to something else.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--force\"\narg \"[rest]...\"\n"
            .parse()
            .unwrap();
        let out = parse(
            &spec,
            &["ex".to_string(), "--wat".to_string(), "x".to_string()],
        )
        .unwrap();
        let rest = out.args.keys().find(|a| a.name == "rest").unwrap();
        assert_eq!(out.args[rest].to_string(), "--wat x");
    }

    #[test]
    fn repeated_scalar_flags_override_by_default_and_can_be_strict() {
        let permissive: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--jobs <n>\"\nflag \"--verbose\"\n"
            .parse()
            .unwrap();
        let out = parse(&permissive, &input(&["ex", "--jobs", "1", "--jobs", "2"]))
            .expect("a repeat is a correction by default");
        let jobs = out.flags.keys().find(|f| f.name == "jobs").unwrap();
        assert_eq!(out.flags[jobs].to_string(), "2");
        parse(&permissive, &input(&["ex", "--verbose", "--verbose"]))
            .expect("switches use the same default");

        let strict: Spec = "name \"ex\"\nbin \"ex\"\nargs_override_self #false\nflag \"--jobs <n>\"\nflag \"--verbose\"\n"
            .parse()
            .unwrap();
        for words in [
            &["ex", "--jobs", "1", "--jobs", "2"][..],
            &["ex", "--verbose", "--verbose"][..],
        ] {
            let err = parse(&strict, &input(words)).unwrap_err();
            assert!(
                err.to_string().contains("cannot be used multiple times"),
                "{err}"
            );
        }

        let reparsed: Spec = strict.to_string().parse().unwrap();
        assert!(!reparsed.cmd.args_override_self);
    }

    #[test]
    fn strict_negated_flags_allow_opposite_forms_but_reject_the_same_form() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nargs_override_self #false\nflag \"--color\" negate=\"--no-color\"\n"
            .parse()
            .unwrap();

        let out = parse(&spec, &input(&["ex", "--color", "--no-color"]))
            .expect("opposite forms override each other");
        let color = out.flags.keys().find(|f| f.name == "color").unwrap();
        assert!(matches!(out.flags[color], ParseValue::Bool(false)));

        for words in [
            &["ex", "--color", "--color"][..],
            &["ex", "--no-color", "--no-color"][..],
        ] {
            let err = parse(&spec, &input(words)).unwrap_err();
            assert!(err.to_string().contains("cannot be used multiple times"));
        }
    }

    #[test]
    fn strict_global_flags_may_repeat_across_command_levels() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nargs_override_self #false\nflag \"--color\" negate=\"--no-color\" global=#true\nflag \"--jobs <n>\" global=#true\ncmd \"run\" {\n  args_override_self #false\n}\n"
            .parse()
            .unwrap();

        let out = parse(
            &spec,
            &input(&[
                "ex", "--color", "--jobs", "1", "run", "--color", "--jobs", "2",
            ]),
        )
        .expect("an inherited global is allowed once at each command level");
        let jobs = out.flags.keys().find(|f| f.name == "jobs").unwrap();
        assert_eq!(out.flags[jobs].to_string(), "2");

        for words in [
            &["ex", "--color", "--color", "run", "--no-color"][..],
            &["ex", "--jobs", "1", "run", "--jobs", "2", "--jobs", "3"][..],
        ] {
            let err = parse(&spec, &input(words)).unwrap_err();
            assert!(err.to_string().contains("cannot be used multiple times"));
        }
    }

    #[test]
    fn a_subcommand_can_negate_only_its_parents_requirements() {
        let base = r#"name "ex"
bin "ex"
flag "--config" required=#true
flag "--mode" requires="--config"
flag "--other"
arg "<input>"
group "source" "--config" "--other" required=#true
cmd "run" { flag "--child" required=#true }
"#;
        let strict: Spec = base.parse().unwrap();
        let err = parse(&strict, &input(&["ex", "run"])).unwrap_err();
        let message = err.to_string();
        assert!(
            message.contains("input") || message.contains("config"),
            "{message}"
        );

        let negated: Spec = base
            .replacen("bin \"ex\"", "bin \"ex\"\nsubcommand_negates_reqs #true", 1)
            .parse()
            .unwrap();
        let err = parse(&negated, &input(&["ex", "run"])).unwrap_err();
        assert!(
            err.to_string().contains("child"),
            "the selected command keeps its own requirements: {err}"
        );

        let mut child_optional = negated.clone();
        child_optional.cmd.subcommands["run"].flags[0].required = false;
        parse(&child_optional, &input(&["ex", "run"]))
            .expect("the child selection satisfies all parent requirements");
        parse(&child_optional, &input(&["ex", "--mode", "run"]))
            .expect("parent requires relationships are negated too");
    }

    #[test]
    fn a_parent_argument_can_conflict_with_a_later_subcommand() {
        let spec: Spec = r#"name "ex"
bin "ex"
args_conflicts_with_subcommands #true
flag "--verbose"
cmd "run"
"#
        .parse()
        .unwrap();

        parse(&spec, &input(&["ex", "run"]))
            .expect("the subcommand is valid without a parent argument");
        let err = parse(&spec, &input(&["ex", "--verbose", "run"])).unwrap_err();
        assert!(
            err.to_string().contains("cannot be used with arguments"),
            "{err}"
        );
    }

    #[test]
    fn a_subcommand_can_take_precedence_over_a_variadic_flag() {
        let base = r#"name "ex"
bin "ex"
flag "--values <value>..."
cmd "run"
"#;
        let plain: Spec = base.parse().unwrap();
        let out = parse(&plain, &input(&["ex", "--values", "a", "run"])).unwrap();
        assert_eq!(out.cmd.name, "ex");

        let precedence: Spec = base
            .replacen(
                "bin \"ex\"",
                "bin \"ex\"\nsubcommand_precedence_over_arg #true",
                1,
            )
            .parse()
            .unwrap();
        let out = parse(&precedence, &input(&["ex", "--values", "a", "run"])).unwrap();
        assert_eq!(out.cmd.name, "run");
    }

    #[test]
    fn a_required_positional_can_follow_an_unfilled_optional_one() {
        let base = r#"name "ex"
bin "ex"
arg "[optional]"
arg "<required>"
"#;
        let plain: Spec = base.parse().unwrap();
        let err = parse(&plain, &input(&["ex", "value"])).unwrap_err();
        assert!(err.to_string().contains("required"), "{err}");

        let enabled: Spec = base
            .replacen(
                "bin \"ex\"",
                "bin \"ex\"\nallow_missing_positional #true",
                1,
            )
            .parse()
            .unwrap();
        let out = parse(&enabled, &input(&["ex", "value"])).unwrap();
        assert!(!out.args.keys().any(|arg| arg.name == "optional"));
        let value = &out
            .args
            .iter()
            .find(|(arg, _)| arg.name == "required")
            .unwrap()
            .1;
        assert!(matches!(value, ParseValue::String(value) if value == "value"));
    }

    #[test]
    fn unknown_flags_can_be_rejected_for_the_whole_cli() {
        let spec: Spec =
            "name \"ex\"\nbin \"ex\"\nunknown_flags \"error\"\nflag \"--force\"\nflag \"-0 --print0\"\narg \"[rest]...\" allow_negative_numbers=#true\n"
                .parse()
                .unwrap();
        let err = parse(&spec, &["ex".to_string(), "--wat".to_string()]).unwrap_err();
        assert!(
            err.to_string().contains("--wat"),
            "the message should name the token: {err}"
        );

        // The positional opts into the narrower negative-number carve-out without
        // accepting arbitrary unknown flags.
        let out = parse(&spec, &["ex".to_string(), "-1".to_string()]).unwrap();
        let rest = out.args.keys().find(|a| a.name == "rest").unwrap();
        assert_eq!(out.args[rest].to_string(), "-1");

        let out = parse(&spec, &["ex".to_string(), "-0".to_string()]).unwrap();
        let print0 = out.flags.keys().find(|flag| flag.name == "print0").unwrap();
        assert!(matches!(out.flags[print0], ParseValue::Bool(true)));
    }

    #[test]
    fn a_declared_digit_short_does_not_stop_the_subcommand_scan() {
        let spec: Spec = r#"
name "ex"
bin "ex"
unknown_flags "error"
flag "-0 --print0" global=#true
cmd "run" {
  flag "--force"
}
"#
        .parse()
        .unwrap();
        let out = parse(&spec, &input(&["ex", "-0", "run", "--force"])).unwrap();
        assert_eq!(out.cmd.name, "run");
        let print0 = out.flags.keys().find(|flag| flag.name == "print0").unwrap();
        let force = out.flags.keys().find(|flag| flag.name == "force").unwrap();
        assert!(matches!(out.flags[print0], ParseValue::Bool(true)));
        assert!(matches!(out.flags[force], ParseValue::Bool(true)));
    }

    #[test]
    fn a_command_may_override_the_cli_wide_setting() {
        // Strict overall, lenient for the one command that forwards options.
        let spec: Spec = r#"
name "ex"
bin "ex"
unknown_flags "error"
cmd "exec" unknown_flags="value" {
  arg "[rest]..."
}
cmd "build" {
  arg "[rest]..."
}
"#
        .parse()
        .unwrap();

        let out = parse(
            &spec,
            &["ex".to_string(), "exec".to_string(), "--wat".to_string()],
        )
        .unwrap();
        let rest = out.args.keys().find(|a| a.name == "rest").unwrap();
        assert_eq!(out.args[rest].to_string(), "--wat");

        assert!(
            parse(
                &spec,
                &["ex".to_string(), "build".to_string(), "--wat".to_string()]
            )
            .is_err(),
            "a command that says nothing inherits the CLI's choice"
        );
    }

    #[test]
    fn the_setting_survives_a_round_trip() {
        let spec: Spec =
            "name \"ex\"\nbin \"ex\"\nunknown_flags \"error\"\ncmd \"x\" unknown_flags=\"value\"\n"
                .parse()
                .unwrap();
        let reparsed: Spec = spec.to_string().parse().unwrap();
        assert_eq!(reparsed.unknown_flags, Some(UnknownFlags::Error));
        assert_eq!(
            reparsed.cmd.subcommands["x"].unknown_flags,
            Some(UnknownFlags::Value)
        );
    }

    #[test]
    fn test_default_subcommand() {
        // Test that default_subcommand routes to the specified subcommand
        let run_cmd = SpecCommand::builder()
            .name("run")
            .arg(SpecArg::builder().name("task").build())
            .build();
        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            default_subcommand: Some("run".to_string()),
            ..Default::default()
        };

        // "test mytask" should be parsed as if it were "test run mytask"
        let input = vec!["test".to_string(), "mytask".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        // Should have two commands: root and "run"
        assert_eq!(parsed.cmds.len(), 2);
        assert_eq!(parsed.cmds[1].name, "run");

        // Should have parsed the task argument
        assert_eq!(parsed.args.len(), 1);
        let arg = parsed.args.keys().next().unwrap();
        assert_eq!(arg.name, "task");
        let value = parsed.args.values().next().unwrap();
        assert_eq!(value.to_string(), "mytask");
    }

    #[test]
    fn test_default_subcommand_explicit_still_works() {
        // Test that explicit subcommand takes precedence
        let run_cmd = SpecCommand::builder()
            .name("run")
            .arg(SpecArg::builder().name("task").build())
            .build();
        let other_cmd = SpecCommand::builder()
            .name("other")
            .arg(SpecArg::builder().name("other_arg").build())
            .build();
        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);
        cmd.subcommands.insert("other".to_string(), other_cmd);

        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            default_subcommand: Some("run".to_string()),
            ..Default::default()
        };

        // "test other foo" should use "other" subcommand, not default
        let input = vec!["test".to_string(), "other".to_string(), "foo".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        // Should have used "other" subcommand
        assert_eq!(parsed.cmds.len(), 2);
        assert_eq!(parsed.cmds[1].name, "other");
    }

    #[test]
    fn test_default_subcommand_applies_only_at_the_root() {
        // `default_subcommand` is declared once, for the whole spec, and only at the top. It
        // was being looked up wherever the parser happened to be standing, so a command with
        // an unrelated subcommand of the same name acquired a default of its own: with
        // `default_subcommand "ls"`, `ex config zzz` descended into `config ls` and bound
        // `zzz` there. Nothing declared that, and nothing could have.
        let mut config_ls = SpecCommand::builder().name("ls").build();
        config_ls.args.push(SpecArg::builder().name("what").build());
        let mut config_cmd = SpecCommand::builder().name("config").build();
        config_cmd.subcommands.insert("ls".to_string(), config_ls);

        // The root's own `ls`, which is what its default points at. It takes an argument so
        // that a routed word has somewhere to land.
        let mut root_ls = SpecCommand::builder().name("ls").build();
        root_ls.args.push(SpecArg::builder().name("what").build());
        let mut cmd = SpecCommand::builder().name("ex").build();
        cmd.subcommands.insert("ls".to_string(), root_ls);
        cmd.subcommands.insert("config".to_string(), config_cmd);

        let spec = Spec {
            name: "ex".to_string(),
            bin: "ex".to_string(),
            cmd,
            default_subcommand: Some("ls".to_string()),
            ..Default::default()
        };

        // `config` has an `ls`, but `config` did not declare a default, so `zzz` is `config`'s
        // own business — and `config` takes no argument, so this is an error rather than a
        // silent descent.
        let input = vec!["ex".to_string(), "config".to_string(), "zzz".to_string()];
        assert!(
            parse(&spec, &input).is_err(),
            "`config` has no default subcommand and no argument, so `zzz` cannot bind"
        );

        // At the root, where it is declared, it still applies.
        let input = vec!["ex".to_string(), "zzz".to_string()];
        let parsed = parse(&spec, &input).expect("the root's default applies");
        assert_eq!(
            parsed
                .cmds
                .iter()
                .map(|c| c.name.as_str())
                .collect::<Vec<_>>(),
            ["ex", "ls"]
        );
        assert_eq!(
            parsed.args.values().next().map(|v| v.to_string()),
            Some("zzz".to_string()),
            "and the word binds inside the command it reached"
        );
    }

    #[test]
    fn test_default_subcommand_with_nested_subcommands() {
        // Test that default_subcommand works when the default subcommand has nested subcommands.
        // This is the mise use case: "mise say" should be parsed as "mise run say"
        // where "say" is a subcommand of "run" (a task).
        let say_cmd = SpecCommand::builder()
            .name("say")
            .arg(SpecArg::builder().name("name").build())
            .build();
        let mut run_cmd = SpecCommand::builder().name("run").build();
        run_cmd.subcommands.insert("say".to_string(), say_cmd);

        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            default_subcommand: Some("run".to_string()),
            ..Default::default()
        };

        // "test say hello" should be parsed as "test run say hello"
        let input = vec!["test".to_string(), "say".to_string(), "hello".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        // Should have three commands: root, "run", and "say"
        assert_eq!(parsed.cmds.len(), 3);
        assert_eq!(parsed.cmds[0].name, "test");
        assert_eq!(parsed.cmds[1].name, "run");
        assert_eq!(parsed.cmds[2].name, "say");

        // Should have parsed the "name" argument
        assert_eq!(parsed.args.len(), 1);
        let arg = parsed.args.keys().next().unwrap();
        assert_eq!(arg.name, "name");
        let value = parsed.args.values().next().unwrap();
        assert_eq!(value.to_string(), "hello");
    }

    /// Build a spec equivalent to the post-mount structure produced by mise's
    /// `mise usage` output: a root with a value-taking global flag (`-C/--cd`), a `run`
    /// subcommand that re-declares the same flag as NON-global, and a mounted task
    /// (`sample:run`) carrying a positional arg with `choices`.
    ///
    /// We construct the merged structure directly instead of executing a real mount so the
    /// test stays hermetic and cross-platform while still exercising the parser defect.
    fn mounted_global_flag_spec() -> Spec {
        let task_cmd = SpecCommand::builder()
            .name("sample:run")
            .arg(
                SpecArg::builder()
                    .name("profile")
                    .choices(["alpha", "beta", "gamma"])
                    .build(),
            )
            .build();
        // `run` re-declares `-C/--cd` but as a NON-global flag, mirroring the mise spec.
        let mut run_cmd = SpecCommand::builder()
            .name("run")
            .flag(
                SpecFlag::builder()
                    .name("cd")
                    .short('C')
                    .long("cd")
                    .arg(SpecArg::builder().name("dir").build())
                    .global(false)
                    .build(),
            )
            .build();
        run_cmd
            .subcommands
            .insert("sample:run".to_string(), task_cmd);

        let mut cmd = SpecCommand::builder()
            .name("test")
            .flag(
                SpecFlag::builder()
                    .name("cd")
                    .short('C')
                    .long("cd")
                    .arg(SpecArg::builder().name("dir").build())
                    .global(true)
                    .build(),
            )
            .build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        }
    }

    #[test]
    fn test_prefix_global_flag_does_not_pollute_choices() {
        // Regression for the parser-side root cause referenced by jdx/mise#10069.
        //
        // When `run` re-declares the global `-C/--cd` as non-global, descending into it (and
        // then into the mounted `sample:run`) used to drop the inherited global flag from
        // `available_flags`. Phase 2 then no longer recognized the prefix `-C`, so it was
        // mis-validated against the task's `choices` positional arg.
        let spec = mounted_global_flag_spec();

        // The prefix global flag must stay recognized so it is consumed as a flag (not as the
        // positional). Before the fix this bailed with "Invalid choice for arg profile: -C".
        for words in [
            &["test", "-C", "/tmp", "run", "sample:run"][..],
            // Embedded-value form must behave identically.
            &["test", "--cd=/tmp", "run", "sample:run"][..],
        ] {
            let parsed = parse_partial(&spec, &input(words)).unwrap();
            assert_eq!(
                parsed
                    .cmds
                    .iter()
                    .map(|c| c.name.as_str())
                    .collect::<Vec<_>>(),
                vec!["test", "run", "sample:run"],
            );
            // No positional arg should have been consumed by the leftover global-flag tokens.
            assert!(
                parsed.args.is_empty(),
                "args should be empty, got {:?}",
                parsed.args
            );

            // Fix (B): the inherited global flag survives the descent even though `run`
            // re-declares `-C/--cd` as non-global.
            let cd = parsed
                .available_flags
                .get("--cd")
                .expect("--cd should remain available after descending into the subcommand");
            assert!(cd.global, "--cd must stay global after descent");
            assert!(
                parsed.available_flags.get("-C").is_some_and(|f| f.global),
                "-C must stay global after descent",
            );

            // The global flag must still be recorded in `out.flags` so it reaches `as_env()`
            // for normal execution and for the env passed to mount scripts. (Removing the
            // token in Phase 1 instead of re-parsing it would silently drop `usage_cd`.)
            assert_eq!(
                parsed.as_env().get("usage_cd").map(String::as_str),
                Some("/tmp"),
                "global flag value must survive in as_env(), got {:?}",
                parsed.as_env(),
            );
        }

        // A real, valid choice still parses through the global flag prefix.
        let parsed = parse_partial(
            &spec,
            &input(&["test", "-C", "/tmp", "run", "sample:run", "alpha"]),
        )
        .unwrap();
        assert_eq!(parsed.args.len(), 1);
        assert_eq!(parsed.args.values().next().unwrap().to_string(), "alpha");

        // And genuinely invalid choices are still rejected (we didn't disable validation).
        assert_parse_err(
            parse_partial(&spec, &input(&["test", "run", "sample:run", "wrong"])),
            "Invalid choice for arg profile: wrong, expected one of alpha, beta, gamma",
        );
    }

    /// Build a spec mirroring mise's orphan-short re-declarations: a root with a LONG-ONLY
    /// global boolean flag (`--raw`, no short), a `run` subcommand that re-declares it as a
    /// NON-global flag while ADDING a short (`-r --raw`) plus a purely-local `-f/--force`
    /// flag, and a mounted task (`sample:run`) with a `choices` positional arg.
    fn mounted_orphan_short_spec() -> Spec {
        let task_cmd = SpecCommand::builder()
            .name("sample:run")
            .arg(
                SpecArg::builder()
                    .name("profile")
                    .choices(["alpha", "beta", "gamma"])
                    .build(),
            )
            .build();
        // `run` re-declares `--raw` as NON-global but adds a `-r` short that exists only here,
        // and also carries a purely-local `-f/--force` flag (shares nothing with a global).
        let mut run_cmd = SpecCommand::builder()
            .name("run")
            .flag(
                SpecFlag::builder()
                    .name("raw")
                    .short('r')
                    .long("raw")
                    .global(false)
                    .build(),
            )
            .flag(
                SpecFlag::builder()
                    .name("force")
                    .short('f')
                    .long("force")
                    .global(false)
                    .build(),
            )
            .build();
        run_cmd
            .subcommands
            .insert("sample:run".to_string(), task_cmd);

        // Root global is LONG-ONLY: `--raw` with no short.
        let mut cmd = SpecCommand::builder()
            .name("test")
            .flag(
                SpecFlag::builder()
                    .name("raw")
                    .long("raw")
                    .global(true)
                    .build(),
            )
            .build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        }
    }

    #[test]
    fn test_orphan_short_alias_survives_merge() {
        // Follow-up to test_prefix_global_flag_does_not_pollute_choices (jdx/mise#10069):
        // when `run` re-declares the long-only global `--raw` as a non-global `-r --raw`, the
        // added short `-r` must be unioned onto the surviving inherited global flag instead of
        // being discarded with the wholesale re-declaration. Otherwise `mycli run -r <task>`
        // would not recognize `-r` and would mis-validate it against the task's `choices` arg.
        let spec = mounted_orphan_short_spec();

        let parsed = parse_partial(&spec, &input(&["test", "run", "-r", "sample:run"])).unwrap();
        assert_eq!(
            parsed
                .cmds
                .iter()
                .map(|c| c.name.as_str())
                .collect::<Vec<_>>(),
            vec!["test", "run", "sample:run"],
        );

        // (a) The orphan short `-r` survives the descent, merged onto the inherited global flag,
        // and the original long `--raw` is still global too.
        assert!(
            parsed.available_flags.get("-r").is_some_and(|f| f.global),
            "-r must be merged onto the inherited global flag and stay global after descent",
        );
        assert!(
            parsed
                .available_flags
                .get("--raw")
                .is_some_and(|f| f.global),
            "--raw must stay global after descent",
        );

        // (b) The token is consumed as a flag, not mistaken for the `choices` positional.
        assert!(
            parsed.args.is_empty(),
            "args should be empty, got {:?}",
            parsed.args
        );

        // (c) The value still reaches as_env() so `usage_raw` is produced for execution/mounts.
        assert_eq!(
            parsed.as_env().get("usage_raw").map(String::as_str),
            Some("true"),
            "merged short's value must survive in as_env(), got {:?}",
            parsed.as_env(),
        );

        // (d) Negative case: a purely-local flag that shares nothing with a global is NOT
        // promoted/merged — it is correctly dropped when descending into the mount.
        assert!(
            !parsed.available_flags.contains_key("-f"),
            "purely-local -f must not be promoted onto a global",
        );
        assert!(
            !parsed.available_flags.contains_key("--force"),
            "purely-local --force must not be promoted onto a global",
        );

        // A real, valid choice still parses through the merged short prefix.
        let parsed =
            parse_partial(&spec, &input(&["test", "run", "-r", "sample:run", "alpha"])).unwrap();
        assert_eq!(parsed.args.len(), 1);
        assert_eq!(parsed.args.values().next().unwrap().to_string(), "alpha");

        // And genuinely invalid choices are still rejected.
        assert_parse_err(
            parse_partial(&spec, &input(&["test", "run", "-r", "sample:run", "wrong"])),
            "Invalid choice for arg profile: wrong, expected one of alpha, beta, gamma",
        );
    }

    #[test]
    fn test_orphan_short_does_not_clobber_unrelated_global() {
        // When a re-declaration's orphan short collides with a DIFFERENT inherited global's
        // short, the merge must not steal it. Here the root has both a long-only `--raw` global
        // and a `-r --restrict` global; `run` re-declares `-r --raw` as non-global. `-r` is a
        // genuine collision with `--restrict`, so global precedence must keep `-r -> restrict`.
        let run_cmd = SpecCommand::builder()
            .name("run")
            .flag(
                SpecFlag::builder()
                    .name("raw")
                    .short('r')
                    .long("raw")
                    .global(false)
                    .build(),
            )
            .build();
        let mut cmd = SpecCommand::builder()
            .name("test")
            .flag(
                SpecFlag::builder()
                    .name("raw")
                    .long("raw")
                    .global(true)
                    .build(),
            )
            .flag(
                SpecFlag::builder()
                    .name("restrict")
                    .short('r')
                    .long("restrict")
                    .global(true)
                    .build(),
            )
            .build();
        cmd.subcommands.insert("run".to_string(), run_cmd);
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        let parsed = parse_partial(&spec, &input(&["test", "run"])).unwrap();
        // `-r` stays owned by the unrelated `--restrict` global, not stolen by the merged raw.
        assert_eq!(
            parsed.available_flags.get("-r").map(|f| f.name.as_str()),
            Some("restrict"),
            "-r must remain owned by the unrelated global it already belonged to",
        );
        // Both globals are still recognized and global after the descent.
        assert!(parsed
            .available_flags
            .get("--raw")
            .is_some_and(|f| f.global));
        assert!(parsed
            .available_flags
            .get("--restrict")
            .is_some_and(|f| f.global));
    }

    #[test]
    fn test_redeclared_global_aliases_share_one_flag() {
        // A global declared with BOTH a short and a long, re-declared non-globally by a
        // subcommand that adds a third alias. Every alias key must resolve to the SAME merged
        // flag: the child's keys iterate in BTreeMap order (`--assume-yes`, `--yes`, `-y`), so by
        // the time `-y` is reached the long already points at the merged flag. That merged flag is
        // not a *different* inherited global, so the collision guard must not skip `-y` and leave
        // it pointing at the pre-merge global (which lacks the added `assume-yes` alias).
        let spec = r#"
flag "-y --yes" global=#true effect="write"
cmd "run" {
    flag "-y --yes --assume-yes"
}
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse_partial(&spec, &input(&["test", "run"])).unwrap();

        for key in ["-y", "--yes", "--assume-yes"] {
            let flag = parsed
                .available_flags
                .get(key)
                .unwrap_or_else(|| panic!("{key} must be recognized after the descent"));
            assert!(flag.global, "{key} must stay global after the descent");
            assert_eq!(
                flag.long,
                vec!["yes".to_string(), "assume-yes".to_string()],
                "{key} must resolve to the flag carrying every alias",
            );
            assert_eq!(flag.short, vec!['y'], "{key} must keep the global's short");
        }

        // One logical flag means one object: all three keys share a single `Arc`.
        assert_eq!(
            unique_flags(parsed.available_flags.values()).count(),
            1,
            "all aliases must point at one flag object, got {:?}",
            parsed.available_flags,
        );

        // The global's effect survives the merge, so `-y` still marks the command as writing.
        assert_eq!(
            parsed.available_flags["-y"].effect,
            Some(crate::SpecCommandEffect::Write),
        );
    }

    #[test]
    fn test_redeclared_global_keeps_hidden_alias_metadata() {
        let spec = r#"
flag "--yes" global=#true {
    alias "-q" "--quietly" hide=#true
}
cmd "run" {
    flag "--yes --assume-yes" {
        alias "-s" "--secret" hide=#true
    }
}
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse_partial(&spec, &input(&["test", "run"])).unwrap();
        let merged = &parsed.available_flags["--yes"];
        assert_eq!(merged.hidden_short_aliases, ['q', 's']);
        assert_eq!(merged.hidden_aliases, ["quietly", "secret"]);
        for key in ["-q", "-s", "--quietly", "--secret"] {
            assert!(Arc::ptr_eq(&parsed.available_flags[key], merged), "{key}");
        }
    }

    #[test]
    fn test_redeclared_global_can_promote_hidden_aliases() {
        let spec = r#"
flag "--yes" global=#true {
    alias "-q" "--quietly" hide=#true
}
cmd "run" {
    flag "-q --yes --quietly"
}
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse_partial(&spec, &input(&["test", "run"])).unwrap();
        let merged = &parsed.available_flags["--yes"];
        assert!(merged.hidden_short_aliases.is_empty());
        assert!(merged.hidden_aliases.is_empty());
        for key in ["-q", "--quietly"] {
            assert!(Arc::ptr_eq(&parsed.available_flags[key], merged), "{key}");
        }
    }

    #[test]
    fn test_partially_redeclared_global_keeps_all_aliases_on_one_flag() {
        // Same one-flag-one-object requirement as above, but the child re-declares only ONE of
        // the global's three aliases (`--yes`, not `-y`/`--confirm`) while adding a new one. The
        // aliases the child omits are never visited by the merge loop, so they must be rebound to
        // the merged flag explicitly — otherwise `-y` and `--confirm` keep pointing at the
        // pre-merge global and miss the added `assume-yes`.
        let spec = r#"
flag "-y --yes --confirm" global=#true
cmd "run" {
    flag "--yes --assume-yes"
}
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse_partial(&spec, &input(&["test", "run"])).unwrap();

        for key in ["-y", "--yes", "--confirm", "--assume-yes"] {
            let flag = parsed
                .available_flags
                .get(key)
                .unwrap_or_else(|| panic!("{key} must be recognized after the descent"));
            assert!(flag.global, "{key} must stay global after the descent");
            assert_eq!(
                flag.long,
                vec![
                    "yes".to_string(),
                    "confirm".to_string(),
                    "assume-yes".to_string()
                ],
                "{key} must resolve to the flag carrying every alias",
            );
        }

        assert_eq!(
            unique_flags(parsed.available_flags.values()).count(),
            1,
            "all aliases must point at one flag object, got {:?}",
            parsed.available_flags,
        );
    }

    /// Build a spec shaped like mise's post-mount structure for jdx/mise#11282: a root with
    /// globals (`-E/--env <ENV>`, `--silent`), a `run` subcommand with a non-global flag, and a
    /// MOUNTED task command that declares its own `--env` (with choices) plus `--bump`.
    ///
    /// The task command is marked `mounted` the same way `SpecCommand::mount()` marks the
    /// commands it merges in, so the test stays hermetic (no mount subprocess).
    fn mounted_task_flag_spec() -> Spec {
        let mut task_cmd = SpecCommand::builder()
            .name("mytask")
            .flag(
                SpecFlag::builder()
                    .name("env")
                    .long("env")
                    .arg(
                        SpecArg::builder()
                            .name("name")
                            .choices(["dev", "stage", "prod"])
                            .build(),
                    )
                    .global(false)
                    .build(),
            )
            .flag(
                SpecFlag::builder()
                    .name("bump")
                    .long("bump")
                    .arg(
                        SpecArg::builder()
                            .name("type")
                            .choices(["auto", "major"])
                            .build(),
                    )
                    .global(false)
                    .build(),
            )
            .build();
        task_cmd.mounted = true;

        let mut run_cmd = SpecCommand::builder()
            .name("run")
            .flag(
                SpecFlag::builder()
                    .name("force")
                    .short('f')
                    .long("force")
                    .global(false)
                    .build(),
            )
            .build();
        run_cmd.subcommands.insert("mytask".to_string(), task_cmd);

        let mut cmd = SpecCommand::builder()
            .name("test")
            .flag(
                SpecFlag::builder()
                    .name("env")
                    .short('E')
                    .long("env")
                    .arg(SpecArg::builder().name("ENV").build())
                    .global(true)
                    .build(),
            )
            .flag(
                SpecFlag::builder()
                    .name("silent")
                    .long("silent")
                    .global(true)
                    .build(),
            )
            .build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        }
    }

    #[test]
    fn test_mount_boundary_does_not_apply_inside_the_mounted_tree() {
        // The mounted program's own commands are ordinary commands relative to each other, so
        // descending *within* the mounted tree must follow the normal rules — including keeping
        // an inherited global that a nested command re-declares as non-global (jdx/usage#649).
        // Treating every level of the tree as a mount boundary let the re-declaration shadow the
        // global, which the next descent's `retain(global)` then dropped entirely.
        let deep = SpecCommand::builder().name("deep").build();
        let mut sub = SpecCommand::builder()
            .name("sub")
            // Re-declares the mounted program's own global as non-global.
            .flag(
                SpecFlag::builder()
                    .name("cd")
                    .short('C')
                    .long("cd")
                    .arg(SpecArg::builder().name("dir").build())
                    .global(false)
                    .build(),
            )
            .build();
        sub.subcommands.insert("deep".to_string(), deep);
        let mut task = SpecCommand::builder()
            .name("task")
            .flag(
                SpecFlag::builder()
                    .name("cd")
                    .short('C')
                    .long("cd")
                    .arg(SpecArg::builder().name("dir").build())
                    .global(true)
                    .build(),
            )
            .build();
        task.subcommands.insert("sub".to_string(), sub);
        task.mark_mounted();

        let mut run_cmd = SpecCommand::builder().name("run").build();
        run_cmd.subcommands.insert("task".to_string(), task);
        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        let parsed = parse_partial(&spec, &input(&["test", "run", "task", "sub", "deep"])).unwrap();
        assert!(
            parsed.available_flags.get("--cd").is_some_and(|f| f.global),
            "the mounted program's own global must survive descents inside the mounted tree",
        );
        assert!(
            parsed.completion_flags().contains_key("--cd"),
            "and must still be offered there: it belongs to the mounted program",
        );
        assert!(
            parsed.completion_flags().contains_key("-C"),
            "including the short the nested command re-declared",
        );
    }

    #[test]
    fn test_mount_flags_merged_into_the_mounting_cmd_are_offered() {
        // A mounted spec may declare flags on its own root, which `SpecCommand::merge` folds
        // into the command the mount sits on. They belong to the mounted program, so they must
        // be offered inside the mounted commands rather than filtered out with the mounting
        // CLI's own flags.
        let mut task = SpecCommand::builder()
            .name("task")
            .flag(
                SpecFlag::builder()
                    .name("bump")
                    .long("bump")
                    .global(false)
                    .build(),
            )
            .build();
        task.mark_mounted();

        let mut run_cmd = SpecCommand::builder().name("run").build();
        run_cmd.subcommands.insert("task".to_string(), task);
        // What `mount()` leaves behind when the mounted spec's root declares flags.
        run_cmd.flags = vec![
            SpecFlag::builder()
                .name("tglobal")
                .long("tglobal")
                .global(true)
                .build(),
            SpecFlag::builder()
                .name("tlocal")
                .long("tlocal")
                .global(false)
                .build(),
        ];
        run_cmd.flags_from_mount = true;

        let mut cmd = SpecCommand::builder()
            .name("test")
            .flag(
                SpecFlag::builder()
                    .name("silent")
                    .long("silent")
                    .global(true)
                    .build(),
            )
            .build();
        cmd.subcommands.insert("run".to_string(), run_cmd);
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        let parsed = parse_partial(&spec, &input(&["test", "run", "task"])).unwrap();
        assert_eq!(
            parsed.completion_flags().keys().collect::<Vec<_>>(),
            vec!["--bump", "--tglobal"],
            "the mounted spec's root global belongs to the mounted program; the mounting CLI's \
             `--silent` does not, and the mount's non-global root flag is not inherited",
        );
    }

    #[test]
    fn test_mounted_cmd_does_not_offer_mounting_cli_globals() {
        // Regression for jdx/mise#11282. A mounted command describes another program, which
        // does not accept the mounting CLI's globals (mise forwards everything after a task
        // name to the task). They must stay recognized — they may appear before the mounted
        // command — but must not be offered in completions there.
        let spec = mounted_task_flag_spec();
        let parsed = parse_partial(&spec, &input(&["test", "run", "mytask"])).unwrap();

        // Still recognized for parsing...
        assert!(parsed.available_flags.contains_key("--silent"));
        assert!(parsed.available_flags.contains_key("-E"));
        // ...but belonging to a command above the mount, so not offered.
        assert_eq!(
            parsed.completion_flags().keys().collect::<Vec<_>>(),
            vec!["--bump", "--env"],
            "only the mounted command's own flags may be offered",
        );

        // `run`'s own non-global flag is dropped on descent, as it always was.
        assert!(!parsed.available_flags.contains_key("--force"));
    }

    #[test]
    fn test_mounted_cmd_flag_wins_over_inherited_global() {
        // Second half of jdx/mise#11282: the mounted `--env` (with choices) used to be shadowed
        // by the root's `--env` global, so completing its value fell back to file completion.
        let spec = mounted_task_flag_spec();
        let parsed = parse_partial(&spec, &input(&["test", "run", "mytask", "--env"])).unwrap();

        let awaiting = parsed
            .flag_awaiting_value
            .first()
            .expect("--env should await a value");
        assert_eq!(
            awaiting
                .arg
                .as_ref()
                .and_then(|a| a.choices.as_ref())
                .map(|c| c.choices.clone()),
            Some(vec![
                "dev".to_string(),
                "stage".to_string(),
                "prod".to_string()
            ]),
            "the mounted command's own --env must win over the inherited global",
        );

        // The global's short is not declared by the mounted command, so it keeps pointing at
        // the global and a value passed before the mounted command still parses.
        let parsed =
            parse_partial(&spec, &input(&["test", "-E", "anything", "run", "mytask"])).unwrap();
        assert!(
            parsed.args.is_empty(),
            "prefix global tokens must not be consumed as positionals, got {:?}",
            parsed.args
        );
        assert_eq!(
            parsed.as_env().get("usage_env").map(String::as_str),
            Some("anything"),
        );
    }

    #[test]
    fn test_prefix_flag_keeps_the_flag_it_was_read_as() {
        // A word before the mounted command is re-parsed by Phase 2, when the mounted command
        // already owns the name. It has to stay bound to the flag Phase 1 read it as, or the
        // global's value would be validated against the mounted flag's choices and a legitimate
        // value would be rejected.
        let spec = mounted_task_flag_spec();
        let parsed = parse_partial(
            &spec,
            &input(&["test", "--env", "not-a-task-choice", "run", "mytask"]),
        )
        .unwrap();
        assert!(
            parsed.errors.is_empty(),
            "prefix global value must not be validated against the mounted flag: {:?}",
            parsed
                .errors
                .iter()
                .map(|e| e.to_string())
                .collect::<Vec<_>>(),
        );
        assert_eq!(
            parsed.as_env().get("usage_env").map(String::as_str),
            Some("not-a-task-choice"),
        );

        // The embedded-value form binds the same way.
        let parsed = parse_partial(
            &spec,
            &input(&["test", "--env=not-a-task-choice", "run", "mytask"]),
        )
        .unwrap();
        assert!(parsed.errors.is_empty());
        assert_eq!(
            parsed.as_env().get("usage_env").map(String::as_str),
            Some("not-a-task-choice"),
        );

        // Meanwhile a word *after* the mounted command belongs to the mounted flag, even when
        // the same name was already used before it.
        let parsed = parse_partial(
            &spec,
            &input(&["test", "--env", "prod", "run", "mytask", "--env"]),
        )
        .unwrap();
        let awaiting = parsed
            .flag_awaiting_value
            .first()
            .expect("--env should await a value");
        assert_eq!(
            awaiting
                .arg
                .as_ref()
                .and_then(|a| a.choices.as_ref())
                .map(|c| c.choices.clone()),
            Some(vec![
                "dev".to_string(),
                "stage".to_string(),
                "prod".to_string()
            ]),
            "the mounted command's --env must own the name after the mounted command",
        );
    }

    #[test]
    fn test_non_global_flag_does_not_hide_subcommand() {
        // A non-global flag may precede a subcommand (`mycli run --force task`). Phase 1 used to
        // stop scanning at one, so the subcommand — and any mount on it — was never reached and
        // its name was left to Phase 2 to mis-read as a positional: `unexpected word: mytask`.
        let spec = mounted_task_flag_spec();

        for words in [
            // `run` declares `-f/--force` as non-global.
            &["test", "run", "--force", "mytask"][..],
            &["test", "run", "-f", "mytask"][..],
            // Mixed with a global before the subcommand.
            &["test", "-E", "prod", "run", "--force", "mytask"][..],
        ] {
            let parsed = parse_partial(&spec, &input(words)).unwrap();
            assert_eq!(
                parsed
                    .cmds
                    .iter()
                    .map(|c| c.name.as_str())
                    .collect::<Vec<_>>(),
                vec!["test", "run", "mytask"],
                "{words:?} should descend into the mounted command",
            );
            assert!(
                parsed.args.is_empty(),
                "{words:?} should not consume a positional, got {:?}",
                parsed.args,
            );
            assert_eq!(
                parsed.as_env().get("usage_force").map(String::as_str),
                Some("true"),
                "the non-global flag must still be recorded for {words:?}",
            );
        }

        // A non-global flag that takes a value consumes it, rather than reading the value as the
        // subcommand.
        let mut run_cmd = SpecCommand::builder()
            .name("run")
            .flag(
                SpecFlag::builder()
                    .name("output")
                    .short('o')
                    .long("output")
                    .arg(SpecArg::builder().name("mode").build())
                    .global(false)
                    .build(),
            )
            .build();
        run_cmd.subcommands.insert(
            "task".to_string(),
            SpecCommand::builder().name("task").build(),
        );
        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        let parsed =
            parse_partial(&spec, &input(&["test", "run", "--output", "quiet", "task"])).unwrap();
        assert_eq!(
            parsed
                .cmds
                .iter()
                .map(|c| c.name.as_str())
                .collect::<Vec<_>>(),
            vec!["test", "run", "task"],
        );
        assert_eq!(
            parsed.as_env().get("usage_output").map(String::as_str),
            Some("quiet"),
        );

        // An unknown flag still stops the scan: it may take a value, so the next word cannot be
        // assumed to be a subcommand. `run` takes no positional, so this stays an error.
        assert_parse_err(
            parse_partial(&spec, &input(&["test", "run", "--nope", "task"])),
            "unexpected word: --nope",
        );
    }

    #[test]
    fn test_non_mounted_subcommand_offers_inherited_globals() {
        // Nothing changes for ordinary (non-mounted) subcommands: a global declared above is
        // still both recognized and offered.
        let mut run_cmd = SpecCommand::builder().name("run").build();
        run_cmd.subcommands.insert(
            "nested".to_string(),
            SpecCommand::builder().name("nested").build(),
        );
        let mut cmd = SpecCommand::builder()
            .name("test")
            .flag(
                SpecFlag::builder()
                    .name("silent")
                    .long("silent")
                    .global(true)
                    .build(),
            )
            .build();
        cmd.subcommands.insert("run".to_string(), run_cmd);
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        let parsed = parse_partial(&spec, &input(&["test", "run", "nested"])).unwrap();
        assert_eq!(
            parsed.completion_flags().keys().collect::<Vec<_>>(),
            parsed.available_flags.keys().collect::<Vec<_>>(),
        );
        assert!(parsed.completion_flags().contains_key("--silent"));
    }

    #[test]
    fn test_subcommand_alias_collision_keeps_last_owner() {
        // The orphan-alias merge must not disturb how two flags in the SAME subcommand that
        // share an alias are resolved. Historically the flattened flag map gave the shared
        // alias to the LAST-declared flag (last-writer-wins); that must be preserved.
        let run_cmd = SpecCommand::builder()
            .name("run")
            .flag(
                SpecFlag::builder()
                    .name("alpha")
                    .short('x')
                    .long("alpha")
                    .global(false)
                    .build(),
            )
            .flag(
                SpecFlag::builder()
                    .name("beta")
                    .short('x')
                    .long("beta")
                    .global(false)
                    .build(),
            )
            .build();
        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        let parsed = parse_partial(&spec, &input(&["test", "run"])).unwrap();
        // `-x` is declared by both flags; the last one (`beta`) keeps it, as before the fix.
        assert_eq!(
            parsed.available_flags.get("-x").map(|f| f.name.as_str()),
            Some("beta"),
            "the last-declared flag must keep a shared short alias",
        );
        // Both distinct long aliases remain recognized and point to their own flag.
        assert_eq!(
            parsed
                .available_flags
                .get("--alpha")
                .map(|f| f.name.as_str()),
            Some("alpha"),
        );
        assert_eq!(
            parsed
                .available_flags
                .get("--beta")
                .map(|f| f.name.as_str()),
            Some("beta"),
        );
    }

    #[test]
    fn test_default_subcommand_same_name_child() {
        // Test that default_subcommand doesn't cause issues when the default subcommand
        // has a child with the same name (e.g., "run" has a task named "run").
        // This verifies we don't switch multiple times or get stuck in a loop.
        let run_task = SpecCommand::builder()
            .name("run")
            .arg(SpecArg::builder().name("args").build())
            .build();
        let mut run_cmd = SpecCommand::builder().name("run").build();
        run_cmd.subcommands.insert("run".to_string(), run_task);

        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            default_subcommand: Some("run".to_string()),
            ..Default::default()
        };

        // "test run" explicitly matches the "run" subcommand (not via default_subcommand)
        let input = vec!["test".to_string(), "run".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        // Should have two commands: root and "run"
        assert_eq!(parsed.cmds.len(), 2);
        assert_eq!(parsed.cmds[0].name, "test");
        assert_eq!(parsed.cmds[1].name, "run");

        // "test run run" should descend into the "run" task (child of "run" subcommand)
        let input = vec![
            "test".to_string(),
            "run".to_string(),
            "run".to_string(),
            "hello".to_string(),
        ];
        let parsed = parse(&spec, &input).unwrap();

        assert_eq!(parsed.cmds.len(), 3);
        assert_eq!(parsed.cmds[0].name, "test");
        assert_eq!(parsed.cmds[1].name, "run");
        assert_eq!(parsed.cmds[2].name, "run");
        assert_eq!(parsed.args.len(), 1);
        let value = parsed.args.values().next().unwrap();
        assert_eq!(value.to_string(), "hello");

        // Key test case: "test other" should switch to default subcommand "run"
        // and treat "other" as a positional arg (not try to switch again because
        // "run" also has a "run" child).
        let mut run_cmd = SpecCommand::builder()
            .name("run")
            .arg(SpecArg::builder().name("task").build())
            .build();
        let run_task = SpecCommand::builder().name("run").build();
        run_cmd.subcommands.insert("run".to_string(), run_task);

        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            default_subcommand: Some("run".to_string()),
            ..Default::default()
        };

        let input = vec!["test".to_string(), "other".to_string()];
        let parsed = parse(&spec, &input).unwrap();

        // Should have two commands: root and "run" (the default)
        // We should NOT have switched again to the "run" task child
        assert_eq!(parsed.cmds.len(), 2);
        assert_eq!(parsed.cmds[0].name, "test");
        assert_eq!(parsed.cmds[1].name, "run");

        // "other" should be parsed as a positional arg
        assert_eq!(parsed.args.len(), 1);
        let value = parsed.args.values().next().unwrap();
        assert_eq!(value.to_string(), "other");
    }

    #[test]
    fn test_restart_token() {
        // Test that restart_token resets argument parsing
        let run_cmd = SpecCommand::builder()
            .name("run")
            .arg(SpecArg::builder().name("task").build())
            .restart_token(":::".to_string())
            .build();
        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // "test run task1 ::: task2" - should end up with task2 as the arg
        let input = vec![
            "test".to_string(),
            "run".to_string(),
            "task1".to_string(),
            ":::".to_string(),
            "task2".to_string(),
        ];
        let parsed = parse(&spec, &input).unwrap();

        // After restart, args were cleared and task2 was parsed
        assert_eq!(parsed.args.len(), 1);
        let value = parsed.args.values().next().unwrap();
        assert_eq!(value.to_string(), "task2");
    }

    #[test]
    fn test_restart_token_multiple() {
        // Test multiple restart tokens
        let run_cmd = SpecCommand::builder()
            .name("run")
            .arg(SpecArg::builder().name("task").build())
            .restart_token(":::".to_string())
            .build();
        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // "test run task1 ::: task2 ::: task3" - should end up with task3 as the arg
        let input = vec![
            "test".to_string(),
            "run".to_string(),
            "task1".to_string(),
            ":::".to_string(),
            "task2".to_string(),
            ":::".to_string(),
            "task3".to_string(),
        ];
        let parsed = parse(&spec, &input).unwrap();

        // After multiple restarts, args were cleared and task3 was parsed
        assert_eq!(parsed.args.len(), 1);
        let value = parsed.args.values().next().unwrap();
        assert_eq!(value.to_string(), "task3");
    }

    #[test]
    fn test_restart_token_clears_flag_awaiting_value() {
        // Test that restart_token clears pending flag values
        let run_cmd = SpecCommand::builder()
            .name("run")
            .arg(SpecArg::builder().name("task").build())
            .flag(
                SpecFlag::builder()
                    .name("jobs")
                    .long("jobs")
                    .arg(SpecArg::builder().name("count").build())
                    .build(),
            )
            .restart_token(":::".to_string())
            .build();
        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // "test run task1 --jobs ::: task2" - task2 should be an arg, not a flag value
        let input = vec![
            "test".to_string(),
            "run".to_string(),
            "task1".to_string(),
            "--jobs".to_string(),
            ":::".to_string(),
            "task2".to_string(),
        ];
        let parsed = parse(&spec, &input).unwrap();

        // task2 should be parsed as the task arg, not as --jobs value
        assert_eq!(parsed.args.len(), 1);
        let value = parsed.args.values().next().unwrap();
        assert_eq!(value.to_string(), "task2");
        // --jobs should not have a value
        assert!(parsed.flag_awaiting_value.is_empty());
    }

    #[test]
    fn test_restart_token_resets_double_dash() {
        // Test that restart_token resets the -- separator effect
        let run_cmd = SpecCommand::builder()
            .name("run")
            .arg(SpecArg::builder().name("task").build())
            .arg(SpecArg::builder().name("extra_args").var(true).build())
            .flag(SpecFlag::builder().name("verbose").long("verbose").build())
            .restart_token(":::".to_string())
            .build();
        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // "test run task1 -- extra ::: --verbose task2" - --verbose should be a flag after :::
        let input = vec![
            "test".to_string(),
            "run".to_string(),
            "task1".to_string(),
            "--".to_string(),
            "extra".to_string(),
            ":::".to_string(),
            "--verbose".to_string(),
            "task2".to_string(),
        ];
        let parsed = parse(&spec, &input).unwrap();

        // --verbose should be parsed as a flag (not an arg) after the restart
        assert!(parsed.flags.keys().any(|f| f.name == "verbose"));
        // task2 should be the arg after restart
        let task_arg = parsed.args.keys().find(|a| a.name == "task").unwrap();
        let value = parsed.args.get(task_arg).unwrap();
        assert_eq!(value.to_string(), "task2");
    }

    #[test]
    fn test_double_dashes_without_preserve() {
        // Only the first `--` is a separator; a later one is a value, because flag
        // parsing has already stopped and there is nothing left for it to do.
        // `preserve` is about the *first* one — see the test below, where none is
        // consumed at all.
        let run_cmd = SpecCommand::builder()
            .name("run")
            .arg(SpecArg::builder().name("args").var(true).build())
            .build();
        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // "test run arg1 -- arg2 -- arg3": the first separates, the second is a value
        let input = vec![
            "test".to_string(),
            "run".to_string(),
            "arg1".to_string(),
            "--".to_string(),
            "arg2".to_string(),
            "--".to_string(),
            "arg3".to_string(),
        ];
        let parsed = parse(&spec, &input).unwrap();

        let args_arg = parsed.args.keys().find(|a| a.name == "args").unwrap();
        let value = parsed.args.get(args_arg).unwrap();
        assert_eq!(value.to_string(), "arg1 arg2 -- arg3");
    }

    #[test]
    fn test_double_dashes_with_preserve() {
        // Test that variadic args WITH `preserve` keep all double dashes
        let run_cmd = SpecCommand::builder()
            .name("run")
            .arg(
                SpecArg::builder()
                    .name("args")
                    .var(true)
                    .double_dash(SpecDoubleDashChoices::Preserve)
                    .build(),
            )
            .build();
        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // "test run arg1 -- arg2 -- arg3" - all double dashes should be preserved
        let input = vec![
            "test".to_string(),
            "run".to_string(),
            "arg1".to_string(),
            "--".to_string(),
            "arg2".to_string(),
            "--".to_string(),
            "arg3".to_string(),
        ];
        let parsed = parse(&spec, &input).unwrap();

        let args_arg = parsed.args.keys().find(|a| a.name == "args").unwrap();
        let value = parsed.args.get(args_arg).unwrap();
        assert_eq!(value.to_string(), "arg1 -- arg2 -- arg3");
    }

    #[test]
    fn test_double_dashes_with_preserve_only_dashes() {
        // Test that variadic args WITH `preserve` keep all double dashes even
        // if the values are just double dashes
        let run_cmd = SpecCommand::builder()
            .name("run")
            .arg(
                SpecArg::builder()
                    .name("args")
                    .var(true)
                    .double_dash(SpecDoubleDashChoices::Preserve)
                    .build(),
            )
            .build();
        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // "test run -- --" - all double dashes should be preserved
        let input = vec![
            "test".to_string(),
            "run".to_string(),
            "--".to_string(),
            "--".to_string(),
        ];
        let parsed = parse(&spec, &input).unwrap();

        let args_arg = parsed.args.keys().find(|a| a.name == "args").unwrap();
        let value = parsed.args.get(args_arg).unwrap();
        assert_eq!(value.to_string(), "-- --");
    }

    #[test]
    fn test_double_dashes_with_preserve_multiple_args() {
        // Test with multiple args where only the second has has `preserve`
        let run_cmd = SpecCommand::builder()
            .name("run")
            .arg(SpecArg::builder().name("task").build())
            .arg(
                SpecArg::builder()
                    .name("extra_args")
                    .var(true)
                    .double_dash(SpecDoubleDashChoices::Preserve)
                    .build(),
            )
            .build();
        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);

        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // The first arg "task1" is captured normally
        // Then extra_args with `preserve` captures everything, including the "--" tokens
        let input = vec![
            "test".to_string(),
            "run".to_string(),
            "task1".to_string(),
            "--".to_string(),
            "arg1".to_string(),
            "--".to_string(),
            "--foo".to_string(),
        ];
        let parsed = parse(&spec, &input).unwrap();

        let task_arg = parsed.args.keys().find(|a| a.name == "task").unwrap();
        let task_value = parsed.args.get(task_arg).unwrap();
        assert_eq!(task_value.to_string(), "task1");

        let extra_arg = parsed.args.keys().find(|a| a.name == "extra_args").unwrap();
        let extra_value = parsed.args.get(extra_arg).unwrap();
        assert_eq!(extra_value.to_string(), "-- arg1 -- --foo");
    }

    fn spec_with_args(args: impl IntoIterator<Item = SpecArg>) -> Spec {
        let cmd = SpecCommand::builder().name("test").args(args).build();
        Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        }
    }

    fn arg_value(parsed: &ParseOutput, name: &str) -> String {
        let arg = parsed
            .args
            .keys()
            .find(|a| a.name == name)
            .unwrap_or_else(|| panic!("expected arg {name} to be parsed"));
        parsed.args.get(arg).unwrap().to_string()
    }

    fn required_arg(name: &str) -> SpecArg {
        SpecArg::builder()
            .name(name)
            .var(true)
            .required(false)
            .double_dash(SpecDoubleDashChoices::Required)
            .build()
    }

    #[test]
    fn test_double_dash_required_reports_error_once_for_variadic() {
        // A variadic arg is offered every remaining word, but the mistake is one mistake.
        let spec = spec_with_args([required_arg("files")]);

        let parsed = parse_partial(&spec, &input(&["test", "a", "b", "c"])).unwrap();

        assert!(parsed.args.is_empty());
        assert_eq!(parsed.errors.len(), 1);
        assert!(
            matches!(&parsed.errors[0], UsageErr::ArgRequiresDoubleDash(name) if name == "files")
        );
    }

    #[test]
    fn test_double_dash_required_suppresses_missing_arg() {
        // The arg is never filled, so the end-of-parse check would also call it missing.
        let spec = spec_with_args([SpecArg::builder()
            .name("file")
            .required(true)
            .double_dash(SpecDoubleDashChoices::Required)
            .build()]);

        let parsed = parse_partial(&spec, &input(&["test", "x"])).unwrap();

        assert_eq!(parsed.errors.len(), 1);
        assert!(matches!(
            &parsed.errors[0],
            UsageErr::ArgRequiresDoubleDash(_)
        ));
        // The cursor stays put, so a completion keeps offering the same arg.
        assert_eq!(
            parsed.next_arg.as_ref().map(|a| a.name.as_str()),
            Some("file")
        );
        assert!(!parsed.double_dash_seen);
    }

    #[test]
    fn test_double_dash_routes_to_required_arg() {
        // Everything after `--` belongs to the arg that requires it, even though the greedy
        // variadic before it would otherwise swallow the rest (clap's `Arg::last(true)`).
        let spec = spec_with_args([
            SpecArg::builder()
                .name("tool")
                .var(true)
                .required(false)
                .build(),
            required_arg("command"),
        ]);

        let parsed = parse(&spec, &input(&["test", "node@20", "--", "node", "app.js"])).unwrap();

        assert_eq!(arg_value(&parsed, "tool"), "node@20");
        assert_eq!(arg_value(&parsed, "command"), "node app.js");
        assert!(parsed.double_dash_seen);
    }

    #[test]
    fn test_double_dash_routes_with_gap_reports_missing_arg() {
        // Jumping the cursor leaves `tool` empty even though `command` is filled, so the
        // "is it filled?" check cannot be a count of how many args were filled.
        let spec = spec_with_args([
            SpecArg::builder()
                .name("tool")
                .var(true)
                .required(true)
                .build(),
            required_arg("command"),
        ]);

        let parsed = parse_partial(&spec, &input(&["test", "--", "ls"])).unwrap();

        assert_eq!(arg_value(&parsed, "command"), "ls");
        assert!(parsed.args.keys().all(|a| a.name != "tool"));
        assert!(parsed
            .errors
            .iter()
            .any(|e| matches!(e, UsageErr::MissingArg(name) if name == "tool")));
    }

    #[test]
    fn test_double_dash_gap_applies_defaults() {
        // Same gap, seen from `Parser::parse`: the skipped arg still gets its default.
        let spec = spec_with_args([
            SpecArg::builder()
                .name("tool")
                .var(true)
                .required(false)
                .default_value("node@20")
                .build(),
            required_arg("command"),
        ]);

        let parsed = parse(&spec, &input(&["test", "--", "ls"])).unwrap();

        assert_eq!(arg_value(&parsed, "command"), "ls");
        assert_eq!(arg_value(&parsed, "tool"), "node@20");
    }

    fn spec_with_restart_token_and_required_arg() -> Spec {
        let run_cmd = SpecCommand::builder()
            .name("run")
            .arg(SpecArg::builder().name("task").build())
            .arg(required_arg("run_args"))
            .restart_token(":::".to_string())
            .build();
        let mut cmd = SpecCommand::builder().name("test").build();
        cmd.subcommands.insert("run".to_string(), run_cmd);
        Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        }
    }

    #[test]
    fn test_double_dash_required_restart_token_resets_separator() {
        // The `--` before `:::` belongs to the previous invocation only.
        let spec = spec_with_restart_token_and_required_arg();

        let parsed = parse_partial(
            &spec,
            &input(&["test", "run", "task1", "--", "a", ":::", "task2", "b"]),
        )
        .unwrap();

        assert_eq!(arg_value(&parsed, "task"), "task2");
        assert!(parsed.args.keys().all(|a| a.name != "run_args"));
        // Reported once even though the arg was violated after already succeeding once.
        assert_eq!(
            parsed
                .errors
                .iter()
                .filter(|e| matches!(e, UsageErr::ArgRequiresDoubleDash(_)))
                .count(),
            1
        );
    }

    #[test]
    fn test_double_dash_required_restart_token_accepts_new_separator() {
        let spec = spec_with_restart_token_and_required_arg();

        let parsed = parse(
            &spec,
            &input(&["test", "run", "task1", "--", "a", ":::", "task2", "--", "c"]),
        )
        .unwrap();

        assert_eq!(arg_value(&parsed, "task"), "task2");
        assert_eq!(arg_value(&parsed, "run_args"), "c");
    }

    #[test]
    fn test_double_dash_preserve_is_not_a_separator() {
        // A `--` that `preserve` keeps is a *value* of that arg, so it must not unlock the
        // arg that requires a separator. Deliberate: one token cannot be both.
        let spec = spec_with_args([
            SpecArg::builder()
                .name("kept")
                .var(true)
                .var_max(1)
                .required(false)
                .double_dash(SpecDoubleDashChoices::Preserve)
                .build(),
            required_arg("rest"),
        ]);

        let parsed = parse_partial(&spec, &input(&["test", "--", "x"])).unwrap();

        assert_eq!(arg_value(&parsed, "kept"), "--");
        assert!(parsed.args.keys().all(|a| a.name != "rest"));
        assert!(!parsed.double_dash_seen);
        assert_eq!(parsed.errors.len(), 1);
    }

    #[test]
    fn test_double_dash_required_does_not_bail_in_parse_partial() {
        // Completions parse half-typed command lines; they must still get a result.
        let spec = spec_with_args([required_arg("file")]);

        assert!(parse_partial(&spec, &input(&["test", "x"])).is_ok());
        assert!(parse(&spec, &input(&["test", "x"])).is_err());
    }

    #[test]
    fn test_double_dash_without_required_arg_does_not_move_cursor() {
        // Specs with no `double_dash="required"` arg are untouched by the jump.
        let spec = spec_with_args([
            SpecArg::builder().name("first").required(false).build(),
            SpecArg::builder().name("second").required(false).build(),
        ]);

        let parsed = parse(&spec, &input(&["test", "--", "a", "b"])).unwrap();

        assert_eq!(arg_value(&parsed, "first"), "a");
        assert_eq!(arg_value(&parsed, "second"), "b");
        assert!(parsed.next_arg.is_none());
    }

    #[test]
    fn test_parser_with_custom_env_for_required_arg() {
        let spec = spec_with_arg(
            SpecArg::builder()
                .name("name")
                .env("NAME")
                .required(true)
                .build(),
        );
        std::env::remove_var("NAME");

        let parsed = parse_with_env(&spec, &["test"], &[("NAME", "john")])
            .expect("parse should succeed with custom env");
        assert_eq!(parsed.args.len(), 1);
        assert_eq!(first_string_value(&parsed), "john");
    }

    #[test]
    fn test_parser_with_custom_env_for_required_flag() {
        let spec = spec_with_flag(
            SpecFlag::builder()
                .long("name")
                .env("NAME")
                .required(true)
                .arg(SpecArg::builder().name("name").build())
                .build(),
        );
        std::env::remove_var("NAME");

        let parsed = parse_with_env(&spec, &["test"], &[("NAME", "jane")])
            .expect("parse should succeed with custom env");
        assert_eq!(parsed.flags.len(), 1);
        assert_eq!(first_string_value(&parsed), "jane");
    }

    #[test]
    fn test_flag_environment_fallbacks_preserve_declaration_order() {
        let spec = spec_with_flag(
            SpecFlag::builder()
                .long("name")
                .env("NAME")
                .env_fallback("OLD_NAME")
                .env_fallback("OLDER_NAME")
                .deprecated_env("DEPRECATED_NAME")
                .arg(SpecArg::builder().name("name").build())
                .build(),
        );

        let parsed = parse_with_env(
            &spec,
            &["test"],
            &[
                ("NAME", "canonical"),
                ("OLD_NAME", "fallback"),
                ("DEPRECATED_NAME", "deprecated"),
            ],
        )
        .unwrap();
        assert_eq!(first_string_value(&parsed), "canonical");

        let parsed = parse_with_env(
            &spec,
            &["test"],
            &[("OLDER_NAME", "older"), ("OLD_NAME", "old")],
        )
        .unwrap();
        assert_eq!(first_string_value(&parsed), "old");

        let parsed =
            parse_with_env(&spec, &["test"], &[("DEPRECATED_NAME", "deprecated")]).unwrap();
        assert_eq!(first_string_value(&parsed), "deprecated");
    }

    #[test]
    fn a_value_from_a_deprecated_alias_says_which_name_to_use() {
        let spec = spec_with_flag(
            SpecFlag::builder()
                .long("name")
                .env("NAME")
                .deprecated_env("DEPRECATED_NAME")
                .arg(SpecArg::builder().name("name").build())
                .build(),
        );

        // The current name is not a deprecated one, and says nothing.
        let parsed = parse_with_env(&spec, &["test"], &[("NAME", "canonical")]).unwrap();
        assert!(parsed.warnings.is_empty(), "{:?}", parsed.warnings);

        let parsed =
            parse_with_env(&spec, &["test"], &[("DEPRECATED_NAME", "deprecated")]).unwrap();
        assert_eq!(parsed.warnings.len(), 1, "{:?}", parsed.warnings);
        assert_eq!(
            parsed.warnings[0].kind,
            crate::warn::WarningKind::DeprecatedEnv
        );
        assert_eq!(parsed.warnings[0].name, "DEPRECATED_NAME");
        assert_eq!(parsed.warnings[0].replacement.as_deref(), Some("NAME"));
        // Reported, not printed, and the value still arrives.
        assert_eq!(first_string_value(&parsed), "deprecated");
    }

    #[test]
    fn a_deprecated_flag_reports_only_when_it_was_used() {
        let spec = spec_with_flag(
            SpecFlag::builder()
                .long("output")
                .deprecated("use --out")
                .deprecated_remove_at("3.0.0")
                .arg(SpecArg::builder().name("output").build())
                .build(),
        );

        let parsed = parse_with_env(&spec, &["test"], &[]).unwrap();
        assert!(parsed.warnings.is_empty(), "{:?}", parsed.warnings);

        let parsed = parse_with_env(&spec, &["test", "--output", "a.txt"], &[]).unwrap();
        assert_eq!(parsed.warnings.len(), 1, "{:?}", parsed.warnings);
        assert_eq!(
            parsed.warnings[0].kind,
            crate::warn::WarningKind::DeprecatedFlag
        );
        // Named the way it was typed, dashes and all.
        assert_eq!(parsed.warnings[0].name, "--output");
        assert_eq!(parsed.warnings[0].remove_at.as_deref(), Some("3.0.0"));
        assert_eq!(
            parsed.warnings[0].render(),
            "warning: --output is deprecated, removed at 3.0.0: use --out\n",
        );
    }

    #[test]
    fn a_milestone_the_spec_has_not_reached_stays_quiet() {
        let flag = SpecFlag::builder()
            .long("output")
            .deprecated("use --out")
            .deprecated_warn_at("9.0.0")
            .arg(SpecArg::builder().name("output").build())
            .build();
        let mut spec = spec_with_flag(flag);
        spec.version = Some("2.0.0".to_string());

        let parsed = parse_with_env(&spec, &["test", "--output", "a.txt"], &[]).unwrap();
        assert!(parsed.warnings.is_empty(), "{:?}", parsed.warnings);

        // And once the CLI is the release that was named, it speaks up.
        spec.version = Some("9.0.0".to_string());
        let parsed = parse_with_env(&spec, &["test", "--output", "a.txt"], &[]).unwrap();
        assert_eq!(parsed.warnings.len(), 1, "{:?}", parsed.warnings);
    }

    #[test]
    fn test_parser_with_custom_env_still_fails_when_missing() {
        let spec = spec_with_arg(
            SpecArg::builder()
                .name("name")
                .env("NAME")
                .required(true)
                .build(),
        );
        std::env::remove_var("NAME");
        assert!(parse_with_env(&spec, &["test"], &[]).is_err());
    }

    #[test]
    fn test_parser_does_not_treat_env_choice_value_as_help() {
        let spec = spec_with_arg(
            SpecArg::builder()
                .name("env")
                .env("CURRENT_ENV")
                .choices(["dev", "staging"])
                .required(false)
                .build(),
        );

        assert_parse_err(
            parse_with_env(&spec, &["test"], &[("CURRENT_ENV", "--help")]),
            "Invalid choice for arg env: --help, expected one of dev, staging",
        );
    }

    #[test]
    fn test_parser_does_not_treat_default_choice_value_as_help() {
        let spec = spec_with_flag(
            SpecFlag::builder()
                .long("env")
                .arg(
                    SpecArg::builder()
                        .name("env")
                        .choices(["dev", "staging"])
                        .build(),
                )
                .default_value("--help")
                .build(),
        );

        assert_parse_err(
            parse_with_env(&spec, &["test"], &[]),
            "Invalid choice for option env: --help, expected one of dev, staging",
        );
    }

    /// argv as `parse` wants it, program name included.
    fn words(of: &[&str]) -> Vec<String> {
        of.iter().map(|s| s.to_string()).collect()
    }

    #[test]
    fn a_command_that_needs_a_subcommand_says_so() {
        // The spec has carried `subcommand_required` since the derive needed it, and this parser
        // never read it — so `mise generate`, which declares it, parsed as a complete
        // invocation while usage-argv and clap both refused. Found by the differential fuzzer.
        let spec: Spec = r#"
name "ex"
bin "ex"
cmd "gen" subcommand_required=#true {
    cmd "two" {}
    cmd "one" {}
    cmd "secret" hide=#true {}
    alias "g"
}
cmd "open" {
    cmd "sub" {}
}
"#
        .parse()
        .unwrap();

        let err = parse(&spec, &words(&["ex", "gen"])).unwrap_err();
        // Sorted, so the message does not depend on map order; hidden commands left out,
        // because a message telling someone to type a hidden name is worse than a vague one;
        // and the alias not listed beside the name it points at.
        assert_eq!(err.to_string(), "`gen` needs a subcommand: one of one, two");

        // Reached through its alias, and still about the command rather than the spelling.
        let err = parse(&spec, &words(&["ex", "g"])).unwrap_err();
        assert!(err.to_string().starts_with("`gen` needs a subcommand"));

        // Given one: fine.
        parse(&spec, &words(&["ex", "gen", "one"])).unwrap();

        // And a command that has subcommands without declaring them required is untouched —
        // this is the half that keeps the check from being "any command with children".
        parse(&spec, &words(&["ex", "open"])).unwrap();
        parse(&spec, &words(&["ex", "open", "sub"])).unwrap();
    }

    #[test]
    fn arg_required_else_help_observes_the_selected_commands_argv() {
        let spec: Spec = r#"
name "ex"
bin "ex"
flag "--verbose" global=#true
cmd "run" arg_required_else_help=#true {
    flag "--all"
}
"#
        .parse()
        .unwrap();
        let words = |items: &[&str]| items.iter().map(|s| (*s).to_string()).collect::<Vec<_>>();

        let err = parse(&spec, &words(&["ex", "run"])).unwrap_err();
        assert!(err.to_string().contains("Usage: ex run"), "{err}");

        // A global before the command belongs to the ancestor. It selected `run`, but did not
        // give `run` an argument of its own.
        let err = parse(&spec, &words(&["ex", "--verbose", "run"])).unwrap_err();
        assert!(err.to_string().contains("Usage: ex run"), "{err}");

        parse(&spec, &words(&["ex", "run", "--all"])).expect("run received an argv token");
    }

    #[test]
    fn an_unmatched_word_is_forwarded_when_external_subcommand_is_set() {
        let spec: Spec = r#"
name "ex"
bin "ex"
unknown_flags "error"
external_subcommand #true
cmd "install"
flag "-v --verbose" global=#true
"#
        .parse()
        .unwrap();

        let parsed = parse(&spec, &input(&["ex", "foo", "--help", "bar"])).unwrap();
        assert_eq!(
            parsed.external,
            Some(vec!["foo".into(), "--help".into(), "bar".into()])
        );
        assert!(parsed.flags.is_empty());

        // Known subcommands still win.
        let parsed = parse(&spec, &input(&["ex", "install"])).unwrap();
        assert_eq!(parsed.cmd.name, "install");
        assert!(parsed.external.is_none());

        // A global flag before the unmatched word still binds on the parent.
        let parsed = parse(&spec, &input(&["ex", "-v", "foo", "--verbose"])).unwrap();
        assert_eq!(
            parsed.external,
            Some(vec!["foo".into(), "--verbose".into()])
        );
        assert!(parsed.flags.keys().any(|flag| flag.name == "verbose"));

        // An unknown flag on the parent is still an error, which is what clap does.
        assert!(parse(&spec, &input(&["ex", "--wat"])).is_err());

        // A negative number is a value, not a flag, so it can be the unmatched word.
        // usage-argv already forwarded `-1`; Phase 1 used to treat every `starts_with('-')`
        // token as a flag and never reach the catch-all.
        let parsed = parse(&spec, &input(&["ex", "-1", "rest"])).unwrap();
        assert_eq!(parsed.external, Some(vec!["-1".into(), "rest".into()]));
    }

    #[test]
    fn an_external_subcommand_satisfies_subcommand_required() {
        let mut spec: Spec = r#"
name "ex"
bin "ex"
external_subcommand #true
cmd "install"
"#
        .parse()
        .unwrap();
        spec.cmd.subcommand_required = true;

        parse(&spec, &input(&["ex", "foo", "--help"])).unwrap();
        assert!(parse(&spec, &input(&["ex"])).is_err());
    }

    #[test]
    fn a_default_subcommand_outranks_an_external_one() {
        let spec: Spec = r#"
name "ex"
bin "ex"
default_subcommand "run"
external_subcommand #true
cmd "run" {
    arg "[task]"
}
"#
        .parse()
        .unwrap();

        let parsed = parse(&spec, &input(&["ex", "build"])).unwrap();
        assert_eq!(parsed.cmd.name, "run");
        assert!(parsed.external.is_none());
        assert_eq!(first_string_value(&parsed), "build");
    }

    #[test]
    fn multicall_basename_strips_a_path_and_exe() {
        assert_eq!(multicall_basename("/usr/bin/ls"), "ls");
        assert_eq!(multicall_basename(r"C:\busybox\ls.exe"), "ls");
        assert_eq!(multicall_basename("LS.EXE"), "LS");
        assert_eq!(multicall_basename("busybox"), "busybox");
    }

    #[test]
    fn a_multicall_applet_is_the_first_word() {
        let spec: Spec = r#"
name "busybox"
bin "busybox"
multicall #true
cmd "ls" {
    arg "[ARGS]" var=#true
}
cmd "cat"
"#
        .parse()
        .unwrap();

        // A symlink: argv[0] is the applet.
        let parsed = parse(&spec, &input(&["/usr/bin/ls", "-l"])).unwrap();
        assert_eq!(parsed.cmd.name, "ls");
        match parsed.args.values().next() {
            Some(ParseValue::MultiString(values)) => assert_eq!(values, &["-l".to_string()]),
            other => panic!("expected ARGS to collect -l, got {other:?}"),
        }

        // A dispatcher invocation still skips argv[0].
        let parsed = parse(&spec, &input(&["/usr/bin/busybox", "ls", "-l"])).unwrap();
        assert_eq!(parsed.cmd.name, "ls");

        // Configured dispatcher values receive the same path and extension normalization.
        let mut configured = spec.clone();
        configured.name = "BusyBox".to_string();
        configured.bin = "/opt/bin/busybox.exe".to_string();
        let parsed = parse(&configured, &input(&["/usr/bin/busybox.exe", "ls", "-l"])).unwrap();
        assert_eq!(parsed.cmd.name, "ls");

        // `.exe` is stripped so Windows and Unix agree.
        let parsed = parse(&spec, &input(&["ls.exe"])).unwrap();
        assert_eq!(parsed.cmd.name, "ls");

        // Without the property, argv[0] is discarded as usual.
        let mut plain = spec.clone();
        plain.multicall = false;
        let parsed = parse(&plain, &input(&["/usr/bin/ls", "ls"])).unwrap();
        assert_eq!(parsed.cmd.name, "ls");
    }

    #[test]
    fn a_multicall_unknown_applet_can_be_external() {
        let spec: Spec = r#"
name "busybox"
bin "busybox"
multicall #true
unknown_flags "error"
external_subcommand #true
cmd "ls"
"#
        .parse()
        .unwrap();

        let parsed = parse(&spec, &input(&["/usr/bin/git", "--help"])).unwrap();
        assert_eq!(parsed.external, Some(vec!["git".into(), "--help".into()]));

        let mut closed = spec.clone();
        closed.cmd.external_subcommand = false;
        assert!(parse(&closed, &input(&["wat"])).is_err());
    }

    #[cfg(feature = "unstable_choices_env")]
    #[test]
    fn test_parser_arg_choices_from_custom_env() {
        let spec = spec_arg_choices_env("DEPLOY_ENVS");

        let parsed =
            parse_with_env(&spec, &["test", "bar"], &[("DEPLOY_ENVS", "foo,bar baz")]).unwrap();
        assert_eq!(first_string_value(&parsed), "bar");

        assert_parse_err(
            parse_with_env(&spec, &["test", "prod"], &[("DEPLOY_ENVS", "foo,bar baz")]),
            "Invalid choice for arg env: prod, expected one of foo, bar, baz",
        );
        assert_parse_err(
            parse_with_env(&spec, &["test", "prod"], &[]),
            "Invalid choice for arg env: prod, no choices resolved from env DEPLOY_ENVS",
        );
    }

    #[cfg(feature = "unstable_choices_env")]
    #[test]
    fn test_parser_validates_flag_choices_from_custom_env() {
        let spec = spec_flag_choices_env("DEPLOY_ENVS");
        let parsed = parse_with_env(
            &spec,
            &["test", "--env", "baz"],
            &[("DEPLOY_ENVS", "foo,bar baz")],
        )
        .unwrap();
        assert_eq!(first_string_value(&parsed), "baz");
    }

    #[cfg(feature = "unstable_choices_env")]
    #[test]
    fn test_parser_revalidates_env_and_default_values_against_choices_env() {
        let arg_env_spec = spec_with_arg(
            SpecArg::builder()
                .name("env")
                .env("CURRENT_ENV")
                .choices_env("DEPLOY_ENVS")
                .build(),
        );
        assert_parse_err(
            parse_with_env(
                &arg_env_spec,
                &["test"],
                &[("CURRENT_ENV", "prod"), ("DEPLOY_ENVS", "dev,staging")],
            ),
            "Invalid choice for arg env: prod, expected one of dev, staging",
        );

        let flag_default_spec = spec_with_flag(
            SpecFlag::builder()
                .long("env")
                .arg(
                    SpecArg::builder()
                        .name("env")
                        .choices_env("DEPLOY_ENVS")
                        .build(),
                )
                .default_value("prod")
                .build(),
        );
        assert_parse_err(
            parse_with_env(
                &flag_default_spec,
                &["test"],
                &[("DEPLOY_ENVS", "dev,staging")],
            ),
            "Invalid choice for option env: prod, expected one of dev, staging",
        );
    }

    #[test]
    fn test_variadic_arg_captures_unknown_flags_from_spec_string() {
        let spec: Spec = r#"
            flag "-v --verbose" var=#true
            arg "[database]" default="myapp_dev"
            arg "[args...]"
        "#
        .parse()
        .unwrap();
        let input: Vec<String> = vec!["test", "mydb", "--host", "localhost"]
            .into_iter()
            .map(String::from)
            .collect();
        let parsed = parse(&spec, &input).unwrap();
        let env = parsed.as_env();
        assert_eq!(env.get("usage_database").unwrap(), "mydb");
        assert_eq!(env.get("usage_args").unwrap(), "--host localhost");
    }

    #[test]
    fn test_variadic_arg_captures_unknown_flags() {
        let cmd = SpecCommand::builder()
            .name("test")
            .flag(SpecFlag::builder().short('v').long("verbose").build())
            .arg(SpecArg::builder().name("database").required(false).build())
            .arg(
                SpecArg::builder()
                    .name("args")
                    .required(false)
                    .var(true)
                    .build(),
            )
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // Unknown --host flag and its value should be captured by [args...]
        let input: Vec<String> = vec!["test", "mydb", "--host", "localhost"]
            .into_iter()
            .map(String::from)
            .collect();
        let parsed = parse(&spec, &input).unwrap();
        assert_eq!(parsed.args.len(), 2);
        let args_val = parsed
            .args
            .iter()
            .find(|(a, _)| a.name == "args")
            .unwrap()
            .1;
        match args_val {
            ParseValue::MultiString(v) => {
                assert_eq!(v, &vec!["--host".to_string(), "localhost".to_string()]);
            }
            _ => panic!("Expected MultiString, got {:?}", args_val),
        }
    }

    #[test]
    fn test_variadic_arg_captures_unknown_flags_with_double_dash() {
        let cmd = SpecCommand::builder()
            .name("test")
            .flag(SpecFlag::builder().short('v').long("verbose").build())
            .arg(SpecArg::builder().name("database").required(false).build())
            .arg(
                SpecArg::builder()
                    .name("args")
                    .required(false)
                    .var(true)
                    .build(),
            )
            .build();
        let spec = Spec {
            name: "test".to_string(),
            bin: "test".to_string(),
            cmd,
            ..Default::default()
        };

        // With explicit -- separator
        let input: Vec<String> = vec!["test", "--", "mydb", "--host", "localhost"]
            .into_iter()
            .map(String::from)
            .collect();
        let parsed = parse(&spec, &input).unwrap();
        assert_eq!(parsed.args.len(), 2);
        let args_val = parsed
            .args
            .iter()
            .find(|(a, _)| a.name == "args")
            .unwrap()
            .1;
        match args_val {
            ParseValue::MultiString(v) => {
                assert_eq!(v, &vec!["--host".to_string(), "localhost".to_string()]);
            }
            _ => panic!("Expected MultiString, got {:?}", args_val),
        }
    }

    #[test]
    fn test_variadic_arg_unknown_flag_equals_value_not_split() {
        // Regression: --flag=value should be treated as a single positional token when
        // --flag is not a known spec flag, not split into "--flag=value" AND "value".
        let spec: Spec = r#"arg "[other_args]" var=#true"#.parse().unwrap();

        // Single unknown --flag=value: must not produce a stray "3" positional.
        // as_env() shell-joins via shell_words::join, so "=" gets quoted.
        let input: Vec<String> = vec!["test", "--option=3"]
            .into_iter()
            .map(String::from)
            .collect();
        let parsed = parse(&spec, &input).unwrap();
        let env = parsed.as_env();
        assert_eq!(
            env.get("usage_other_args").map(String::as_str),
            Some("'--option=3'"),
            "expected a single --option=3 token, got {:?}",
            env.get("usage_other_args"),
        );

        // Multiple unknown --flag=value args should each be kept intact
        let input2: Vec<String> = vec!["test", "--foo=bar", "--baz=qux"]
            .into_iter()
            .map(String::from)
            .collect();
        let parsed2 = parse(&spec, &input2).unwrap();
        let env2 = parsed2.as_env();
        assert_eq!(
            env2.get("usage_other_args").map(String::as_str),
            Some("'--foo=bar' '--baz=qux'"),
            "expected two intact tokens, got {:?}",
            env2.get("usage_other_args"),
        );

        // Mix of plain positional args and unknown --flag=value tokens
        let input3: Vec<String> = vec!["test", "positional1", "--option=3", "positional2"]
            .into_iter()
            .map(String::from)
            .collect();
        let parsed3 = parse(&spec, &input3).unwrap();
        let env3 = parsed3.as_env();
        assert_eq!(
            env3.get("usage_other_args").map(String::as_str),
            Some("positional1 '--option=3' positional2"),
            "expected positional args and intact flag token, got {:?}",
            env3.get("usage_other_args"),
        );
    }

    #[test]
    fn test_allow_hyphen_values_consumes_short_flag_collision() {
        let spec = r#"
flag "-d --working-dir <DIR>"
flag "-a --args <ARGS>" allow_hyphen_values=#true
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse(&spec, &input(&["test", "-a", "-destroy"])).unwrap();

        assert_eq!(parsed.flags.len(), 1);
        assert_eq!(flag_string_value(&parsed, "args"), "-destroy");
    }

    #[test]
    fn test_allow_hyphen_values_consumes_embedded_long_value() {
        let spec = r#"
flag "-d --working-dir <DIR>"
flag "-a --args <ARGS>" allow_hyphen_values=#true
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse(&spec, &input(&["test", "--args=-destroy"])).unwrap();

        assert_eq!(parsed.flags.len(), 1);
        assert_eq!(flag_string_value(&parsed, "args"), "-destroy");
    }

    #[test]
    fn test_allow_hyphen_values_takes_the_separator_as_its_value() {
        // The flag is declared to accept a token that looks like a flag, and `--` looks
        // like one, so it binds — which is what clap does with the same declaration.
        // Letting the separator arm run first consumed it and left the flag hungry, and
        // the flag then ate the word past it: `-a -- -x` bound `-x` with the `--` gone.
        let spec = r#"
flag "-a --args <ARGS>" allow_hyphen_values=#true
arg "[rest]..."
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse(&spec, &input(&["test", "-a", "--", "-x"])).unwrap();

        assert_eq!(flag_string_value(&parsed, "args"), "--");
        let rest = parsed
            .args
            .values()
            .next()
            .expect("expected the word after the separator to reach the argument");
        assert_eq!(rest.to_string(), "-x");
    }

    #[test]
    fn test_variadic_allow_hyphen_values_collects_after_a_hyphenated_first_value() {
        // Which token supplied the first value says nothing about how many the argument
        // takes, so collection carries on from a hyphenated one exactly as from a plain
        // one. It still stops at the next flag-like token, which is what keeps a second
        // occurrence of the flag from being eaten as a value.
        let spec = r#"
flag "-a --args <ARGS>..." allow_hyphen_values=#true
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse(&spec, &input(&["test", "-a", "-x", "b", "c"])).unwrap();

        let flag = parsed
            .flags
            .keys()
            .find(|flag| flag.name == "args")
            .expect("expected args flag");
        match parsed.flags.get(flag).expect("expected args value") {
            ParseValue::MultiString(values) => assert_eq!(values, &["-x", "b", "c"]),
            other => panic!("expected a list of values, got {other:?}"),
        }
    }

    #[test]
    fn test_variadic_allow_hyphen_values_consumes_repeated_flag_values() {
        let spec = r#"
flag "-a --args <ARGS>" var=#true allow_hyphen_values=#true
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse(&spec, &input(&["test", "-a", "-val1", "-a", "-val2"])).unwrap();

        let flag = parsed
            .flags
            .keys()
            .find(|flag| flag.name == "args")
            .expect("expected args flag");
        let value = parsed.flags.get(flag).expect("expected args value");
        match value {
            ParseValue::MultiString(values) => {
                assert_eq!(values, &vec!["-val1".to_string(), "-val2".to_string()]);
            }
            _ => panic!("expected MultiString, got {value:?}"),
        }
    }

    #[test]
    fn test_require_equals_accepts_attached_and_refuses_detached() {
        let spec = r#"
flag "--inspect <PORT>" require_equals=#true
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse(&spec, &input(&["test", "--inspect=9229"])).unwrap();
        assert_eq!(flag_string_value(&parsed, "inspect"), "9229");

        let err = parse(&spec, &input(&["test", "--inspect", "9229"])).unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("requires an argument") || msg.contains("inspect"),
            "detached value must be refused: {msg}"
        );
    }

    #[test]
    fn boolean_flags_can_accept_attached_values_when_enabled() {
        let spec: Spec = r#"
name "ex"
bin "ex"
flag "--color" negate="--no-color" bool_value=#true
arg "[rest]"
"#
        .parse()
        .unwrap();

        for (token, expected) in [
            ("--color", true),
            ("--color=true", true),
            ("--color=false", false),
            ("--no-color", false),
            ("--no-color=false", true),
        ] {
            let parsed = parse(&spec, &input(&["ex", token])).unwrap();
            assert!(
                matches!(
                    parsed.flags.get(&spec.cmd.flags[0]),
                    Some(ParseValue::Bool(value)) if *value == expected
                ),
                "{token}"
            );
        }

        let parsed = parse(&spec, &input(&["ex", "--color=false", "word"])).unwrap();
        assert!(matches!(
            parsed.args.get(&spec.cmd.args[0]),
            Some(ParseValue::String(value)) if value == "word"
        ));
        let err = parse(&spec, &input(&["ex", "--color=maybe"])).unwrap_err();
        assert!(err.to_string().contains("expected `true` or `false`"));

        let strict: Spec = r#"
name "ex"
bin "ex"
args_override_self #false
flag "--color" negate="--no-color" bool_value=#true
"#
        .parse()
        .unwrap();
        assert!(parse(&strict, &input(&["ex", "--color=false", "--color=true"])).is_err());
        let parsed = parse(
            &strict,
            &input(&["ex", "--color=false", "--no-color=false"]),
        )
        .unwrap();
        assert!(matches!(
            parsed.flags.get(&strict.cmd.flags[0]),
            Some(ParseValue::Bool(true))
        ));
    }

    #[test]
    fn test_require_equals_refuses_a_detached_value_after_a_short_bundle() {
        let spec = r#"
flag "-a --all"
flag "-i --inspect <PORT>" require_equals=#true
"#
        .parse::<Spec>()
        .unwrap();

        let err = parse(&spec, &input(&["test", "-ai", "9229"])).unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("requires an argument") || msg.contains("inspect"),
            "bundled short must refuse the following word: {msg}"
        );
    }

    #[test]
    fn test_default_missing_binds_when_the_value_is_left_off() {
        let spec = r#"
flag "-c --color <WHEN>" default_missing="always"
flag "-v --verbose"
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse(&spec, &input(&["test", "--color"])).unwrap();
        assert_eq!(flag_string_value(&parsed, "color"), "always");

        let parsed = parse(&spec, &input(&["test", "--color=never"])).unwrap();
        assert_eq!(flag_string_value(&parsed, "color"), "never");

        let parsed = parse(&spec, &input(&["test", "--color", "never"])).unwrap();
        assert_eq!(flag_string_value(&parsed, "color"), "never");

        let parsed = parse(&spec, &input(&["test", "--color", "--verbose"])).unwrap();
        assert_eq!(flag_string_value(&parsed, "color"), "always");
        assert!(parsed.flags.keys().any(|f| f.name == "verbose"));

        let parsed = parse(&spec, &input(&["test", "--color="])).unwrap();
        assert_eq!(flag_string_value(&parsed, "color"), "");

        let parsed = parse(&spec, &input(&["test", "-cnever"])).unwrap();
        assert_eq!(flag_string_value(&parsed, "color"), "never");

        let parsed = parse(&spec, &input(&["test", "-c", "-v"])).unwrap();
        assert_eq!(flag_string_value(&parsed, "color"), "always");
        assert!(parsed.flags.keys().any(|f| f.name == "verbose"));
    }

    #[test]
    fn test_optional_flag_value_preserves_bare_and_explicit_empty_forms() {
        let spec = r#"
flag "--bump [LEVEL]" value_optional=#true
flag "--verbose"
arg "[FILE]"
"#
        .parse::<Spec>()
        .unwrap();

        let absent = parse(&spec, &input(&["test"])).unwrap();
        assert!(!absent.flags.keys().any(|flag| flag.name == "bump"));

        let bare = parse(&spec, &input(&["test", "--bump", "--verbose", "file.txt"])).unwrap();
        let bump = bare
            .flags
            .iter()
            .find(|(flag, _)| flag.name == "bump")
            .map(|(_, value)| value)
            .unwrap();
        assert!(matches!(bump, ParseValue::MultiString(values) if values.is_empty()));
        assert!(bare.flags.keys().any(|flag| flag.name == "verbose"));
        assert_eq!(arg_value(&bare, "FILE"), "file.txt");

        let explicit = parse(&spec, &input(&["test", "--bump=", "file.txt"])).unwrap();
        assert_eq!(flag_string_value(&explicit, "bump"), "");

        let corrected = parse(
            &spec,
            &input(&["test", "--bump=2", "--bump", "--verbose", "file.txt"]),
        )
        .unwrap();
        let bump = corrected
            .flags
            .iter()
            .find(|(flag, _)| flag.name == "bump")
            .map(|(_, value)| value)
            .unwrap();
        assert!(matches!(bump, ParseValue::MultiString(values) if values.is_empty()));

        let collecting = r#"
flag "--tag [TAG]..." value_optional=#true
flag "--verbose"
"#
        .parse::<Spec>()
        .unwrap();
        let valued = parse(
            &collecting,
            &input(&["test", "--tag", "one", "two", "--verbose"]),
        )
        .unwrap();
        let tag = valued
            .flags
            .iter()
            .find(|(flag, _)| flag.name == "tag")
            .map(|(_, value)| value)
            .unwrap();
        assert!(matches!(tag, ParseValue::MultiString(values) if values == &["one", "two"]));
    }

    #[test]
    fn test_repeatable_bare_optional_values_count_each_occurrence() {
        let spec = r#"
flag "--tag [TAG]" var=#true var_min=2 var_max=2 value_optional=#true
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse(&spec, &input(&["test", "--tag", "--tag"])).unwrap();
        let tag = parsed
            .flags
            .iter()
            .find(|(flag, _)| flag.name == "tag")
            .map(|(_, value)| value)
            .unwrap();
        assert!(matches!(tag, ParseValue::MultiString(values) if values == &["", ""]));

        assert!(parse(&spec, &input(&["test", "--tag"])).is_err());
        assert!(parse(&spec, &input(&["test", "--tag", "--tag", "--tag"])).is_err());
    }

    #[test]
    fn test_repeatable_variadic_optional_values_do_not_gain_bare_occurrences() {
        let spec = r#"
flag "--tag [TAG]..." var=#true value_optional=#true
flag "--verbose"
"#
        .parse::<Spec>()
        .unwrap();

        for argv in [
            &["test", "--tag", "one", "two"][..],
            &["test", "--tag", "one", "two", "--verbose"][..],
            &["test", "--tag", "one", "--tag", "two"][..],
        ] {
            let parsed = parse(&spec, &input(argv)).unwrap();
            let tag = parsed
                .flags
                .iter()
                .find(|(flag, _)| flag.name == "tag")
                .map(|(_, value)| value)
                .unwrap();
            assert!(
                matches!(tag, ParseValue::MultiString(values) if values == &["one", "two"]),
                "argv={argv:?}: {tag:?}"
            );
        }

        let bare = parse(&spec, &input(&["test", "--tag", "--verbose"])).unwrap();
        let tag = bare
            .flags
            .iter()
            .find(|(flag, _)| flag.name == "tag")
            .map(|(_, value)| value)
            .unwrap();
        assert!(matches!(tag, ParseValue::MultiString(values) if values == &[""]));
    }

    #[test]
    fn test_default_missing_with_require_equals_refuses_the_following_word() {
        let spec = r#"
flag "--inspect <PORT>" require_equals=#true default_missing="9229"
arg "[rest]"
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse(&spec, &input(&["test", "--inspect"])).unwrap();
        assert_eq!(flag_string_value(&parsed, "inspect"), "9229");

        let parsed = parse(&spec, &input(&["test", "--inspect=1234"])).unwrap();
        assert_eq!(flag_string_value(&parsed, "inspect"), "1234");

        // The following word is not the value; the missing value is, and 80 is a positional.
        let parsed = parse(&spec, &input(&["test", "--inspect", "80"])).unwrap();
        assert_eq!(flag_string_value(&parsed, "inspect"), "9229");
        assert_eq!(
            parsed
                .args
                .values()
                .next()
                .map(|v| v.to_string())
                .as_deref(),
            Some("80")
        );

        let parsed = parse(&spec, &input(&["test", "--inspect="])).unwrap();
        assert_eq!(flag_string_value(&parsed, "inspect"), "");
    }

    #[test]
    fn test_default_missing_must_be_a_choice() {
        let spec = r#"
flag "--color <WHEN>" default_missing="always" {
    choices "auto" "always" "never"
}
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse(&spec, &input(&["test", "--color"])).unwrap();
        assert_eq!(flag_string_value(&parsed, "color"), "always");

        let parsed = parse(&spec, &input(&["test", "--color=never"])).unwrap();
        assert_eq!(flag_string_value(&parsed, "color"), "never");

        let spec = r#"
flag "--color <WHEN>" default_missing="wat" {
    choices "auto" "always" "never"
}
"#
        .parse::<Spec>()
        .unwrap();

        let err = parse(&spec, &input(&["test", "--color"])).unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("Invalid choice for option color: wat"),
            "missing default has to pass choices the same way a typed value does: {msg}"
        );

        let err = parse(&spec, &input(&["test", "--color=wat"])).unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("Invalid choice for option color: wat"),
            "an attached value that is not a choice is still refused: {msg}"
        );

        let spec = r#"
flag "--inspect <PORT>" require_equals=#true default_missing="wat" {
    choices "9229" "80"
}
arg "[rest]"
"#
        .parse::<Spec>()
        .unwrap();

        let err = parse(&spec, &input(&["test", "--inspect", "80"])).unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("Invalid choice for option inspect: wat"),
            "require_equals still binds the missing string, so the error is the choice: {msg}"
        );
    }

    #[test]
    fn test_hyphen_values_still_default_to_short_flag_parsing() {
        let spec = r#"
flag "-d --working-dir <DIR>"
flag "-a --args <ARGS>"
"#
        .parse::<Spec>()
        .unwrap();

        let parsed = parse(&spec, &input(&["test", "-a", "-destroy"])).unwrap();

        assert_eq!(flag_string_value(&parsed, "working-dir"), "estroy");
    }

    /// `available_flags` has to agree with what an actual parse accepts, since
    /// its whole reason to exist is answering that question without one.
    mod available_flags {
        use super::*;

        fn spec() -> Spec {
            r#"
bin "test"
flag "-v --verbose" global=#true
flag "--raw" global=#true effect="write"
flag "--local-only"
cmd "run" {
    flag "-r --raw"
    flag "-w --watch"
    cmd "once"
}
"#
            .parse::<Spec>()
            .unwrap()
        }

        fn chain<'a>(spec: &'a Spec, path: &[&str]) -> Vec<&'a SpecCommand> {
            let mut chain = vec![&spec.cmd];
            for segment in path {
                chain.push(chain.last().unwrap().find_subcommand(segment).unwrap());
            }
            chain
        }

        fn names(spec: &Spec, path: &[&str]) -> Vec<String> {
            let mut names: Vec<_> = available_flags(&chain(spec, path))
                .iter()
                .map(|f| f.name.clone())
                .collect();
            names.sort();
            names
        }

        #[test]
        fn an_empty_chain_yields_nothing() {
            assert!(available_flags(&[]).is_empty());
        }

        #[test]
        fn the_root_gets_its_own_flags() {
            let spec = spec();
            assert_eq!(names(&spec, &[]), ["local-only", "raw", "verbose"]);
        }

        #[test]
        fn a_subcommand_keeps_globals_and_drops_local_only_ancestors() {
            let spec = spec();
            assert_eq!(names(&spec, &["run"]), ["raw", "verbose", "watch"]);
        }

        #[test]
        fn a_re_declared_global_is_listed_once() {
            // The merge can leave the long key on the merged flag and the short
            // key on the pre-merge one. Same flag; it must not be listed twice.
            let spec = r#"
bin "test"
flag "-y --yes" global=#true effect="write"
cmd "rm" {
    flag "-y --yes"
}
"#
            .parse::<Spec>()
            .unwrap();
            let flags = available_flags(&chain(&spec, &["rm"]));
            assert_eq!(flags.len(), 1, "{flags:?}");
            assert_eq!(flags[0].effect.map(|e| e.as_str()), Some("write"));
        }

        #[test]
        fn a_re_declared_global_keeps_the_globals_declaration() {
            // `run` re-declares the long-only global `--raw` as `-r --raw`
            // without `global`. That is the same flag: the global's `effect`
            // survives, the orphan short is unioned in, and it stays global.
            let spec = spec();
            let flags = available_flags(&chain(&spec, &["run"]));
            let raw = flags.iter().find(|f| f.name == "raw").unwrap();
            assert!(raw.global);
            assert_eq!(raw.effect.map(|e| e.as_str()), Some("write"));
            assert_eq!(raw.short, ['r']);
        }

        #[test]
        fn it_matches_what_a_parse_accepts() {
            // The invariant. If these ever disagree, one of them is lying to a
            // caller about which flags a command takes.
            let spec = spec();
            for path in [vec![], vec!["run"], vec!["run", "once"]] {
                let argv = std::iter::once("test".to_string())
                    .chain(path.iter().map(|s| s.to_string()))
                    .collect::<Vec<_>>();
                let parsed = parse_partial(&spec, &argv).unwrap();

                let mut from_parse: Vec<_> = unique_flags(parsed.available_flags.values())
                    .map(|f| f.name.clone())
                    .collect();
                from_parse.sort();
                assert_eq!(names(&spec, &path), from_parse, "path {path:?}");
            }
        }
    }

    // Provenance: which token bound what, and where a value came from when no token did.

    /// Every role a token was given, rendered the way `Debug` renders it, so a test can
    /// assert on the whole picture rather than on one field at a time.
    fn roles(parsed: &ParseOutput, index: usize) -> Vec<String> {
        parsed
            .tokens
            .iter()
            .find(|token| token.index == index)
            .unwrap_or_else(|| panic!("no token at {index}"))
            .roles
            .iter()
            .map(render_role)
            .collect()
    }

    fn origins(parsed: &ParseOutput, flag: &str) -> Vec<ValueOrigin> {
        parsed
            .flag_origins
            .iter()
            .find(|(f, _)| f.name == flag)
            .map(|(_, origins)| origins.clone())
            .unwrap_or_default()
    }

    fn explain_with_env(spec: &Spec, words: &[&str], env: &[(&str, &str)]) -> ParseOutput {
        let env = env
            .iter()
            .map(|(k, v)| ((*k).to_string(), (*v).to_string()))
            .collect();
        Parser::new(spec)
            .with_env(env)
            .explain(&input(words))
            .unwrap()
    }

    fn explain(spec: &Spec, words: &[&str]) -> ParseOutput {
        explain_with_env(spec, words, &[])
    }

    #[test]
    fn an_attached_long_value_is_recorded_on_the_flag_token() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--env <env>\"\n"
            .parse()
            .unwrap();

        let parsed = explain(&spec, &["ex", "--env=prod"]);

        assert_eq!(roles(&parsed, 0), ["program"]);
        assert_eq!(
            roles(&parsed, 1),
            ["flag env as --env", "value of env = [\"prod\"], attached"]
        );
        // This is jdx/mise discussion #8883: a hand-written scanner dropped the attached
        // form while the detached one worked, and nothing could show the difference.
        assert!(origins(&parsed, "env").is_empty(), "typed, so no fallback");
    }

    #[test]
    fn a_detached_long_value_is_recorded_on_its_own_token() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--env <env>\"\n"
            .parse()
            .unwrap();

        let parsed = explain(&spec, &["ex", "--env", "prod"]);

        assert_eq!(roles(&parsed, 1), ["flag env as --env"]);
        assert_eq!(roles(&parsed, 2), ["value of env = [\"prod\"]"]);
    }

    #[test]
    fn a_short_bundle_is_attributed_to_the_bundle_token() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"-a\"\nflag \"-b\"\nflag \"-j <n>\"\n"
            .parse()
            .unwrap();

        let parsed = explain(&spec, &["ex", "-abj8"]);

        // One word the caller wrote, four things it did — and the re-queued tails are
        // folded back onto it rather than appearing as tokens nobody typed.
        assert_eq!(
            roles(&parsed, 1),
            [
                "flag a as -a",
                "flag b as -b",
                "flag j as -j",
                "value of j = [\"8\"], attached",
            ]
        );
        assert_eq!(parsed.tokens.len(), 2);
    }

    #[test]
    fn a_delimiter_splits_one_token_into_several_values() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--tags <tags>...\" delimiter=\",\"\n"
            .parse()
            .unwrap();

        let parsed = explain(&spec, &["ex", "--tags", "a,b,c"]);

        assert_eq!(
            roles(&parsed, 2),
            ["value of tags = [\"a\", \"b\", \"c\"]"],
            "the values meant, not the word typed"
        );
    }

    #[test]
    fn a_separator_and_the_words_after_it_are_distinguished() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\narg \"<src>\"\narg \"[raw]...\"\n"
            .parse()
            .unwrap();

        let parsed = explain(&spec, &["ex", "a", "--", "-x"]);

        assert_eq!(roles(&parsed, 1), ["arg src = [\"a\"]"]);
        assert_eq!(roles(&parsed, 2), ["separator"]);
        // Past the separator `-x` is data, not an unknown flag.
        assert_eq!(roles(&parsed, 3), ["arg raw = [\"-x\"]"]);
    }

    #[test]
    fn a_second_separator_is_data() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\narg \"[raw]...\"\n"
            .parse()
            .unwrap();

        let parsed = explain(&spec, &["ex", "--", "a", "--", "b"]);

        assert_eq!(roles(&parsed, 1), ["separator"]);
        assert_eq!(roles(&parsed, 3), ["arg raw = [\"--\"]"]);
    }

    #[test]
    fn an_unknown_flag_says_what_took_it() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\narg \"[rest]...\"\n"
            .parse()
            .unwrap();

        let parsed = explain(&spec, &["ex", "--wat"]);

        // The default is lax, so the word became data. Which is the useful thing to be
        // told: the alternative reading is "you have a typo".
        assert_eq!(roles(&parsed, 1), ["unknown flag, bound as rest"]);
    }

    #[test]
    fn a_subcommand_word_is_not_a_positional() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\ncmd \"build\" {\n    arg \"<target>\"\n}\n"
            .parse()
            .unwrap();

        let parsed = explain(&spec, &["ex", "build", "a"]);

        assert_eq!(roles(&parsed, 1), ["subcommand build"]);
        assert_eq!(roles(&parsed, 2), ["arg target = [\"a\"]"]);
    }

    #[test]
    fn a_multicall_applet_is_read_at_argv0() {
        let spec: Spec =
            "name \"box\"\nbin \"box\"\nmulticall #true\ncmd \"ls\" {\n    flag \"-l\"\n}\n"
                .parse()
                .unwrap();

        let parsed = explain(&spec, &["/usr/bin/ls", "-l"]);

        // argv[0] is both the program and the word that selected the applet, and the word
        // read there is not the word the caller wrote.
        assert_eq!(roles(&parsed, 0), ["program", "subcommand ls"]);
        assert!(parsed.tokens[0].synthesized);
        assert_eq!(parsed.tokens[0].word, "/usr/bin/ls");
    }

    #[test]
    fn words_the_parse_never_reached_say_so() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\narg \"[rest]...\"\n"
            .parse()
            .unwrap();

        let parsed = Parser::new(&spec)
            .explain(&input(&["ex", "--help", "a"]))
            .unwrap();

        assert_eq!(roles(&parsed, 2), ["unread"]);
    }

    #[test]
    fn an_env_origin_names_the_variable_that_fired() {
        let spec: Spec =
            "name \"ex\"\nbin \"ex\"\nflag \"--token <t>\" env=\"EX_TOKEN\" env_fallback=\"EX_TOKEN_OLD\"\n"
                .parse()
                .unwrap();

        let primary = explain_with_env(&spec, &["ex"], &[("EX_TOKEN", "a")]);
        assert_eq!(
            origins(&primary, "token"),
            [ValueOrigin::Env("EX_TOKEN".to_string())]
        );

        // The fallback firing is a different fact from the primary firing, and which one it
        // was is what says which declaration to delete.
        let fallback = explain_with_env(&spec, &["ex"], &[("EX_TOKEN_OLD", "b")]);
        assert_eq!(
            origins(&fallback, "token"),
            [ValueOrigin::Env("EX_TOKEN_OLD".to_string())]
        );
    }

    #[test]
    fn a_default_origin_is_recorded_for_flags_and_args() {
        let spec: Spec =
            "name \"ex\"\nbin \"ex\"\nflag \"--color <when>\" default=\"auto\"\narg \"[src]\" default=\".\"\n"
                .parse()
                .unwrap();

        let parsed = explain(&spec, &["ex"]);

        assert_eq!(origins(&parsed, "color"), [ValueOrigin::Default]);
        let (arg, origins) = parsed.arg_origins.iter().next().unwrap();
        assert_eq!(arg.name, "src");
        assert_eq!(origins, &[ValueOrigin::Default]);
    }

    #[test]
    fn a_default_if_origin_carries_the_condition_that_fired() {
        let spec: Spec = r#"
name "ex"
bin "ex"
flag "--profile <p>"
flag "--strict" {
    default_if "--profile" "prod" "true"
}
        "#
        .parse()
        .unwrap();

        let parsed = explain(&spec, &["ex", "--profile", "prod"]);

        // The selector alone is ambiguous: several conditions may name it with different
        // `when` values, so the report has to say which one matched.
        assert_eq!(
            origins(&parsed, "strict"),
            [ValueOrigin::DefaultIf {
                selector: "--profile".to_string(),
                when: Some("prod".to_string()),
            }]
        );
    }

    #[test]
    fn a_bare_optional_value_flag_records_default_missing() {
        let spec: Spec =
            "name \"ex\"\nbin \"ex\"\nflag \"--color <when>\" default_missing=\"always\"\nflag \"-v\"\n"
                .parse()
                .unwrap();

        let parsed = explain(&spec, &["ex", "--color", "-v"]);

        // The flag was typed and the value was not, which is the distinction a spec author
        // is asking about when they ask why `--color` came out `always`.
        assert_eq!(roles(&parsed, 1), ["flag color as --color"]);
        assert_eq!(origins(&parsed, "color"), [ValueOrigin::DefaultMissing]);
        assert_eq!(roles(&parsed, 2), ["flag v as -v"]);
    }

    #[test]
    fn a_var_flag_can_take_one_value_from_argv_and_one_from_default_missing() {
        let spec: Spec =
            "name \"ex\"\nbin \"ex\"\nflag \"--color <when>\" var=#true default_missing=\"always\"\n"
                .parse()
                .unwrap();

        let parsed = explain(&spec, &["ex", "--color=red", "--color"]);

        // Why origins are a list: one declaration, two occurrences, two different answers.
        assert_eq!(
            roles(&parsed, 1),
            [
                "flag color as --color",
                "value of color = [\"red\"], attached"
            ]
        );
        assert_eq!(origins(&parsed, "color"), [ValueOrigin::DefaultMissing]);
    }

    #[test]
    fn an_override_names_the_flag_that_did_it() {
        let spec: Spec =
            "name \"ex\"\nbin \"ex\"\nflag \"--quiet\" default=\"true\"\nflag \"--loud\" overrides=\"--quiet\"\n"
                .parse()
                .unwrap();

        let parsed = explain(&spec, &["ex", "--loud"]);

        // Without the overriding name, "`--quiet` is unset despite its default" has no
        // answer: the fallback phase silently declines to fill an overridden flag.
        assert_eq!(parsed.overridden_flags.get("quiet").unwrap(), "loud");
        assert!(origins(&parsed, "quiet").is_empty());
    }

    #[test]
    fn a_restart_token_leaves_the_tokens_and_clears_the_arg_origins() {
        let spec: Spec = r#"
name "ex"
bin "ex"
cmd "run" restart_token=":::" {
    arg "<task>" default="build"
}
        "#
        .parse()
        .unwrap();

        let parsed = explain(&spec, &["ex", "run", "lint", ":::", "test"]);

        // The values belong to the last invocation, so provenance must too — but the words
        // of the first were still read, and a report that dropped them would show a command
        // line with a hole in it.
        assert_eq!(roles(&parsed, 2), ["arg task = [\"lint\"]"]);
        // And the token that did the resetting says so: without a role of its own it reads
        // as a word that did nothing, next to a `lint` that filled an arg now empty.
        assert_eq!(roles(&parsed, 3), ["restart"]);
        assert_eq!(roles(&parsed, 4), ["arg task = [\"test\"]"]);
        assert!(parsed.arg_origins.is_empty());
    }

    #[test]
    fn a_value_terminator_says_which_run_it_ended() {
        let spec: Spec = r#"
name "ex"
bin "ex"
flag "--exec <cmd>..." value_terminator=";"
arg "<src>"
        "#
        .parse()
        .unwrap();

        let parsed = explain(&spec, &["ex", "--exec", "rm", "tmp", ";", "a"]);

        assert_eq!(roles(&parsed, 3), ["value of exec = [\"tmp\"]"]);
        // The terminator is consumed and is not one of the values, which is the whole reason
        // it was declared — so it needs a row saying that rather than an empty one.
        assert_eq!(roles(&parsed, 4), ["value terminator, ends exec"]);
        assert_eq!(roles(&parsed, 5), ["arg src = [\"a\"]"]);
    }

    #[test]
    fn an_args_value_terminator_says_which_run_it_ended() {
        let spec: Spec = r#"
name "ex"
bin "ex"
arg "<files>..." value_terminator=";"
arg "[dest]"
        "#
        .parse()
        .unwrap();

        let parsed = explain(&spec, &["ex", "a", "b", ";", "out"]);

        assert_eq!(roles(&parsed, 2), ["arg files = [\"b\"]"]);
        assert_eq!(roles(&parsed, 3), ["value terminator, ends files"]);
        assert_eq!(roles(&parsed, 4), ["arg dest = [\"out\"]"]);
    }

    #[test]
    fn explain_keeps_the_bindings_of_a_command_line_that_fails() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nflag \"--env <env>\"\narg \"<src>\"\n"
            .parse()
            .unwrap();

        let parsed = Parser::new(&spec)
            .explain(&input(&["ex", "--env=prod"]))
            .unwrap();

        // `parse` reports "missing required <src>" and nothing else, which is the report the
        // caller already had. This is the case the whole thing exists for.
        assert!(Parser::new(&spec)
            .parse(&input(&["ex", "--env=prod"]))
            .is_err());
        assert_eq!(
            roles(&parsed, 1),
            ["flag env as --env", "value of env = [\"prod\"], attached"]
        );
        assert!(
            parsed.errors.iter().any(|e| e.to_string().contains("src")),
            "{:?}",
            parsed.errors
        );
    }

    #[test]
    fn an_external_subcommand_forwards_whole_tokens() {
        let spec: Spec = "name \"ex\"\nbin \"ex\"\nexternal_subcommand #true\ncmd \"build\"\n"
            .parse()
            .unwrap();

        let parsed = explain(&spec, &["ex", "deploy", "--now"]);

        assert_eq!(roles(&parsed, 1), ["external"]);
        assert_eq!(roles(&parsed, 2), ["external"]);
    }

    #[test]
    fn a_view_keeps_the_callers_argv_positions() {
        let spec: Spec = r#"
bin "ex"
view "runner" root="run"
cmd "run" {
    flag "--token <token>"
}
        "#
        .parse()
        .unwrap();

        let parsed = explain(&spec, &["runner", "--token", "secret"]);

        // A view re-enters the parse with the same argv, so the positions still mean what
        // the caller wrote.
        assert_eq!(roles(&parsed, 0), ["program"]);
        assert_eq!(roles(&parsed, 2), ["value of token = [\"secret\"]"]);
    }
}