manby 0.2.6

Builds vector tilesets from geographic features, like tippecanoe, several times faster
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
//! Everything a run needs to know, parsed from tippecanoe's command line options
//! or filled in by a library user. Replaces tippecanoe's global variables.
//!
//! [`parse_args`] follows the option loop of tippecanoe's main.cpp, and
//! [`Options::resolve`] the checks and adjustments main.cpp makes after it.

use std::collections::{BTreeMap, BTreeSet, HashMap};

use tippecanoe::attribute::AttributeOp;
use tippecanoe::errors::*;
use tippecanoe::geometry::ClipBbox;
use tippecanoe::getopt::{Getopt, LongOption, NO_ARGUMENT, REQUIRED_ARGUMENT};
use tippecanoe::globals::{MAX_ZOOM, ORDER_BY_SIZE, OrderField};
use tippecanoe::jsonpull::{JsonPull, JsonRef, JsonTree, JsonType};
use tippecanoe::mvt::{MVT_BOOL, MVT_DOUBLE, MVT_FLOAT, MVT_INT, MVT_NULL, MVT_STRING};
use tippecanoe::options::*;
use tippecanoe::projection::PROJECTIONS;
use tippecanoe::serial::SerialVal;
use tippecanoe::usage::{UsageRequiredOption, print_usage, strip_usage_headings};

use crate::Error;

/// Where the tiles go.
#[derive(Clone, Debug, Default)]
pub enum Output {
    #[default]
    None,
    Pmtiles(String),
    Mbtiles(String),
    Directory(String),
}

impl Output {
    /// The path given with `-o` or `-e`.
    pub fn path(&self) -> Option<&str> {
        match self {
            Output::None => None,
            Output::Pmtiles(p) | Output::Mbtiles(p) | Output::Directory(p) => Some(p),
        }
    }
}

/// tippecanoe's `prevent[]` or `additional[]` flags, indexed by the option
/// letters in `tippecanoe::options` (`P_*` and `A_*`).
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Flags(pub [bool; 256]);

impl Default for Flags {
    fn default() -> Self {
        Flags([false; 256])
    }
}

impl std::fmt::Debug for Flags {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let set: String = (0..256u32)
            .filter(|&c| self.0[c as usize])
            .filter_map(char::from_u32)
            .collect();
        write!(f, "Flags({:?})", set)
    }
}

impl Flags {
    pub fn get(&self, c: usize) -> bool {
        self.0[c]
    }

    pub fn set(&mut self, c: usize, v: bool) {
        self.0[c] = v;
    }
}

/// An input: tippecanoe's `struct source`. An empty `file` is standard input.
#[derive(Clone, Debug, Default)]
pub struct Source {
    pub layer: String,
    pub file: String,
    pub description: String,
    pub format: String,
}

/// tippecanoe's options, and manby's own. [`Options::default`] has
/// tippecanoe's defaults, and [`parse_args`] reads them from a command line.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Options {
    /// Input files named on the command line, after the `-L` sources.
    pub inputs: Vec<String>,
    /// `-o`, `--output` or `-e`, `--output-to-directory`
    pub output: Output,
    /// `-f`, `--force`: replaces an existing tileset
    pub force: bool,
    /// `-q`, `--quiet`
    pub quiet: bool,
    /// Worker threads; 0 means one per available CPU, as tippecanoe chooses.
    pub threads: usize,
    /// `--memory-budget`: the bytes of memory to stay near; what does not
    /// fit goes to temporary files. None means half the physical memory, as
    /// tippecanoe sizes its sorting.
    pub memory_budget: Option<u64>,
    /// Make different tiles from tippecanoe's where that is faster. See DESIGN.md.
    pub fast: bool,
    /// `--as-needed-per-tile`: a tile too big keeps the thresholds it raised
    /// for itself, rather than its zoom level being made again with the
    /// highest. Different tiles from tippecanoe's.
    pub as_needed_per_tile: bool,
    /// `--shard I/N`: make part `I` of `N` of the tileset, for `manby merge`.
    pub shard: Option<crate::shard::Shard>,
    /// `--shard-zoom`: the zoom at which the shards split the tiles.
    pub shard_zoom: Option<i32>,
    /// `--compression=zlib`: store every tile as zlib compresses it, so the
    /// archive is byte for byte tippecanoe's. Otherwise tiles that fit
    /// whatever zlib does are stored compressed with libdeflate.
    pub zlib_tiles: bool,
    /// `--tile-format`: MVT, as tippecanoe writes, or MapLibre Tiles.
    pub tile_format: crate::mlt::TileFormat,
    /// `--mlt-profile`: which encodings MLT tiles may use.
    pub mlt_profile: crate::mlt::MltProfile,
    /// `--bbox`: only the rows of GeoParquet and Arrow inputs whose
    /// geometries' bounding boxes overlap this longitude and latitude box,
    /// as minimum longitude, minimum latitude, maximum longitude, maximum
    /// latitude.
    pub bbox: Option<[f64; 4]>,

    /// `argv[0]`, for messages.
    pub program: String,
    /// The command line, for the metadata's `generator_options`.
    pub commandline: String,

    /// `-L` sources, which come before `inputs`.
    pub sources: Vec<Source>,
    /// `-l`: the layer name for every input.
    pub layer_name: Option<String>,
    /// `-n`
    pub name: Option<String>,
    /// `-N`
    pub description: Option<String>,
    /// `-A`
    pub attribution: Option<String>,
    /// `-P`
    pub read_parallel: bool,
    /// Index into `tippecanoe::projection::PROJECTIONS` (`-s`).
    pub projection: usize,
    /// `-s` with a CRS that tippecanoe doesn't have, such as `EPSG:25832`,
    /// which crs-defs defines: the input's coordinates are made longitude
    /// and latitude first.
    pub source_crs: Option<String>,
    /// `--h3`: below `--h3-base-zoom`, each layer's points binned into H3
    /// hexagons in a layer of their own; `--h3-categories` counts the
    /// values of an attribute in each.
    pub h3: bool,
    /// `--h3-base-zoom`; None is two below the maximum zoom
    pub h3_base_zoom: Option<i32>,
    /// `--h3-categories`
    pub h3_categories: Option<String>,
    /// `--supercluster`: each layer's points clustered as MapLibre clusters
    /// a GeoJSON source, with Supercluster's properties.
    pub supercluster: bool,
    /// `--supercluster-radius`: in pixels of a 512-pixel tile, 50 unless given.
    pub supercluster_radius: f64,
    /// `--supercluster-min-points`: 2 unless given.
    pub supercluster_min_points: u64,
    /// `--cluster-categories=kind`, or `kind:park,museum`: each feature
    /// gets `kind:<value>` of 1, which clustering and coalescing add up, so
    /// a cluster counts the values of its points; values not listed count
    /// as `kind:_other`.
    pub cluster_categories: Option<(String, Option<Vec<String>>)>,

    /// `-z`, `--maximum-zoom`
    pub maxzoom: i32,
    /// `-Z`, `--minimum-zoom`
    pub minzoom: i32,
    /// `-zg`, `--smallest-maximum-zoom-guess` or `--largest-maximum-zoom-guess`
    pub guess_maxzoom: bool,
    /// `--smallest-maximum-zoom-guess`: the least `-zg` may choose
    pub minimum_maxzoom: i32,
    /// `--largest-maximum-zoom-guess`: the most `-zg` may choose
    pub maximum_maxzoom: i32,
    /// `-B`, `--base-zoom`. -1 is the maximum zoom, and -2 is a guess (`-Bg`).
    pub basezoom: i32,
    /// The marker width of `-Bg<width>` and `-rg<width>`
    pub basezoom_marker_width: f64,
    /// `-r`, `--drop-rate`. -2 is a guess (`-rg`), and -3 is `-rp`.
    pub droprate: f64,
    /// `-g`, `--gamma`
    pub gamma: f64,
    /// `-b`, `--buffer`
    pub buffer: i32,
    /// `-R z/x/y`: the x and y of the one tile.
    pub one_tile: Option<(u32, u32)>,

    /// `-d`, `--full-detail`; -1 is 12, tippecanoe's default
    pub full_detail: i32,
    /// `-D`, `--low-detail`
    pub low_detail: i32,
    /// `-m`, `--minimum-detail`
    pub min_detail: i32,
    /// `--extra-detail`; -1 is none
    pub extra_detail: i32,

    /// `-x`, `--exclude`
    pub exclude: BTreeSet<String>,
    /// `-y`, `--include`
    pub include: BTreeSet<String>,
    /// `-X`, `--exclude-all`, which `-y` sets too
    pub exclude_all: bool,
    /// `-T`, `--attribute-type`: an MVT type (`tippecanoe::mvt::MVT_*`) for an attribute
    pub attribute_types: HashMap<String, i32>,
    /// `-E`, `--accumulate-attribute`
    pub attribute_accum: HashMap<String, AttributeOp>,
    /// `-Y`, `--attribute-description`
    pub attribute_descriptions: BTreeMap<String, String>,
    /// `--set-attribute`
    pub set_attributes: BTreeMap<String, SerialVal>,
    /// `--use-attribute-for-id`; empty is none
    pub attribute_for_id: String,
    /// `--use-attribute-for-minzoom`: the attribute that holds the lowest
    /// zoom each feature shows at, as `tippecanoe.minzoom` does in GeoJSON.
    /// A feature without a value shows from the minimum zoom.
    pub attribute_for_minzoom: Option<String>,
    /// `--use-attribute-for-maxzoom`: the same for the highest zoom, as
    /// `tippecanoe.maxzoom` does
    pub attribute_for_maxzoom: Option<String>,
    /// `--maximum-string-attribute-length`; 0 is no limit
    pub maximum_string_attribute_length: usize,

    /// `-J` or `-j`
    pub filter: Option<crate::filter::Filter>,
    /// `-C`, `--prefilter`
    pub prefilter: Option<String>,
    /// `-c`, `--postfilter`
    pub postfilter: Option<String>,
    /// `-t`, `--temporary-directory`
    pub tmpdir: String,

    /// `-p`, `--prevent`, and the options named after its letters, such as
    /// `--no-tile-size-limit`
    pub prevent: Flags,
    /// `-a`, `--additional`, and the options named after its letters, such as
    /// `--drop-densest-as-needed`
    pub additional: Flags,

    /// `-S`, `--simplification`
    pub simplification: f64,
    /// `--simplification-at-maximum-zoom`; -1 is `simplification`
    pub maxzoom_simplification: f64,
    /// `--tiny-polygon-size`
    pub tiny_polygon_size: i32,

    /// `-M`, `--maximum-tile-bytes`
    pub max_tile_size: usize,
    /// `-O`, `--maximum-tile-features`
    pub max_tile_features: usize,
    /// `--limit-tile-feature-count`; 0 is no limit
    pub limit_tile_feature_count: usize,
    /// `--limit-tile-feature-count-at-maximum-zoom`; 0 is no limit
    pub limit_tile_feature_count_at_maxzoom: usize,

    /// `-K`, `--cluster-distance`
    pub cluster_distance: i32,
    /// `-k`, `--cluster-maxzoom`
    pub cluster_maxzoom: i32,
    /// `-kg`
    pub guess_cluster_maxzoom: bool,
    /// `--retain-points-multiplier`
    pub retain_points_multiplier: i32,
    /// `--drop-denser`
    pub drop_denser: u32,
    /// `--preserve-point-density-threshold`
    pub preserve_point_density_threshold: u64,
    /// `--preserve-multiplier-density-threshold`
    pub preserve_multiplier_density_threshold: u64,
    /// `--extend-zooms-if-still-dropping-maximum`
    pub extend_zooms_max: i64,

    /// `--order-by` and `--order-descending-by`
    pub order_by: Vec<OrderField>,
    /// `--order-smallest-first` or `--order-largest-first`
    pub order_by_size: bool,
    /// `--clip-bounding-box`
    pub clip_bboxes: Vec<ClipBbox>,
    /// `--unidecode-data`
    pub unidecode_data: Vec<String>,
    /// `--drop-by-attribute-as-needed`
    pub drop_by_attribute_as_needed_attribute: String,
    /// `--drop-by-attribute-order=desc`
    pub drop_by_attribute_descending: bool,

    /// `--tile-stats-attributes-limit`
    pub max_tilestats_attributes: usize,
    /// `--tile-stats-sample-values-limit`
    pub max_tilestats_sample_values: usize,
    /// `--tile-stats-values-limit`
    pub max_tilestats_values: usize,

    /// `-Q`, `--no-progress-indicator`
    pub quiet_progress: bool,
    /// `-U`, `--progress-interval`
    pub progress_interval: f64,
    /// `-u`, `--json-progress`
    pub json_progress: bool,
    /// `-F`
    pub forcetable: bool,

    /// How many segments tippecanoe splits parallel input into, its `CPUS`.
    /// It decides tippecanoe's sequence numbers, which `--generate-ids`
    /// turns into feature ids. 0 means as tippecanoe would choose it.
    pub tippecanoe_cpus: usize,

    /// Set by [`Options::resolve`]: tippecanoe's `geometry_scale`.
    pub geometry_scale: i32,

    /// The size of the ranges the input is split into for parallel reading;
    /// 0 chooses it from the input size and the number of threads.
    pub chunk_bytes: usize,
}

