usage-lib 6.1.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
//! Emitting Go parse tables from a spec.
//!
//! The Go side of usage has no derive macro to emit its tables, because Go has no
//! macros: what a Rust CLI gets from `#[derive(Cli)]` at compile time, a Go CLI
//! gets from this, at build time, through `go:generate`. The output is a plain Go
//! file an author checks in and a reviewer can read.
//!
//! # What it emits, and what it does not
//!
//! Two tables, kept apart on purpose. The hot one is what binding reads: which
//! token becomes which flag or argument, and nothing else. The cold one — `Meta` —
//! carries what the rules decided after the last token need: `required`,
//! `choices`, `default`, `env`, the var bounds, and the four that compare one
//! entry against another. A parse never touches the second.
//!
//! Help text is in neither. mise's runs to several hundred kilobytes, and a table
//! carrying it would put all of that in front of the parser; rendering help is its
//! own cold table and its own piece of work.
//!
//! # Why package-level `var` and not `const`
//!
//! Go has no `const` for composite data. What it does have is a linker that
//! statically initializes package-level variables holding plain data, which is
//! the property the whole design rests on: `go tool nm` reports these symbols as
//! type `D`, and the generated package has no `init` function. So a 211-command
//! table costs bytes in the binary and no instructions at startup — the thing
//! cobra and kong each pay a million or more for.
//!
//! Commands are emitted as separate variables rather than one nested literal
//! because `default_subcommand` has to point at a node inside the tree, and a
//! composite literal cannot refer to its own interior.

mod structs;

use std::collections::{BTreeMap, HashMap};
use std::fmt::Write as _;

use heck::AsPascalCase;

use crate::spec::unknown_flags::UnknownFlags;
use crate::{
    Spec, SpecArg, SpecChoices, SpecCommand, SpecDoubleDashChoices, SpecFlag, SpecFlagAction,
};

/// How to emit.
#[derive(Debug, Clone, Default)]
pub struct GoOptions {
    /// The Go package clause. Defaults to the spec's `bin`, made into an
    /// identifier.
    ///
    /// Must satisfy [`is_valid_package`]. A caller taking this from a user should
    /// check it and say so; one that does not gets it sanitized, because emitting a
    /// file that cannot compile helps nobody.
    pub package: Option<String>,
}

/// Turn a spec into a Go source file declaring its parse tables.
pub fn generate(spec: &Spec, opts: &GoOptions) -> String {
    Emitter::new(spec, opts).run()
}

/// One entry's identifiers: the exported key constant, and for a command the
/// variable holding it.
struct Named {
    key: String,
    var: String,
    number: u64,
}

struct Emitter<'a> {
    spec: &'a Spec,
    package: String,
    /// Every identifier handed out, so a second entry wanting the same spelling
    /// gets a suffix instead of silently colliding.
    taken: HashMap<String, u32>,
    /// Assigned in emission order, so a key is stable as long as the spec is.
    next_key: u64,
    out: String,
}

impl<'a> Emitter<'a> {
    fn new(spec: &'a Spec, opts: &GoOptions) -> Self {
        // An explicit package that is not an identifier is sanitized rather than
        // emitted: a caller that wants to reject it should ask `is_valid_package`
        // first, which the CLI does.
        let package = match opts.package.as_deref() {
            Some(name) if is_valid_package(name) => name.to_string(),
            Some(name) => package_ident(name),
            None => package_ident(&spec.bin),
        };
        Emitter {
            spec,
            package,
            taken: HashMap::new(),
            next_key: 0,
            out: String::new(),
        }
    }

    /// Reserve an identifier, adding a numeric suffix if the spelling is taken.
    ///
    /// Collisions are ordinary rather than exotic: mise has both a `macos-defaults`
    /// command and a `macos defaults` path, and both want to be spelled
    /// `CmdMacosDefaults`.
    ///
    /// The suffixed spelling is reserved too, and the loop is what makes that
    /// safe. Counting alone was not enough: `macos-defaults` and `macos defaults`
    /// produce `CmdMacosDefaults` and `CmdMacosDefaults2`, and a third command
    /// named `macos-defaults2` asks for `CmdMacosDefaults2` directly — which was
    /// unclaimed, so the file declared it twice and did not compile.
    fn unique(&mut self, base: &str) -> String {
        let mut n = self.taken.get(base).copied().unwrap_or(0);
        loop {
            n += 1;
            let candidate = if n == 1 {
                base.to_string()
            } else {
                format!("{base}{n}")
            };
            if !self.taken.contains_key(&candidate) {
                self.taken.insert(base.to_string(), n);
                // The spelling itself, so a later entry that asks for it by name is
                // suffixed rather than handed a duplicate.
                self.taken.entry(candidate.clone()).or_insert(0);
                return candidate;
            }
        }
    }

    fn name(&mut self, prefix: &str, path: &[&str], own: &str) -> Named {
        let mut base = String::from(prefix);
        for segment in path {
            let _ = write!(base, "{}", AsPascalCase(segment));
        }
        let _ = write!(base, "{}", AsPascalCase(own));
        let key = self.unique(&base);
        self.next_key += 1;
        Named {
            var: format!("cmd{}", &key[prefix.len()..]),
            key,
            number: self.next_key,
        }
    }

    fn run(mut self) -> String {
        // Collected first so the constants can be emitted in one block before any
        // table refers to them, which is also the order a reader wants: the names
        // they will switch on, then the data.
        let mut commands = Vec::new();
        self.collect(&self.spec.cmd.clone(), &[], true, &mut commands);

        self.header();
        self.constants(&commands);
        self.tables(&commands);
        self.metadata(&commands);
        self.help_table(&commands);
        structs::emit(&mut self.out, &commands);

        // Each command is followed by a blank line, which leaves one at the end of
        // the file. gofmt strips it, and a generated file that is not gofmt-clean
        // is one every adopter has to run a formatter over before committing.
        let trimmed = self.out.trim_end().len();
        self.out.truncate(trimmed);
        self.out.push('\n');
        self.out
    }

    /// Walk the tree, naming everything, so that emission is a second pass with no
    /// lookaheads.
    fn collect(&mut self, cmd: &SpecCommand, path: &[&str], root: bool, out: &mut Vec<Emitted>) {
        let named = if root {
            self.next_key += 1;
            Named {
                // Claimed through the same counter as everything else, not just
                // spelled: a subcommand named `root` would otherwise be handed
                // `CmdRoot` too, and the file would declare the constant twice and
                // fail to compile.
                key: self.unique("CmdRoot"),
                var: "Root".to_string(),
                number: self.next_key,
            }
        } else {
            self.name("Cmd", &path[..path.len() - 1], path[path.len() - 1])
        };

        let flags = cmd
            .flags
            .iter()
            .map(|f| (f.clone(), self.name("Flag", path, &f.name)))
            .collect::<Vec<_>>();
        let args = cmd
            .args
            .iter()
            .map(|a| (a.clone(), self.name("Arg", path, &a.name)))
            .collect::<Vec<_>>();

        let index = out.len();
        out.push(Emitted {
            named,
            cmd: cmd.clone(),
            flags,
            args,
            subcommands: Vec::new(),
            root,
        });

        // Declaration order, not sorted: a recent change made the spec hold the
        // order a CLI declares its commands in, and a generated file that reordered
        // them would lose it for no gain — lookup is by name either way.
        let mut children = Vec::new();
        for (name, sub) in &cmd.subcommands {
            // An alias appears in `subcommands` under its own key as well as the
            // canonical name; emitting it twice would declare two commands where the
            // spec has one.
            if name != &sub.name {
                continue;
            }
            let mut child_path = path.to_vec();
            child_path.push(name);
            let at = out.len();
            self.collect(sub, &child_path, false, out);
            children.push(at);
        }
        out[index].subcommands = children;
    }

    fn header(&mut self) {
        let _ = writeln!(
            self.out,
            "// Code generated by `usage generate go`. DO NOT EDIT.\n\
             //\n\
             // Binding tables for `{}`, read by\n\
             // [github.com/jdx/usage/go/argv]. Regenerate rather than editing: the spec is\n\
             // the definition, and a hand-edit here is a difference no reviewer can see.\n\
             //\n\
             // These are package-level variables holding plain data, so the linker lays them\n\
             // out and nothing runs before main.\n\
             \n\
             package {}\n\
             \n\
             import \"github.com/jdx/usage/go/argv\"\n",
            self.spec.bin, self.package
        );

        if let Some(version) = self
            .spec
            .version
            .as_ref()
            .or(self.spec.long_version.as_ref())
        {
            let _ = writeln!(
                self.out,
                "// Version is what the spec declares, so a caller answering `--version` has it\n\
                 // without the parse tables carrying a string binding never reads.\n\
                 const Version = {}\n",
                go_string(version)
            );
        }
        if let Some(version) = &self.spec.long_version {
            let _ = writeln!(
                self.out,
                "// LongVersion is the extended text printed for `--version`; `-V` uses Version.\n\
                 const LongVersion = {}\n",
                go_string(version)
            );
        }
    }

    fn constants(&mut self, commands: &[Emitted]) {
        let _ = writeln!(
            self.out,
            "// Keys identify a table entry without a string comparison: switch on the Key an\n\
             // event carries rather than on its Name, which is there for diagnostics.\n\
             const ("
        );
        let mut entries: Vec<(&str, u64)> = Vec::new();
        for e in commands {
            entries.push((&e.named.key, e.named.number));
            entries.extend(e.flags.iter().map(|(_, n)| (n.key.as_str(), n.number)));
            entries.extend(e.args.iter().map(|(_, n)| (n.key.as_str(), n.number)));
        }
        // One run, so every name pads to the longest — which is what gofmt does to
        // a const block with no blank line in it.
        let width = entries.iter().map(|(k, _)| k.len()).max().unwrap_or(0);
        for (key, number) in entries {
            let _ = writeln!(
                self.out,
                "\t{key}{:pad$} uint64 = {number}",
                "",
                pad = width - key.len()
            );
        }
        let _ = writeln!(self.out, ")\n");
    }

    fn tables(&mut self, commands: &[Emitted]) {
        // Resolved once, against the root's *direct* subcommands, because the spec
        // declares it once at the top and it names one of them. Searching the whole
        // tree instead is what wired mise's `default_subcommand run` to `oci run`,
        // which comes first in a depth-first walk — the parser would then have
        // descended into a command that is not the root's child at all. A name
        // nothing answers to is left unset rather than guessed at.
        let default_subcommand = self.spec.default_subcommand.as_ref().and_then(|name| {
            let direct = || commands[0].subcommands.iter().map(|at| &commands[*at]);
            // Names before aliases, as the grammar says: a command's own name outranks
            // another command's alias, so which one this resolves to does not depend on
            // the order the spec declares them in.
            direct()
                .find(|e| &e.cmd.name == name)
                .or_else(|| {
                    direct().find(|e| {
                        e.cmd.aliases.contains(name) || e.cmd.hidden_aliases.contains(name)
                    })
                })
                .map(|e| e.named.var.clone())
        });

        for (i, e) in commands.iter().enumerate() {
            let doc = if e.root {
                format!(
                    "// Root is the command tree for `{}`. Pass it to argv.New.",
                    self.spec.bin
                )
            } else {
                format!("// {}", e.cmd.full_cmd.join(" "))
            };
            let mut lines = vec![
                Line::Field("Name".into(), go_string(&e.cmd.name)),
                Line::Field("Key".into(), e.named.key.clone()),
            ];

            let aliases: Vec<&String> = e
                .cmd
                .aliases
                .iter()
                .chain(e.cmd.hidden_aliases.iter())
                .collect();
            if !aliases.is_empty() {
                // A hidden alias selects a command exactly as a visible one does:
                // hiding is about help output, which binding never reads.
                let list = aliases
                    .iter()
                    .map(|a| go_string(a))
                    .collect::<Vec<_>>()
                    .join(", ");
                lines.push(Line::Field("Aliases".into(), format!("[]string{{{list}}}")));
            }

            if !e.flags.is_empty() {
                let mut block = vec!["Flags: []*argv.Flag{".to_string()];
                for (flag, named) in &e.flags {
                    block.push(format!("\t{},", flag_literal(flag, named)));
                }
                block.push("},".to_string());
                lines.push(Line::Block(block));
            }

            if !e.args.is_empty() {
                let mut block = vec!["Args: []*argv.Arg{".to_string()];
                for (arg, named) in &e.args {
                    block.push(format!("\t{},", arg_literal(arg, named)));
                }
                block.push("},".to_string());
                lines.push(Line::Block(block));
            }

            if !e.subcommands.is_empty() {
                let list = e
                    .subcommands
                    .iter()
                    .map(|at| commands[*at].named.var.clone())
                    .collect::<Vec<_>>()
                    .join(", ");
                lines.push(Line::Field(
                    "Subcommands".into(),
                    format!("[]*argv.Command{{{list}}}"),
                ));
            }

            // Already resolved: inheritance is the generator's job, so that the
            // parser reads one field rather than walking ancestors per token.
            if effective_unknown_flags(self.spec, commands, i) == UnknownFlags::Error {
                lines.push(Line::Field(
                    "UnknownFlags".into(),
                    "argv.UnknownFlagsError".into(),
                ));
            }

            if e.cmd.external_subcommand {
                lines.push(Line::Field("ExternalSubcommand".into(), "true".into()));
            }
            if e.cmd.arg_required_else_help {
                lines.push(Line::Field("ArgRequiredElseHelp".into(), "true".into()));
            }
            if e.cmd.disable_help_flag {
                lines.push(Line::Field("DisableHelpFlag".into(), "true".into()));
            }
            if e.cmd.disable_help_subcommand {
                lines.push(Line::Field("DisableHelpSubcommand".into(), "true".into()));
            }
            if e.cmd.disable_version_flag {
                lines.push(Line::Field("DisableVersionFlag".into(), "true".into()));
            }
            if e.cmd.subcommand_negates_reqs {
                lines.push(Line::Field("SubcommandNegatesReqs".into(), "true".into()));
            }
            if e.cmd.args_conflicts_with_subcommands {
                lines.push(Line::Field(
                    "ArgsConflictWithSubcommands".into(),
                    "true".into(),
                ));
            }
            if e.cmd.subcommand_precedence_over_arg {
                lines.push(Line::Field(
                    "SubcommandPrecedenceOverArg".into(),
                    "true".into(),
                ));
            }
            if e.cmd.allow_missing_positional {
                lines.push(Line::Field("AllowMissingPositional".into(), "true".into()));
            }
            if e.cmd.dont_delimit_trailing_values {
                lines.push(Line::Field(
                    "DontDelimitTrailingValues".into(),
                    "true".into(),
                ));
            }
            if e.root {
                if let Some(var) = &default_subcommand {
                    lines.push(Line::Field("DefaultSubcommand".into(), var.clone()));
                }
                if self.spec.version.is_some() || self.spec.long_version.is_some() {
                    // Only where the CLI declares a version: a `--version` that answers
                    // with nothing is worse than one that is not there.
                    lines.push(Line::Field("Version".into(), "true".into()));
                }
            }

            let _ = writeln!(self.out, "{doc}");
            let _ = writeln!(self.out, "var {} = &argv.Command{{", e.named.var);
            render(&mut self.out, "\t", &lines);
            let _ = writeln!(self.out, "}}\n");
        }
    }
}

impl Emitter<'_> {
    /// Emit the cold table: everything binding deliberately does not know.
    ///
    /// Indexed by key, which is what makes a lookup an index rather than a map —
    /// and a Go map would have to be built at init, which is the one thing these
    /// tables are for avoiding. Keys are handed out to commands as well as to
    /// flags and arguments, and a command has no cold half, so its slot is an
    /// empty entry rather than a gap: `Metadata.Lookup` checks the key it finds
    /// and reports nothing when it does not match, so an empty slot answers
    /// correctly and the index stays dense.
    fn metadata(&mut self, commands: &[Emitted]) {
        // By key, so the slice can be written in one pass in index order.
        let mut by_key: BTreeMap<u64, String> = BTreeMap::new();
        for e in commands {
            for (flag, named) in &e.flags {
                by_key.insert(named.number, self.flag_meta(flag, named, e, commands));
            }
            for (arg, named) in &e.args {
                by_key.insert(named.number, arg_meta(self.spec, arg, named, e, commands));
            }
        }

        let total = commands
            .iter()
            .map(|e| 1 + e.flags.len() + e.args.len())
            .sum::<usize>() as u64;

        let _ = writeln!(
            self.out,
            "// Meta is the cold table, read only by the rules that are decided once the\n\
             // last token has been read: required, choices, the env-then-default fallback,\n\
             // the var bounds, and the four that compare one entry against another. A parse\n\
             // never touches it.\n\
             //\n\
             // Indexed by key, so entry Key sits at Meta[Key-1]. A command's slot is empty:\n\
             // commands take keys too, and have no cold half.\n\
             var Meta = argv.Metadata{{"
        );
        for key in 1..=total {
            match by_key.get(&key) {
                Some(entry) => {
                    let _ = writeln!(self.out, "\t{entry},");
                }
                None => {
                    let _ = writeln!(self.out, "\t{{}},");
                }
            }
        }
        let _ = writeln!(self.out, "}}\n");
    }

    /// The cold half of a flag.
    fn flag_meta(
        &self,
        flag: &SpecFlag,
        named: &Named,
        owner: &Emitted,
        commands: &[Emitted],
    ) -> String {
        let mut fields = vec![
            format!("Key: {}", named.key),
            format!("Name: {}", go_string(&flag.name)),
            "Flag: true".to_string(),
        ];
        if !owner.cmd.args_override_self
            && !flag.var
            && !flag.count
            && !flag.arg.as_ref().is_some_and(|arg| arg.var)
        {
            fields.push("RejectDuplicate: true".to_string());
        }
        if flag.required {
            fields.push("Required: true".to_string());
        }
        if flag.arg.is_none() {
            fields.push("RequiresIfBoolean: true".to_string());
        }
        // How a user types it, worked out where the forms are visible: the rules
        // that judge an entry never see a flag, and guessing from the name gets a
        // one-letter long form and a short the wrong way round.
        if let Some(long) = flag.long.first() {
            fields.push(format!("Spelling: {}", go_string(&format!("--{long}"))));
        } else if let Some(short) = flag.short.first() {
            fields.push(format!("Spelling: {}", go_string(&format!("-{short}"))));
        }
        // What the value is called, which is what says whether a path belongs
        // there — `--into <DIR>` completes directories because of the name.
        if let Some(value) = flag.arg.as_ref() {
            fields.push(format!("ValueName: {}", go_string(&value.name)));
        }
        let named_value = flag
            .arg
            .as_ref()
            .map(|a| a.name.as_str())
            .unwrap_or(flag.name.as_str());
        if let Some(kind) = complete_type(self.spec, named_value) {
            fields.push(format!("CompleteType: {}", go_string(kind)));
        }
        // Written on the value a flag takes, never on the flag.
        if let Some(choices) = flag.arg.as_ref().and_then(|a| a.choices.as_ref()) {
            fields.push(format!(
                "Choices: {}",
                string_slice(&visible_choices(choices))
            ));
            fields.push(format!(
                "AcceptedChoices: {}",
                string_slice(&accepted_choices(choices))
            ));
            if choices.ignore_case {
                fields.push("IgnoreCase: true".to_string());
            }
            if !choices.strict {
                fields.push("AllowUnknownChoices: true".to_string());
            }
        }
        // A default can be written in either place, and usage-lib falls back to
        // the one on the value. `env` deliberately does not follow the same
        // nesting, because usage-lib does not read it there either.
        let default = if !flag.default.is_empty() {
            &flag.default
        } else {
            flag.arg
                .as_ref()
                .map(|a| &a.default)
                .unwrap_or(&flag.default)
        };
        if !default.is_empty() {
            fields.push(format!("Default: {}", string_slice(default)));
        }
        if let Some(env) = &flag.env {
            fields.push(format!("Env: {}", go_string(env)));
        }
        if !flag.env_fallback.is_empty() {
            fields.push(format!("EnvFallback: {}", string_slice(&flag.env_fallback)));
        }
        if !flag.deprecated_env.is_empty() {
            fields.push(format!(
                "DeprecatedEnv: {}",
                string_slice(&flag.deprecated_env)
            ));
        }
        let minimum = flag
            .arg
            .as_ref()
            .filter(|arg| arg.var)
            .and_then(|arg| arg.var_min)
            .or(flag.var_min);
        if let Some(min) = minimum {
            fields.push(format!("VarMin: {}", clamp_var_max(min)));
        }
        // Occurrences. The per-occurrence value bound is a limit binding applies
        // and lives on the parse table.
        if let Some(max) = flag.var_max {
            fields.push(format!("VarMax: {}", clamp_var_max(max)));
        }

        for (label, names) in [
            ("Conflicts", &flag.conflicts),
            ("Overrides", &flag.overrides),
            ("RequiredUnless", &flag.required_unless),
            ("RequiredUnlessAll", &flag.required_unless_all),
            ("RequiredIf", &flag.required_if),
            ("Requires", &flag.requires),
        ] {
            let keys = resolve_relationship(names, owner, commands);
            if !keys.is_empty() {
                fields.push(format!("{label}: {}", key_slice(&keys)));
            }
        }
        for (label, conditions) in [
            ("RequiredIfEq", &flag.required_if_eq),
            ("RequiredIfEqAll", &flag.required_if_eq_all),
        ] {
            let values = conditions
                .iter()
                .filter_map(|condition| {
                    resolve_relationship(std::slice::from_ref(&condition.selector), owner, commands)
                        .into_iter()
                        .next()
                        .map(|key| {
                            format!("{{Key: {key}, Value: {}}}", go_string(&condition.value))
                        })
                })
                .collect::<Vec<_>>();
            if !values.is_empty() {
                fields.push(format!(
                    "{label}: []argv.ValueCondition{{{}}}",
                    values.join(", ")
                ));
            }
        }
        let requires_if = flag
            .requires_if
            .iter()
            .filter_map(|condition| {
                resolve_relationship(std::slice::from_ref(&condition.requires), owner, commands)
                    .into_iter()
                    .next()
                    .map(|key| format!("{{Value: {}, Key: {key}}}", go_string(&condition.value)))
            })
            .collect::<Vec<_>>();
        if !requires_if.is_empty() {
            fields.push(format!(
                "RequiresIf: []argv.ValueRequirement{{{}}}",
                requires_if.join(", ")
            ));
        }
        let default_if = flag
            .default_if
            .iter()
            .filter_map(|condition| {
                resolve_relationship(std::slice::from_ref(&condition.selector), owner, commands)
                    .into_iter()
                    .next()
                    .map(|key| match &condition.when {
                        None => format!("{{Key: {key}, Value: {}}}", go_string(&condition.value)),
                        Some(when) => format!(
                            "{{Key: {key}, When: {}, Value: {}}}",
                            go_string(when),
                            go_string(&condition.value)
                        ),
                    })
            })
            .collect::<Vec<_>>();
        if !default_if.is_empty() {
            fields.push(format!(
                "DefaultIf: []argv.DefaultIf{{{}}}",
                default_if.join(", ")
            ));
        }

        format!("{{{}}}", fields.join(", "))
    }
}