impl Default for Options {
    fn default() -> Self {
        Options {
            inputs: Vec::new(),
            output: Output::None,
            force: false,
            quiet: false,
            threads: 0,
            memory_budget: None,
            fast: false,
            as_needed_per_tile: false,
            shard: None,
            shard_zoom: None,
            zlib_tiles: false,
            tile_format: crate::mlt::TileFormat::Mvt,
            mlt_profile: crate::mlt::MltProfile::Web,
            bbox: None,
            program: "manby".to_string(),
            commandline: String::new(),
            sources: Vec::new(),
            layer_name: None,
            name: None,
            description: None,
            attribution: None,
            read_parallel: false,
            projection: 0,
            source_crs: None,
            h3: false,
            supercluster: false,
            supercluster_radius: 50.0,
            supercluster_min_points: 2,
            h3_base_zoom: None,
            h3_categories: None,
            cluster_categories: None,
            maxzoom: 14,
            minzoom: 0,
            guess_maxzoom: false,
            minimum_maxzoom: 0,
            maximum_maxzoom: MAX_ZOOM,
            basezoom: -1,
            basezoom_marker_width: 1.0,
            droprate: 2.5,
            gamma: 0.0,
            buffer: 5,
            one_tile: None,
            full_detail: -1,
            low_detail: 12,
            min_detail: 7,
            extra_detail: -1,
            exclude: BTreeSet::new(),
            include: BTreeSet::new(),
            exclude_all: false,
            attribute_types: HashMap::new(),
            attribute_accum: HashMap::new(),
            attribute_descriptions: BTreeMap::new(),
            set_attributes: BTreeMap::new(),
            attribute_for_id: String::new(),
            attribute_for_minzoom: None,
            attribute_for_maxzoom: None,
            maximum_string_attribute_length: 0,
            filter: None,
            prefilter: None,
            postfilter: None,
            tmpdir: tippecanoe::platform::default_tmpdir(),
            prevent: Flags::default(),
            additional: Flags::default(),
            simplification: 1.0,
            maxzoom_simplification: -1.0,
            tiny_polygon_size: 2,
            max_tile_size: 500000,
            max_tile_features: 200000,
            limit_tile_feature_count: 0,
            limit_tile_feature_count_at_maxzoom: 0,
            cluster_distance: 0,
            cluster_maxzoom: MAX_ZOOM,
            guess_cluster_maxzoom: false,
            retain_points_multiplier: 1,
            drop_denser: 0,
            preserve_point_density_threshold: 0,
            preserve_multiplier_density_threshold: 0,
            extend_zooms_max: 0,
            order_by: Vec::new(),
            order_by_size: false,
            clip_bboxes: Vec::new(),
            unidecode_data: Vec::new(),
            drop_by_attribute_as_needed_attribute: String::new(),
            drop_by_attribute_descending: false,
            max_tilestats_attributes: 1000,
            max_tilestats_sample_values: 1000,
            max_tilestats_values: 100,
            quiet_progress: false,
            progress_interval: 0.0,
            json_progress: false,
            forcetable: false,
            tippecanoe_cpus: 0,
            geometry_scale: 0,
            chunk_bytes: 0,
        }
    }
}

/// A size in bytes, with an optional K, M, G or T for powers of 1024.
fn parse_size(s: &str) -> Option<u64> {
    let s = s.trim();
    let (num, shift) = match s.char_indices().last()? {
        (i, 'k' | 'K') => (&s[..i], 10),
        (i, 'm' | 'M') => (&s[..i], 20),
        (i, 'g' | 'G') => (&s[..i], 30),
        (i, 't' | 'T') => (&s[..i], 40),
        _ => (s, 0),
    };
    let n: f64 = num.parse().ok()?;
    (n.is_finite() && n >= 0.0).then(|| (n * (1u64 << shift) as f64) as u64)
}

impl Options {
    /// How the input's coordinates become world coordinates (`-s`).
    #[doc(hidden)]
    pub fn project(&self) -> crate::crs::Project {
        match &self.source_crs {
            Some(code) => crate::crs::Project::Crs(std::sync::Arc::new(
                crate::crs::Reprojection::new(code).expect("a CRS checked when parsed"),
            )),
            None => crate::crs::Project::Tippecanoe(PROJECTIONS[self.projection].project),
        }
    }

    /// The name of the input's CRS that a GeoJSON file's `crs` is expected
    /// to give.
    pub fn projection_alias(&self) -> String {
        match &self.source_crs {
            Some(code) => format!("urn:ogc:def:crs:{}", code.replacen(':', "::", 1)),
            None => PROJECTIONS[self.projection].alias.to_string(),
        }
    }

    /// `--h3`'s base zoom: given, or two zooms below the maximum.
    /// The last zoom `--supercluster` clusters at: `-k`, or one below the
    /// maximum zoom, as MapLibre's `clusterMaxZoom` is.
    pub fn supercluster_maxzoom(&self) -> i32 {
        if self.cluster_maxzoom < MAX_ZOOM && !self.guess_cluster_maxzoom {
            self.cluster_maxzoom
        } else {
            (self.maxzoom - 1).max(0)
        }
    }

    /// What `--supercluster` needs of the other options. Every point and
    /// cluster shows at the zooms Supercluster puts it at, so nothing is
    /// dropped by rate (`-r1`), and tippecanoe's clustering is off.
    fn check_supercluster(&mut self) -> crate::Result<()> {
        let argv0 = &self.program;
        let refuse = |what: &str| {
            Err(Error::new(
                EXIT_ARGS,
                format!("{}: --supercluster does not work with {}", argv0, what),
            ))
        };
        if self.cluster_distance > 0 {
            return refuse("-K (--cluster-distance), tippecanoe's clustering");
        }
        if self.additional.get(A_CLUSTER_DENSEST_AS_NEEDED) {
            return refuse("--cluster-densest-as-needed");
        }
        if self.h3 {
            return refuse("--h3");
        }
        if self.cluster_categories.is_some() {
            return refuse("--cluster-categories");
        }
        if self.guess_maxzoom && (self.cluster_maxzoom >= MAX_ZOOM || self.guess_cluster_maxzoom) {
            return Err(Error::new(
                EXIT_ARGS,
                format!(
                    "{}: --supercluster with -zg needs -k (--cluster-maxzoom), the last zoom with clusters",
                    argv0
                ),
            ));
        }
        if self.droprate == 2.5 {
            self.droprate = 1.0;
        } else if self.droprate != 1.0 {
            return refuse("-r other than -r1, as it places every point and cluster itself");
        }
        Ok(())
    }

    pub fn h3_base(&self) -> i32 {
        self.h3_base_zoom
            .unwrap_or(self.maxzoom - 2)
            .clamp(self.minzoom, self.maxzoom.max(self.minzoom))
    }

    pub fn memory_budget(&self) -> u64 {
        self.memory_budget
            .unwrap_or_else(|| tippecanoe::platform::calc_memsize() as u64 / 2)
    }

    /// The sources in tippecanoe's order: the `-L` sources, then the input
    /// files, or standard input if there are neither, with `-l` applied.
    pub fn all_sources(&self) -> Vec<Source> {
        let mut sources = self.sources.clone();
        for f in &self.inputs {
            sources.push(Source {
                file: f.clone(),
                ..Default::default()
            });
        }
        if sources.is_empty() {
            sources.push(Source::default());
        }
        if let Some(l) = &self.layer_name {
            for s in sources.iter_mut() {
                s.layer = l.clone();
            }
        }
        sources
    }