/// The cold half of a positional argument.
/// The type a spec's `complete` block names for an entry, if it names one.
///
/// By lowercased name, which is how usage-lib files them: `complete "FILE"` and
/// an argument written `<file>` are the same position as far as the reference is
/// concerned.
fn complete_type<'a>(spec: &'a Spec, name: &str) -> Option<&'a str> {
    spec.complete
        .get(&name.to_lowercase())
        .and_then(|c| c.type_.as_deref())
}

fn arg_meta(
    spec: &Spec,
    arg: &SpecArg,
    named: &Named,
    owner: &Emitted,
    commands: &[Emitted],
) -> String {
    let mut fields = vec![
        format!("Key: {}", named.key),
        format!("Name: {}", go_string(&arg.name)),
    ];
    if arg.required {
        fields.push("Required: true".to_string());
    }
    // What the position takes, where the spec said so. Read by completion rather
    // than by any post-binding rule: an author who wrote `complete "input"
    // type="file"` named what belongs there, and the alternative is inferring it
    // from a name they did not choose.
    if let Some(kind) = complete_type(spec, &arg.name) {
        fields.push(format!("CompleteType: {}", go_string(kind)));
    }
    if let Some(choices) = &arg.choices {
        fields.push(format!(
            "Choices: {}",
            string_slice(&visible_choices(choices))
        ));
        fields.push(format!(
            "AcceptedChoices: {}",
            string_slice(&accepted_choices(choices))
        ));
        if choices.ignore_case {
            fields.push("IgnoreCase: true".to_string());
        }
        if !choices.strict {
            fields.push("AllowUnknownChoices: true".to_string());
        }
    }
    if !arg.default.is_empty() {
        fields.push(format!("Default: {}", string_slice(&arg.default)));
    }
    if let Some(env) = &arg.env {
        fields.push(format!("Env: {}", go_string(env)));
    }
    if !arg.env_fallback.is_empty() {
        fields.push(format!("EnvFallback: {}", string_slice(&arg.env_fallback)));
    }
    if !arg.deprecated_env.is_empty() {
        fields.push(format!(
            "DeprecatedEnv: {}",
            string_slice(&arg.deprecated_env)
        ));
    }
    if let Some(min) = arg.var_min {
        fields.push(format!("VarMin: {}", clamp_var_max(min)));
    }
    let conflicts = resolve_relationship(&arg.conflicts, owner, commands);
    if !conflicts.is_empty() {
        fields.push(format!("Conflicts: {}", key_slice(&conflicts)));
    }
    for (label, names) in [
        ("Requires", &arg.requires),
        ("RequiredIf", &arg.required_if),
        ("RequiredUnless", &arg.required_unless),
        ("RequiredUnlessAll", &arg.required_unless_all),
    ] {
        let keys = resolve_relationship(names, owner, commands);
        if !keys.is_empty() {
            fields.push(format!("{label}: {}", key_slice(&keys)));
        }
    }
    for (label, conditions) in [
        ("RequiredIfEq", &arg.required_if_eq),
        ("RequiredIfEqAll", &arg.required_if_eq_all),
    ] {
        let values = conditions
            .iter()
            .filter_map(|condition| {
                resolve_relationship(std::slice::from_ref(&condition.selector), owner, commands)
                    .into_iter()
                    .next()
                    .map(|key| format!("{{Key: {key}, Value: {}}}", go_string(&condition.value)))
            })
            .collect::<Vec<_>>();
        if !values.is_empty() {
            fields.push(format!(
                "{label}: []argv.ValueCondition{{{}}}",
                values.join(", ")
            ));
        }
    }
    // No VarMax: for an argument the bound is a limit binding applies, which is
    // what makes `[a]… [b]` fillable at all, so judging it again would fail an
    // invocation that never broke it.
    format!("{{{}}}", fields.join(", "))
}

/// Turn the names in a relationship into the keys they refer to.
///
/// Resolved here, where the whole command is visible, so that nothing downstream
/// searches by name on a path it would repeat per parse. The names arrive as
/// written — `--stdin`, dashes and all — so they are matched against a flag's long
/// forms, its shorts, and the name the spec gives it.
///
/// A name nothing answers to is dropped. That is a spec bug worth reporting, but
/// this function has no way to; the check belongs beside the duplicate-form and
/// duplicate-key checks that already run where the whole tree is visible.
fn resolve_relationship(names: &[String], owner: &Emitted, commands: &[Emitted]) -> Vec<String> {
    let mut out = Vec::new();
    for name in names {
        // The declaring command's own flags first, then any ancestor's globals —
        // the scope a token has, in the order a token gets it, so a subcommand
        // redeclaring an inherited name shadows it here as it does at parse time.
        let mut found = match_flag(owner, name, false);
        if found.is_none() && !name.starts_with('-') {
            found = owner
                .args
                .iter()
                .find(|(arg, _)| arg.name == *name)
                .map(|(_, named)| named.key.clone());
        }
        if found.is_none() {
            let path = &owner.cmd.full_cmd;
            for depth in (0..path.len()).rev() {
                let ancestor = commands
                    .iter()
                    .find(|e| e.cmd.full_cmd.len() == depth && e.cmd.full_cmd[..] == path[..depth]);
                if let Some(key) = ancestor.and_then(|a| match_flag(a, name, true)) {
                    found = Some(key);
                    break;
                }
            }
        }
        if let Some(key) = found {
            out.push(key);
        }
    }
    out
}

/// Find a flag by any spelling a declaration may use for it.
///
/// The negation counts, and resolves to the same entry: usage-lib treats
/// `conflicts = "--no-color"` as naming the `color` flag and reports the conflict
/// whichever of the two spellings was typed. The relationship is between entries
/// rather than between tokens, which is what the key model already assumes.
fn match_flag(cmd: &Emitted, name: &str, globals_only: bool) -> Option<String> {
    // Two passes, in the order the parser itself looks: every ordinary form
    // first, then negations.
    //
    // That order is not a nicety. The parser tries every long form before it
    // tries any negation, so with `--a` declaring `negate = "--zap"` and a
    // separate `--zap`, typing `--zap` binds *zap*. A per-candidate search hands
    // the relationship to `a`, and the table then enforces a rule against a flag
    // the command line never binds. The table has to agree with the binder it
    // feeds.
    let eligible = |flag: &SpecFlag| !globals_only || flag.global;

    // The form is part of the name: `--q` does not reach the short `-q`, and
    // `-color` does not reach the long `--color`. usage-lib resolves neither.
    let (long, short, bare) = if let Some(rest) = name.strip_prefix("--") {
        (Some(rest), None, None)
    } else if let Some(rest) = name.strip_prefix('-') {
        let mut chars = rest.chars();
        match (chars.next(), chars.next()) {
            (Some(c), None) => (None, Some(c), None),
            _ => (None, None, None),
        }
    } else {
        (None, None, Some(name))
    };

    let ordinary = cmd.flags.iter().find(|(flag, _)| {
        if !eligible(flag) {
            return false;
        }
        if let Some(bare) = bare {
            return flag.name == bare;
        }
        if let Some(long) = long {
            return flag.long.iter().any(|l| l == long);
        }
        short.is_some_and(|c| flag.short.contains(&c))
    });
    if let Some((_, named)) = ordinary {
        return Some(named.key.clone());
    }

    // Negations, compared exactly as both sides were written — dashes included.
    // `negate = "-no-tint"` is named by `-no-tint` and not by `--no-tint`, and
    // usage-lib resolves it that way round too.
    cmd.flags
        .iter()
        .find(|(flag, _)| eligible(flag) && flag.negate.as_deref() == Some(name))
        .map(|(_, named)| named.key.clone())
}
fn string_slice(values: &[String]) -> String {
    let list = values
        .iter()
        .map(|v| go_string(v))
        .collect::<Vec<_>>()
        .join(", ");
    format!("[]string{{{list}}}")
}

fn accepted_choices(choices: &SpecChoices) -> Vec<String> {
    choices
        .choices
        .iter()
        .chain(
            choices
                .details
                .iter()
                .flat_map(|choice| choice.aliases.iter().map(|alias| &alias.value)),
        )
        .cloned()
        .collect()
}

fn visible_choices(choices: &SpecChoices) -> Vec<String> {
    choices
        .choices
        .iter()
        .filter(|value| {
            !choices
                .details
                .iter()
                .any(|choice| choice.value == value.as_str() && choice.hide)
        })
        .chain(choices.details.iter().flat_map(|choice| {
            choice
                .aliases
                .iter()
                .filter(|alias| !alias.hide)
                .map(|alias| &alias.value)
        }))
        .cloned()
        .collect()
}

fn key_slice(keys: &[String]) -> String {
    format!("[]uint64{{{}}}", keys.join(", "))
}