    /// The name tippecanoe gives the tileset: `-n`, or else the output path.
    pub fn tileset_name(&self) -> String {
        match (&self.name, self.output.path()) {
            (Some(n), _) => n.clone(),
            (None, Some(p)) => p.to_string(),
            (None, None) => String::new(),
        }
    }

    /// Whether `-C` or `-c` is in use, which moves the layer counts to tiling time.
    pub fn filters(&self) -> bool {
        self.prefilter.is_some() || self.postfilter.is_some()
    }

    /// The number of worker threads to use.
    pub fn thread_count(&self) -> usize {
        crate::pool::threads(self.threads)
    }

    /// The checks and adjustments tippecanoe's main.cpp makes once the options
    /// are parsed. Returns the warnings tippecanoe prints along the way.
    /// Running it more than once changes nothing more.
    pub fn resolve(&mut self) -> crate::Result<Vec<String>> {
        let mut warnings = Vec::new();
        let argv0 = self.program.clone();

        if self.tippecanoe_cpus == 0 {
            self.tippecanoe_cpus = tippecanoe_cpus()?;
        }

        crate::shard::check_options(self)?;

        if self.supercluster {
            self.check_supercluster()?;
        }

        if self.h3 && self.guess_maxzoom && self.h3_base_zoom.is_none() {
            return Err(Error::new(
                EXIT_ARGS,
                format!(
                    "{}: --h3 with -zg needs --h3-base-zoom, the zoom from which the points show",
                    argv0
                ),
            ));
        }

        // Wait until here to project the bounding box, so that the behavior is
        // the same no matter what order the projection and bounding box are
        // specified in
        let project = self.project();
        for c in self.clip_bboxes.iter_mut() {
            (c.minx, c.maxy) = project.world(c.lon1, c.lat1, 32);
            (c.maxx, c.miny) = project.world(c.lon2, c.lat2, 32);
        }

        if self.max_tilestats_sample_values < self.max_tilestats_values {
            self.max_tilestats_sample_values = self.max_tilestats_values;
        }

        if self.full_detail <= 0 {
            self.full_detail = 12;
        }

        if self.droprate == -3.0 && !self.guess_maxzoom {
            return Err(Error::new(
                EXIT_ARGS,
                "Can't use -rp without either -zg or --smallest-maximum-zoom-guess",
            ));
        }

        if self.minimum_maxzoom > self.maximum_maxzoom {
            return Err(Error::new(
                EXIT_ARGS,
                format!(
                    "--smallest-maximum-zoom-guess ({}) is larger than --largest-maximum-zoom-guess ({})",
                    self.minimum_maxzoom, self.maximum_maxzoom
                ),
            ));
        }

        if self.maxzoom > MAX_ZOOM {
            self.maxzoom = MAX_ZOOM;
            warnings.push(format!("Highest supported zoom is -z{}", self.maxzoom));
        }

        // This previously dropped the maxzoom rather than the detail when they were in conflict,
        // which proved to be annoying.
        if !self.guess_maxzoom {
            if self.maxzoom > 32 - self.full_detail {
                self.full_detail = 32 - self.maxzoom;
                warnings.push(format!(
                    "Highest supported detail with maxzoom {} is {}",
                    self.maxzoom, self.full_detail
                ));
            }
            if self.maxzoom > 33 - self.low_detail {
                // that is, maxzoom - 1 > 32 - low_detail
                self.low_detail = 33 - self.maxzoom;
                warnings.push(format!(
                    "Highest supported low detail with maxzoom {} is {}",
                    self.maxzoom, self.low_detail
                ));
            }
        }
        if self.minzoom > self.maxzoom {
            return Err(Error::new(
                EXIT_ARGS,
                format!(
                    "{}: Minimum zoom -Z{} cannot be greater than maxzoom -z{}",
                    argv0, self.minzoom, self.maxzoom
                ),
            ));
        }

        if self.full_detail < self.min_detail {
            self.min_detail = self.full_detail;
            warnings.push(format!(
                "{}: Reducing minimum detail to match full detail {}",
                argv0, self.min_detail
            ));
        }

        if self.low_detail < self.min_detail {
            self.min_detail = self.low_detail;
            warnings.push(format!(
                "{}: Reducing minimum detail to match low detail {}",
                argv0, self.min_detail
            ));
        }

        if self.basezoom == -1 && !self.guess_maxzoom {
            // basezoom unspecified
            self.basezoom = self.maxzoom;
        }

        if self.extra_detail >= 0
            || self.prevent.get(P_SIMPLIFY_SHARED_NODES)
            || self.additional.get(A_EXTEND_ZOOMS)
            || self.extend_zooms_max > 0
        {
            self.geometry_scale = 0;
        } else {
            self.geometry_scale = 32 - (self.full_detail + self.maxzoom);
            if self.geometry_scale < 0 {
                self.geometry_scale = 0;
                if !self.guess_maxzoom {
                    // This shouldn't be able to happen any more. Can it still?
                    warnings.push(
                        "Full detail + maxzoom > 32, so you are asking for more detail than is available."
                            .to_string(),
                    );
                }
            }
        }

        if (self.basezoom < 0 || self.droprate < 0.0) && self.gamma < 0.0 {
            // Can't use randomized (as opposed to evenly distributed) dot dropping
            // if rate and base aren't known during feature reading.
            self.gamma = 0.0;
            warnings.push("Forcing -g0 since -B or -r is not known".to_string());
        }

        if let Output::None = self.output {
            return Err(Error::new(
                EXIT_ARGS,
                format!("{}: must specify -o out.mbtiles or -e directory", argv0),
            ));
        }

        if self.additional.get(A_DROP_BY_ATTRIBUTE_AS_NEEDED)
            && self.drop_by_attribute_as_needed_attribute.is_empty()
        {
            return Err(Error::new(
                EXIT_ARGS,
                format!(
                    "{}: --drop-by-attribute-as-needed requires an attribute name",
                    argv0
                ),
            ));
        }

        Ok(warnings)
    }
}

/// tippecanoe's `CPUS`: `TIPPECANOE_MAX_THREADS` or the number of CPUs,
/// rounded down to a power of 2.
pub fn tippecanoe_cpus() -> crate::Result<usize> {
    let cpus: usize = match std::env::var("TIPPECANOE_MAX_THREADS") {
        Ok(v) => require_int(&v, "TIPPECANOE_MAX_THREADS", "tippecanoe")? as usize,
        Err(_) => tippecanoe::platform::get_num_avail_cpus() as usize,
    };
    // At least 1, and guard against short struct index.segment
    let cpus = cpus.clamp(1, 32767);
    // Round down to a power of 2
    Ok(1 << ((cpus as f64).ln() / 2f64.ln()) as i32)
}

/// Sets tippecanoe-rs's globals from `options`.
///
/// TRANSITIONAL: the algorithms manby borrows from tippecanoe-rs read these
/// globals. This is the one place manby writes them, once per run, before any
/// borrowed function is called. Each borrowed function that manby replaces
/// with a parameterized copy removes a reason for this function to exist.
#[doc(hidden)]
pub fn set_tippecanoe_globals(options: &Options, cluster_maxzoom: i32) {
    use std::sync::atomic::Ordering::Relaxed;
    use tippecanoe::globals as g;

    for c in 0..256 {
        g::set_prevent(c, options.prevent.get(c) as i32);
        g::set_additional(c, options.additional.get(c) as i32);
    }
    tippecanoe::projection::set_projection_or_exit(PROJECTIONS[options.projection].name);
    tippecanoe::projection::set_index_hilbert(options.additional.get(A_HILBERT));

    g::GEOMETRY_SCALE.store(options.geometry_scale, Relaxed);
    g::QUIET.store(options.quiet as i32, Relaxed);
    g::QUIET_PROGRESS.store(options.quiet_progress as i32, Relaxed);
    g::LOGGER.json_enabled.store(options.json_progress, Relaxed);
    g::PROGRESS_INTERVAL.store(options.progress_interval);
    g::EXTRA_DETAIL.store(options.extra_detail, Relaxed);
    let cpus = options.tippecanoe_cpus.max(1);
    g::CPUS.store(cpus, Relaxed);
    g::TEMP_FILES.store(cpus * 4, Relaxed);
    g::MEMSIZE.store(tippecanoe::platform::calc_memsize(), Relaxed);
    g::MAX_TILE_SIZE.store(options.max_tile_size, Relaxed);
    g::MAX_TILE_FEATURES.store(options.max_tile_features, Relaxed);
    g::CLUSTER_DISTANCE.store(options.cluster_distance, Relaxed);
    g::CLUSTER_MAXZOOM.store(cluster_maxzoom, Relaxed);
    *g::ATTRIBUTE_FOR_ID.write().unwrap() = options.attribute_for_id.clone();
    g::TINY_POLYGON_SIZE.store(options.tiny_polygon_size, Relaxed);
    g::LIMIT_TILE_FEATURE_COUNT.store(options.limit_tile_feature_count, Relaxed);
    g::LIMIT_TILE_FEATURE_COUNT_AT_MAXZOOM
        .store(options.limit_tile_feature_count_at_maxzoom, Relaxed);
    *g::SET_ATTRIBUTES.write().unwrap() = options.set_attributes.clone();
    g::EXTEND_ZOOMS_MAX.store(options.extend_zooms_max, Relaxed);
    g::RETAIN_POINTS_MULTIPLIER.store(options.retain_points_multiplier, Relaxed);
    g::MAXIMUM_STRING_ATTRIBUTE_LENGTH.store(options.maximum_string_attribute_length, Relaxed);
    g::PRESERVE_MULTIPLIER_DENSITY_THRESHOLD
        .store(options.preserve_multiplier_density_threshold, Relaxed);
    g::MAX_TILESTATS_ATTRIBUTES.store(options.max_tilestats_attributes, Relaxed);
    g::MAX_TILESTATS_SAMPLE_VALUES.store(options.max_tilestats_sample_values, Relaxed);
    g::MAX_TILESTATS_VALUES.store(options.max_tilestats_values, Relaxed);
    *g::ORDER_BY.write().unwrap() = options.order_by.clone();
    g::ORDER_BY_SIZE_ENABLED.store(options.order_by_size, Relaxed);
    *g::CLIPBBOXES.write().unwrap() = options.clip_bboxes.clone();
}