impl Emitter<'_> {
    /// Emit the help table: what a page prints.
    ///
    /// A third table rather than more fields on `Meta`, because Go's linker drops
    /// an unreferenced package-level symbol whole — folding help text into the
    /// post-binding table would make every CLI that applies a rule carry mise's
    /// several hundred kilobytes of help strings too.
    fn help_table(&mut self, commands: &[Emitted]) {
        let mut by_key: BTreeMap<u64, String> = BTreeMap::new();
        for e in commands {
            by_key.insert(e.named.number, command_help(e));
            for (flag, named) in &e.flags {
                by_key.insert(named.number, flag_help(flag, named));
            }
            for (arg, named) in &e.args {
                by_key.insert(named.number, arg_help(arg, named));
            }
        }

        let total = commands
            .iter()
            .map(|e| 1 + e.flags.len() + e.args.len())
            .sum::<usize>() as u64;

        let _ = writeln!(
            self.out,
            "// HelpText is the third table, read only when a page is rendered. Neither the\n\
             // parser nor the post-binding rules touch it, and a CLI that never prints help\n\
             // does not carry it: Go's linker drops an unreferenced table whole.\n\
             //\n\
             // Indexed by key, like the others.\n\
             var HelpText = argv.HelpTable{{"
        );
        for key in 1..=total {
            match by_key.get(&key) {
                Some(entry) => {
                    let _ = writeln!(self.out, "\t{entry},");
                }
                None => {
                    let _ = writeln!(self.out, "\t{{}},");
                }
            }
        }
        let _ = writeln!(self.out, "}}\n");

        let mut fields = vec![
            format!("Name: {}", go_string(&self.spec.name)),
            format!("Bin: {}", go_string(&self.spec.bin)),
        ];
        if let Some(version) = self
            .spec
            .version
            .as_ref()
            .or(self.spec.long_version.as_ref())
        {
            fields.push(format!("Version: {}", go_string(version)));
        }
        if let Some(version) = &self.spec.long_version {
            fields.push(format!("LongVersion: {}", go_string(version)));
        }
        // `about` alone, with no fall back to the long one: usage-lib's short page
        // prints nothing where a spec wrote only `about_long`, and the long page
        // is what reads LongAbout.
        if let Some(about) = &self.spec.about {
            fields.push(format!("About: {}", go_string(about)));
        }
        if let Some(long) = &self.spec.about_long {
            fields.push(format!("LongAbout: {}", go_string(long)));
        }
        if let Some(author) = &self.spec.author {
            fields.push(format!("Author: {}", go_string(author)));
        }
        if let Some(license) = &self.spec.license {
            fields.push(format!("License: {}", go_string(license)));
        }
        if let Some(before) = &self.spec.before_help {
            fields.push(format!("BeforeHelp: {}", go_string(before)));
        }
        if let Some(after) = &self.spec.after_help {
            fields.push(format!("AfterHelp: {}", go_string(after)));
        }
        if let Some(before) = &self.spec.before_help_long {
            fields.push(format!("BeforeLongHelp: {}", go_string(before)));
        }
        if let Some(after) = &self.spec.after_help_long {
            fields.push(format!("AfterLongHelp: {}", go_string(after)));
        }
        // One template for the whole tree, naming the sections a page is assembled from.
        if let Some(template) = &self.spec.help_template {
            fields.push(format!("HelpTemplate: {}", go_string(template)));
        }
        let _ = writeln!(
            self.out,
            "// HelpMeta is what a page needs from the spec's root rather than from any one\n\
             // command: the header, and the text that brackets every page.\n\
             var HelpMeta = argv.HelpSpec{{{}}}\n",
            fields.join(", ")
        );
    }
}

/// The help entry for a command: its about text.
fn command_help(e: &Emitted) -> String {
    let mut fields = vec![format!("Key: {}", e.named.key)];
    if e.cmd.hide {
        fields.push("Hide: true".to_string());
    }
    if let Some(heading) = &e.cmd.help_heading {
        fields.push(format!("Heading: {}", go_string(heading)));
    }
    if let Some(order) = e.cmd.display_order {
        fields.push(format!("DisplayOrder: {order}"));
        fields.push("DisplayOrderSet: true".to_string());
    }
    if let Some(help) = e.cmd.help.as_deref().or(e.cmd.help_long.as_deref()) {
        fields.push(format!("Short: {}", go_string(help)));
    }
    if let Some(long) = &e.cmd.help_long {
        fields.push(format!("Long: {}", go_string(long)));
    }
    if let Some(message) = &e.cmd.deprecated {
        fields.push(format!("Deprecated: {}", go_string(message)));
    }
    if let Some(at) = &e.cmd.deprecated_warn_at {
        fields.push(format!("DeprecatedWarnAt: {}", go_string(at)));
    }
    if let Some(at) = &e.cmd.deprecated_remove_at {
        fields.push(format!("DeprecatedRemoveAt: {}", go_string(at)));
    }
    if let Some(heading) = &e.cmd.subcommand_help_heading {
        fields.push(format!("SubcommandHelpHeading: {}", go_string(heading)));
    }
    if let Some(name) = &e.cmd.subcommand_value_name {
        fields.push(format!("SubcommandValueName: {}", go_string(name)));
    }
    if e.cmd.next_line_help {
        fields.push("NextLineHelp: true".to_string());
    }
    if e.cmd.flatten_help {
        fields.push("FlattenHelp: true".to_string());
    }
    if e.cmd.subcommand_required {
        fields.push("SubcommandRequired: true".to_string());
    }
    // Visible only: the parse table merges hidden aliases in beside these,
    // because binding does not care which is which. A page does.
    let visible: Vec<String> = e
        .cmd
        .aliases
        .iter()
        .filter(|a| !e.cmd.hidden_aliases.contains(a))
        .cloned()
        .collect();
    if !visible.is_empty() {
        fields.push(format!("VisibleAliases: {}", string_slice(&visible)));
    }
    if let Some(before) = &e.cmd.before_help {
        fields.push(format!("BeforeHelp: {}", go_string(before)));
    }
    if let Some(after) = &e.cmd.after_help {
        fields.push(format!("AfterHelp: {}", go_string(after)));
    }
    // The long page's own brackets, which most of mise's commands use: their
    // examples are written as `after_long_help`, and a generated CLI that dropped
    // them printed a page with the examples missing.
    if let Some(before) = &e.cmd.before_help_long {
        fields.push(format!("BeforeLongHelp: {}", go_string(before)));
    }
    if let Some(after) = &e.cmd.after_help_long {
        fields.push(format!("AfterLongHelp: {}", go_string(after)));
    }
    if !e.cmd.examples.is_empty() {
        let items = e
            .cmd
            .examples
            .iter()
            .map(|x| {
                let mut parts = Vec::new();
                if let Some(header) = &x.header {
                    parts.push(format!("Header: {}", go_string(header)));
                }
                parts.push(format!("Code: {}", go_string(&x.code)));
                // The line the long page prints above the command. It introduces
                // the invocation rather than commenting on it, and a generated CLI
                // that dropped it printed the command with nothing to say why.
                if let Some(help) = &x.help {
                    parts.push(format!("Help: {}", go_string(help)));
                }
                format!("{{{}}}", parts.join(", "))
            })
            .collect::<Vec<_>>()
            .join(", ");
        fields.push(format!("Examples: []argv.Example{{{items}}}"));
    }
    format!("{{{}}}", fields.join(", "))
}

fn flag_help(flag: &SpecFlag, named: &Named) -> String {
    let mut fields = vec![format!("Key: {}", named.key)];
    if let Some(message) = &flag.deprecated {
        fields.push(format!("Deprecated: {}", go_string(message)));
    }
    if let Some(at) = &flag.deprecated_warn_at {
        fields.push(format!("DeprecatedWarnAt: {}", go_string(at)));
    }
    if let Some(at) = &flag.deprecated_remove_at {
        fields.push(format!("DeprecatedRemoveAt: {}", go_string(at)));
    }
    if flag.hide {
        fields.push("Hide: true".to_string());
    }
    if let Some(order) = flag.display_order {
        fields.push(format!("DisplayOrder: {order}"));
        fields.push("DisplayOrderSet: true".to_string());
    }
    for (name, hidden) in [
        ("HideDefaultValue", flag.hide_default_value),
        ("HideEnv", flag.hide_env),
        ("HideEnvValues", flag.hide_env_values),
        ("HidePossibleValues", flag.hide_possible_values),
        ("HideShortHelp", flag.hide_short_help),
        ("HideLongHelp", flag.hide_long_help),
    ] {
        if hidden {
            fields.push(format!("{name}: true"));
        }
    }
    // Required *and* undefaulted, which is what decides the brackets: a required
    // flag with a default is one the user never has to type.
    if flag.required && flag.default.is_empty() {
        fields.push("Demanded: true".to_string());
    }
    if flag.var {
        fields.push("Repeatable: true".to_string());
    }
    if let Some(arg) = &flag.arg {
        if arg.name != flag.name {
            fields.push(format!("ValueName: {}", go_string(&arg.name)));
        }
        // The value's own requiredness, which is independent of the flag's:
        // `<--v <n>>` is a required flag whose value must be given, and
        // `<--jobs [n]>` a required flag whose value has a default.
        if arg.required && arg.default.is_empty() {
            fields.push("ValueDemanded: true".to_string());
        }
        if !arg.value_names.is_empty() {
            fields.push(format!("ValueNames: {}", string_slice(&arg.value_names)));
        }
        if arg.var && arg.var_min == arg.var_max && arg.var_min.is_some_and(|n| n > 1) {
            fields.push(format!("ValueArity: {}", arg.var_min.unwrap()));
        }
    }
    // The whole `help`, not its first line: usage-lib's short page prints the
    // text as declared, and mise has flags whose help is two lines.
    if let Some(help) = flag.help.as_deref().or(flag.help_first_line.as_deref()) {
        fields.push(format!("Short: {}", go_string(help)));
    }
    if let Some(long) = flag.help_long.as_deref().or(flag.help.as_deref()) {
        fields.push(format!("Long: {}", go_string(long)));
    }
    if let Some(heading) = &flag.help_heading {
        fields.push(format!("Heading: {}", go_string(heading)));
    }
    // Annotations. A flag's choices are declared on the value it takes.
    if let Some(choices) = flag.arg.as_ref().and_then(|a| a.choices.as_ref()) {
        fields.push(format!(
            "Choices: {}",
            string_slice(&visible_choices(choices))
        ));
    }
    if let Some(env) = &flag.env {
        fields.push(format!("Env: {}", go_string(env)));
    }
    if !flag.env_fallback.is_empty() {
        fields.push(format!("EnvFallback: {}", string_slice(&flag.env_fallback)));
    }
    if !flag.deprecated_env.is_empty() {
        fields.push(format!(
            "DeprecatedEnv: {}",
            string_slice(&flag.deprecated_env)
        ));
    }
    let default = if !flag.default.is_empty() {
        &flag.default
    } else {
        flag.arg
            .as_ref()
            .map(|a| &a.default)
            .unwrap_or(&flag.default)
    };
    if !default.is_empty() {
        fields.push(format!("Default: {}", string_slice(default)));
    }
    format!("{{{}}}", fields.join(", "))
}