/// The length of the C string `s` would be: up to its first NUL.
fn cstr(s: &str) -> &str {
    match s.find('\0') {
        Some(n) => &s[..n],
        None => s,
    }
}

fn not_a_number(argv0: &str, what: &str, s: &str) -> Error {
    Error::new(
        EXIT_ARGS,
        format!("{}: {} must be a number (got {})", argv0, what, cstr(s)),
    )
}

/// `atoi_require`
/// The command line without `--shard` and `--shard-zoom`, so that every
/// shard records the command line of the whole run.
fn without_shard_options(argv: &[String]) -> Vec<String> {
    let mut out = Vec::with_capacity(argv.len());
    let mut skip = false;
    for (i, a) in argv.iter().enumerate() {
        if skip {
            skip = false;
            continue;
        }
        if i > 0 && (a == "--shard" || a == "--shard-zoom") {
            skip = true;
            continue;
        }
        if i > 0 && (a.starts_with("--shard=") || a.starts_with("--shard-zoom=")) {
            continue;
        }
        if a == "--" {
            out.extend(argv[i..].iter().cloned());
            break;
        }
        out.push(a.clone());
    }
    out
}

fn require_int(s: &str, what: &str, argv0: &str) -> crate::Result<i32> {
    Ok(require_long(s, what, argv0)? as i32)
}

/// `atoll_require`
fn require_long(s: &str, what: &str, argv0: &str) -> crate::Result<i64> {
    let s1 = cstr(s);
    if s1.is_empty() {
        return Err(not_a_number(argv0, what, s));
    }
    let (ret, consumed) = tippecanoe::cstd::strtoll(s1, 10);
    if consumed < s1.len() {
        return Err(not_a_number(argv0, what, s));
    }
    Ok(ret)
}

/// `atof_require`
fn require_double(s: &str, what: &str, argv0: &str) -> crate::Result<f64> {
    let s1 = cstr(s);
    if s1.is_empty() {
        return Err(not_a_number(argv0, what, s));
    }
    let (ret, consumed) = tippecanoe::cstd::strtod(s1);
    if consumed < s1.len() {
        return Err(not_a_number(argv0, what, s));
    }
    Ok(ret)
}

/// `stringify_value` for a value given on the command line.
fn command_line_value(value: JsonRef<'_>, what: &str) -> crate::Result<SerialVal> {
    let (type_, s) = match value.type_() {
        JsonType::String => {
            let err = tippecanoe::text::check_utf8(value.raw_string_bytes());
            if !err.is_empty() {
                return Err(Error::new(EXIT_UTF8, format!("{}:1: {}", what, err)));
            }
            (MVT_STRING, value.string().to_string())
        }
        JsonType::Number => {
            let s = if value.large_unsigned() != 0 {
                value.large_unsigned().to_string()
            } else if value.large_signed() != 0 {
                value.large_signed().to_string()
            } else {
                tippecanoe::milo::dtoa_milo(value.number())
            };
            (MVT_DOUBLE, s)
        }
        JsonType::True => (MVT_BOOL, "true".to_string()),
        JsonType::False => (MVT_BOOL, "false".to_string()),
        JsonType::Null => (MVT_NULL, "null".to_string()),
        _ => (MVT_STRING, value.stringify()),
    };
    Ok(SerialVal { type_, s })
}

fn set_attribute_type(attribute_types: &mut HashMap<String, i32>, arg: &str) -> crate::Result<()> {
    let arg = cstr(arg);
    let s = arg.find(':').ok_or_else(|| {
        Error::new(
            EXIT_ARGS,
            format!("-T{} option must be in the form -Tname:type", arg),
        )
    })?;

    let name = arg[..s].to_string();
    let t = match &arg[s + 1..] {
        "int" => MVT_INT,
        "float" => MVT_FLOAT,
        "string" => MVT_STRING,
        "bool" => MVT_BOOL,
        other => {
            return Err(Error::new(
                EXIT_ARGS,
                format!(
                    "Attribute type ({}) must be int, float, string, or bool",
                    other
                ),
            ));
        }
    };

    attribute_types.entry(name).or_insert(t);
    Ok(())
}

fn set_attribute_value(
    set_attributes: &mut BTreeMap<String, SerialVal>,
    arg: &str,
    argv0: &str,
) -> crate::Result<()> {
    let arg = cstr(arg);
    if arg.starts_with('{') {
        let mut jp = JsonPull::begin_string(arg.as_bytes());
        let o = jp.read_tree().ok_or_else(|| {
            Error::new(
                EXIT_JSON,
                format!(
                    "{}: --set-attribute {}: {}",
                    argv0,
                    arg,
                    jp.error.unwrap_or("(null)")
                ),
            )
        })?;

        let root = o.root();
        if root.type_() != JsonType::Hash {
            return Err(Error::new(
                EXIT_JSON,
                format!("{}: --set-attribute {}: not a JSON object", argv0, arg),
            ));
        }

        for (i, (key, value)) in root.entries_iter().enumerate() {
            if key.type_() != JsonType::String {
                return Err(Error::new(
                    EXIT_JSON,
                    format!("{}: --set-attribute {}: key {} not a string", argv0, arg, i),
                ));
            }
            let val = command_line_value(value, "json")?;
            set_attributes
                .entry(key.string().to_string())
                .or_insert(val);
        }

        return Ok(());
    }

    let s = arg.find(':').ok_or_else(|| {
        Error::new(
            EXIT_ARGS,
            format!(
                "--set-attribute {} option must be in the form --set-attribute name:value",
                arg
            ),
        )
    })?;

    let value = arg[s + 1..].to_string();
    let first = value.bytes().next().unwrap_or(0);
    let type_ = if first.is_ascii_digit() || first == b'-' {
        MVT_DOUBLE
    } else {
        MVT_STRING
    };
    set_attributes
        .entry(arg[..s].to_string())
        .or_insert(SerialVal { type_, s: value });
    Ok(())
}

fn attribute_op(type_: &str) -> crate::Result<AttributeOp> {
    Ok(match type_ {
        "sum" => AttributeOp::Sum,
        "product" => AttributeOp::Product,
        "mean" => AttributeOp::Mean,
        "max" => AttributeOp::Max,
        "min" => AttributeOp::Min,
        "concat" => AttributeOp::Concat,
        "comma" => AttributeOp::Comma,
        "count" => AttributeOp::Count,
        _ => {
            return Err(Error::new(
                EXIT_ARGS,
                format!(
                    "Attribute method ({}) must be sum, product, mean, max, min, concat, comma, or count",
                    type_
                ),
            ));
        }
    })
}

/// `set_attribute_accum` for `-E`.
fn set_attribute_accum(
    attribute_accum: &mut HashMap<String, AttributeOp>,
    arg: &str,
    argv0: &str,
) -> crate::Result<()> {
    if arg.starts_with('{') {
        let mut jp = JsonPull::begin_string(arg.as_bytes());
        let o = jp.read_tree().ok_or_else(|| {
            Error::new(
                EXIT_JSON,
                format!("{}: -E{}: {}", argv0, arg, jp.error.unwrap_or("(null)")),
            )
        })?;
        let root = o.root();
        if root.type_() != JsonType::Hash {
            return Err(Error::new(
                EXIT_JSON,
                format!("{}: -E{}: not a JSON object", argv0, arg),
            ));
        }
        for (i, (key, value)) in root.entries_iter().enumerate() {
            if key.type_() != JsonType::String {
                return Err(Error::new(
                    EXIT_JSON,
                    format!("{}: -E{}: key {} not a string", argv0, arg, i),
                ));
            }
            if value.type_() != JsonType::String {
                return Err(Error::new(
                    EXIT_JSON,
                    format!("{}: -E{}: value {} not a string", argv0, arg, i),
                ));
            }
            let op = attribute_op(value.string())?;
            attribute_accum
                .entry(key.string().to_string())
                .or_insert(op);
        }
        return Ok(());
    }

    let s = arg.find(':').ok_or_else(|| {
        Error::new(
            EXIT_ARGS,
            format!("-E{} option must be in the form -Ename:method", cstr(arg)),
        )
    })?;
    let op = attribute_op(&arg[s + 1..])?;
    attribute_accum.entry(arg[..s].to_string()).or_insert(op);
    Ok(())
}

fn parse_json_source(arg: &str, src: &mut Source, argv0: &str) -> crate::Result<()> {
    let arg = cstr(arg);
    let mut jp = JsonPull::begin_string(arg.as_bytes());
    let o = jp.read_tree().ok_or_else(|| {
        Error::new(
            EXIT_JSON,
            format!("{}: -L{}: {}", argv0, arg, jp.error.unwrap_or("(null)")),
        )
    })?;

    let root = o.root();
    if root.type_() != JsonType::Hash {
        return Err(Error::new(
            EXIT_JSON,
            format!("{}: -L{}: not a JSON object", argv0, arg),
        ));
    }

    match root.hash_get("file") {
        Some(f) if f.type_() == JsonType::String => src.file = f.string().to_string(),
        _ => {
            return Err(Error::new(
                EXIT_JSON,
                format!("{}: -L{}: requires \"file\": filename", argv0, arg),
            ));
        }
    }
    let string = |key: &str| {
        root.hash_get(key)
            .filter(|v| v.type_() == JsonType::String)
            .map(|v| v.string().to_string())
    };
    if let Some(l) = string("layer") {
        src.layer = l;
    }
    if let Some(d) = string("description") {
        src.description = d;
    }
    if let Some(f) = string("format") {
        src.format = f;
    }
    Ok(())
}

/// A filter given with `-j` or read with `-J`.
fn parse_filter(s: &str) -> crate::Result<JsonTree> {
    let mut jp = JsonPull::begin_string(s.as_bytes());
    jp.read_tree().ok_or_else(|| {
        Error::new(
            EXIT_JSON,
            format!(
                "Could not parse filter {}\n{}",
                s,
                jp.error.unwrap_or("(null)")
            ),
        )
    })
}

fn read_filter(fname: &str) -> crate::Result<JsonTree> {
    let data = std::fs::read(fname)
        .map_err(|e| Error::new(EXIT_OPEN, format!("{}: {}", fname, os_message(&e))))?;
    let mut jp = JsonPull::begin_string(&data);
    jp.read_tree().ok_or_else(|| {
        Error::new(
            EXIT_JSON,
            format!("{}: {}", fname, jp.error.unwrap_or("(null)")),
        )
    })
}

/// What `perror` would print after the colon for `e`.
#[doc(hidden)]
pub fn os_message(e: &std::io::Error) -> String {
    match e.raw_os_error() {
        Some(n) => tippecanoe::cstd::strerror(n),
        None => e.to_string(),
    }
}

/// libc `sscanf` with only numeric conversions, as main.cpp uses it.
fn sscanf_f64x4(s: &str) -> Option<[f64; 4]> {
    let cs = std::ffi::CString::new(cstr(s)).ok()?;
    let mut v = [0.0f64; 4];
    let [a, b, c, d] = &mut v;
    // SAFETY: the format has four %lf conversions, each given a pointer to an f64.
    let n = unsafe {
        libc::sscanf(
            cs.as_ptr(),
            c"%lf,%lf,%lf,%lf".as_ptr(),
            a as *mut f64,
            b as *mut f64,
            c as *mut f64,
            d as *mut f64,
        )
    };
    (n == 4).then_some(v)
}

fn sscanf_u32x3(s: &str) -> Option<[u32; 3]> {
    let cs = std::ffi::CString::new(cstr(s)).ok()?;
    let mut v = [0u32; 3];
    let [a, b, c] = &mut v;
    // SAFETY: the format has three %u conversions, each given a pointer to a u32.
    let n = unsafe {
        libc::sscanf(
            cs.as_ptr(),
            c"%u/%u/%u".as_ptr(),
            a as *mut u32,
            b as *mut u32,
            c as *mut u32,
        )
    };
    (n == 3).then_some(v)
}

/// Option values for the flags that tippecanoe keeps in `additional[]` and
/// `prevent[]`, which it sets through `flag` pointers. manby keeps no globals,
/// so these come back from getopt as values instead.
const ADD: i32 = 0x100;
const PREV: i32 = 0x200;

const fn heading(name: &'static str) -> LongOption {
    LongOption {
        name,
        has_arg: 0,
        flag: None,
        val: 0,
    }
}

const fn opt(name: &'static str, has_arg: i32, val: u8) -> LongOption {
    LongOption {
        name,
        has_arg,
        flag: None,
        val: val as i32,
    }
}

const fn add(name: &'static str, c: usize) -> LongOption {
    LongOption {
        name,
        has_arg: NO_ARGUMENT,
        flag: None,
        val: ADD + c as i32,
    }
}

const fn prev(name: &'static str, c: usize) -> LongOption {
    LongOption {
        name,
        has_arg: NO_ARGUMENT,
        flag: None,
        val: PREV + c as i32,
    }
}

const REQ: i32 = REQUIRED_ARGUMENT;
const NO: i32 = NO_ARGUMENT;

// The options of tippecanoe's main.cpp, with tippecanoe's usage headings,
// and manby's own options at the end.

fn long_options_orig() -> Vec<LongOption> {
    vec![
        heading("Output tileset"),
        opt("output", REQ, b'o'),
        opt("output-to-directory", REQ, b'e'),
        opt("force", NO, b'f'),
        opt("allow-existing", NO, b'F'),
        heading("Tileset description and attribution"),
        opt("name", REQ, b'n'),
        opt("attribution", REQ, b'A'),
        opt("description", REQ, b'N'),
        heading("Input files and layer names"),
        opt("layer", REQ, b'l'),
        opt("named-layer", REQ, b'L'),
        heading("Parallel processing of input"),
        opt("read-parallel", NO, b'P'),
        heading("Projection of input"),
        opt("projection", REQ, b's'),
        heading("Zoom levels"),
        opt("maximum-zoom", REQ, b'z'),
        opt("minimum-zoom", REQ, b'Z'),
        opt("smallest-maximum-zoom-guess", REQ, b'~'),
        opt("largest-maximum-zoom-guess", REQ, b'~'),
        add("extend-zooms-if-still-dropping", A_EXTEND_ZOOMS),
        opt("extend-zooms-if-still-dropping-maximum", REQ, b'~'),
        add(
            "generate-variable-depth-tile-pyramid",
            A_VARIABLE_DEPTH_PYRAMID,
        ),
        opt("one-tile", REQ, b'R'),
        heading("Tile resolution"),
        opt("full-detail", REQ, b'd'),
        opt("low-detail", REQ, b'D'),
        opt("minimum-detail", REQ, b'm'),
        opt("extra-detail", REQ, b'~'),
        heading("Filtering feature attributes"),
        opt("exclude", REQ, b'x'),
        opt("include", REQ, b'y'),
        opt("exclude-all", NO, b'X'),
        heading("Modifying feature attributes"),
        opt("attribute-type", REQ, b'T'),
        opt("attribute-description", REQ, b'Y'),
        opt("accumulate-attribute", REQ, b'E'),
        prev("empty-csv-columns-are-null", P_EMPTY_CSV_COLUMNS),
        add("convert-stringified-ids-to-numbers", A_CONVERT_NUMERIC_IDS),
        opt("use-attribute-for-id", REQ, b'~'),
        prev("single-precision", P_SINGLE_PRECISION),
        opt("set-attribute", REQ, b'~'),
        opt("maximum-string-attribute-length", REQ, b'~'),
        heading("Filtering features by attributes"),
        opt("feature-filter-file", REQ, b'J'),
        opt("feature-filter", REQ, b'j'),
        heading("Dropping a fixed fraction of features by zoom level"),
        opt("drop-rate", REQ, b'r'),
        opt("retain-points-multiplier", REQ, b'~'),
        opt("base-zoom", REQ, b'B'),
        opt("drop-denser", REQ, b'~'),
        prev("limit-base-zoom-to-maximum-zoom", P_BASEZOOM_ABOVE_MAXZOOM),
        add("drop-lines", A_LINE_DROP),
        add("drop-polygons", A_POLYGON_DROP),
        opt("cluster-distance", REQ, b'K'),
        opt("cluster-maxzoom", REQ, b'k'),
        opt("preserve-point-density-threshold", REQ, b'~'),
        opt("preserve-multiplier-density-threshold", REQ, b'~'),
        heading("Dropping or merging a fraction of features to keep under tile size limits"),
        add("drop-densest-as-needed", A_DROP_DENSEST_AS_NEEDED),
        add("drop-fraction-as-needed", A_DROP_FRACTION_AS_NEEDED),
        add("drop-smallest-as-needed", A_DROP_SMALLEST_AS_NEEDED),
        opt("drop-by-attribute-as-needed", REQ, b'~'),
        opt("drop-by-attribute-order", REQ, b'~'),
        add("coalesce-densest-as-needed", A_COALESCE_DENSEST_AS_NEEDED),
        add("coalesce-fraction-as-needed", A_COALESCE_FRACTION_AS_NEEDED),
        add("coalesce-smallest-as-needed", A_COALESCE_SMALLEST_AS_NEEDED),
        prev("force-feature-limit", P_DYNAMIC_DROP),
        add("cluster-densest-as-needed", A_CLUSTER_DENSEST_AS_NEEDED),
        add("keep-point-cluster-position", A_KEEP_POINT_CLUSTER_POSITION),
        heading("Dropping tightly overlapping features"),
        opt("gamma", REQ, b'g'),
        add("increase-gamma-as-needed", A_INCREASE_GAMMA_AS_NEEDED),
        heading("Line and polygon simplification"),
        opt("simplification", REQ, b'S'),
        prev("no-line-simplification", P_SIMPLIFY),
        prev("simplify-only-low-zooms", P_SIMPLIFY_LOW),
        opt("simplification-at-maximum-zoom", REQ, b'~'),
        prev("no-tiny-polygon-reduction", P_TINY_POLYGON_REDUCTION),
        prev(
            "no-tiny-polygon-reduction-at-maximum-zoom",
            P_TINY_POLYGON_REDUCTION_AT_MAXZOOM,
        ),
        opt("tiny-polygon-size", REQ, b'~'),
        prev("no-simplification-of-shared-nodes", P_SIMPLIFY_SHARED_NODES),
        add("visvalingam", A_VISVALINGAM),
        heading("Attempts to improve shared polygon boundaries"),
        add("detect-shared-borders", A_DETECT_SHARED_BORDERS),
        add("grid-low-zooms", A_GRID_LOW_ZOOMS),
        heading("Controlling clipping to tile boundaries"),
        opt("buffer", REQ, b'b'),
        prev("no-clipping", P_CLIPPING),
        prev("no-duplication", P_DUPLICATION),
        heading("Reordering features within each tile"),
        prev("preserve-input-order", P_INPUT_ORDER),
        add("reorder", A_REORDER),
        add("coalesce", A_COALESCE),
        add("reverse", A_REVERSE),
        add("hilbert", A_HILBERT),
        opt("order-by", REQ, b'~'),
        opt("order-descending-by", REQ, b'~'),
        opt("order-smallest-first", NO, b'~'),
        opt("order-largest-first", NO, b'~'),
        heading("Adding calculated attributes"),
        add("calculate-feature-density", A_CALCULATE_FEATURE_DENSITY),
        add("generate-ids", A_GENERATE_IDS),
        add("calculate-feature-index", A_CALCULATE_INDEX),
        heading("Trying to correct bad source geometry"),
        add("detect-longitude-wraparound", A_DETECT_WRAPAROUND),
        prev("use-source-polygon-winding", P_USE_SOURCE_POLYGON_WINDING),
        prev(
            "reverse-source-polygon-winding",
            P_REVERSE_SOURCE_POLYGON_WINDING,
        ),
        opt("clip-bounding-box", REQ, b'~'),
        add(
            "convert-polygons-to-label-points",
            A_GENERATE_POLYGON_LABEL_POINTS,
        ),
        heading("Filtering tile contents"),
        opt("prefilter", REQ, b'C'),
        opt("postfilter", REQ, b'c'),
        heading("Setting or disabling tile size limits"),
        opt("maximum-tile-bytes", REQ, b'M'),
        opt("maximum-tile-features", REQ, b'O'),
        opt("limit-tile-feature-count", REQ, b'~'),
        opt("limit-tile-feature-count-at-maximum-zoom", REQ, b'~'),
        prev("no-feature-limit", P_FEATURE_LIMIT),
        prev("no-tile-size-limit", P_KILOBYTE_LIMIT),
        prev("no-tile-compression", P_TILE_COMPRESSION),
        prev("no-tile-stats", P_TILE_STATS),
        opt("tile-stats-attributes-limit", REQ, b'~'),
        opt("tile-stats-sample-values-limit", REQ, b'~'),
        opt("tile-stats-values-limit", REQ, b'~'),
        heading("Temporary storage"),
        opt("temporary-directory", REQ, b't'),
        heading("Progress indicator"),
        opt("quiet", NO, b'q'),
        opt("no-progress-indicator", NO, b'Q'),
        opt("progress-interval", REQ, b'U'),
        opt("json-progress", NO, b'u'),
        heading("Version"),
        opt("version", NO, b'v'),
        heading("manby"),
        opt("fast", NO, b'~'),
        opt("as-needed-per-tile", NO, b'~'),
        opt("shard", REQ, b'~'),
        opt("shard-zoom", REQ, b'~'),
        opt("compression", REQ, b'~'),
        opt("threads", REQ, b'~'),
        opt("memory-budget", REQ, b'~'),
        opt("tile-format", REQ, b'~'),
        opt("mlt-profile", REQ, b'~'),
        opt("bbox", REQ, b'~'),
        opt("h3", NO, b'~'),
        opt("h3-base-zoom", REQ, b'~'),
        opt("supercluster", NO, b'~'),
        opt("supercluster-radius", REQ, b'~'),
        opt("supercluster-min-points", REQ, b'~'),
        opt("h3-categories", REQ, b'~'),
        opt("cluster-categories", REQ, b'~'),
        opt("use-attribute-for-minzoom", REQ, b'~'),
        opt("use-attribute-for-maxzoom", REQ, b'~'),
        heading(""),
        opt("prevent", REQ, b'p'),
        opt("additional", REQ, b'a'),
        add("check-polygons", A_DEBUG_POLYGON),
        prev("no-polygon-splitting", P_POLYGON_SPLIT),
        add("prefer-radix-sort", A_PREFER_RADIX_SORT),
        opt("unidecode-data", REQ, b'~'),
        opt("help", NO, b'H'),
    ]
}