fn arg_help(arg: &SpecArg, named: &Named) -> String {
    let mut fields = vec![format!("Key: {}", named.key)];
    if let Some(order) = arg.display_order {
        fields.push(format!("DisplayOrder: {order}"));
        fields.push("DisplayOrderSet: true".to_string());
    }
    if arg.hide {
        fields.push("Hide: true".to_string());
    }
    for (name, hidden) in [
        ("HideDefaultValue", arg.hide_default_value),
        ("HideEnv", arg.hide_env),
        ("HideEnvValues", arg.hide_env_values),
        ("HidePossibleValues", arg.hide_possible_values),
        ("HideShortHelp", arg.hide_short_help),
        ("HideLongHelp", arg.hide_long_help),
    ] {
        if hidden {
            fields.push(format!("{name}: true"));
        }
    }
    if arg.required && arg.default.is_empty() {
        fields.push("Demanded: true".to_string());
    }
    if !arg.value_names.is_empty() {
        fields.push(format!("ValueNames: {}", string_slice(&arg.value_names)));
    }
    if arg.var && arg.var_min == arg.var_max && arg.var_min.is_some_and(|n| n > 1) {
        fields.push(format!("ValueArity: {}", arg.var_min.unwrap()));
    }
    if let Some(help) = arg.help.as_deref().or(arg.help_first_line.as_deref()) {
        fields.push(format!("Short: {}", go_string(help)));
    }
    if let Some(long) = arg.help_long.as_deref().or(arg.help.as_deref()) {
        fields.push(format!("Long: {}", go_string(long)));
    }
    if let Some(heading) = &arg.help_heading {
        fields.push(format!("Heading: {}", go_string(heading)));
    }
    if let Some(choices) = &arg.choices {
        fields.push(format!(
            "Choices: {}",
            string_slice(&visible_choices(choices))
        ));
    }
    if let Some(env) = &arg.env {
        fields.push(format!("Env: {}", go_string(env)));
    }
    if !arg.env_fallback.is_empty() {
        fields.push(format!("EnvFallback: {}", string_slice(&arg.env_fallback)));
    }
    if !arg.deprecated_env.is_empty() {
        fields.push(format!(
            "DeprecatedEnv: {}",
            string_slice(&arg.deprecated_env)
        ));
    }
    if !arg.default.is_empty() {
        fields.push(format!("Default: {}", string_slice(&arg.default)));
    }
    format!("{{{}}}", fields.join(", "))
}

/// A line inside a `const` block or a composite literal.
///
/// The distinction exists only to reproduce gofmt's alignment, which pads within
/// *runs* of consecutive single-line entries and starts a new run after anything
/// that spans lines. Emitting gofmt-clean output rather than close-enough output
/// is what lets a generated file be committed as it comes out: the alternative is
/// every adopter needing a formatting step, and this repo's own CI failing
/// `gofmt -l` on the table it checks in.
enum Line {
    /// `Key: value,` — aligned against its neighbours.
    Field(String, String),
    /// Verbatim, and it breaks the run either side of it.
    Block(Vec<String>),
}

/// Render lines with gofmt's column alignment.
fn render(out: &mut String, indent: &str, lines: &[Line]) {
    let mut run: Vec<(&String, &String)> = Vec::new();

    fn flush(out: &mut String, indent: &str, run: &mut Vec<(&String, &String)>) {
        let width = run.iter().map(|(k, _)| k.len()).max().unwrap_or(0);
        for (key, value) in run.iter() {
            let _ = writeln!(
                out,
                "{indent}{key}:{:width$} {value},",
                "",
                width = width - key.len()
            );
        }
        run.clear();
    }

    for line in lines {
        match line {
            Line::Field(key, value) => run.push((key, value)),
            Line::Block(block) => {
                flush(out, indent, &mut run);
                for l in block {
                    let _ = writeln!(out, "{indent}{l}");
                }
            }
        }
    }
    flush(out, indent, &mut run);
}

/// One command, named and ready to emit.
struct Emitted {
    named: Named,
    cmd: SpecCommand,
    flags: Vec<(SpecFlag, Named)>,
    args: Vec<(SpecArg, Named)>,
    /// Indices into the flat list, in declaration order.
    subcommands: Vec<usize>,
    root: bool,
}

/// What an unrecognized flag-like token means at a command, with inheritance
/// applied.
///
/// The nearest enclosing command that states a preference wins, then the spec,
/// then `value`. Walked over `full_cmd` rather than threaded through the collect
/// pass, so that emission does not depend on the order commands happen to sit in.
fn effective_unknown_flags(spec: &Spec, commands: &[Emitted], at: usize) -> UnknownFlags {
    let path = &commands[at].cmd.full_cmd;
    for depth in (0..=path.len()).rev() {
        let ancestor = commands
            .iter()
            .find(|e| e.cmd.full_cmd.len() == depth && e.cmd.full_cmd[..] == path[..depth]);
        if let Some(mode) = ancestor.and_then(|e| e.cmd.unknown_flags) {
            return mode;
        }
    }
    spec.unknown_flags.unwrap_or_default()
}

fn flag_literal(flag: &SpecFlag, named: &Named) -> String {
    let mut fields = vec![
        format!("Key: {}", named.key),
        format!("Name: {}", go_string(&flag.name)),
    ];
    if !flag.long.is_empty() {
        let longs = flag
            .long
            .iter()
            .map(|l| go_string(l))
            .collect::<Vec<_>>()
            .join(", ");
        fields.push(format!("Longs: []string{{{longs}}}"));
    }
    if !flag.hidden_aliases.is_empty() {
        fields.push(format!(
            "HiddenLongs: {}",
            string_slice(&flag.hidden_aliases)
        ));
    }
    if !flag.short.is_empty() {
        let shorts = flag
            .short
            .iter()
            .map(|c| go_byte(*c))
            .collect::<Vec<_>>()
            .join(", ");
        fields.push(format!("Shorts: []byte{{{shorts}}}"));
    }
    if !flag.hidden_short_aliases.is_empty() {
        let shorts = flag
            .hidden_short_aliases
            .iter()
            .map(|c| go_byte(*c))
            .collect::<Vec<_>>()
            .join(", ");
        fields.push(format!("HiddenShorts: []byte{{{shorts}}}"));
    }
    if let Some(negate) = &flag.negate {
        // The spec stores the negation with its dashes; the table wants the bare
        // name, since that is what the parser has after stripping the `--`.
        fields.push(format!(
            "Negate: {}",
            go_string(negate.trim_start_matches('-'))
        ));
    }
    if flag.arg.is_some() {
        fields.push("TakesValue: true".to_string());
    }
    if flag.value_optional {
        fields.push("ValueOptional: true".to_string());
    }
    if flag.bool_value {
        fields.push("BoolValue: true".to_string());
    }
    let action = match flag.action {
        SpecFlagAction::Set => None,
        SpecFlagAction::Help => Some("argv.ActionHelp"),
        SpecFlagAction::HelpShort => Some("argv.ActionHelpShort"),
        SpecFlagAction::HelpLong => Some("argv.ActionHelpLong"),
        SpecFlagAction::HelpAll => Some("argv.ActionHelpAll"),
        SpecFlagAction::Version => Some("argv.ActionVersion"),
    };
    if let Some(action) = action {
        fields.push(format!("Action: {action}"));
    }
    // Only a variadic *argument* is greedy. The spec's flag-level `var` means the
    // flag may be repeated and takes one value each time, which needs nothing from
    // the parser: it reports every occurrence separately either way. Conflating the
    // two makes a merely repeatable flag greedy enough to eat a positional.
    if let Some(arg) = flag.arg.as_ref().filter(|a| a.var) {
        fields.push("Variadic: true".to_string());
        if let Some(max) = arg.var_max {
            fields.push(format!("VarMax: {}", clamp_var_max(max)));
        }
    }
    if flag.allow_hyphen_values() {
        fields.push("AllowHyphenValues: true".to_string());
    }
    if let Some(arg) = &flag.arg {
        if arg.allow_negative_numbers {
            fields.push("AllowNegativeNumbers: true".to_string());
        }
        if let Some(terminator) = &arg.value_terminator {
            fields.push(format!("ValueTerminator: {}", go_string(terminator)));
        }
        if let Some(delimiter) = arg.delimiter {
            fields.push(format!("Delimiter: {}", go_byte(delimiter)));
        }
    }
    if flag.require_equals {
        fields.push("RequireEquals: true".to_string());
    }
    if let Some(missing) = &flag.default_missing {
        fields.push(format!("DefaultMissing: {}", go_string(missing)));
    }
    if flag.global {
        fields.push("Global: true".to_string());
    }
    format!("{{{}}}", fields.join(", "))
}

fn arg_literal(arg: &SpecArg, named: &Named) -> String {
    let mut fields = vec![
        format!("Key: {}", named.key),
        format!("Name: {}", go_string(&arg.name)),
    ];
    if arg.required {
        fields.push("Required: true".to_string());
    }
    if arg.var {
        fields.push("Var: true".to_string());
        if let Some(max) = arg.var_max {
            fields.push(format!("VarMax: {}", clamp_var_max(max)));
        }
    }
    if arg.allow_negative_numbers {
        fields.push("AllowNegativeNumbers: true".to_string());
    }
    if let Some(terminator) = &arg.value_terminator {
        fields.push(format!("ValueTerminator: {}", go_string(terminator)));
    }
    if let Some(delimiter) = arg.delimiter {
        fields.push(format!("Delimiter: {}", go_byte(delimiter)));
    }
    let double_dash = match arg.double_dash {
        SpecDoubleDashChoices::Required => Some("argv.DoubleDashRequired"),
        SpecDoubleDashChoices::Preserve => Some("argv.DoubleDashPreserve"),
        SpecDoubleDashChoices::Automatic => Some("argv.DoubleDashAutomatic"),
        _ => None,
    };
    if let Some(dd) = double_dash {
        fields.push(format!("DoubleDash: {dd}"));
    }
    format!("{{{}}}", fields.join(", "))
}

/// Zero means unbounded in the table, which is also what an absent `var_max`
/// lowers to, so the two agree. A bound past a `uint32` saturates rather than
/// wrapping: truncating four billion and one to one would read as "stop at once"
/// rather than "no real limit".
fn clamp_var_max(max: usize) -> u32 {
    u32::try_from(max).unwrap_or(u32::MAX)
}

/// Go's reserved words, which cannot be a package name.
///
/// Not hypothetical: `go`, `range`, `select`, `import` and `package` are all
/// plausible names for a CLI, and `package go` does not compile.
const GO_KEYWORDS: &[&str] = &[
    "break",
    "case",
    "chan",
    "const",
    "continue",
    "default",
    "defer",
    "else",
    "fallthrough",
    "for",
    "func",
    "go",
    "goto",
    "if",
    "import",
    "interface",
    "map",
    "package",
    "range",
    "return",
    "select",
    "struct",
    "switch",
    "type",
    "var",
];

/// Two more names a table package cannot have, for two different reasons.
///
/// `_` is refused where it is written: `invalid package name _`. `init` declares
/// perfectly well and cannot be *imported* — an import binds the package name as
/// an identifier in file scope, and `init` may only be a func, so an importer gets
/// `cannot import package as init - init must be a func`. A table package exists
/// to be imported, so it is out either way.
///
/// Both checked against the compiler rather than taken from a citation. The issue
/// usually cited for `init` is about the import, and `package init` on its own
/// does build — so a validator written from the citation would have rejected it
/// for a reason that is not true.
const UNUSABLE_PACKAGE_NAMES: &[&str] = &["_", "init"];

/// Whether a string can be written after `package` and then imported.
///
/// Deliberately ASCII-only. Go itself allows a Unicode letter, but a package name
/// that needs one is a worse problem for an adopter than the restriction is.
pub fn is_valid_package(name: &str) -> bool {
    !name.is_empty()
        && !GO_KEYWORDS.contains(&name)
        && !UNUSABLE_PACKAGE_NAMES.contains(&name)
        && !name.starts_with(|c: char| c.is_ascii_digit())
        && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
}

/// A Go field name from a spec name: exported, and an identifier.
fn field_name(name: &str) -> String {
    let ident = format!("{}", AsPascalCase(name));
    if ident.is_empty() || ident.starts_with(|c: char| c.is_ascii_digit()) {
        format!("X{ident}")
    } else {
        ident
    }
}

/// A Go package identifier from a binary name: `my-cli` is not one, `mycli` is.
///
/// Only ever applied to a name derived from the spec, which the author did not
/// choose for this purpose and cannot be asked to fix. A `--package` given
/// explicitly is checked rather than mangled — see [`is_valid_package`] — because
/// silently emitting `mypkg` for someone who asked for `my-pkg` is a surprise
/// waiting in a build script.
fn package_ident(bin: &str) -> String {
    let lowered: String = bin
        .chars()
        .filter(|c| c.is_ascii_alphanumeric() || *c == '_')
        .collect::<String>()
        .to_ascii_lowercase();
    if is_valid_package(&lowered) {
        lowered
    } else {
        // One rule rather than a second copy of the conditions, so the sanitizer
        // cannot come to disagree with the validator about what is acceptable.
        // `cli` in front keeps it recognizable: `cligo`, `cli7zip`, `cliinit`.
        format!("cli{lowered}")
    }
}

/// A Go string literal.
///
/// Written out rather than borrowed from Rust's `{:?}`, which escapes to Rust's
/// rules: it spells a delete character `\u{7f}`, which Go does not accept.
fn go_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 || c as u32 == 0x7f => {
                let _ = write!(out, "\\x{:02x}", c as u32);
            }
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