/// The short options for getopt: the options whose value is a printable letter.
fn getopt_string(long_options: &[LongOption]) -> String {
    let mut s = String::new();
    for lo in long_options {
        if lo.val > ' ' as i32 && lo.val < 0x7F {
            s.push(lo.val as u8 as char);
            if lo.has_arg == REQUIRED_ARGUMENT {
                s.push(':');
            }
        }
    }
    s
}

/// The usage message, as an error with `status`.
fn usage(argv0: &str, status: i32) -> Error {
    let forms = ["[options] [file.json ...]"];
    let required = [
        UsageRequiredOption {
            name: "output",
            placeholder: "output.mbtiles",
            alternation: 1,
        },
        UsageRequiredOption {
            name: "output-to-directory",
            placeholder: "directory",
            alternation: 1,
        },
    ];
    let mut out: Vec<u8> = Vec::new();
    print_usage(&mut out, argv0, &forms, &long_options_orig(), &required);
    let mut msg = String::from_utf8_lossy(&out).into_owned();
    if msg.ends_with('\n') {
        msg.pop();
    }
    Error::new(status, msg)
}

/// A byte as `%c` prints it.
fn letter(c: u8) -> String {
    String::from_utf8_lossy(&[c]).into_owned()
}

/// Parses a tippecanoe-style command line (`argv[0]` included). Errors carry
/// the full message to print, and so does the usage message for `--help`
/// (with exit status 0).
pub fn parse_args(argv: &[String]) -> crate::Result<Options> {
    let mut argv: Vec<String> = argv.to_vec();
    let argv0 = argv.first().cloned().unwrap_or_else(|| "manby".to_string());

    if argv.len() <= 1 {
        // with no arguments at all, there is nothing to complain about
        // specifically, so say in general what the arguments should be
        return Err(usage(&argv0, EXIT_ARGS));
    }

    let mut o = Options {
        program: argv0.clone(),
        commandline: tippecanoe::text::format_commandline(&without_shard_options(&argv)),
        ..Default::default()
    };

    let long_options_orig = long_options_orig();
    let long_options = strip_usage_headings(&long_options_orig);
    let getopt_str = getopt_string(&long_options);

    let mut out_mbtiles: Option<String> = None;
    let mut out_dir: Option<String> = None;

    let mut g = Getopt::new();
    let mut option_index: usize = 0;
    loop {
        let i = g.getopt_long(
            &mut argv,
            &getopt_str,
            &long_options,
            Some(&mut option_index),
        );
        if i == -1 {
            break;
        }
        let optarg: String = g.optarg.clone().unwrap_or_default();
        let arg = cstr(&optarg);
        let err_args = |msg: String| Err(Error::new(EXIT_ARGS, msg));

        if (ADD..ADD + 256).contains(&i) {
            o.additional.set((i - ADD) as usize, true);
            continue;
        }
        if (PREV..PREV + 256).contains(&i) {
            o.prevent.set((i - PREV) as usize, true);
            continue;
        }

        match i as u8 {
            b'~' => {
                let opt = long_options[option_index].name;
                match opt {
                    "tile-stats-attributes-limit" => {
                        o.max_tilestats_attributes = tippecanoe::cstd::atoi(arg) as usize
                    }
                    "tile-stats-sample-values-limit" => {
                        o.max_tilestats_sample_values = tippecanoe::cstd::atoi(arg) as usize
                    }
                    "tile-stats-values-limit" => {
                        o.max_tilestats_values = tippecanoe::cstd::atoi(arg) as usize
                    }
                    "clip-bounding-box" => match sscanf_f64x4(arg) {
                        Some(v) => o.clip_bboxes.push(ClipBbox {
                            lon1: v[0],
                            lat1: v[1],
                            lon2: v[2],
                            lat2: v[3],
                            ..Default::default()
                        }),
                        None => {
                            return err_args(format!(
                                "{}: Can't parse bounding box --{}={}",
                                argv0, opt, arg
                            ));
                        }
                    },
                    "use-attribute-for-id" => o.attribute_for_id = arg.to_string(),
                    "set-attribute" => set_attribute_value(&mut o.set_attributes, &optarg, &argv0)?,
                    "smallest-maximum-zoom-guess" => {
                        o.maxzoom = MAX_ZOOM;
                        o.guess_maxzoom = true;
                        o.minimum_maxzoom = require_int(&optarg, "Minimum maxzoom", &argv0)?;
                        if o.minimum_maxzoom > MAX_ZOOM {
                            return err_args(format!(
                                "{}: {}: minimum maxzoom can be at most {}",
                                argv0, arg, MAX_ZOOM
                            ));
                        }
                    }
                    "largest-maximum-zoom-guess" => {
                        o.maxzoom = MAX_ZOOM;
                        o.guess_maxzoom = true;
                        o.maximum_maxzoom = require_int(&optarg, "Largest maxzoom guess", &argv0)?;
                        if !(0..=MAX_ZOOM).contains(&o.maximum_maxzoom) {
                            return err_args(format!(
                                "{}: {}: largest maxzoom guess must be from 0 to {}",
                                argv0, arg, MAX_ZOOM
                            ));
                        }
                    }
                    "tiny-polygon-size" => o.tiny_polygon_size = tippecanoe::cstd::atoi(arg),
                    "extra-detail" => {
                        o.extra_detail = require_int(&optarg, "Extra detail", &argv0)?;
                        if o.extra_detail > 30 {
                            // So the maximum geometry delta of just under 2 tile extents
                            // is less than 2^31
                            return err_args(format!(
                                "{}: --extra-detail can be at most 30",
                                argv0
                            ));
                        }
                    }
                    "order-by" => o.order_by.push(OrderField::new(arg.to_string(), false)),
                    "order-descending-by" => {
                        o.order_by.push(OrderField::new(arg.to_string(), true))
                    }
                    "order-smallest-first" => {
                        o.order_by
                            .push(OrderField::new(ORDER_BY_SIZE.to_string(), false));
                        o.order_by_size = true;
                    }
                    "order-largest-first" => {
                        o.order_by
                            .push(OrderField::new(ORDER_BY_SIZE.to_string(), true));
                        o.order_by_size = true;
                    }
                    "drop-by-attribute-as-needed" => {
                        if arg.is_empty() {
                            return err_args(format!(
                                "{}: --drop-by-attribute-as-needed requires a non-empty attribute name",
                                argv0
                            ));
                        }
                        o.drop_by_attribute_as_needed_attribute = arg.to_string();
                        o.additional.set(A_DROP_BY_ATTRIBUTE_AS_NEEDED, true);
                    }
                    "drop-by-attribute-order" => match arg {
                        "asc" => o.drop_by_attribute_descending = false,
                        "desc" => o.drop_by_attribute_descending = true,
                        _ => {
                            return err_args(format!(
                                "{}: {}: order must be one of: asc, desc",
                                argv0, arg
                            ));
                        }
                    },
                    "simplification-at-maximum-zoom" => {
                        o.maxzoom_simplification =
                            require_double(&optarg, "Mazoom simplification", &argv0)?;
                        if o.maxzoom_simplification <= 0.0 {
                            return err_args(format!(
                                "{}: --simplification-at-maximum-zoom must be > 0",
                                argv0
                            ));
                        }
                    }
                    "limit-tile-feature-count" => {
                        o.limit_tile_feature_count =
                            require_long(&optarg, "Limit tile feature count", &argv0)? as usize
                    }
                    "limit-tile-feature-count-at-maximum-zoom" => {
                        o.limit_tile_feature_count_at_maxzoom =
                            require_long(&optarg, "Limit tile feature count at maxzoom", &argv0)?
                                as usize
                    }
                    "drop-denser" => {
                        o.drop_denser = require_int(&optarg, "Drop denser rate", &argv0)? as u32;
                        if o.drop_denser > 100 {
                            return err_args(format!(
                                "{}: --drop-denser can be at most 100",
                                argv0
                            ));
                        }
                    }
                    "preserve-point-density-threshold" => {
                        o.preserve_point_density_threshold =
                            require_long(&optarg, "Preserve point density threshold", &argv0)?
                                as u64
                    }
                    "preserve-multiplier-density-threshold" => {
                        o.preserve_multiplier_density_threshold =
                            require_long(&optarg, "Preserve multiplier density threshold", &argv0)?
                                as u64
                    }
                    "extend-zooms-if-still-dropping-maximum" => {
                        o.extend_zooms_max = require_long(
                            &optarg,
                            "Maximum number by which to extend zooms",
                            &argv0,
                        )?
                    }
                    "retain-points-multiplier" => {
                        o.retain_points_multiplier = require_long(
                            &optarg,
                            "Multiply the fraction of points retained by zoom level",
                            &argv0,
                        )? as i32
                    }
                    "unidecode-data" => {
                        o.unidecode_data = tippecanoe::text::read_unidecode(arg);
                    }
                    "maximum-string-attribute-length" => {
                        o.maximum_string_attribute_length =
                            require_long(&optarg, "Maximum string attribute length", &argv0)?
                                as usize
                    }
                    "fast" => o.fast = true,
                    "as-needed-per-tile" => o.as_needed_per_tile = true,
                    "shard" => match crate::shard::Shard::parse(arg) {
                        Some(shard) => o.shard = Some(shard),
                        None => {
                            return err_args(format!(
                                "{}: --shard must be I/N, with I from 0 to N - 1, not {}",
                                argv0, arg
                            ));
                        }
                    },
                    "shard-zoom" => {
                        o.shard_zoom = Some(require_int(&optarg, "Shard zoom", &argv0)?)
                    }
                    "compression" => match arg {
                        "zlib" => o.zlib_tiles = true,
                        "libdeflate" => o.zlib_tiles = false,
                        _ => {
                            return err_args(format!(
                                "{}: --compression must be zlib or libdeflate, not {}",
                                argv0, arg
                            ));
                        }
                    },
                    "threads" => {
                        o.threads = require_int(&optarg, "Threads", &argv0)?.max(0) as usize
                    }
                    "memory-budget" => match parse_size(arg) {
                        Some(n) if n > 0 => o.memory_budget = Some(n),
                        _ => {
                            return err_args(format!(
                                "{}: --memory-budget must be a size such as 4G or 500M, not {}",
                                argv0, arg
                            ));
                        }
                    },
                    "tile-format" => match crate::mlt::TileFormat::parse(arg) {
                        Some(crate::mlt::TileFormat::Mlt) if !crate::mlt::AVAILABLE => {
                            return err_args(format!("{}: {}", argv0, crate::mlt::NOT_BUILT));
                        }
                        Some(f) => o.tile_format = f,
                        None => {
                            return err_args(format!(
                                "{}: --tile-format must be mvt or mlt, not {}",
                                argv0, arg
                            ));
                        }
                    },
                    "h3" => o.h3 = true,
                    "supercluster" => o.supercluster = true,
                    "supercluster-radius" => match arg.parse::<f64>() {
                        Ok(r) if r > 0.0 && r.is_finite() => {
                            o.supercluster = true;
                            o.supercluster_radius = r
                        }
                        _ => {
                            return err_args(format!(
                                "{}: --supercluster-radius must be a number of pixels above 0, not {}",
                                argv0, arg
                            ));
                        }
                    },
                    "supercluster-min-points" => match arg.parse::<u64>() {
                        Ok(n) if n >= 2 => {
                            o.supercluster = true;
                            o.supercluster_min_points = n
                        }
                        _ => {
                            return err_args(format!(
                                "{}: --supercluster-min-points must be 2 or more, not {}",
                                argv0, arg
                            ));
                        }
                    },
                    "h3-base-zoom" => {
                        o.h3 = true;
                        o.h3_base_zoom = Some(require_int(&optarg, "H3 base zoom", &argv0)?)
                    }
                    "h3-categories" => {
                        o.h3 = true;
                        o.h3_categories = Some(arg.to_string())
                    }
                    "cluster-categories" => {
                        o.cluster_categories = Some(match arg.split_once(':') {
                            Some((attr, values)) => (
                                attr.to_string(),
                                Some(values.split(',').map(String::from).collect()),
                            ),
                            None => (arg.to_string(), None),
                        })
                    }
                    "use-attribute-for-minzoom" => o.attribute_for_minzoom = Some(arg.to_string()),
                    "use-attribute-for-maxzoom" => o.attribute_for_maxzoom = Some(arg.to_string()),
                    "bbox" => match sscanf_f64x4(arg) {
                        Some(b) if b[0] < b[2] && b[1] < b[3] => o.bbox = Some(b),
                        _ => {
                            return err_args(format!(
                                "{}: --bbox must be minlon,minlat,maxlon,maxlat, with the minimums below the maximums, not {}",
                                argv0, arg
                            ));
                        }
                    },
                    "mlt-profile" => match crate::mlt::MltProfile::parse(arg) {
                        Some(p) => o.mlt_profile = p,
                        None => {
                            return err_args(format!(
                                "{}: --mlt-profile must be web, native, min-size or fast, not {}",
                                argv0, arg
                            ));
                        }
                    },
                    _ => {
                        return err_args(format!("{}: Unrecognized option --{}", argv0, opt));
                    }
                }
            }

            b'n' => o.name = Some(arg.to_string()),
            b'N' => o.description = Some(arg.to_string()),
            b'l' => o.layer_name = Some(arg.to_string()),
            b'A' => o.attribution = Some(arg.to_string()),

            b'L' => {
                let mut src = Source::default();
                if optarg.starts_with('{') {
                    parse_json_source(&optarg, &mut src, &argv0)?;
                } else {
                    match arg.find(':') {
                        Some(cp) if cp != 0 => {
                            src.layer = arg[..cp].to_string();
                            src.file = arg[cp + 1..].to_string();
                        }
                        _ => return err_args(format!("{}: -L requires layername:file", argv0)),
                    }
                }
                o.sources.push(src);
            }

            b'z' => {
                if arg == "g" {
                    o.maxzoom = MAX_ZOOM;
                    o.guess_maxzoom = true;
                } else {
                    o.maxzoom = require_int(&optarg, "Maxzoom", &argv0)?;
                }
            }

            b'Z' => o.minzoom = require_int(&optarg, "Minzoom", &argv0)?,

            b'R' => match sscanf_u32x3(arg) {
                Some([z, x, y]) => {
                    o.minzoom = z as i32;
                    o.maxzoom = z as i32;
                    o.one_tile = Some((x, y));
                }
                None => return err_args("--one-tile argument must be z/x/y".to_string()),
            },

            b'B' | b'r' => {
                let letter_ = i as u8;
                let first = arg.bytes().next().unwrap_or(0);
                if arg == "g" {
                    if letter_ == b'B' {
                        o.basezoom = -2;
                    } else {
                        o.droprate = -2.0;
                    }
                } else if letter_ == b'r' && arg == "p" {
                    o.droprate = -3.0;
                } else if first == b'g' || first == b'f' {
                    if letter_ == b'B' {
                        o.basezoom = -2;
                    } else {
                        o.droprate = -2.0;
                    }
                    let rest = &optarg[1..];
                    if first == b'g' {
                        o.basezoom_marker_width = require_double(rest, "Marker width", &argv0)?;
                    } else {
                        o.basezoom_marker_width =
                            (50000.0 / require_double(rest, "Marker width", &argv0)?).sqrt();
                    }
                    if o.basezoom_marker_width == 0.0
                        || require_double(rest, "Marker width", &argv0)? == 0.0
                    {
                        return err_args(format!(
                            "{}: Must specify value >0 with -{}{}",
                            argv0,
                            letter(letter_),
                            letter(first)
                        ));
                    }
                } else if letter_ == b'B' {
                    o.basezoom = require_int(&optarg, "Basezoom", &argv0)?;
                    if o.basezoom == 0 && arg != "0" {
                        return err_args(format!("{}: Couldn't understand -B{}", argv0, arg));
                    }
                } else {
                    o.droprate = require_double(&optarg, "Drop rate", &argv0)?;
                }
            }

            b'K' => {
                o.cluster_distance = require_int(&optarg, "Cluster distance", &argv0)?;
                if o.cluster_distance > 255 {
                    return err_args(format!(
                        "{}: --cluster-distance {} is too big; limit is 255",
                        argv0, o.cluster_distance
                    ));
                }
            }

            b'k' => {
                if arg == "g" {
                    o.cluster_maxzoom = MAX_ZOOM - 1;
                    o.guess_cluster_maxzoom = true;
                } else {
                    o.cluster_maxzoom = require_int(&optarg, "Cluster maxzoom", &argv0)?;
                }
            }

            b'd' => {
                o.full_detail = require_int(&optarg, "Full detail", &argv0)?;
                if o.full_detail > 30 {
                    // So the maximum geometry delta of just under 2 tile extents
                    // is less than 2^31
                    return err_args(format!("{}: --full-detail can be at most 30", argv0));
                }
            }

            b'D' => {
                o.low_detail = require_int(&optarg, "Low detail", &argv0)?;
                if o.low_detail > 30 {
                    return err_args(format!("{}: --low-detail can be at most 30", argv0));
                }
            }

            b'm' => o.min_detail = require_int(&optarg, "Min detail", &argv0)?,

            b'o' | b'e' => {
                if let Some(prev) = out_mbtiles.as_ref().or(out_dir.as_ref()) {
                    return err_args(format!(
                        "{}: Can't specify both {} and {} as output",
                        argv0, prev, arg
                    ));
                }
                if i as u8 == b'o' {
                    out_mbtiles = Some(arg.to_string());
                } else {
                    out_dir = Some(arg.to_string());
                }
            }

            b'x' => {
                o.exclude.insert(arg.to_string());
            }
            b'y' => {
                o.exclude_all = true;
                o.include.insert(arg.to_string());
            }
            b'X' => o.exclude_all = true,

            b'Y' => match arg.find(':') {
                Some(cp) if cp != 0 => {
                    o.attribute_descriptions
                        .entry(arg[..cp].to_string())
                        .or_insert_with(|| arg[cp + 1..].to_string());
                }
                _ => return err_args(format!("{}: -Y requires attribute:description", argv0)),
            },

            b'J' => o.filter = Some(crate::filter::Filter::new(read_filter(arg)?, false)?),
            b'j' => o.filter = Some(crate::filter::Filter::new(parse_filter(arg)?, false)?),

            b'b' => {
                o.buffer = require_int(&optarg, "Buffer", &argv0)?;
                if o.buffer > 127 {
                    // So the maximum geometry delta is under 2 tile extents,
                    // from less than half a tile beyond one side to less than
                    // half a tile beyond the other.
                    return err_args(format!("{}: --buffer can be at most 127", argv0));
                }
            }

            b'f' => o.force = true,
            b'F' => o.forcetable = true,

            b't' => {
                o.tmpdir = arg.to_string();
                if !o.tmpdir.starts_with('/') {
                    eprintln!("Warning: temp directory {} doesn't begin with /", o.tmpdir);
                }
            }

            b'g' => o.gamma = require_double(&optarg, "Gamma", &argv0)?,
            b'q' => o.quiet = true,
            b'Q' => o.quiet_progress = true,
            b'u' => {
                o.quiet = true;
                o.json_progress = true;
            }
            b'U' => o.progress_interval = require_double(&optarg, "Progress interval", &argv0)?,

            b'p' | b'a' => {
                let is_prevent = i as u8 == b'p';
                for &c in arg.as_bytes() {
                    let code = if is_prevent { PREV } else { ADD } + c as i32;
                    if long_options.iter().any(|lo| lo.val == code) {
                        if is_prevent {
                            o.prevent.set(c as usize, true);
                        } else {
                            o.additional.set(c as usize, true);
                        }
                    } else {
                        return err_args(format!(
                            "{}: Unknown option -{}{}",
                            argv0,
                            letter(i as u8),
                            letter(c)
                        ));
                    }
                }
            }

            b'v' => {
                return Err(Error::new(
                    EXIT_SUCCESS,
                    format!(
                        "manby {} (tippecanoe {})",
                        env!("CARGO_PKG_VERSION"),
                        tippecanoe::version::VERSION
                    ),
                ));
            }

            b'P' => o.read_parallel = true,

            b's' => match PROJECTIONS
                .iter()
                .position(|p| p.name == arg || p.alias == arg)
            {
                Some(p) => {
                    o.projection = p;
                    o.source_crs = None;
                }
                None => match crate::crs::Reprojection::new(arg) {
                    Ok(r) => {
                        o.projection = 0;
                        o.source_crs = Some(r.code);
                    }
                    Err(_) => {
                        return err_args(format!("Unknown projection (-s): {}", arg));
                    }
                },
            },

            b'S' => {
                o.simplification = require_double(&optarg, "Simplification", &argv0)?;
                if o.simplification <= 0.0 {
                    return err_args(format!("{}: --simplification must be > 0", argv0));
                }
            }

            b'M' => o.max_tile_size = require_long(&optarg, "Max tile size", &argv0)? as usize,
            b'O' => {
                o.max_tile_features = require_long(&optarg, "Max tile features", &argv0)? as usize
            }
            b'c' => o.postfilter = Some(arg.to_string()),
            b'C' => o.prefilter = Some(arg.to_string()),
            b'T' => set_attribute_type(&mut o.attribute_types, &optarg)?,
            b'E' => set_attribute_accum(&mut o.attribute_accum, arg, &argv0)?,

            _ => {
                let mut msg = String::new();
                if i != b'H' as i32 && i != b'?' as i32 {
                    msg = format!("Unknown option -{}\n", letter(i as u8));
                }
                let status = if i == b'H' as i32 {
                    EXIT_SUCCESS
                } else {
                    EXIT_ARGS
                };
                let u = usage(&argv0, status);
                return Err(Error::new(status, msg + &u.message));
            }
        }
    }

    if let Some(out) = out_mbtiles {
        o.output = if tippecanoe::pmtiles_file::pmtiles_has_suffix(&out) {
            Output::Pmtiles(out)
        } else {
            Output::Mbtiles(out)
        };
    }
    if let Some(dir) = out_dir {
        o.output = Output::Directory(dir);
    }

    o.inputs = argv[g.optind.min(argv.len())..]
        .iter()
        .map(|a| cstr(a).to_string())
        .collect();

    Ok(o)
}

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

    fn args(s: &str) -> Vec<String> {
        s.split(' ').map(|a| a.to_string()).collect()
    }

    #[test]
    fn tile_format() {
        use crate::mlt::{MltProfile, TileFormat};
        let o = parse_args(&args("tippecanoe -o out.pmtiles in.json")).unwrap();
        assert_eq!(o.tile_format, TileFormat::Mvt);
        assert_eq!(o.mlt_profile, MltProfile::Web);
        let o = parse_args(&args("tippecanoe -o out.pmtiles --tile-format=mvt in.json")).unwrap();
        assert_eq!(o.tile_format, TileFormat::Mvt);
        let mlt = parse_args(&args(
            "tippecanoe -o out.pmtiles --tile-format=mlt --mlt-profile=native in.json",
        ));
        if crate::mlt::AVAILABLE {
            let o = mlt.unwrap();
            assert_eq!(o.tile_format, TileFormat::Mlt);
            assert_eq!(o.mlt_profile, MltProfile::Native);
        } else {
            assert!(mlt.unwrap_err().message.contains(crate::mlt::NOT_BUILT));
        }
        assert!(parse_args(&args("tippecanoe -o x.pmtiles --tile-format=png in.json")).is_err());
        assert!(parse_args(&args("tippecanoe -o x.pmtiles --mlt-profile=tiny in.json")).is_err());
    }

    #[test]
    fn compression() {
        let o = parse_args(&args("tippecanoe -o out.pmtiles in.json")).unwrap();
        assert!(!o.zlib_tiles);
        let o = parse_args(&args(
            "tippecanoe -o out.pmtiles --compression=zlib in.json",
        ))
        .unwrap();
        assert!(o.zlib_tiles);
        let o = parse_args(&args(
            "tippecanoe -o x.pmtiles --compression libdeflate in.json",
        ))
        .unwrap();
        assert!(!o.zlib_tiles);
        assert!(parse_args(&args("tippecanoe -o x.pmtiles --compression=zstd in.json")).is_err());
    }

    #[test]
    fn benchmark_command_lines() {
        let o = parse_args(&args(
            "tippecanoe -q -f -o out.pmtiles -zg --drop-densest-as-needed -P in.geojsonl",
        ))
        .unwrap();
        assert!(o.quiet && o.force && o.read_parallel && o.guess_maxzoom);
        assert_eq!(o.maxzoom, MAX_ZOOM);
        assert!(o.additional.get(A_DROP_DENSEST_AS_NEEDED));
        assert!(matches!(o.output, Output::Pmtiles(ref p) if p == "out.pmtiles"));
        assert_eq!(o.inputs, vec!["in.geojsonl".to_string()]);

        let mut o = parse_args(&args(
            "tippecanoe -f -o out.mbtiles -z12 -Z0 --coalesce-densest-as-needed --fast --as-needed-per-tile in.json",
        ))
        .unwrap();
        assert!(o.fast && o.as_needed_per_tile);
        assert!(matches!(o.output, Output::Mbtiles(_)));
        o.tippecanoe_cpus = 4;
        o.resolve().unwrap();
        assert_eq!(o.full_detail, 12);
        assert_eq!(o.basezoom, 12);
        assert_eq!(o.geometry_scale, 32 - (12 + 12));
    }

    #[test]
    fn largest_maximum_zoom_guess() {
        let o = parse_args(&args(
            "tippecanoe -o x.pmtiles --largest-maximum-zoom-guess=14 a",
        ))
        .unwrap();
        assert!(o.guess_maxzoom);
        assert_eq!(o.maximum_maxzoom, 14);

        let o = parse_args(&args("tippecanoe -o x.pmtiles -zg a")).unwrap();
        assert_eq!(o.maximum_maxzoom, MAX_ZOOM);

        let mut o = parse_args(&args(
            "tippecanoe -o x.pmtiles --smallest-maximum-zoom-guess=10 --largest-maximum-zoom-guess=8 a",
        ))
        .unwrap();
        o.tippecanoe_cpus = 1;
        assert_eq!(o.resolve().unwrap_err().exit_code, EXIT_ARGS);
    }

    #[test]
    fn flags_and_errors() {
        let o = parse_args(&args("tippecanoe -o x.mbtiles -pi -ab --no-tile-stats a b")).unwrap();
        assert!(o.prevent.get(P_INPUT_ORDER));
        assert!(o.prevent.get(P_TILE_STATS));
        assert!(o.additional.get(A_DETECT_SHARED_BORDERS));
        assert_eq!(o.inputs.len(), 2);

        let e = parse_args(&args("tippecanoe -o x.mbtiles -pZ")).unwrap_err();
        assert_eq!(e.exit_code, EXIT_ARGS);
        assert_eq!(e.message, "tippecanoe: Unknown option -pZ");

        let e = parse_args(&args("tippecanoe -o x.mbtiles -zx")).unwrap_err();
        assert_eq!(e.message, "tippecanoe: Maxzoom must be a number (got x)");

        let e = parse_args(&args("tippecanoe -H")).unwrap_err();
        assert_eq!(e.exit_code, EXIT_SUCCESS);
        assert!(e.message.starts_with("Usage: tippecanoe [options]"));

        let mut o = parse_args(&args("tippecanoe -z3 -Z5")).unwrap();
        o.tippecanoe_cpus = 1;
        assert_eq!(o.resolve().unwrap_err().exit_code, EXIT_ARGS);
    }

    #[test]
    fn attribute_options() {
        let o = parse_args(&[
            "tippecanoe".to_string(),
            "-o".to_string(),
            "x.mbtiles".to_string(),
            "--set-attribute".to_string(),
            "{\"a\":1,\"b\":\"x\"}".to_string(),
            "--set-attribute=c:-5".to_string(),
            "-Tfoo:int".to_string(),
            "-Ebar:sum".to_string(),
            "-L{\"file\":\"in.json\",\"layer\":\"l\",\"description\":\"d\"}".to_string(),
        ])
        .unwrap();
        assert_eq!(o.set_attributes["a"].s, "1");
        assert_eq!(o.set_attributes["b"].type_, MVT_STRING);
        assert_eq!(o.set_attributes["c"].type_, MVT_DOUBLE);
        assert_eq!(o.attribute_types["foo"], MVT_INT);
        assert_eq!(o.attribute_accum["bar"], AttributeOp::Sum);
        assert_eq!(o.sources[0].file, "in.json");
        assert_eq!(o.sources[0].description, "d");
        let s = o.all_sources();
        assert_eq!(s.len(), 1);
    }
}