/// A Go byte literal for a short flag.
///
/// Non-ASCII shorts are emitted as their low byte, which can never match: a
/// cluster is walked one byte at a time. The spec is what should refuse them, and
/// silently dropping one here would be a flag that vanished.
fn go_byte(c: char) -> String {
    match c {
        '\'' => "'\\''".to_string(),
        '\\' => "'\\\\'".to_string(),
        c if c.is_ascii_graphic() => format!("'{c}'"),
        c => format!("0x{:02x}", (c as u32) & 0xff),
    }
}

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

    /// The emitted `Meta` line for an entry, so a test can assert about the part
    /// it cares about rather than the whole rendered row — which grows a field
    /// every time the cold table learns something.
    ///
    /// Used for what a row *does* say as well as for what it does not. Two
    /// substring checks over the whole file — one for the name, one for the
    /// relationship — pass when the relationship is attached to a different flag
    /// entirely, which is the regression these tests exist to catch.
    fn entry_of(out: &str, name: &str) -> String {
        out.lines()
            .find(|l| l.contains(&format!("Name: \"{name}\", Flag: true")))
            .unwrap_or_default()
            .to_string()
    }

    fn go(kdl: &str) -> String {
        let spec: Spec = kdl.parse().expect("the fixture spec should parse");
        generate(&spec, &GoOptions::default())
    }

    #[test]
    fn a_whole_cli() {
        let out = go(r#"
name "ex"
bin "ex"
version "1.2.3"
long_version "1.2.3\ncommit abc123"
flag "-v --verbose" global=#true help="be loud"
flag "--color" negate="--no-color"
flag "-j --jobs <n>"
flag "--include <pattern>..." var_max=3
arg "<file>"
arg "[rest]..." var=#true
cmd "install" {
    alias "i"
    flag "-f --force"
    arg "<pkg>"
}
cmd "config" {
    cmd "ls" {
        flag "--no-header"
    }
}
"#);
        insta::assert_snapshot!(out);
    }

    #[test]
    fn rich_choices_keep_acceptance_visibility_and_strictness_separate() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--color <when>" {
    choices ignore_case=#true strict=#false {
        choice "always" {
            alias "yes"
            alias "on" hide=#true
        }
        choice "never" hide=#true
    }
}
"#);
        let entry = entry_of(&out, "color");
        assert!(
            entry.contains(r#"Choices: []string{"always", "yes"}"#),
            "{entry}"
        );
        assert!(
            entry.contains(r#"AcceptedChoices: []string{"always", "never", "yes", "on"}"#),
            "{entry}"
        );
        assert!(entry.contains("IgnoreCase: true"), "{entry}");
        assert!(entry.contains("AllowUnknownChoices: true"), "{entry}");
    }

    /// Inheritance is resolved here so the parser reads one field per command.
    #[test]
    fn unknown_flags_are_inherited_and_overridable() {
        let out = go(r#"
name "ex"
bin "ex"
unknown_flags "error"
cmd "strict" {
    cmd "deep" {}
}
cmd "exec" unknown_flags="value" {
    cmd "nested" {}
}
"#);
        insta::assert_snapshot!(out);
    }

    /// mise declares both a `macos-defaults` command and a `macos defaults` path,
    /// and both want the same Go identifier.
    #[test]
    fn colliding_names_get_distinct_identifiers() {
        let out = go(r#"
name "ex"
bin "ex"
cmd "macos-defaults" {
    flag "--apply"
}
cmd "macos" {
    cmd "defaults" {
        flag "--apply"
    }
}
"#);
        insta::assert_snapshot!(out);
    }

    #[test]
    fn a_default_subcommand_points_into_the_tree() {
        let out = go(r#"
name "ex"
bin "ex"
default_subcommand "run"
arg "[task]"
cmd "run" {
    arg "[args]..." var=#true
}
"#);
        insta::assert_snapshot!(out);
    }

    #[test]
    fn a_bin_name_that_is_not_an_identifier_still_gives_a_package() {
        assert_eq!(package_ident("my-cli"), "mycli");
        assert_eq!(package_ident("7zip"), "cli7zip");
        assert_eq!(package_ident(""), "cli");
        // `package go` does not compile, and `go` is a plausible name for a CLI.
        assert_eq!(package_ident("go"), "cligo");
        assert_eq!(package_ident("type"), "clitype");
        // `package _` is refused outright; `package init` declares fine and cannot
        // be imported, which for a table package is the same thing.
        assert_eq!(package_ident("_"), "cli_");
        assert_eq!(package_ident("init"), "cliinit");
        // Two underscores is fine, and only the exact name is reserved.
        assert_eq!(package_ident("__"), "__");
        assert_eq!(package_ident("initialize"), "initialize");

        // Whatever it produces must be something the validator accepts, for every
        // one of these — the sanitizer disagreeing with the check is how a file
        // that does not compile gets emitted.
        for bin in [
            "my-cli", "7zip", "", "go", "type", "_", "init", "__", "MiSe",
        ] {
            let out = package_ident(bin);
            assert!(is_valid_package(&out), "{bin:?} sanitized to {out:?}");
        }
    }

    /// Counting alone let a third command collide with a generated suffix.
    #[test]
    fn a_name_matching_a_generated_suffix_still_gets_its_own() {
        let out = go(r#"
name "ex"
bin "ex"
cmd "macos-defaults" {}
cmd "macos" {
    cmd "defaults" {}
}
cmd "macos-defaults2" {}
"#);
        // The invariant, not a guess at the spelling. The third command lands on
        // `CmdMacosDefaults22` rather than `...3`, which is unlovely and correct;
        // asserting the exact name would pin the suffix scheme instead of the
        // property that matters, which is that nothing is declared twice.
        assert_declares_each_constant_once(&out);
    }

    /// Every constant in the emitted `const` block, in declaration order.
    fn constant_names(out: &str) -> Vec<&str> {
        out.lines()
            .skip_while(|l| !l.starts_with("const ("))
            .skip(1)
            .take_while(|l| !l.starts_with(')'))
            .filter_map(|l| l.split_whitespace().next())
            .collect()
    }

    /// Two entries sharing a constant is a file that does not compile.
    fn assert_declares_each_constant_once(out: &str) {
        let names = constant_names(out);
        assert!(!names.is_empty(), "no constants at all:\n{out}");
        let mut seen = std::collections::HashSet::new();
        for name in &names {
            assert!(seen.insert(*name), "{name} is declared twice:\n{out}");
        }
    }

    #[test]
    fn a_package_that_would_not_compile_is_refused_rather_than_emitted() {
        assert!(is_valid_package("mycli"));
        assert!(is_valid_package("mise_tables"));
        assert!(!is_valid_package("my-pkg"));
        assert!(!is_valid_package("7zip"));
        assert!(!is_valid_package(""));
        assert!(!is_valid_package("range"));
        assert!(!is_valid_package("_"));
        assert!(!is_valid_package("init"));
        assert!(is_valid_package("__"));

        // A library caller that skips the check still gets a file that compiles.
        let spec: Spec = "name \"ex\"\nbin \"ex\"\n".parse().unwrap();
        let out = generate(
            &spec,
            &GoOptions {
                package: Some("my-pkg".into()),
            },
        );
        assert!(out.contains("package mypkg"), "{out}");
    }

    /// The bug the checked-in mise tables caught: `default_subcommand run` was
    /// wired to `oci run`, which a depth-first walk reaches first.
    ///
    /// It names a subcommand *of the root*, so nothing deeper is a candidate — and
    /// the parser would otherwise descend into a command that is not the root's
    /// child at all.
    /// The long page's text is a table entry too.
    ///
    /// mise writes its examples as `after_long_help`, on 115 of its commands, so a
    /// generator that dropped them emitted a `--help` with every example missing —
    /// while the page tests, which build their tables by lowering rather than by
    /// generating, saw nothing wrong. The two producers are compared against each
    /// other now; this is the same rule from the emitter's side.
    #[test]
    fn the_long_pages_text_reaches_the_tables() {
        let out = go(r#"
name "ex"
bin "ex"
about "Short."
about_long "Long."
before_long_help "ROOT-BEFORE"
after_long_help "ROOT-AFTER"
cmd "run" help="Run it" {
    before_long_help "RUN-BEFORE"
    after_long_help "RUN-AFTER"
}
"#);
        let meta = out
            .lines()
            .find(|l| l.contains("var HelpMeta"))
            .expect("a root header is emitted");
        assert!(
            meta.contains(r#"About: "Short.""#) && meta.contains(r#"LongAbout: "Long.""#),
            "the two abouts are separate fields: {meta}"
        );
        assert!(
            meta.contains(r#"BeforeLongHelp: "ROOT-BEFORE""#)
                && meta.contains(r#"AfterLongHelp: "ROOT-AFTER""#),
            "the root's long brackets are emitted: {meta}"
        );

        let run = out
            .lines()
            .find(|l| l.contains("Short: \"Run it\""))
            .expect("the command has a help entry");
        assert!(
            run.contains(r#"BeforeLongHelp: "RUN-BEFORE""#)
                && run.contains(r#"AfterLongHelp: "RUN-AFTER""#),
            "a command's long brackets are emitted: {run}"
        );
    }

    /// An example's help line reaches the tables.
    ///
    /// The long page prints it above the command, where it introduces the
    /// invocation; a generated CLI that dropped it printed the command with
    /// nothing to say why. mise cannot show this — it writes its examples as
    /// `after_long_help` text rather than as `example` nodes — so the producer
    /// comparison over mise's spec cannot see it either.
    #[test]
    fn an_examples_help_line_reaches_the_tables() {
        let out = go(r#"
name "ex"
bin "ex"
cmd "run" help="Run it" {
    example "ex run --fast" header="Speed" help="When you are in a hurry"
    example "ex run"
}
"#);
        let run = out
            .lines()
            .find(|l| l.contains("Examples: []argv.Example"))
            .expect("the command's examples are emitted");
        assert!(
            run.contains(
                r#"{Header: "Speed", Code: "ex run --fast", Help: "When you are in a hurry"}"#
            ),
            "all three fields are emitted: {run}"
        );
        // And a bare example says only what it has, rather than an empty header.
        assert!(
            run.contains(r#"{Code: "ex run"}"#),
            "an example with no header emits no header: {run}"
        );
    }

    /// `about_long` alone leaves the short page's About unset, because usage-lib
    /// prints nothing there: the long text belongs to the long page.
    #[test]
    fn a_long_about_alone_does_not_become_the_short_one() {
        let out = go(r#"
name "ex"
bin "ex"
about_long "Long only."
"#);
        let meta = out
            .lines()
            .find(|l| l.contains("var HelpMeta"))
            .expect("a root header is emitted");
        assert!(
            !meta.contains(", About: ") && meta.contains(r#"LongAbout: "Long only.""#),
            "only the long one is set: {meta}"
        );
    }

    #[test]
    fn a_default_subcommand_ignores_a_deeper_command_of_the_same_name() {
        let out = go(r#"
name "ex"
bin "ex"
default_subcommand "run"
cmd "oci" {
    cmd "run" {}
}
cmd "run" {
    arg "[args]..." var=#true
}
"#);
        assert!(
            out.contains("DefaultSubcommand: cmdRun,"),
            "should point at the root's own `run`, got:\n{out}"
        );
    }

    #[test]
    fn command_builtin_controls_reach_generated_go_tables() {
        let out = go(r#"
name "ex"
bin "ex"
disable_help_flag #true
disable_help_subcommand #true
disable_version_flag #true
"#);
        let root = out
            .split("var Root = &argv.Command{")
            .nth(1)
            .expect("the root command should be emitted")
            .split("}\n")
            .next()
            .unwrap();
        assert!(root.contains("DisableHelpFlag:"), "{root}");
        assert!(root.contains("DisableHelpSubcommand:"), "{root}");
        assert!(root.contains("DisableVersionFlag:"), "{root}");
    }

    /// A command's own name outranks another command's alias, so which command the
    /// emitted `DefaultSubcommand` points at does not depend on declaration order.
    #[test]
    fn a_default_subcommand_prefers_a_name_to_another_commands_alias() {
        let ordered = |first: &str, second: &str| {
            go(&format!(
                r#"
name "ex"
bin "ex"
default_subcommand "run"
{first}
{second}
"#
            ))
        };
        let alpha = "cmd \"alpha\" {\n    alias \"run\"\n}";
        let run = "cmd \"run\" {\n    arg \"[args]...\" var=#true\n}";
        for out in [ordered(alpha, run), ordered(run, alpha)] {
            assert!(
                out.contains("DefaultSubcommand: cmdRun,"),
                "should point at the command named `run`, got:\n{out}"
            );
        }
    }

    #[test]
    fn an_external_subcommand_is_emitted_on_the_command_that_declares_it() {
        let out = go(r#"
name "ex"
bin "ex"
external_subcommand #true
cmd "install"
cmd "exec" external_subcommand=#true
"#);
        let block = |var: &str| {
            let start = out
                .find(&format!("var {var} ="))
                .unwrap_or_else(|| panic!("{var} should be emitted, got:\n{out}"));
            let rest = &out[start..];
            let end = rest[1..]
                .find("\nvar ")
                .map(|i| i + 1)
                .unwrap_or(rest.len());
            &rest[..end]
        };
        assert!(
            block("Root").contains("ExternalSubcommand: true"),
            "the root should forward unmatched words:\n{}",
            block("Root")
        );
        assert!(
            block("cmdExec").contains("ExternalSubcommand: true"),
            "a nested command can forward too:\n{}",
            block("cmdExec")
        );
        assert!(
            !block("cmdInstall").contains("ExternalSubcommand"),
            "a command that does not declare it should not carry it:\n{}",
            block("cmdInstall")
        );
    }

    #[test]
    fn arg_required_else_help_reaches_the_table_and_typed_front_door() {
        let out = go(r#"
name "ex"
bin "ex"
cmd "run" arg_required_else_help=#true {
    flag "--all"
}
"#);
        assert!(
            out.contains("ArgRequiredElseHelp: true"),
            "the command table should carry the policy:\n{out}"
        );
        assert!(
            out.contains("p.Command().ArgRequiredElseHelp && p.CommandStart() == len(args)"),
            "the typed parser should enforce it before fallbacks:\n{out}"
        );
    }

    #[test]
    fn subcommand_negates_requirements_reaches_generated_go() {
        let out = go(
            "name \"ex\"\nbin \"ex\"\nsubcommand_negates_reqs #true\nflag \"--config\" required=#true\ncmd \"run\"\n",
        );
        assert!(out.contains("SubcommandNegatesReqs: true"), "{out}");
        assert!(
            out.contains("checkRequirements := i == len(chain)-1 || !cmd.SubcommandNegatesReqs"),
            "{out}"
        );
        assert!(
            out.contains("CheckRelationshipsWithValuesAndRequirements"),
            "{out}"
        );
    }

    #[test]
    fn argument_subcommand_conflicts_reach_generated_go() {
        let out = go(
            "name \"ex\"\nbin \"ex\"\nargs_conflicts_with_subcommands #true\nflag \"--verbose\"\ncmd \"run\"\n",
        );
        assert!(out.contains("ArgsConflictWithSubcommands: true"), "{out}");
    }

    #[test]
    fn allow_missing_positional_reaches_generated_go() {
        let out = go(
            "name \"ex\"\nbin \"ex\"\nallow_missing_positional #true\narg \"[optional]\"\narg \"<required>\"\n",
        );
        assert!(out.contains("AllowMissingPositional: true"), "{out}");
        assert!(out.contains("Name: \"optional\""), "{out}");
        assert!(out.contains("Name: \"required\", Required: true"), "{out}");
    }

    #[test]
    fn optional_flag_values_reach_generated_go() {
        let out = go("name \"ex\"\nbin \"ex\"\nflag \"--color [WHEN]\" value_optional=#true\n");
        assert!(
            out.contains("TakesValue: true, ValueOptional: true"),
            "{out}"
        );
    }

    #[test]
    fn explicit_boolean_values_reach_generated_go() {
        let out = go(
            "name \"ex\"\nbin \"ex\"\nflag \"--color\" negate=\"--no-color\" bool_value=#true\n",
        );
        assert!(out.contains("BoolValue: true"), "{out}");
        assert!(out.contains("if ev.Flag.BoolValue"), "{out}");
        assert!(
            out.contains("given[ev.Flag.Key] = []string{ev.Value}"),
            "{out}"
        );
        assert!(
            out.contains("(ev.Value == \"true\") != ev.Negated"),
            "{out}"
        );
    }

    #[test]
    fn flag_actions_reach_generated_go() {
        let out = go(
            "name \"ex\"\nbin \"ex\"\nflag \"--help-all\" action=\"help_all\"\nflag \"--version\" action=\"version\"\n",
        );
        assert!(out.contains("Action: argv.ActionHelpAll"), "{out}");
        assert!(out.contains("Action: argv.ActionVersion"), "{out}");
    }

    #[test]
    fn granular_help_hides_reach_generated_go() {
        let out = go(
            "name \"ex\"\nbin \"ex\"\nflag \"--mode <mode>\" hide_default_value=#true hide_env=#true hide_env_values=#true hide_possible_values=#true hide_short_help=#true hide_long_help=#true\n",
        );
        for field in [
            "HideDefaultValue: true",
            "HideEnv: true",
            "HideEnvValues: true",
            "HidePossibleValues: true",
            "HideShortHelp: true",
            "HideLongHelp: true",
        ] {
            assert!(out.contains(field), "missing {field}:\n{out}");
        }
    }

    #[test]
    fn strict_duplicate_policy_reaches_metadata() {
        let permissive = go("name \"ex\"\nbin \"ex\"\nflag \"--jobs <n>\"\n");
        assert!(!permissive.contains("RejectDuplicate"), "{permissive}");

        let strict =
            go("name \"ex\"\nbin \"ex\"\nargs_override_self #false\nflag \"--jobs <n>\"\n");
        assert!(strict.contains("RejectDuplicate: true"), "{strict}");
    }

    #[test]
    fn strict_negated_flags_track_each_spelling_separately() {
        let out = go(
            "name \"ex\"\nbin \"ex\"\nargs_override_self #false\nflag \"--color\" negate=\"--no-color\"\n",
        );
        assert!(out.contains("polaritySeen := map[uint64]uint8{}"), "{out}");
        assert!(
            out.contains("polaritySeen[ev.Flag.Key]&polarity != 0"),
            "{out}"
        );
        assert!(out.contains("if duplicateSeen[key]"), "{out}");
    }

    #[test]
    fn strict_global_duplicate_tracking_resets_at_subcommands() {
        let out = go(
            "name \"ex\"\nbin \"ex\"\nargs_override_self #false\nflag \"--jobs <n>\" global=#true\ncmd \"run\" {\n  args_override_self #false\n}\n",
        );
        assert!(
            out.contains("levelSeen = map[uint64]int{}"),
            "a subcommand should start a new duplicate scope:\n{out}"
        );
        assert!(out.contains("strictSeen[ev.Flag.Key] = true"), "{out}");
        assert!(out.contains("if strictSeen[key]"), "{out}");
    }

    /// A subcommand actually named `root` wants the constant the root has.
    #[test]
    fn a_subcommand_named_root_does_not_collide_with_the_root() {
        let out = go(r#"
name "ex"
bin "ex"
cmd "root" {
    flag "--wat"
}
"#);
        // By first token, because the const block is column-aligned: matching
        // "CmdRoot uint64" would find nothing and pass for the wrong reason.
        let declared = |name: &str| {
            out.lines()
                .filter(|l| l.split_whitespace().next() == Some(name))
                .count()
        };
        assert_eq!(declared("CmdRoot"), 1, "CmdRoot declared twice:\n{out}");
        assert_eq!(declared("CmdRoot2"), 1, "no distinct key for it:\n{out}");
        assert_declares_each_constant_once(&out);
    }

    /// The two `var_max` are different questions, and the corpus pins them apart:
    /// on a flag's *argument* it bounds one occurrence's values and belongs in the
    /// binding table, while on the flag it counts occurrences and is checked after
    /// the parse.
    #[test]
    fn only_the_per_occurrence_bound_reaches_the_table() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--include <pattern>..." {
    arg "<pattern>..." var=#true var_min=2 var_max=2
}
flag "--tag <t>" var=#true var_max=1
"#);
        assert!(
            out.contains("Name: \"include\", Longs: []string{\"include\"}, TakesValue: true, Variadic: true, VarMax: 2"),
            "{out}"
        );
        assert!(
            out.contains("Name: \"include\", Flag: true") && out.contains("VarMin: 2"),
            "the nested value minimum must reach post-binding metadata:\n{out}"
        );
        let tag = out.lines().find(|l| l.contains("\"tag\"")).unwrap();
        assert!(!tag.contains("VarMax"), "occurrence bound leaked: {tag}");
    }

    #[test]
    fn exact_arity_with_one_label_reaches_go_help() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--pair <ITEM>..." {
    arg "<ITEM>..." var=#true var_min=2 var_max=2 {
        value_names "ITEM"
    }
}
arg "<ITEM>..." var=#true var_min=2 var_max=2 {
    value_names "ITEM"
}
"#);
        assert_eq!(out.matches("ValueArity: 2").count(), 2, "{out}");
        assert_eq!(
            out.matches("ValueNames: []string{\"ITEM\"}").count(),
            2,
            "{out}"
        );
    }

    #[test]
    fn allow_hyphen_values_reaches_the_table() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--args <ARGS>" allow_hyphen_values=#true
"#);
        assert!(
            out.contains("Name: \"args\", Longs: []string{\"args\"}, TakesValue: true, AllowHyphenValues: true"),
            "{out}"
        );
    }

    #[test]
    fn require_equals_reaches_the_table() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--inspect <PORT>" require_equals=#true
"#);
        assert!(
            out.contains("Name: \"inspect\", Longs: []string{\"inspect\"}, TakesValue: true, RequireEquals: true"),
            "{out}"
        );
    }

    #[test]
    fn default_missing_reaches_the_table() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--color <WHEN>" default_missing="always"
"#);
        assert!(
            out.contains(
                "Name: \"color\", Longs: []string{\"color\"}, TakesValue: true, DefaultMissing: \"always\""
            ),
            "{out}"
        );
    }

    /// A relationship names a flag by any spelling that reaches it, and from
    /// anywhere the flag is in scope.
    ///
    /// Both halves were silently resolving to nothing, which is worse than an
    /// error: the rule simply never fired, while usage-lib enforced it.
    #[test]
    fn a_relationship_resolves_through_scope_and_negation() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--quiet" global=#true
flag "--color" negate="--no-color"
flag "--plain" conflicts="--no-color"
cmd "run" {
    flag "--loud" conflicts="--quiet"
    flag "--solo" conflicts="--plain"
}
"#);
        // A negation names the flag it belongs to.
        assert!(out.contains("Conflicts: []uint64{FlagColor}"), "{out}");
        // An inherited global is in scope from below.
        assert!(out.contains("Conflicts: []uint64{FlagQuiet}"), "{out}");
        // `--plain` is not global, so from a subcommand it names nothing — the
        // other half, and the one a looser search would get wrong.
        assert!(
            !entry_of(&out, "solo").contains("Conflicts"),
            "a non-global should not resolve from below:\n{out}"
        );
    }

    #[test]
    fn positional_conflicts_reach_go_metadata_in_both_directions() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--from-file <file>" conflicts="value"
arg "[value]" conflicts="--from-file"
"#);

        assert!(
            entry_of(&out, "from-file").contains("Conflicts: []uint64{ArgValue}"),
            "{out}"
        );
        assert!(
            out.lines().any(|line| {
                line.contains("{Key: ArgValue, Name: \"value\"")
                    && line.contains("Conflicts: []uint64{FlagFromFile}")
            }),
            "{out}"
        );
    }

    #[test]
    fn a_value_conditional_requirement_reaches_go_metadata() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--format <format>" {
    requires_if "json" "--schema"
}
flag "--schema <file>"
"#);
        assert!(
            entry_of(&out, "format").contains(
                "RequiresIf: []argv.ValueRequirement{{Value: \"json\", Key: FlagSchema}}"
            ),
            "{out}"
        );
        assert!(
            out.contains("argv.CheckRelationshipsWithValues"),
            "the emitted parser must enforce the metadata:\n{out}"
        );
    }

    #[test]
    fn required_if_eq_makes_generated_go_supply_values() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--token <token>" {
    required_if_eq "--mode" "remote"
}
flag "--mode <mode>"
"#);
        assert!(
            entry_of(&out, "token").contains(
                "RequiredIfEq: []argv.ValueCondition{{Key: FlagMode, Value: \"remote\"}}"
            ),
            "{out}"
        );
        assert!(out.contains("resolved := map[uint64][]string{}"), "{out}");
        assert!(out.contains("argv.CheckRelationshipsWithValues"), "{out}");
    }

    #[test]
    fn boolean_sources_are_normalized_for_value_relationships() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--token <token>" {
    required_if_eq "--mode" "true"
}
flag "--mode" negate="--no-mode" bool_value=#true
"#);
        assert!(
            entry_of(&out, "mode").contains("RequiresIfBoolean: true"),
            "{out}"
        );
    }

    #[test]
    fn a_conditional_default_reaches_go_metadata() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--bin-names" {
    default_if "--json" "true"
    default_if "--output" "json" "pretty"
}
flag "--json"
flag "--output <fmt>"
"#);
        assert!(
            entry_of(&out, "bin-names")
                .contains("DefaultIf: []argv.DefaultIf{{Key: FlagJson, Value: \"true\"}"),
            "{out}"
        );
        assert!(
            entry_of(&out, "bin-names").contains("When: \"json\""),
            "{out}"
        );
        assert!(
            out.contains("argv.ApplyDefaultIf"),
            "the emitted parser must apply the metadata:\n{out}"
        );
        assert!(
            out.contains("negated[ev.Flag.Key] = ev.Negated"),
            "Equals default_if needs the negate form:\n{out}"
        );
    }

    /// The form is part of the name, and usage-lib resolves neither of the
    /// mismatched ones — so resolving them would have a generated CLI enforcing a
    /// rule the reference does not.
    #[test]
    fn a_relationship_needs_the_right_form() {
        let out = go(r#"
name "ex"
bin "ex"
flag "-q --quiet"
flag "--color"
flag "--a" conflicts="--q"
flag "--b" conflicts="-color"
flag "--c" conflicts="-q"
flag "--d" conflicts="--color"
"#);
        // `--q` is not a long form of anything, and `-color` is not a short.
        assert!(!entry_of(&out, "a").contains("Conflicts"), "{out}");
        assert!(!entry_of(&out, "b").contains("Conflicts"), "{out}");
        // The forms the flags actually have.
        assert!(
            entry_of(&out, "c").contains("Conflicts: []uint64{FlagQuiet}"),
            "{out}"
        );
        assert!(
            entry_of(&out, "d").contains("Conflicts: []uint64{FlagColor}"),
            "{out}"
        );
    }

    /// The table has to agree with the binder it feeds.
    ///
    /// The parser tries every long form before any negation, so with `--a`
    /// declaring `negate="--zap"` and a separate `--zap`, typing `--zap` binds
    /// *zap*. A per-candidate search handed the relationship to `a`, which would
    /// have enforced the rule against a flag the command line never binds.
    #[test]
    fn an_ordinary_form_beats_another_flags_negation() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--a" negate="--zap"
flag "--zap"
flag "--p" conflicts="--zap"
"#);
        assert!(
            entry_of(&out, "p").contains("Conflicts: []uint64{FlagZap}"),
            "should name the flag `--zap` binds, not the one negating to it:\n{out}"
        );
    }

    /// A negation is named by the form it was written as, whatever the dashes.
    #[test]
    fn a_single_dash_negation_is_named_by_its_own_form() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--tint" negate="-no-tint"
flag "--plain" conflicts="-no-tint"
flag "--other" conflicts="--no-tint"
"#);
        assert!(
            entry_of(&out, "plain").contains("Conflicts: []uint64{FlagTint}"),
            "the exact form should resolve:\n{out}"
        );
        // And the form it was not written as does not.
        assert!(
            !entry_of(&out, "other").contains("Conflicts"),
            "`--no-tint` is not how it was declared:\n{out}"
        );
    }

    /// A negation is matched as the spec wrote it, dashes and all.
    #[test]
    fn a_negation_is_matched_as_written() {
        let out = go(r#"
name "ex"
bin "ex"
flag "--color" negate="--no-color"
flag "--tint" negate="-no-tint"
flag "--a" conflicts="--no-color"
flag "--b" conflicts="--no-tint"
"#);
        assert!(
            entry_of(&out, "a").contains("Conflicts: []uint64{FlagColor}"),
            "{out}"
        );
        // `--no-tint` is not the form `-no-tint`, so it names nothing — as in
        // usage-lib, which does not resolve it either.
        assert!(!entry_of(&out, "b").contains("Conflicts"), "{out}");
    }

    #[test]
    fn strings_are_escaped_to_go_rules() {
        assert_eq!(go_string(r#"a"b\c"#), r#""a\"b\\c""#);
        assert_eq!(go_string("tab\there"), r#""tab\there""#);
        // Rust would spell this `\u{7f}`, which Go rejects.
        assert_eq!(go_string("\u{7f}"), r#""\x7f""#);
    }
}