1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
open! Base
open! Import
include Command_intf
module Shape = Shape
let raise_instead_of_exit =
match Ppx_inline_test_lib.testing with
| `Testing `Am_test_runner -> true
| `Testing `Am_child_of_test_runner | `Not_testing -> false
;;
exception Exit_called of { status : int } [@@deriving sexp_of]
include struct
let exit status =
if raise_instead_of_exit then raise (Exit_called { status }) else Stdlib.exit status
;;
module Exn = struct
let handle_uncaught_and_exit f =
if raise_instead_of_exit
then (
try f () with
| Exit_called { status = 0 } as exn -> print_s [%sexp (exn : exn)])
else Exn.handle_uncaught_and_exit f
;;
end
end
let unwords xs = String.concat ~sep:" " xs
let unparagraphs xs = String.concat ~sep:"\n\n" xs
exception Failed_to_parse_command_line of string
let die fmt = ksprintf (fun msg () -> raise (Failed_to_parse_command_line msg)) fmt
let help_screen_compare = Shape.Private.help_screen_compare
module Env = struct
include Univ_map
let key_create name = Univ_map.Key.create ~name Sexplib.Conv.sexp_of_opaque
let multi_add = Univ_map.Multi.add
let set_with_default = Univ_map.With_default.set
end
let key_internal_validate_parsing =
Env.Key.create ~name:"----internal-validate-parsing" [%sexp_of: unit]
;;
module Parsing_outcome : sig
type 'a t =
{ result : ('a, [ `Missing_required_flags of Error.t ]) Result.t
; has_arg : bool
}
val return_no_arg : 'a -> 'a t
val return_with_arg : 'a -> 'a t
val error : has_arg:bool -> [ `Missing_required_flags of Error.t ] -> 'a t
val recover_from_missing_required_flags : 'a t -> 'a t t
val introduce_missing_required_flags
: ('a, [ `Missing_required_flags of Error.t ]) Result.t t
-> 'a t
include Applicative.S with type 'a t := 'a t
end = struct
type 'a t =
{ result : ('a, [ `Missing_required_flags of Error.t ]) Result.t
; has_arg : bool
}
let apply f x =
{ result =
Result.combine
f.result
x.result
~ok:(fun f x -> f x)
~err:(fun (`Missing_required_flags err_1) (`Missing_required_flags _err_2) ->
`Missing_required_flags err_1)
; has_arg = f.has_arg || x.has_arg
}
;;
let recover_from_missing_required_flags t = { result = Ok t; has_arg = t.has_arg }
let introduce_missing_required_flags t =
{ result = Result.join t.result; has_arg = t.has_arg }
;;
let map { result; has_arg } ~f = { result = Result.map result ~f; has_arg }
let return_no_arg v = { result = Ok v; has_arg = false }
let return_with_arg v = { result = Ok v; has_arg = true }
let error ~has_arg err = { result = Error err; has_arg }
include Applicative.Make (struct
type nonrec 'a t = 'a t
let return = return_no_arg
let map = `Custom map
let apply = apply
end)
end
module Auto_complete = struct
type t = Env.t -> part:string -> string list
module For_escape = struct
type t = Env.t -> part:string list -> string list
end
end
module Completer = struct
type t = Auto_complete.t option
module For_escape = struct
type t = Auto_complete.For_escape.t option
end
let run_and_exit t env ~part : Nothing.t =
Option.iter t ~f:(fun completions ->
List.iter ~f:print_endline (completions env ~part));
exit 0
;;
end
module Arg_type : sig
type 'a t
val key : 'a t -> 'a Env.Multi.Key.t option
val complete : 'a t -> Completer.t
val parse : 'a t -> string -> 'a Or_error.t
val create
: ?complete:Auto_complete.t
-> ?key:'a Env.Multi.Key.t
-> (string -> 'a)
-> 'a t
val map : ?key:'a Env.Multi.Key.t -> 'b t -> f:('b -> 'a) -> 'a t
val of_lazy : ?key:'a Env.Multi.Key.t -> 'a t lazy_t -> 'a t
val of_map
: ?accept_unique_prefixes:bool
-> ?case_sensitive:bool
-> ?list_values_in_help:bool
-> ?auto_complete:Auto_complete.t
-> ?key:'a Env.Multi.Key.t
-> 'a Map.M(String).t
-> 'a t
val of_alist_exn
: ?accept_unique_prefixes:bool
-> ?case_sensitive:bool
-> ?list_values_in_help:bool
-> ?auto_complete:Auto_complete.t
-> ?key:'a Env.Multi.Key.t
-> (string * 'a) list
-> 'a t
val enumerated
: ?accept_unique_prefixes:bool
-> ?case_sensitive:bool
-> ?list_values_in_help:bool
-> ?auto_complete:Auto_complete.t
-> ?key:'a Env.Multi.Key.t
-> (module Enumerable_stringable with type t = 'a)
-> 'a t
val enumerated_sexpable
: ?accept_unique_prefixes:bool
-> ?case_sensitive:bool
-> ?list_values_in_help:bool
-> ?auto_complete:Auto_complete.t
-> ?key:'a Env.Multi.Key.t
-> (module Enumerable_sexpable with type t = 'a)
-> 'a t
val comma_separated
: ?allow_empty:bool
-> ?key:'a list Env.Multi.Key.t
-> ?strip_whitespace:bool
-> ?unique_values:bool
-> 'a t
-> 'a list t
module Export : sig
val string : string t
val int : int t
val char : char t
val float : float t
val bool : bool t
val sexp : Sexp.t t
val sexp_conv : ?complete:Auto_complete.t -> (Sexp.t -> 'a) -> 'a t
end
val auto_complete : _ t -> Auto_complete.t
end = struct
type 'a t =
{ parse : string -> 'a
; complete : Completer.t
; key : 'a Univ_map.Multi.Key.t option
; : string option Lazy.t
}
[@@deriving fields ~getters]
let parse t s = Or_error.try_with (fun () -> t.parse s)
let create' ?complete ?key parse ~ = { parse; key; complete; extra_doc }
let create ?complete ?key of_string =
create' ?complete ?key of_string ~extra_doc:(Lazy.from_val None)
;;
let map ?key t ~f = { t with key; parse = (fun s -> f (t.parse s)) }
let of_lazy ?key t =
let parse str = (force t).parse str in
let complete env ~part =
match (force t).complete with
| None ->
[]
| Some complete -> complete env ~part
in
let = Lazy.bind t ~f:extra_doc in
{ parse; complete = Some complete; key; extra_doc }
;;
let string = create Fn.id
let int = create Int.of_string
let char = create Char.of_string
let float = create Float.of_string
let sexp = create Parsexp.Single.parse_string_exn
let sexp_conv ?complete of_sexp =
create ?complete (fun s -> of_sexp (Parsexp.Single.parse_string_exn s))
;;
let associative
?(accept_unique_prefixes = true)
?(list_values_in_help = true)
?auto_complete
?key
~case_sensitive
alist
=
let open struct
module type S = sig
include Comparator.S with type t = string
val is_prefix : string -> prefix:string -> bool
end
type 'a t =
| T :
{ cmp : (module S with type comparator_witness = 'cmp)
; map : (string, 'a, 'cmp) Map.t
}
-> 'a t
end in
let (T { cmp = (module S); map }) =
let make_map_raise_duplicate_key
(type cmp)
(module S : S with type comparator_witness = cmp)
alist
=
match Map.of_alist (module S) alist with
| `Ok map -> map
| `Duplicate_key (_ : S.t) ->
let duplicate_keys =
List.map alist ~f:(fun (k, (_ : 'a)) -> k, k)
|> Map.of_alist_multi (module S)
|> Map.filter ~f:(function
| [] | [ _ ] -> false
| _ :: _ :: _ -> true)
|> Map.data
in
raise_s
[%message
"Command.Spec.Arg_type.of_alist_exn" (duplicate_keys : string list list)]
in
let make cmp = T { cmp; map = make_map_raise_duplicate_key cmp alist } in
if case_sensitive then make (module String) else make (module String.Caseless)
in
let complete univ_map ~part:prefix =
match auto_complete with
| Some complete -> complete univ_map ~part:prefix
| None ->
List.filter_map (Map.to_alist map) ~f:(fun (name, _) ->
match S.is_prefix name ~prefix with
| false -> None
| true ->
let suffix = String.subo name ~pos:(String.length prefix) in
let name = prefix ^ suffix in
Some name)
in
let find arg =
match Map.find map arg with
| Some _ as s -> s
| None ->
(match accept_unique_prefixes with
| false -> None
| true ->
(match
Map.to_alist map
|> List.filter ~f:(fun (name, _) -> S.is_prefix name ~prefix:arg)
with
| [ (_singleton_key, v) ] -> Some v
| [] | _ :: _ :: _ ->
None))
in
create'
~extra_doc:
(lazy
(if list_values_in_help
then (
let values = String.concat ~sep:", " (Map.keys map) in
Some [%string "(can be: %{values})"])
else None))
?key
~complete
(fun arg ->
match find arg with
| Some v -> v
| None ->
let =
if case_sensitive then "" else " (case insensitive)"
in
failwithf
"valid arguments%s: {%s}"
valid_arguments_extra
(String.concat ~sep:"," (Map.keys map))
())
;;
let of_alist_exn
?accept_unique_prefixes
?(case_sensitive = true)
?list_values_in_help
?auto_complete
?key
alist
=
associative
?accept_unique_prefixes
?list_values_in_help
?auto_complete
?key
~case_sensitive
alist
;;
let of_map
?accept_unique_prefixes
?case_sensitive
?list_values_in_help
?auto_complete
?key
map
=
of_alist_exn
?accept_unique_prefixes
?case_sensitive
?list_values_in_help
?auto_complete
?key
(Map.to_alist map)
;;
let enumerated
(type t)
?accept_unique_prefixes
?case_sensitive
?list_values_in_help
?auto_complete
?key
(module E : Enumerable_stringable with type t = t)
=
of_alist_exn
?accept_unique_prefixes
?case_sensitive
?list_values_in_help
?auto_complete
?key
(let%map.List t = E.all in
E.to_string t, t)
;;
let enumerated_sexpable
(type t)
?accept_unique_prefixes
?case_sensitive
?list_values_in_help
?auto_complete
?key
(module E : Enumerable_sexpable with type t = t)
=
enumerated
?accept_unique_prefixes
?case_sensitive
?list_values_in_help
?auto_complete
?key
(module struct
include E
let to_string t = Sexp.to_string [%sexp (t : E.t)]
end)
;;
let bool = enumerated ~list_values_in_help:false (module Bool)
let comma_separated
?(allow_empty = false)
?key
?(strip_whitespace = false)
?(unique_values = false)
t
=
let strip = if strip_whitespace then fun str -> String.strip str else Fn.id in
let complete =
Option.map t.complete ~f:(fun complete_elt env ~part ->
let prefixes, suffix =
match String.split part ~on:',' |> List.rev with
| [] -> [], part
| hd :: tl -> List.rev tl, hd
in
let is_allowed =
if not unique_values
then fun (_ : string) -> true
else (
let seen_already =
prefixes |> List.map ~f:strip |> Set.of_list (module String)
in
fun choice -> not (Set.mem seen_already (strip choice)))
in
let choices =
match
List.filter (complete_elt env ~part:suffix) ~f:(fun choice ->
(not (String.mem choice ',')) && is_allowed choice)
with
| [ choice ] -> [ choice; choice ^ "," ]
| choices -> choices
in
List.map choices ~f:(fun choice -> String.concat ~sep:"," (prefixes @ [ choice ])))
in
let of_string string =
let string = strip string in
if String.is_empty string
then
if allow_empty
then []
else failwith "Command.Spec.Arg_type.comma_separated: empty list not allowed"
else List.map (String.split string ~on:',') ~f:(fun str -> t.parse (strip str))
in
create ?key ?complete of_string
;;
module Export = struct
let string = string
let int = int
let char = char
let float = float
let bool = bool
let sexp = sexp
let sexp_conv = sexp_conv
end
let auto_complete t =
match t.complete with
| Some f -> f
| None -> fun _ ~part:_ -> []
;;
end
module Flag = struct
module Num_occurrences = struct
type t = Shape.Num_occurrences.t =
{ at_least_once : bool
; at_most_once : bool
}
[@@deriving compare, enumerate, sexp_of]
let to_help_string = Shape.Num_occurrences.to_help_string
let to_help_string_deprecated { at_least_once; at_most_once = _ } flag_name =
to_help_string { at_least_once; at_most_once = true } ~flag_name
;;
let any = { at_least_once = false; at_most_once = false }
let at_least_once = { at_least_once = true; at_most_once = false }
let at_most_once = { at_least_once = false; at_most_once = true }
let exactly_once = { at_least_once = true; at_most_once = true }
end
type action =
| No_arg of (Env.t -> Env.t)
| Print_info_and_quit of (Env.t -> string)
| Arg of (Env.t -> string -> Env.t) * Completer.t
| Rest of (Env.t -> string list -> Env.t) * Completer.For_escape.t
module Internal = struct
type t =
{ name : string
; aliases : string list
; aliases_excluded_from_help : string list
; action : action
; doc : string
; num_occurrences : Num_occurrences.t
; check_available : Env.t -> unit
; name_matching : [ `Prefix | `Full_match_required ]
}
let wrap_if_optional t flag_name =
Num_occurrences.to_help_string t.num_occurrences ~flag_name
;;
module Doc = struct
type t =
{ arg_doc : string option
; doc : string
}
let parse ~action ~doc =
let arg_doc, doc =
match (action : action), String.lsplit2 doc ~on:' ' with
| (No_arg _ | Print_info_and_quit _), _ -> None, doc
| Arg _, (None | Some ("", _)) -> Some "_", doc
| Rest _, (None | Some ("", _)) -> None, doc
| (Arg _ | Rest _), Some (arg, doc) -> Some arg, doc
in
{ doc = String.strip doc; arg_doc }
;;
let concat ~name ~arg_doc =
match arg_doc with
| None -> name
| Some arg_doc -> name ^ " " ^ arg_doc
;;
end
module Deprecated = struct
let wrap_if_optional t x =
Num_occurrences.to_help_string_deprecated t.num_occurrences x
;;
let help
({ name
; doc
; aliases
; action
; num_occurrences = _
; check_available = _
; name_matching = _
; aliases_excluded_from_help = _
} as t)
=
if String.is_prefix doc ~prefix:" "
then
(name, String.lstrip doc)
:: List.map aliases ~f:(fun x -> x, sprintf "same as \"%s\"" name)
else (
let { Doc.arg_doc; doc } = Doc.parse ~action ~doc in
(wrap_if_optional t (Doc.concat ~name ~arg_doc), doc)
:: List.map aliases ~f:(fun x ->
( wrap_if_optional t (Doc.concat ~name:x ~arg_doc)
, sprintf "same as \"%s\"" name )))
;;
end
let align
({ name
; doc
; aliases
; action
; num_occurrences = _
; check_available = _
; name_matching = _
; aliases_excluded_from_help = _
} as t)
: Shape.Flag_info.t
=
let { Doc.arg_doc; doc } = Doc.parse ~action ~doc in
let name = wrap_if_optional t (Doc.concat ~name ~arg_doc) in
{ name; doc; aliases }
;;
let create flags =
match
Map.of_alist (module String) (List.map flags ~f:(fun flag -> flag.name, flag))
with
| `Duplicate_key flag -> failwithf "multiple flags named %s" flag ()
| `Ok map ->
List.concat_map flags ~f:(fun flag -> flag.name :: flag.aliases)
|> List.find_a_dup ~compare:[%compare: string]
|> Option.iter ~f:(fun x -> failwithf "multiple flags or aliases named %s" x ());
map
;;
end
type 'a state =
{ action : action
; read : Env.t -> 'a Parsing_outcome.t
; num_occurrences : Num_occurrences.t
; extra_doc : string option Lazy.t
}
type 'a t = string -> 'a state
let arg_flag name arg_type read write num_occurrences =
{ read
; num_occurrences
; action =
(let update env arg =
match Arg_type.parse arg_type arg with
| Error error ->
die
"failed to parse %s value %S.\n%s"
name
arg
(Error.to_string_hum error)
()
| Ok arg ->
let env = write env arg in
(match Arg_type.key arg_type with
| None -> env
| Some key -> Env.multi_add env ~key ~data:arg)
in
Arg (update, Arg_type.complete arg_type))
; extra_doc = Arg_type.extra_doc arg_type
}
;;
let map_flag (t : _ t) ~f input =
let { action; read; num_occurrences; } = t input in
{ action
; read = (fun env -> Parsing_outcome.map (read env) ~f)
; num_occurrences
; extra_doc
}
;;
let write_option name key env arg =
Env.update env key ~f:(function
| None -> arg
| Some _ -> die "flag %s passed more than once" name ())
;;
let required_value ?default arg_type name num_occurrences =
let key = Env.Key.create ~name [%sexp_of: _] in
let read env =
match Env.find env key with
| Some v -> Parsing_outcome.return_with_arg v
| None ->
(match default with
| Some v -> Parsing_outcome.return_no_arg v
| None ->
Parsing_outcome.error
~has_arg:false
(`Missing_required_flags
(Error.of_string (sprintf "missing required flag: %s" name))))
in
let write env arg = write_option name key env arg in
arg_flag name arg_type read write num_occurrences
;;
let required arg_type name = required_value arg_type name Num_occurrences.exactly_once
let optional_with_default default arg_type name =
required_value ~default arg_type name Num_occurrences.at_most_once
;;
let optional arg_type name =
let key = Env.Key.create ~name [%sexp_of: _] in
let read env =
match Env.find env key with
| None -> Parsing_outcome.return_no_arg None
| Some _ as value -> Parsing_outcome.return_with_arg value
in
let write env arg = write_option name key env arg in
arg_flag name arg_type read write Num_occurrences.at_most_once
;;
let no_arg_general ~is_required ~key_value ~deprecated_hook name =
let key = Env.Key.create ~name [%sexp_of: unit] in
let read env =
match Env.mem env key with
| true -> Parsing_outcome.return_with_arg true
| false ->
if is_required
then
Parsing_outcome.error
~has_arg:false
(`Missing_required_flags
(Error.of_string (sprintf "missing required flag: %s" name)))
else Parsing_outcome.return_no_arg false
in
let write env =
if Env.mem env key
then die "flag %s passed more than once" name ()
else Env.set env ~key ~data:()
in
let action env =
let env =
Option.fold key_value ~init:env ~f:(fun env (key, value) ->
Env.set_with_default env ~key ~data:value)
in
write env
in
let action =
match deprecated_hook with
| None -> action
| Some f ->
fun env ->
let env = action env in
f ();
env
in
{ read
; action = No_arg action
; num_occurrences =
(if is_required
then Num_occurrences.exactly_once
else Num_occurrences.at_most_once)
; extra_doc = Lazy.from_val None
}
;;
let no_arg name =
no_arg_general name ~is_required:false ~key_value:None ~deprecated_hook:None
;;
let no_arg_required v name =
map_flag
(no_arg_general ~is_required:true ~key_value:None ~deprecated_hook:None)
~f:(function
| true -> v
| false -> assert false)
name
;;
let no_arg_register ~key ~value name =
no_arg_general
name
~is_required:false
~key_value:(Some (key, value))
~deprecated_hook:None
;;
let no_arg_some value =
map_flag no_arg ~f:(function
| true -> Some value
| false -> None)
;;
let listed arg_type name =
let key = Env.With_default.Key.create ~default:[] ~name [%sexp_of: _ list] in
let read env =
match List.rev (Env.With_default.find env key) with
| [] -> Parsing_outcome.return_no_arg []
| _ :: _ as value_list -> Parsing_outcome.return_with_arg value_list
in
let write env arg = Env.With_default.change env key ~f:(fun list -> arg :: list) in
arg_flag name arg_type read write Num_occurrences.any
;;
let one_or_more_as_pair arg_type name =
let key = Env.With_default.Key.create ~default:[] ~name [%sexp_of: _ list] in
let read env =
match List.rev (Env.With_default.find env key) with
| first :: rest -> Parsing_outcome.return_with_arg (first, rest)
| [] ->
Parsing_outcome.error
~has_arg:false
(`Missing_required_flags
(Error.of_string (sprintf "missing required flag: %s" name)))
in
let write env arg = Env.With_default.change env key ~f:(fun q -> arg :: q) in
arg_flag name arg_type read write Num_occurrences.at_least_once
;;
let one_or_more_as_list arg_type =
one_or_more_as_pair arg_type |> map_flag ~f:(fun (x, xs) -> x :: xs)
;;
let escape_general ~complete ~deprecated_hook name =
let key = Env.Key.create ~name [%sexp_of: string list] in
let action env cmd_line = Env.set env ~key ~data:cmd_line in
let read env =
match Env.find env key with
| None -> Parsing_outcome.return_no_arg None
| Some _ as value -> Parsing_outcome.return_with_arg value
in
let action =
match deprecated_hook with
| None -> action
| Some f ->
fun env x ->
f x;
action env x
in
{ action = Rest (action, complete)
; read
; num_occurrences = Num_occurrences.at_most_once
; extra_doc = Lazy.from_val None
}
;;
let no_arg_abort ~exit _name =
{ action = No_arg (fun _ -> Nothing.unreachable_code (exit ()))
; num_occurrences = Num_occurrences.at_most_once
; read =
(fun _ ->
Parsing_outcome.return_no_arg ())
; extra_doc = Lazy.from_val None
}
;;
let escape name = escape_general ~complete:None ~deprecated_hook:None name
let escape_with_autocomplete ~complete name =
escape_general ~complete:(Some complete) ~deprecated_hook:None name
;;
module Deprecated = struct
let no_arg ~hook name =
no_arg_general ~is_required:false ~deprecated_hook:(Some hook) ~key_value:None name
;;
let escape ~hook = escape_general ~complete:None ~deprecated_hook:(Some hook)
end
end
module Path : sig
type t
val empty : t
val create : path_to_exe:string -> t
val of_parts : string list -> t
val append : t -> subcommand:string -> t
val replace_first : t -> from:string -> to_:string -> t
val parts : t -> string list
val parts_exe_basename : t -> string list
val to_string : t -> string
val to_string_dots : t -> string
val pop_help : t -> t
val length : t -> int
val is_empty : t -> bool
end = struct
type t = string list
let empty = []
let create ~path_to_exe = [ path_to_exe ]
let of_parts parts = List.rev parts
let append t ~subcommand = subcommand :: t
let parts = List.rev
let parts_exe_basename t =
match List.rev t with
| [] -> []
| hd :: tl -> Filename_base.basename hd :: tl
;;
let to_string t = unwords (parts_exe_basename t)
let length = List.length
let replace_first t ~from ~to_ =
let rec aux parts ~acc ~from ~to_ =
match parts with
| [] -> acc
| hd :: tl ->
if String.( = ) hd from
then List.rev_append tl (to_ :: acc)
else aux tl ~acc:(hd :: acc) ~from ~to_
in
aux (parts t) ~acc:[] ~from ~to_
;;
let pop_help = function
| "help" :: t -> t
| _ -> assert false
;;
let to_string_dots t =
(match t with
| [] -> []
| last :: init -> last :: List.map init ~f:(Fn.const "."))
|> to_string
;;
let is_empty = List.is_empty
end
module Anons = struct
module Grammar : sig
type t = Shape.Anons.Grammar.t
val zero : t
val one : string -> t
val many : t -> t
val maybe : t -> t
val maybe_idempotent : t -> t
val concat : t list -> t
val ad_hoc : usage:string -> t
include Invariant.S with type t := t
val names : t -> string list
end = struct
type t = Shape.Anons.Grammar.t =
| Zero
| One of string
| Many of t
| Maybe of t
| Concat of t list
| Ad_hoc of string
let invariant = Shape.Anons.Grammar.invariant
let usage = Shape.Anons.Grammar.usage
let rec is_fixed_arity = function
| Zero -> true
| One _ -> true
| Many _ -> false
| Maybe _ -> false
| Ad_hoc _ -> false
| Concat ts ->
(match List.rev ts with
| [] -> failwith "bug in command.ml"
| last :: others ->
assert (List.for_all others ~f:is_fixed_arity);
is_fixed_arity last)
;;
let rec names = function
| Zero -> []
| One s -> [ s ]
| Many t -> names t
| Maybe t -> names t
| Ad_hoc s -> [ s ]
| Concat ts -> List.concat_map ts ~f:names
;;
let zero = Zero
let one name = One name
let many = function
| Zero -> Zero
| t ->
if not (is_fixed_arity t)
then
failwithf
"iteration of variable-length grammars such as %s is disallowed"
(usage t)
();
Many t
;;
let maybe = function
| Zero -> Zero
| t -> Maybe t
;;
let maybe_idempotent = function
| Zero -> Zero
| Maybe t -> Maybe t
| Many t -> Many t
| t -> Maybe t
;;
let concat = function
| [] -> Zero
| car :: cdr ->
let car, cdr =
List.fold cdr ~init:(car, []) ~f:(fun (t1, acc) t2 ->
match t1, t2 with
| Zero, t | t, Zero -> t, acc
| _, _ ->
if is_fixed_arity t1
then t2, t1 :: acc
else
failwithf
"the grammar %s for anonymous arguments is not supported because there \
is the possibility for arguments (%s) following a variable number of \
arguments (%s). Supporting such grammars would complicate the \
implementation significantly."
(usage (Concat (List.rev (t2 :: t1 :: acc))))
(usage t2)
(usage t1)
())
in
(match cdr with
| [] -> car
| _ :: _ -> Concat (List.rev (car :: cdr)))
;;
let ad_hoc ~usage = Ad_hoc usage
end
module Parser : sig
module Basic : sig
type +'a t
module For_opening : sig
val return : 'a -> 'a t
val ( <*> ) : ('a -> 'b) t -> 'a t -> 'b t
val ( >>| ) : 'a t -> ('a -> 'b) -> 'b t
end
val from_env : (Env.t -> 'a) -> 'a t
end
type +'a t = 'a Parsing_outcome.t Basic.t
val one : name:string -> 'a Arg_type.t -> 'a t
val maybe : 'a t -> 'a option t
val sequence : 'a t -> 'a list t
val stop_parsing : 'a t -> 'a t
val final_value : 'a Basic.t -> Env.t -> 'a
module Consume_result : sig
type nonrec 'a t =
{
parser : 'a Basic.t
; parse_flags : bool
; update_env : Env.t -> Env.t
}
end
val consume : 'a Basic.t -> string -> for_completion:bool -> 'a Consume_result.t
val complete : 'a Basic.t -> Env.t -> part:string -> Nothing.t
module For_opening : sig
val return : 'a -> 'a t
val ( <*> ) : ('a -> 'b) t -> 'a t -> 'b t
val ( >>| ) : 'a t -> ('a -> 'b) -> 'b t
end
end = struct
module Basic = struct
type 'a t =
| Done of (Env.t -> 'a)
| More of 'a more
| Test of (more:bool -> 'a t)
| Only_for_completion of packed list
| Stop_parsing of 'a t
and 'a more =
{ name : string
; parse : string -> for_completion:bool -> 'a parse_result
; complete : Completer.t
}
and packed = Packed : 'a t -> packed
and 'a parse_result =
{ parser : 'a t
; update_env : Env.t -> Env.t
}
let parse_more { name; parse; complete } ~f =
let parse arg ~for_completion =
let { parser; update_env } = parse arg ~for_completion in
{ parser = f parser; update_env }
in
More { name; parse; complete }
;;
let pack_for_completion = function
| Done _ -> []
| (More _ | Test _ | Stop_parsing _) as x -> [ Packed x ]
| Only_for_completion ps -> ps
;;
let rec ( <*> ) t_left t_right =
match t_left, t_right with
| Done f, Done x ->
Done
(fun env ->
let f_outcome = f env in
let x_outcome = x env in
f_outcome x_outcome)
| More more, _ -> parse_more more ~f:(fun tl -> tl <*> t_right)
| Done _, More more -> parse_more more ~f:(fun tr -> t_left <*> tr)
| Only_for_completion _, _ | Done _, Only_for_completion _ ->
Only_for_completion (pack_for_completion t_left @ pack_for_completion t_right)
| Stop_parsing tl, tr | (Done _ as tl), Stop_parsing tr -> Stop_parsing (tl <*> tr)
| Test test, _ -> Test (fun ~more -> test ~more <*> t_right)
| Done _, Test test -> Test (fun ~more -> t_left <*> test ~more)
;;
let return a = Done (fun _ -> a)
let ( >>| ) t f = return f <*> t
let from_env f = Done (fun env -> f env)
module For_opening = struct
let return = return
let ( <*> ) = ( <*> )
let ( >>| ) = ( >>| )
end
end
open Basic
type 'a t = 'a Parsing_outcome.t Basic.t
let ( >>| ) t f = t >>| Parsing_outcome.map ~f
let ( <*> ) t_left t_right = return Parsing_outcome.( <*> ) <*> t_left <*> t_right
let return a = return (Parsing_outcome.return a)
let return_with_arg a = Done (fun _ -> Parsing_outcome.return_with_arg a)
let stop_parsing t = Stop_parsing t
let one_more ~name arg_type =
let parse anon ~for_completion =
match Arg_type.parse arg_type anon with
| Error error ->
if for_completion
then
{ parser = Only_for_completion []; update_env = Fn.id }
else
die "failed to parse %s value %S\n%s" name anon (Error.to_string_hum error) ()
| Ok v ->
{ parser = return_with_arg v
; update_env =
(fun env ->
Option.fold (Arg_type.key arg_type) ~init:env ~f:(fun env key ->
Env.multi_add env ~key ~data:v))
}
in
More { name; parse; complete = Arg_type.complete arg_type }
;;
let one ~name arg_type =
Test
(fun ~more ->
if more
then one_more ~name arg_type
else
Done
(fun _ ->
Parsing_outcome.error
~has_arg:false
(`Missing_required_flags
(Error.of_string (sprintf "missing anonymous argument: %s" name)))))
;;
let maybe t =
Test
(fun ~more ->
if more then return_with_arg (fun a -> Some a) <*> t else return None)
;;
let sequence t =
let rec loop =
Test
(fun ~more ->
if more then return (fun v acc -> v :: acc) <*> t <*> loop else return [])
in
loop
;;
let rec final_value t env =
match t with
| Done a -> a env
| Stop_parsing t -> final_value t env
| Test f -> final_value (f ~more:false) env
| More _ ->
assert false
| Only_for_completion _ ->
failwith "BUG: asked for final value when doing completion"
;;
module Consume_result = struct
type nonrec 'a t =
{ parser : 'a Basic.t
; parse_flags : bool
; update_env : Env.t -> Env.t
}
end
let rec consume
: type a. a Basic.t -> string -> for_completion:bool -> a Consume_result.t
=
fun t arg ~for_completion ->
match t with
| Done _ -> die "too many anonymous arguments" ()
| Test f -> consume (f ~more:true) arg ~for_completion
| More { parse; _ } ->
let { parser; update_env } = parse arg ~for_completion in
{ parser; parse_flags = true; update_env }
| Stop_parsing t -> { (consume t arg ~for_completion) with parse_flags = false }
| Only_for_completion packed ->
(match packed with
| [] ->
{ parser = Only_for_completion []; parse_flags = true; update_env = Fn.id }
| Packed t :: rest ->
let ({ update_env; parse_flags; parser } : _ Consume_result.t) =
consume t arg ~for_completion
in
{ update_env
; parse_flags
; parser = Only_for_completion (pack_for_completion parser @ rest)
})
;;
let rec complete : type a. a Basic.t -> Env.t -> part:string -> Nothing.t =
fun t env ~part ->
match t with
| Done _ -> exit 0
| Test f -> complete (f ~more:true) env ~part
| More { complete; _ } -> Completer.run_and_exit complete env ~part
| Stop_parsing t -> complete t env ~part
| Only_for_completion t ->
(match t with
| [] -> exit 0
| Packed t :: _ -> complete t env ~part)
;;
module For_opening = struct
let return = return
let ( <*> ) = ( <*> )
let ( >>| ) = ( >>| )
end
end
open Parser.For_opening
type 'a t =
{ p : 'a Parser.t
; grammar : Grammar.t
}
let t2 t1 t2 =
{ p = return (fun a1 a2 -> a1, a2) <*> t1.p <*> t2.p
; grammar = Grammar.concat [ t1.grammar; t2.grammar ]
}
;;
let t3 t1 t2 t3 =
{ p = return (fun a1 a2 a3 -> a1, a2, a3) <*> t1.p <*> t2.p <*> t3.p
; grammar = Grammar.concat [ t1.grammar; t2.grammar; t3.grammar ]
}
;;
let t4 t1 t2 t3 t4 =
{ p = return (fun a1 a2 a3 a4 -> a1, a2, a3, a4) <*> t1.p <*> t2.p <*> t3.p <*> t4.p
; grammar = Grammar.concat [ t1.grammar; t2.grammar; t3.grammar; t4.grammar ]
}
;;
let normalize str =
let strlen = String.length str in
if strlen = 0 then failwith "Empty anonymous argument name provided";
if String.( <> ) (String.strip str) str
then failwithf "argument name %S has surrounding whitespace" str ();
let has_special_chars =
let special_chars =
Set.of_list (module Char) [ '<'; '>'; '['; ']'; '('; ')'; '{'; '}' ]
in
String.exists str ~f:(Set.mem special_chars)
in
if has_special_chars then str else String.uppercase str
;;
let ( %: ) name arg_type =
let name = normalize name in
{ p = Parser.one ~name arg_type; grammar = Grammar.one name }
;;
let map_anons t ~f = { p = t.p >>| f; grammar = t.grammar }
let maybe t = { p = Parser.maybe t.p; grammar = Grammar.maybe t.grammar }
let maybe_with_default default t =
let t = maybe t in
{ t with p = (t.p >>| fun v -> Option.value ~default v) }
;;
let sequence t = { p = Parser.sequence t.p; grammar = Grammar.many t.grammar }
let non_empty_sequence_as_pair t = t2 t (sequence t)
let non_empty_sequence_as_list t =
let t = non_empty_sequence_as_pair t in
{ t with p = (t.p >>| fun (x, xs) -> x :: xs) }
;;
let escape t = { p = Parser.stop_parsing t.p; grammar = t.grammar }
module Deprecated = struct
let ad_hoc ~usage_arg =
{ p =
Parser.sequence
(Parser.one ~name:"WILL NEVER BE PRINTED" Arg_type.Export.string)
; grammar = Grammar.ad_hoc ~usage:usage_arg
}
;;
end
end
module Cmdline = struct
type t =
| Nil
| Cons of string * t
| Complete of string
[@@deriving compare]
let of_list args = List.fold_right args ~init:Nil ~f:(fun arg args -> Cons (arg, args))
let rec to_list = function
| Nil -> []
| Cons (x, xs) -> x :: to_list xs
| Complete x -> [ x ]
;;
let rec ends_in_complete = function
| Complete _ -> true
| Nil -> false
| Cons (_, args) -> ends_in_complete args
;;
let extend t ~extend ~path =
if ends_in_complete t
then t
else (
let path_list = Option.value ~default:[] (List.tl (Path.parts path)) in
of_list (to_list t @ extend path_list))
;;
end
module Key_type = Shape.Private.Key_type
let assert_no_underscores key_type flag_or_subcommand =
if String.exists flag_or_subcommand ~f:(fun c -> Char.( = ) c '_')
then
failwithf
"%s %s contains an underscore. Use a dash instead."
(Key_type.to_string key_type)
flag_or_subcommand
()
;;
let normalize key_type key =
assert_no_underscores key_type key;
match key_type with
| Key_type.Flag ->
if String.equal key "-" then failwithf !"invalid %{Key_type} name: %S" key_type key ();
if String.exists key ~f:Char.is_whitespace
then failwithf !"invalid %{Key_type} name (contains whitespace): %S" key_type key ();
if String.is_prefix ~prefix:"-" key then key else "-" ^ key
| Key_type.Subcommand -> String.lowercase key
;;
let lookup_expand = Shape.Private.lookup_expand
let lookup_expand_with_aliases map prefix key_type =
let alist =
List.concat_map (Map.data map) ~f:(fun flag ->
let { Flag.Internal.name
; aliases
; aliases_excluded_from_help
; action = _
; doc = _
; num_occurrences = _
; check_available = _
; name_matching
}
=
flag
in
let data = flag, name_matching in
let aliases = aliases_excluded_from_help @ aliases in
(name, data) :: List.map aliases ~f:(fun alias -> alias, data))
in
match List.find_a_dup alist ~compare:(fun (s1, _) (s2, _) -> String.compare s1 s2) with
| None -> lookup_expand alist prefix key_type
| Some (flag, _) -> failwithf "multiple flags named %s" flag ()
;;
module Command_base = struct
type t =
{ summary : string
; readme : (unit -> string) option
; flags : Flag.Internal.t Map.M(String).t
; anons : unit -> ([ `Parse_args ] -> [ `Run_main ] -> unit) Anons.Parser.Basic.t
; usage : Anons.Grammar.t
}
module Deprecated = struct
let subcommand_cmp_fst (a, _) (c, _) = help_screen_compare a c
let flags_help ?(display_help_flags = true) t =
let flags = Map.data t.flags in
let flags =
if display_help_flags
then flags
else List.filter flags ~f:(fun f -> String.( <> ) f.name "-help")
in
List.concat_map ~f:Flag.Internal.Deprecated.help flags
;;
end
let formatted_flags t =
Map.data t.flags
|> List.map ~f:Flag.Internal.align
|> List.sort ~compare:(fun a b -> String.compare a.name b.name)
|> Shape.Flag_help_display.sort
;;
let shape t : Shape.Base_info.t =
{ summary = t.summary
; readme = Option.map t.readme ~f:(fun readme -> readme ())
; anons = Grammar t.usage
; flags = formatted_flags t
}
;;
let path_key = Env.key_create "path"
let args_key = Env.key_create "args"
let help_key = Env.key_create "help"
let normalized_path = ref None
let normalized_args = ref None
let indent_by_2 str =
String.split ~on:'\n' str
|> List.map ~f:(fun line -> " " ^ line)
|> String.concat ~sep:"\n"
;;
let get_flag_and_action t arg =
match lookup_expand_with_aliases t.flags arg Flag with
| Error msg -> die "%s" msg ()
| Ok (flag_name, flag) -> flag_name, flag.action
;;
let get_complete_flag_name t arg (args : Cmdline.t) =
let flag, action = get_flag_and_action t arg in
match action with
| Print_info_and_quit _info -> [ flag ]
| No_arg _f -> [ flag ]
| Arg (_f, _comp) ->
(match args with
| Cons (arg, _rest) -> [ flag; arg ]
| Nil | Complete _ -> [])
| Rest (_f, _comp) -> flag :: Cmdline.to_list args
;;
let run_flag t env arg (args : Cmdline.t) =
let flag, action = get_flag_and_action t arg in
match action with
| Print_info_and_quit info ->
let completing = Cmdline.ends_in_complete args in
if completing
then env, args
else (
print_endline (info env);
exit 0)
| No_arg f -> f env, args
| Arg (f, comp) ->
(match args with
| Nil -> die "missing argument for flag %s" flag ()
| Cons (arg, rest) ->
let env =
try f env arg with
| Failed_to_parse_command_line _ as e ->
if Cmdline.ends_in_complete rest then env else raise e
in
env, rest
| Complete part -> Nothing.unreachable_code (Completer.run_and_exit comp env ~part))
| Rest (f, comp) ->
let arg_list = Cmdline.to_list args in
if Cmdline.ends_in_complete args
then Nothing.unreachable_code (Completer.run_and_exit comp env ~part:arg_list);
f env arg_list, Nil
;;
let rec run_cmdline
t
env
parser
(cmdline : Cmdline.t)
~for_completion
~parse_flags
~normalized_args
=
match cmdline with
| Nil ->
List.iter (Map.data t.flags) ~f:(fun flag -> flag.check_available env);
( `Only_validate_parsing (Env.mem env key_internal_validate_parsing)
, Anons.Parser.final_value parser env
, List.concat (List.rev normalized_args) )
| Complete part ->
if parse_flags && String.is_prefix part ~prefix:"-"
then (
List.iter (Map.keys t.flags) ~f:(fun name ->
if String.is_prefix name ~prefix:part then print_endline name);
exit 0)
else Nothing.unreachable_code (Anons.Parser.complete parser env ~part)
| Cons (arg, args) ->
let arg, args, arg_is_flag =
match parse_flags with
| false -> arg, args, false
| true ->
(match arg, args with
| "-anon", Cons (arg, args) -> arg, args, false
| "-", _ -> arg, args, false
| _, _ -> arg, args, String.is_prefix arg ~prefix:"-")
in
(match arg_is_flag with
| true ->
let normalized_args = get_complete_flag_name t arg args :: normalized_args in
let env, args = run_flag t env arg args in
run_cmdline ~normalized_args t env parser args ~parse_flags ~for_completion
| false ->
let parse_flags1 = parse_flags in
let ({ parser; parse_flags = parse_flags2; update_env }
: _ Anons.Parser.Consume_result.t)
=
Anons.Parser.consume parser arg ~for_completion
in
let env = update_env env in
let parse_flags = parse_flags1 && parse_flags2 in
run_cmdline
~normalized_args:([ arg ] :: normalized_args)
t
env
parser
~parse_flags
args
~for_completion)
;;
let run_exn exn ~for_completion ~path ~verbose_on_parse_error =
match exn with
| Failed_to_parse_command_line _ when for_completion -> exit 0
| Exit_called { status } -> exit status
| _ ->
let exn_str =
match exn with
| Failed_to_parse_command_line msg -> msg
| _ -> Sexp.to_string_hum [%sexp (exn : exn)]
in
let verbose = Option.value verbose_on_parse_error ~default:true in
let error_msg =
if verbose
then
String.concat
~sep:"\n\n"
[ "Error parsing command line:"
; indent_by_2 exn_str
; "For usage information, run"
; " " ^ Path.to_string path ^ " -help\n"
]
else exn_str
in
prerr_endline error_msg;
exit 1
;;
let run
t
env
~when_parsing_succeeds
~path
~args
~verbose_on_parse_error
~help_text
~on_failure
=
let for_completion = Cmdline.ends_in_complete args in
let env =
env
|> Env.set ~key:path_key ~data:path
|> Env.set ~key:args_key ~data:(Cmdline.to_list args)
|> Env.set ~key:help_key ~data:help_text
in
match
Result.try_with (fun () ->
let is_using_validate_parsing, main, parsed_normalized_args =
run_cmdline
t
env
(t.anons ())
~for_completion
~parse_flags:true
~normalized_args:[]
args
in
normalized_path := Some path;
normalized_args := Some parsed_normalized_args;
is_using_validate_parsing, main `Parse_args)
with
| Ok (`Only_validate_parsing true, (_thunk : _)) ->
when_parsing_succeeds ();
exit 0
| Ok (`Only_validate_parsing false, thunk) ->
when_parsing_succeeds ();
thunk `Run_main
| Error exn -> on_failure exn ~for_completion ~path ~verbose_on_parse_error
;;
module Param = struct
type +'a t =
{ f : unit -> (unit -> 'a Parsing_outcome.t) Anons.Parser.Basic.t
; usage : unit -> Anons.Grammar.t
; flags : unit -> Flag.Internal.t list
}
open Anons.Parser.Basic.For_opening
let wrap_value v () = Parsing_outcome.return_no_arg v
let apply f x =
{ f =
(fun () ->
return (fun f x () ->
let f_outcome = f () in
let x_outcome = x () in
Parsing_outcome.apply f_outcome x_outcome)
<*> f.f ()
<*> x.f ())
; flags = (fun () -> x.flags () @ f.flags ())
; usage = (fun () -> Anons.Grammar.concat [ f.usage (); x.usage () ])
}
;;
let empty_spec : 'm. ('m -> 'm) t =
{ f = (fun () -> return (fun () -> Parsing_outcome.return_no_arg Fn.id))
; flags = (fun () -> [])
; usage = (fun () -> Anons.Grammar.zero)
}
;;
let map_outcome x ~f =
{ f =
(fun () ->
x.f ()
>>| fun x () ->
let x_outcome = x () in
f x_outcome)
; flags = x.flags
; usage = x.usage
}
;;
let map x ~f = map_outcome x ~f:(Parsing_outcome.map ~f)
let lookup key =
{ f =
(fun () ->
Anons.Parser.Basic.from_env (fun env -> Env.find_exn env key) >>| wrap_value)
; flags = (fun () -> [])
; usage = (fun () -> Anons.Grammar.zero)
}
;;
let path : Path.t t = lookup path_key
let args : string list t = lookup args_key
let help : string Lazy.t t = lookup help_key
let env =
{ f = (fun () -> Anons.Parser.Basic.from_env (fun env -> env) >>| wrap_value)
; flags = (fun () -> [])
; usage = (fun () -> Anons.Grammar.zero)
}
;;
include struct
module Arg_type = Arg_type
include Arg_type.Export
end
include struct
open Anons
let ( %: ) = ( %: )
let map_anons = map_anons
let maybe = maybe
let maybe_with_default = maybe_with_default
let sequence = sequence
let non_empty_sequence_as_pair = non_empty_sequence_as_pair
let non_empty_sequence_as_list = non_empty_sequence_as_list
let t2 = t2
let t3 = t3
let t4 = t4
let anon spec =
Anons.Grammar.invariant spec.grammar;
{ f = (fun () -> spec.p >>| fun outcome () -> outcome)
; flags = (fun () -> [])
; usage = (fun () -> spec.grammar)
}
;;
end
let escape_anon ~final_anon =
Anons.escape (t2 final_anon (sequence ("ARG" %: string))) |> anon
;;
include struct
open Flag
let map_flag = map_flag
let escape = escape
let escape_with_autocomplete = escape_with_autocomplete
let listed = listed
let one_or_more_as_pair = one_or_more_as_pair
let one_or_more_as_list = one_or_more_as_list
let no_arg = no_arg
let no_arg_required = no_arg_required
let no_arg_register = no_arg_register
let no_arg_abort = no_arg_abort
let no_arg_some = no_arg_some
let optional = optional
let optional_with_default = optional_with_default
let required = required
let flag_internal
?(aliases = [])
?full_flag_required
name
mode
~doc
~aliases_excluded_from_help
=
let normalize flag = normalize Key_type.Flag flag in
let name = normalize name in
let aliases = List.map ~f:normalize aliases in
let { read; action; num_occurrences; } = mode name in
let check_available =
match num_occurrences.at_least_once with
| false -> (ignore : Univ_map.t -> unit)
| true -> fun env -> ignore (read env : _)
in
let name_matching =
if Option.is_some full_flag_required then `Full_match_required else `Prefix
in
{ f =
(fun () ->
Anons.Parser.Basic.from_env (fun env -> read env) >>| fun v () -> v)
; flags =
(fun () ->
[ { name
; aliases
; aliases_excluded_from_help
; doc =
(match force extra_doc with
| Some -> [%string "%{doc} %{extra_doc}"]
| None -> doc)
; action
; num_occurrences
; check_available
; name_matching
}
])
; usage = (fun () -> Anons.Grammar.zero)
}
;;
let flag = flag_internal ~aliases_excluded_from_help:[]
let flag_optional_with_default_doc
?aliases
?full_flag_required
name
arg_type
sexp_of_default
~default
~doc
=
let doc =
match sexp_of_default default with
| Sexp.Atom "_" -> doc
| default_sexp -> sprintf !"%s (default: %{Sexp})" doc default_sexp
in
flag
?aliases
?full_flag_required
name
(optional_with_default default arg_type)
~doc
;;
end
let return v =
{ f = (fun () -> return (fun () -> Parsing_outcome.return_no_arg v))
; flags = (fun () -> [])
; usage = (fun () -> Anons.Grammar.zero)
}
;;
let recover_from_missing_required_flags t =
{ t with
f =
(fun () ->
t.f ()
>>| fun f () ->
let outcome = f () in
Parsing_outcome.recover_from_missing_required_flags outcome)
}
;;
let introduce_missing_required_flags t =
{ t with
f =
(fun () ->
t.f ()
>>| fun f () ->
let outcome = f () in
Parsing_outcome.introduce_missing_required_flags outcome)
}
;;
let optional_to_required t =
{ t with
f =
(fun () ->
t.f ()
>>| fun f () ->
let outcome = f () in
Parsing_outcome.introduce_missing_required_flags
(Parsing_outcome.map outcome ~f:(function
| None ->
Error
(`Missing_required_flags
(Error.of_string "[optional_to_required] got a [None] result"))
| Some v -> Ok v)))
}
;;
include Applicative.Make (struct
type nonrec 'a t = 'a t
let return = return
let apply = apply
let map = `Custom map
end)
let arg_names t =
let flags = Flag.Internal.create (t.flags ()) in
let flag_names = Map.keys flags in
let anon_names = Anons.Grammar.names (t.usage ()) in
List.concat [ flag_names; anon_names ]
;;
let required_arg_names t =
let flags = Flag.Internal.create (t.flags ()) in
List.filter_map (Map.to_alist flags) ~f:(fun (name, flag) ->
if flag.num_occurrences.at_least_once then Some name else None)
;;
module Choose_one = struct
type 'a param = 'a t
module Choice_name : sig
type t [@@deriving compare, sexp_of]
include Comparator.S with type t := t
val to_string : t -> string
val list_to_string : t list -> string
val create_exn : 'a param -> t
val enumerate_required_flags : t -> except:string -> string option
end = struct
module T = struct
type t =
{ all_args : string list
; required_args : string list
}
[@@deriving compare]
let sexp_of_t t = [%sexp (t.all_args : string list)]
end
include T
include Comparator.Make (T)
let create_exn param =
let required_args = required_arg_names param in
let names = arg_names param in
let names_with_commas = List.filter names ~f:(fun s -> String.contains s ',') in
if not (List.is_empty names_with_commas)
then
Error.create
~here:[%here]
"For simplicity, [Command.Spec.choose_one] does not support names with \
commas."
names_with_commas
[%sexp_of: string list]
|> Error.raise;
match names with
| [] ->
raise_s
[%message "[choose_one] expects choices to read command-line arguments."]
| _ :: _ -> { all_args = names; required_args }
;;
let to_string t =
match t.required_args with
| [] -> String.concat ~sep:"," t.all_args
| _ :: _ -> String.concat ~sep:"," t.required_args
;;
let enumerate_required_flags t ~except =
match List.filter t.required_args ~f:(fun x -> not (String.equal except x)) with
| [] -> None
| _ :: _ as l -> Some (String.concat ~sep:"," l)
;;
let list_to_string ts = List.map ts ~f:to_string |> String.concat ~sep:"\n "
end
module If_nothing_chosen = struct
type (_, _) t =
| Default_to : 'a -> ('a, 'a) t
| Raise : ('a, 'a) t
| Return_none : ('a, 'a option) t
end
let choose_one_non_optional
(type a b)
?(new_behavior = true)
(ts : a param list)
~(if_nothing_chosen : (a, b) If_nothing_chosen.t)
=
let fix_flag t =
if new_behavior
then (
let name_of_the_group = Choice_name.create_exn t in
let fix_num_occurrences flag =
{ flag with
Flag.Internal.num_occurrences =
{ flag.Flag.Internal.num_occurrences with at_least_once = false }
}
and fix_doc flag =
{ flag with
Flag.Internal.doc =
sprintf
"%s%s"
flag.Flag.Internal.doc
(match
Choice_name.enumerate_required_flags
~except:flag.name
name_of_the_group
with
| None -> ""
| Some group -> sprintf " [requires: \"%s\"]" group)
}
and make_anons_optional (anon : Anons.Grammar.t) =
Anons.Grammar.maybe_idempotent anon
in
{ t with
usage = (fun () -> make_anons_optional (t.usage ()))
; flags =
(fun () ->
List.map (t.flags ()) ~f:(fun flag_internal ->
flag_internal |> fix_num_occurrences |> fix_doc))
})
else t
in
match
List.map ts ~f:(fun t -> Choice_name.create_exn t, fix_flag t)
|> Map.of_alist (module Choice_name)
with
| `Duplicate_key name ->
Error.create
~here:[%here]
"[Command.Spec.choose_one] called with duplicate name"
name
[%sexp_of: Choice_name.t]
|> Error.raise
| `Ok ts ->
Map.fold ts ~init:(return []) ~f:(fun ~key:name ~data:t acc ->
map2
acc
(recover_from_missing_required_flags t)
~f:(fun acc { result = value; has_arg } ->
match has_arg with
| false -> acc
| true -> (name, value) :: acc))
|> map ~f:(fun value_list ->
let arg_counter = List.length value_list in
let missing_flag_error fmt =
ksprintf
(fun msg () -> Error (`Missing_required_flags (Error.of_string msg)))
fmt
in
let more_than_one_error passed =
die
!"Cannot pass more than one of these: \n\
\ %{Choice_name.list_to_string}"
(List.map passed ~f:fst)
()
and success_list, error_list =
List.partition_map value_list ~f:(function
| name, Ok value -> First (name, value)
| name, Error err -> Second (name, err))
in
match success_list with
| _ :: _ :: _ as passed -> more_than_one_error passed
| [ (_, (value : a)) ] ->
if arg_counter > 1
then more_than_one_error value_list
else
Ok
(match if_nothing_chosen with
| Default_to (_ : a) -> (value : b)
| Raise -> (value : b)
| Return_none -> (Some value : b))
| [] ->
(match error_list with
| [ (name, `Missing_required_flags err) ] ->
Error
(`Missing_required_flags
(Error.of_string
(sprintf
"Not all flags in group \"%s\" are given: %s"
(Choice_name.to_string name)
(Error.to_string_hum err))))
| _ ->
(match if_nothing_chosen with
| Default_to value -> Ok value
| Return_none -> Ok None
| Raise ->
missing_flag_error
!"Must pass one of these:\n %{Choice_name.list_to_string}"
(Map.keys ts)
())))
|> introduce_missing_required_flags
;;
let choose_one
(type a b)
(ts : a option param list)
~(if_nothing_chosen : (a, b) If_nothing_chosen.t)
=
choose_one_non_optional
~new_behavior:false
~if_nothing_chosen
(List.map ts ~f:(fun t ->
map_outcome t ~f:(fun { Parsing_outcome.result; has_arg } ->
match result with
| Ok (Some value) -> { Parsing_outcome.result = Ok value; has_arg = true }
| Ok None ->
{ has_arg = false
; result =
Error
(`Missing_required_flags (Error.of_string "missing required flag"))
}
| Error _ as result -> { has_arg; result })))
;;
end
module If_nothing_chosen = Choose_one.If_nothing_chosen
let choose_one = Choose_one.choose_one
let choose_one_non_optional lst ~if_nothing_chosen =
Choose_one.choose_one_non_optional lst ~if_nothing_chosen
;;
let and_arg_names t = map t ~f:(fun value -> value, arg_names t)
let and_arg_name t =
match arg_names t with
| [ name ] -> map t ~f:(fun value -> value, name)
| names ->
raise_s
[%message
"[and_arg_name] expects exactly one name, got" ~_:(names : string list)]
;;
let parse { flags; usage = _; f } args =
let cmdline = Cmdline.of_list args in
let result = ref None in
run
{ summary = ""
; readme = None
; flags = flags () |> Flag.Internal.create
; anons =
(fun () ->
let open Anons.Parser.Basic.For_opening in
f ()
>>| fun params `Parse_args `Run_main ->
let outcome = params () in
match outcome.result with
| Error (`Missing_required_flags err) -> result := Some (Error err)
| Ok x -> result := Some (Ok x))
; usage = Anons.Grammar.zero
}
Univ_map.empty
~when_parsing_succeeds:Fn.id
~args:cmdline
~path:Path.empty
~verbose_on_parse_error:(Some true)
~help_text:(lazy "No help for parsing")
~on_failure:
(fun
exn
~for_completion:(_ : bool)
~path:(_ : Path.t)
~verbose_on_parse_error:(_ : bool option)
-> result := Some (Error (Error.of_exn exn)));
Option.value_exn ~here:[%here] !result
;;
end
module Spec = struct
type ('a, 'b) t = ('a -> 'b) Param.t
type 'a param = 'a Param.t
let apply = Param.apply
let ( ++ ) t1 t2 = Param.map2 t1 t2 ~f:(fun f1 f2 x -> f2 (f1 x))
let ( +> ) t1 p2 = Param.map2 t1 p2 ~f:(fun f1 p2 x -> (f1 x) p2)
let ( +< ) t1 p2 = Param.map2 p2 t1 ~f:(fun p2 f1 x -> f1 (x p2))
let step f = Param.return f
let empty = Param.empty_spec
let const x = Param.return x
let map = Param.map
let wrap f t = Param.map t ~f:(fun run main -> f ~run ~main)
let of_param p = map p ~f:(fun f k -> k f)
let to_param t m = map t ~f:(fun f -> f m)
let path : Path.t param = Param.path
let args : string list param = Param.args
let help : string Lazy.t param = Param.help
include struct
module Arg_type = Arg_type
include Arg_type.Export
end
include struct
open Anons
type 'a anons = 'a t
let ( %: ) = ( %: )
let map_anons = map_anons
let maybe = maybe
let maybe_with_default = maybe_with_default
let non_empty_sequence_as_list = non_empty_sequence_as_list
let non_empty_sequence_as_pair = non_empty_sequence_as_pair
let sequence = sequence
let t2 = t2
let t3 = t3
let t4 = t4
let anon = Param.anon
end
let escape_anon = Param.escape_anon
include struct
open Flag
type 'a flag = 'a t
let map_flag = map_flag
let escape = escape
let escape_with_autocomplete = escape_with_autocomplete
let listed = listed
let one_or_more_as_pair = one_or_more_as_pair
let one_or_more_as_list = one_or_more_as_list
let no_arg = no_arg
let no_arg_required = no_arg_required
let no_arg_register = no_arg_register
let no_arg_abort = no_arg_abort
let no_arg_some = no_arg_some
let optional = optional
let optional_with_default = optional_with_default
let required = required
let flag = Param.flag
let flag_optional_with_default_doc = Param.flag_optional_with_default_doc
include Applicative.Make (struct
type nonrec 'a t = 'a Param.t
let return = Param.return
let apply = apply
let map = `Custom map
end)
let pair = Param.both
end
let flags_of_args_exn args =
List.fold args ~init:empty ~f:(fun acc (name, spec, doc) ->
let gen f flag_type =
step (fun m x ->
f x;
m)
+> Param.flag name flag_type ~doc
in
let call f arg_type = gen (fun x -> Option.iter x ~f) (Param.optional arg_type) in
let set r arg_type = call (fun x -> r := x) arg_type in
let set_bool r b = gen (fun passed -> if passed then r := b) Param.no_arg in
acc
++
match (spec : Stdlib.Arg.spec) with
| Unit f -> gen (fun passed -> if passed then f ()) Param.no_arg
| Set r -> set_bool r true
| Clear r -> set_bool r false
| String f -> call f string
| Set_string r -> set r string
| Int f -> call f int
| Set_int r -> set r int
| Float f -> call f float
| Set_float r -> set r float
| Bool f -> call f bool
| Symbol (syms, f) ->
let arg_type =
Arg_type.of_alist_exn
~list_values_in_help:false
(List.map syms ~f:(fun sym -> sym, sym))
in
call f arg_type
| Rest f -> gen (fun x -> Option.iter x ~f:(List.iter ~f)) Param.escape
| Tuple _ ->
failwith "Arg.Tuple is not supported by Command.Spec.flags_of_args_exn"
| ((Expand _) [@if ocaml_version >= (4, 05, 0)]) ->
failwith "Arg.Expand is not supported by Command.Spec.flags_of_args_exn"
| ((Rest_all _) [@if ocaml_version >= (4, 12, 0)]) ->
failwith "Arg.Rest_all is not supported by Command.Spec.flags_of_args_exn")
;;
module Deprecated = struct
include Flag.Deprecated
include Anons.Deprecated
end
let arg_names = Param.arg_names
module If_nothing_chosen = Param.Choose_one.If_nothing_chosen
let choose_one = Param.choose_one
let choose_one_non_optional = Param.choose_one_non_optional
let and_arg_names = Param.and_arg_names
let and_arg_name = Param.and_arg_name
end
end
module Group = struct
type 'a t =
{ summary : string
; readme : (unit -> string) option
; subcommands : (string * 'a) list Lazy.t
; body : (path:string list -> unit) option
}
let shape ~subcommand_to_shape t : _ Shape.Group_info.t =
{ summary = t.summary
; readme = Option.map ~f:(fun readme -> readme ()) t.readme
; subcommands = Lazy.map t.subcommands ~f:(List.Assoc.map ~f:subcommand_to_shape)
}
;;
end
let abs_path = Shape.Private.abs_path
let comp_cword = Env_var.COMP_CWORD
module Exec = struct
type t =
{ summary : string
; readme : (unit -> string) option
;
working_dir : string
; path_to_exe : string
; child_subcommand : string list
; env : env option
}
let shape t : Shape.Exec_info.t =
{ summary = t.summary
; readme = Option.map ~f:(fun readme -> readme ()) t.readme
; working_dir = t.working_dir
; path_to_exe = t.path_to_exe
; child_subcommand = t.child_subcommand
}
;;
end
module Proxy = struct
module Kind = struct
type 'a t =
| Base of Shape.Base_info.t
| Group of 'a Shape.Group_info.t
| Exec of Shape.Exec_info.t
| Lazy of 'a t Lazy.t
end
type t =
{ working_dir : string
; path_to_exe : string
; path_to_subcommand : string list
; child_subcommand : string list
; kind : t Kind.t
}
end
type t =
| Base of Command_base.t
| Group of t Group.t
| Exec of Exec.t
| Lazy of t Lazy.t
let rec sexpable_shape : t -> Shape.Sexpable.t = function
| Base base -> Base (Command_base.shape base)
| Exec exec -> Exec (Exec.shape exec)
| Group group -> Group (Group.shape ~subcommand_to_shape:sexpable_shape group)
| Lazy thunk -> Lazy (Lazy.map ~f:sexpable_shape thunk)
;;
type ('main, 'result) basic_spec_command =
summary:string
-> ?readme:(unit -> string)
-> ('main, unit -> 'result) Command_base.Spec.t
-> 'main
-> t
let extend_exn ~mem ~add map key_type ~key data =
if mem map key
then failwithf "there is already a %s named %s" (Key_type.to_string key_type) key ();
add map ~key ~data
;;
let extend_map_exn map key_type ~key data =
extend_exn map key_type ~key data ~mem:Map.mem ~add:Map.set
;;
let extend_alist_exn alist key_type ~key data =
extend_exn
alist
key_type
~key
data
~mem:(fun alist key -> List.Assoc.mem alist key ~equal:String.equal)
~add:(fun alist ~key ~data -> List.Assoc.add alist key data ~equal:String.equal)
;;
module Bailout_dump_flag = struct
let add base ~name ~aliases ~aliases_excluded_from_help ~text ~text_summary =
let flags = base.Command_base.flags in
let flags =
extend_map_exn
flags
Key_type.Flag
~key:name
{ name
; aliases_excluded_from_help
; aliases
; num_occurrences = Flag.Num_occurrences.at_most_once
; check_available = ignore
; action = Print_info_and_quit (fun env -> text env)
; doc = sprintf " print %s and exit" text_summary
; name_matching = `Prefix
}
in
{ base with flags }
;;
end
let basic ~summary ?readme { Command_base.Param.usage; flags; f } =
let flags = flags () in
let usage = usage () in
let anons () =
let open Anons.Parser.Basic.For_opening in
f ()
>>| fun params `Parse_args ->
let outcome = params () in
match outcome.result with
| Error (`Missing_required_flags err) -> die "%s" (Error.to_string_hum err) ()
| Ok thunk -> fun `Run_main -> thunk ()
in
let flags = Flag.Internal.create flags in
let base = { Command_base.summary; readme; usage; flags; anons } in
let base =
Bailout_dump_flag.add
base
~name:"-help"
~aliases:[ "-?" ]
~aliases_excluded_from_help:[ "--help" ]
~text_summary:"this help text"
~text:(fun env -> Lazy.force (Env.find_exn env Command_base.help_key))
in
Base base
;;
let basic_spec ~summary ?readme spec main =
basic ~summary ?readme (Command_base.Spec.to_param spec main)
;;
let subs_key : (string * t) list Env.Key.t = Env.key_create "subcommands"
let lazy_group ~summary ?readme ?preserve_subcommand_order ?body alist =
let subcommands =
Lazy.map alist ~f:(fun alist ->
let alist =
List.map alist ~f:(fun (name, t) -> normalize Key_type.Subcommand name, t)
in
match Map.of_alist (module String) alist with
| `Duplicate_key name -> failwithf "multiple subcommands named %s" name ()
| `Ok map ->
(match preserve_subcommand_order with
| Some () -> alist
| None -> Map.to_alist map))
in
Group { summary; readme; subcommands; body }
;;
let group ~summary ?readme ?preserve_subcommand_order ?body alist =
let readme = Option.map readme ~f:(fun f () -> String.strip (f ())) in
lazy_group ~summary ?readme ?preserve_subcommand_order ?body (Lazy.from_val alist)
;;
let exec ~summary ?readme ?(child_subcommand = []) ?env ~path_to_exe () =
let working_dir =
Filename_base.dirname
@@
match path_to_exe with
| `Absolute _ | `Relative_to_me _ -> Stdlib.Sys.executable_name
| `Relative_to_argv0 _ -> Stdlib.Sys.argv.(0)
in
let path_to_exe =
match path_to_exe with
| `Absolute p ->
if not (Filename_base.is_absolute p)
then failwith "Path passed to `Absolute must be absolute"
else p
| `Relative_to_me p | `Relative_to_argv0 p ->
if not (Filename_base.is_relative p)
then failwith "Path passed to `Relative_to_me must be relative"
else p
in
Exec { summary; readme; working_dir; path_to_exe; child_subcommand; env }
;;
let of_lazy thunk = Lazy thunk
let rec proxy_of_sexpable
sexpable
~working_dir
~path_to_exe
~child_subcommand
~path_to_subcommand
: Proxy.t
=
let kind =
kind_of_sexpable
sexpable
~working_dir
~path_to_exe
~child_subcommand
~path_to_subcommand
in
{ working_dir; path_to_exe; path_to_subcommand; child_subcommand; kind }
and kind_of_sexpable
sexpable
~working_dir
~path_to_exe
~child_subcommand
~path_to_subcommand
=
match (sexpable : Shape.Sexpable.t) with
| Base b -> Proxy.Kind.Base b
| Exec e -> Proxy.Kind.Exec e
| Lazy l ->
Proxy.Kind.Lazy
(Lazy.map l ~f:(fun sexpable ->
kind_of_sexpable
sexpable
~working_dir
~path_to_exe
~child_subcommand
~path_to_subcommand))
| Group g ->
Proxy.Kind.Group
{ g with
subcommands =
Lazy.map
g.subcommands
~f:
(List.map ~f:(fun (str, sexpable) ->
let path_to_subcommand = path_to_subcommand @ [ str ] in
let proxy =
proxy_of_sexpable
sexpable
~working_dir
~path_to_exe
~child_subcommand
~path_to_subcommand
in
str, proxy))
}
;;
module Version_info (Version_util : Version_util) = struct
let print_version ~version = print_endline (force version)
let print_build_info ~build_info = print_endline (force build_info)
let command ~version ~build_info =
basic
~summary:"print version information"
Command_base.Param.(
return (fun version_flag build_info_flag ->
if build_info_flag
then print_build_info ~build_info
else if version_flag
then print_version ~version
else (
print_build_info ~build_info;
print_version ~version);
exit 0)
<*> flag "-version" no_arg ~doc:" print the version of this build"
<*> flag "-build-info" no_arg ~doc:" print build info for this build")
;;
let rec add ~version ~build_info unversioned =
match unversioned with
| Base base ->
let base =
Bailout_dump_flag.add
base
~name:"-version"
~aliases:[]
~aliases_excluded_from_help:[ "--version" ]
~text_summary:"the version of this build"
~text:(fun _ -> force version)
in
let base =
Bailout_dump_flag.add
base
~name:"-build-info"
~aliases:[]
~aliases_excluded_from_help:[ "--build-info" ]
~text_summary:"info about this build"
~text:(fun _ -> force build_info)
in
Base base
| Group group ->
let subcommands =
Lazy.map group.Group.subcommands ~f:(fun subcommands ->
extend_alist_exn
subcommands
Key_type.Subcommand
~key:"version"
(command ~version ~build_info))
in
Group { group with Group.subcommands }
| Exec exec -> Exec exec
| Lazy thunk -> Lazy (lazy (add ~version ~build_info (Lazy.force thunk)))
;;
let normalize_version_lines lines =
String.concat ~sep:"\n" (List.sort lines ~compare:String.compare)
;;
let default_version = lazy (normalize_version_lines Version_util.version_list)
let default_build_info =
lazy
(Version_util.reprint_build_info Version_util.Time.sexp_of_t)
;;
end
let%test_module "Version_info" =
(module struct
module Version_info = Version_info (struct
let version_list = [ "hg://some/path_0xdeadbeef"; "ssh://a/path_8badf00d" ]
let reprint_build_info to_sexp = Sexp.to_string (to_sexp ())
module Time = struct
type t = unit [@@deriving sexp_of]
end
end)
let%expect_test "print version where multiple repos are used" =
Version_info.print_version ~version:Version_info.default_version;
[%expect
{|
hg://some/path_0xdeadbeef
ssh://a/path_8badf00d
|}]
;;
let%expect_test "print build info" =
Version_info.print_build_info ~build_info:(lazy "some build info");
[%expect {| some build info |}]
;;
end)
;;
let rec summary = function
| Base x -> x.summary
| Group x -> x.summary
| Exec x -> x.summary
| Lazy thunk -> summary (Lazy.force thunk)
;;
module Spec = struct
include Command_base.Spec
let path = map ~f:Path.parts_exe_basename path
end
module Deprecated = struct
module Spec = Spec.Deprecated
let summary = summary
let rec get_flag_names = function
| Base base -> base.Command_base.flags |> Map.keys
| Lazy thunk -> get_flag_names (Lazy.force thunk)
| Group _ | Exec _ -> assert false
;;
let help_recursive ~cmd ~with_flags ~expand_dots t s =
let rec help_recursive_rec ~cmd t s =
let new_s = s ^ (if expand_dots then cmd else ".") ^ " " in
match t with
| Lazy thunk ->
let t = Lazy.force thunk in
help_recursive_rec ~cmd t s
| Base base ->
let base_help = s ^ cmd, summary (Base base) in
if with_flags
then
base_help
:: List.map
~f:(fun (flag, h) -> new_s ^ flag, h)
(List.sort
~compare:Command_base.Deprecated.subcommand_cmp_fst
(Command_base.Deprecated.flags_help ~display_help_flags:false base))
else [ base_help ]
| Group { summary; subcommands; readme = _; body = _ } ->
(s ^ cmd, summary)
:: (Lazy.force subcommands
|> List.sort ~compare:Command_base.Deprecated.subcommand_cmp_fst
|> List.concat_map ~f:(fun (cmd', t) -> help_recursive_rec ~cmd:cmd' t new_s)
)
| Exec _ ->
[]
in
help_recursive_rec ~cmd t s
;;
end
let autocomplete_function ~argv_0 ~pid =
let fname =
sprintf "_jsautocom_%010d" pid
in
sprintf
"function %s {\n\
\ export COMP_CWORD\n\
\ COMP_WORDS[0]=%s\n\
\ if type readarray > /dev/null\n\
\ then readarray -t COMPREPLY < <(\"${COMP_WORDS[@]}\")\n\
\ else IFS=\"\n\
\" read -d \"\" -A COMPREPLY < <(\"${COMP_WORDS[@]}\")\n\
\ fi\n\
}\n\
complete -F %s %s\n\
%!"
fname
argv_0
fname
argv_0
;;
let%expect_test "Demonstrate [autocomplete_function]" =
autocomplete_function ~argv_0:"<argv_0>" ~pid:12345 |> print_endline;
[%expect
{|
function _jsautocom_0000012345 {
export COMP_CWORD
COMP_WORDS[0]=<argv_0>
if type readarray > /dev/null
then readarray -t COMPREPLY < <("${COMP_WORDS[@]}")
else IFS="
" read -d "" -A COMPREPLY < <("${COMP_WORDS[@]}")
fi
}
complete -F _jsautocom_0000012345 <argv_0>
|}]
;;
module For_unix (For_unix_with_string_env_var : For_unix with type env_var := string) =
struct
module Version_info = Version_info (For_unix_with_string_env_var.Version_util)
module For_unix_with_command_env_var : For_unix with type env_var := Env_var.t = struct
include For_unix_with_string_env_var
module Unix = struct
include Unix
let putenv ~key ~data = putenv ~key:(Env_var.to_string key) ~data
let unsetenv key = unsetenv (Env_var.to_string key)
let unsafe_getenv key = unsafe_getenv (Env_var.to_string key)
let convert_env env =
let convert_command_env_var_to_string list =
List.map list ~f:(fun (env_var, str) -> Env_var.to_string env_var, str)
in
match env with
| `Replace list -> `Replace (convert_command_env_var_to_string list)
| `Extend list -> `Extend (convert_command_env_var_to_string list)
| `Override list -> `Override (convert_command_env_var_to_string list)
| `Replace_raw _ as replace -> replace
;;
let exec ~prog ~argv ?use_path ?env () =
exec ~prog ~argv ?use_path ?env:(Option.map env ~f:convert_env) ()
;;
let create_process_env ?working_dir ?prog_search_path ?argv0 ~prog ~args ~env () =
create_process_env
?working_dir
?prog_search_path
?argv0
~prog
~args
~env:(convert_env env)
()
;;
end
end
open For_unix_with_command_env_var
let getenv_and_clear var =
let value = Unix.unsafe_getenv var in
if Option.is_some value then Unix.unsetenv var;
value
;;
let maybe_comp_cword () = getenv_and_clear comp_cword |> Option.map ~f:Int.of_string
let set_comp_cword new_value =
let new_value = Int.to_string new_value in
Unix.putenv ~key:comp_cword ~data:new_value
;;
module Exec = struct
include Exec
let exec_with_args t ~args ~maybe_new_comp_cword =
let prog = abs_path ~dir:t.working_dir t.path_to_exe in
let args = t.child_subcommand @ args in
let env = t.env in
Option.iter maybe_new_comp_cword ~f:(fun n ->
set_comp_cword (n + List.length t.child_subcommand));
Nothing.unreachable_code
(For_unix_with_string_env_var.Unix.exec ?env ~prog ~argv:(prog :: args) ())
;;
end
module Sexpable = struct
include Shape.Sexpable
let read_stdout_and_stderr (process_info : Unix.Process_info.t) =
let start_reading descr info =
let output = ref None in
let thread =
Thread.create
~on_uncaught_exn:`Print_to_stderr
(fun () ->
let result =
Result.try_with (fun () ->
descr |> Unix.in_channel_of_descr |> In_channel.input_all)
in
output := Some result)
()
in
Staged.stage (fun () ->
Thread.join thread;
Unix.close descr;
match !output with
| None -> raise_s [%message "BUG failed to read" (info : Info.t)]
| Some (Ok output) -> output
| Some (Error exn) -> raise exn)
in
let finish_stdout = start_reading process_info.stdout (Info.of_string "stdout") in
let finish_stderr = start_reading process_info.stderr (Info.of_string "stderr") in
Staged.unstage finish_stdout (), Staged.unstage finish_stderr ()
;;
let of_external ~working_dir ~path_to_exe ~child_subcommand =
let process_info =
Unix.create_process_env
()
~prog:(abs_path ~dir:working_dir path_to_exe)
~args:child_subcommand
~env:
(let help_sexp =
supported_versions |> Set.sexp_of_m__t (module Int) |> Sexp.to_string
in
`Extend [ COMMAND_OUTPUT_HELP_SEXP, help_sexp ])
in
Unix.close process_info.stdin;
let stdout, stderr = read_stdout_and_stderr process_info in
Unix.wait process_info.pid;
match stdout |> Sexplib.Sexp.of_string |> Versioned.t_of_sexp |> of_versioned with
| exception exn ->
raise_s
[%message
"cannot parse command shape"
~_:(exn : exn)
(stdout : string)
(stderr : string)]
| t -> t
;;
let rec find (t : t) ~path_to_subcommand =
match path_to_subcommand with
| [] -> t
| sub :: subs ->
if String.is_prefix sub ~prefix:"-"
then t
else (
match t with
| Base _ -> failwithf "unexpected subcommand %S" sub ()
| Lazy thunk -> find (Lazy.force thunk) ~path_to_subcommand
| Exec { path_to_exe; working_dir; child_subcommand; _ } ->
find
(of_external ~working_dir ~path_to_exe ~child_subcommand)
~path_to_subcommand:(sub :: (subs @ child_subcommand))
| Group g ->
(match List.Assoc.find (Lazy.force g.subcommands) ~equal:String.equal sub with
| None -> failwithf "unknown subcommand %S" sub ()
| Some t -> find t ~path_to_subcommand:subs))
;;
end
let proxy_of_exe ~working_dir path_to_exe child_subcommand =
Sexpable.of_external ~working_dir ~path_to_exe ~child_subcommand
|> proxy_of_sexpable
~working_dir
~path_to_exe
~child_subcommand
~path_to_subcommand:[]
;;
let rec shape_of_proxy proxy : Shape.t = shape_of_proxy_kind proxy.Proxy.kind
and shape_of_exe () ~child_subcommand ~path_to_exe ~working_dir =
shape_of_proxy (proxy_of_exe ~working_dir path_to_exe child_subcommand)
and shape_of_proxy_kind kind =
match kind with
| Base b -> Basic b
| Lazy l -> Lazy (Lazy.map ~f:shape_of_proxy_kind l)
| Group g ->
Group
{ g with
subcommands = Lazy.map g.subcommands ~f:(List.Assoc.map ~f:shape_of_proxy)
}
| Exec ({ child_subcommand; path_to_exe; working_dir; _ } as e) ->
Exec (e, shape_of_exe ~child_subcommand ~path_to_exe ~working_dir)
;;
let rec shape t : Shape.t =
match t with
| Base b -> Basic (Command_base.shape b)
| Group g -> Group (Group.shape ~subcommand_to_shape:shape g)
| Exec ({ Exec.child_subcommand; path_to_exe; working_dir; _ } as e) ->
Exec (Exec.shape e, shape_of_exe ~child_subcommand ~path_to_exe ~working_dir)
| Lazy thunk -> shape (Lazy.force thunk)
;;
let gather_help ~recursive ~flags ~expand_dots shape =
let rec loop path acc shape =
let string_of_path = if expand_dots then Path.to_string else Path.to_string_dots in
let gather_group path acc subcommands =
let filtered_subcommands =
if Path.is_empty path
then subcommands
else List.Assoc.remove ~equal:String.( = ) subcommands "help"
in
filtered_subcommands
|> List.stable_sort ~compare:(fun a b -> help_screen_compare (fst a) (fst b))
|> List.fold ~init:acc ~f:(fun acc (subcommand, shape) ->
let path = Path.append path ~subcommand in
let name = string_of_path path in
let doc = Shape.get_summary shape in
let acc = { Shape.Flag_info.name; doc; aliases = [] } :: acc in
if recursive then loop path acc shape else acc)
in
match shape with
| Exec (_, shape) ->
(try loop path acc (shape ()) with
| _ -> acc)
| Group g -> gather_group path acc (Lazy.force g.subcommands)
| Basic b ->
if flags
then
b.flags
|> List.filter ~f:(fun fmt -> String.( <> ) fmt.name "[-help]")
|> List.fold ~init:acc ~f:(fun acc fmt ->
let path = Path.append path ~subcommand:fmt.name in
let fmt = { fmt with name = string_of_path path } in
fmt :: acc)
else acc
| Lazy thunk -> loop path acc (Lazy.force thunk)
in
loop Path.empty [] shape |> List.rev
;;
let group_or_exec_help_text ~flags ~path ~summary ~readme ~format_list =
unparagraphs
(List.filter_opt
[ Some summary
; Some (String.concat [ " "; Path.to_string path; " SUBCOMMAND" ])
; readme
; Some (if flags then "=== subcommands and flags ===" else "=== subcommands ===")
; Some (Shape.Flag_help_display.to_string format_list)
])
;;
let rec help_for_shape shape path ~expand_dots ~flags ~recursive =
let format_list = gather_help ~expand_dots ~flags ~recursive shape in
match shape with
| Basic b ->
let usage = Shape.Base_info.get_usage b in
unparagraphs
(List.filter_opt
[ Some b.summary
; Some (" " ^ Path.to_string path ^ " " ^ usage)
; b.readme
; Some "=== flags ==="
; Some (Shape.Flag_help_display.to_string b.flags)
])
| Group g ->
group_or_exec_help_text
~flags
~path
~readme:g.readme
~summary:g.summary
~format_list
| Exec (e, _) ->
group_or_exec_help_text
~flags
~path
~readme:e.readme
~summary:e.summary
~format_list
| Lazy thunk -> help_for_shape (Lazy.force thunk) path ~expand_dots ~flags ~recursive
;;
let help_subcommand ~summary ~readme =
basic
~summary:"explain a given subcommand (perhaps recursively)"
Command_base.Param.(
return (fun recursive flags expand_dots path (env : Env.t) cmd_opt () ->
let subs =
match Env.find env subs_key with
| Some subs -> subs
| None -> assert false
in
let path =
let path = Path.pop_help path in
Option.fold cmd_opt ~init:path ~f:(fun path subcommand ->
Path.append path ~subcommand)
in
let path, shape =
match cmd_opt with
| None ->
let subcommands = List.Assoc.map subs ~f:shape |> Lazy.from_val in
let readme = Option.map readme ~f:(fun readme -> readme ()) in
path, Shape.Group { readme; summary; subcommands }
| Some cmd ->
(match
lookup_expand
(List.Assoc.map subs ~f:(fun x -> x, `Prefix))
cmd
Subcommand
with
| Error e ->
die
"unknown subcommand %s for command %s: %s"
cmd
(Path.to_string path)
e
()
| Ok (possibly_expanded_name, t) ->
let path =
Path.replace_first ~from:cmd ~to_:possibly_expanded_name path
in
path, shape t)
in
print_endline (help_for_shape shape path ~recursive ~flags ~expand_dots))
<*> flag "-recursive" no_arg ~doc:" show subcommands of subcommands, etc."
<*> flag "-flags" no_arg ~doc:" show flags as well in recursive help"
<*> flag "-expand-dots" no_arg ~doc:" expand subcommands in recursive help"
<*> path
<*> env
<*> anon (maybe ("SUBCOMMAND" %: string)))
;;
let dump_autocomplete_function () =
autocomplete_function ~argv_0:Stdlib.Sys.argv.(0) ~pid:(Unix.getpid () |> Pid.to_int)
|> printf "%s"
;;
let dump_help_sexp ~supported_versions t ~path_to_subcommand =
Set.inter Sexpable.supported_versions supported_versions
|> Set.max_elt
|> function
| None ->
Error.create
~here:[%here]
"Couldn't choose a supported help output version for Command.exec from the given \
supported versions."
Sexpable.supported_versions
(Set.sexp_of_m__t (module Int))
|> Error.raise
| Some version_to_use ->
sexpable_shape t
|> Sexpable.find ~path_to_subcommand
|> Sexpable.to_versioned ~version_to_use
|> Sexpable.Versioned.sexp_of_t
|> Sexp.to_string
|> print_string
;;
let handle_environment t ~argv =
match argv with
| [] -> failwith "missing executable name"
| cmd :: args ->
Option.iter (getenv_and_clear COMMAND_OUTPUT_HELP_SEXP) ~f:(fun version ->
let supported_versions =
Sexplib.Sexp.of_string version |> Set.m__t_of_sexp (module Int)
in
dump_help_sexp ~supported_versions t ~path_to_subcommand:args;
exit 0);
Option.iter (getenv_and_clear COMMAND_OUTPUT_INSTALLATION_BASH) ~f:(fun _ ->
dump_autocomplete_function ();
exit 0);
cmd, args
;;
let process_args ~cmd ~args =
let maybe_comp_cword = maybe_comp_cword () in
let args =
match maybe_comp_cword with
| None -> Cmdline.of_list args
| Some comp_cword ->
let args = List.take (args @ [ "" ]) comp_cword in
List.fold_right args ~init:Cmdline.Nil ~f:(fun arg args ->
match args with
| Cmdline.Nil -> Cmdline.Complete arg
| _ -> Cmdline.Cons (arg, args))
in
Path.create ~path_to_exe:cmd, args, maybe_comp_cword
;;
module Only_validate_parsing = struct
let flag base =
let name = "-validate-parsing" in
let flags = base.Command_base.flags in
let flags =
extend_map_exn
flags
Key_type.Flag
~key:name
{ name
; aliases_excluded_from_help = [ "--validate-parsing" ]
; aliases = []
; num_occurrences = Flag.Num_occurrences.at_most_once
; check_available = ignore
; action =
No_arg (fun env -> Env.set ~key:key_internal_validate_parsing ~data:() env)
; doc = " validate arguments are parsed correctly and exit immediately"
; name_matching = `Prefix
}
in
{ base with flags }
;;
let rec add = function
| Base base -> Base (flag base)
| Exec _ as t -> t
| Group { summary; readme; subcommands; body } ->
let subcommands =
Lazy.map subcommands ~f:(fun subcommands ->
List.map subcommands ~f:(fun (name, command) -> name, add command))
in
Group { summary; readme; subcommands; body }
| Lazy thunk -> Lazy (lazy (add (Lazy.force thunk)))
;;
end
let rec add_help_subcommands = function
| Base _ as t -> t
| Exec _ as t -> t
| Group { summary; readme; subcommands; body } ->
let subcommands =
Lazy.map subcommands ~f:(fun subcommands ->
extend_alist_exn
(List.Assoc.map subcommands ~f:add_help_subcommands)
Key_type.Subcommand
~key:"help"
(help_subcommand ~summary ~readme))
in
Group { summary; readme; subcommands; body }
| Lazy thunk -> Lazy (lazy (add_help_subcommands (Lazy.force thunk)))
;;
let maybe_apply_extend args ~extend ~path =
Option.value_map extend ~default:args ~f:(fun f ->
Cmdline.extend args ~extend:f ~path)
;;
let rec dispatch
t
env
~extend
~path
~args
~maybe_new_comp_cword
~version
~build_info
~verbose_on_parse_error
~when_parsing_succeeds
~complete_subcommands
=
match t with
| Lazy thunk ->
let t = Lazy.force thunk in
dispatch
t
env
~extend
~path
~args
~maybe_new_comp_cword
~version
~build_info
~verbose_on_parse_error
~when_parsing_succeeds
~complete_subcommands
| Base base ->
let args = maybe_apply_extend args ~extend ~path in
let help_text =
lazy
(help_for_shape (shape t) path ~recursive:false ~flags:true ~expand_dots:false)
in
Command_base.run
base
env
~path
~args
~verbose_on_parse_error
~help_text
~when_parsing_succeeds
~on_failure:Command_base.run_exn
| Exec exec ->
let args = Cmdline.to_list (maybe_apply_extend args ~extend ~path) in
Exec.exec_with_args ~args exec ~maybe_new_comp_cword
| Group ({ summary; readme; subcommands = subs; body } as group) ->
let completing = Cmdline.ends_in_complete args in
let env = Env.set env ~key:subs_key ~data:(Lazy.force subs) in
let die_showing_help msg =
if completing
then exit 0
else (
eprintf
"%s\n%!"
(help_for_shape
~recursive:false
~flags:false
~expand_dots:false
(shape (Group { summary; readme; subcommands = subs; body }))
path);
die "%s" msg ())
in
let rec parse_group args ~maybe_new_comp_cword =
let maybe_new_comp_cword = Option.map ~f:Int.pred maybe_new_comp_cword in
let skip rest = parse_group rest ~maybe_new_comp_cword in
let resolve sub rest =
let subs = List.Assoc.map (Lazy.force subs) ~f:(fun x -> x, `Prefix) in
match lookup_expand subs sub Subcommand with
| Error msg -> die_showing_help msg
| Ok (sub, t) ->
dispatch
t
env
~when_parsing_succeeds
~extend
~path:(Path.append path ~subcommand:sub)
~args:rest
~maybe_new_comp_cword
~version
~build_info
~verbose_on_parse_error
~complete_subcommands
in
match (args : Cmdline.t) with
| Nil ->
(match body with
| None ->
die_showing_help
(sprintf "missing subcommand for command %s" (Path.to_string path))
| Some body -> body ~path:(Path.parts_exe_basename path))
| Cons (sub, rest) ->
(match sub with
| ("-version" | "--version") when Path.length path = 1 ->
if completing
then skip rest
else (
Version_info.print_version ~version;
exit 0)
| ("-build-info" | "--build-info") when Path.length path = 1 ->
if completing
then skip rest
else (
Version_info.print_build_info ~build_info;
exit 0)
| "-help" | "--help" ->
if completing
then skip rest
else (
match rest with
| Nil | Complete (_ : string) ->
print_endline
(help_for_shape
~recursive:false
~flags:false
~expand_dots:false
(shape (Group { group with subcommands = subs }))
path);
exit 0
| Cmdline.Cons (first_of_rest, rest_of_rest) ->
resolve first_of_rest (Cons (sub, rest_of_rest)))
| (_ : string) -> resolve sub rest)
| Complete part ->
let subs =
Lazy.force subs
|> List.map ~f:fst
|> List.filter ~f:(fun name -> String.is_prefix name ~prefix:part)
|> List.sort ~compare:String.compare
in
(match complete_subcommands with
| Some f ->
let subcommands =
shape t |> Shape.fully_forced |> Shape.Fully_forced.expanded_subcommands
in
(match f ~path:(Path.parts path) ~part subcommands with
| None -> exit 1
| Some to_output ->
print_endline (String.concat ~sep:" " to_output);
exit 0)
| None ->
List.iter subs ~f:print_endline;
exit 0)
in
parse_group args ~maybe_new_comp_cword
;;
let run
?(add_validate_parsing_flag = false)
?verbose_on_parse_error
?version
?build_info
?(argv = Array.to_list Stdlib.Sys.argv)
?extend
?(when_parsing_succeeds = Fn.id)
?complete_subcommands
t
=
let build_info =
match build_info with
| Some v -> lazy v
| None -> Version_info.default_build_info
in
let version =
match version with
| None -> Version_info.default_version
| Some v ->
lazy
(Version_info.normalize_version_lines
(String.split v ~on:' ' |> List.concat_map ~f:(String.split ~on:'\n')))
in
Exn.handle_uncaught_and_exit (fun () ->
let t = Version_info.add t ~version ~build_info in
let t = add_help_subcommands t in
let t = if add_validate_parsing_flag then Only_validate_parsing.add t else t in
let cmd, args = handle_environment t ~argv in
let path, args, maybe_new_comp_cword = process_args ~cmd ~args in
try
dispatch
t
Env.empty
~extend
~path
~args
~maybe_new_comp_cword
~version
~build_info
~verbose_on_parse_error
~when_parsing_succeeds
~complete_subcommands
with
| Failed_to_parse_command_line msg ->
if Cmdline.ends_in_complete args
then exit 0
else (
prerr_endline msg;
exit 1))
;;
let deprecated_run t ~cmd ~args ~is_help ~is_help_rec ~is_help_rec_flags ~is_expand_dots
=
let path_strings = String.split cmd ~on:' ' in
let path = Path.of_parts path_strings in
let args = if is_expand_dots then "-expand-dots" :: args else args in
let args = if is_help_rec_flags then "-flags" :: args else args in
let args = if is_help_rec then "-r" :: args else args in
let args = if is_help then "-help" :: args else args in
let args = Cmdline.of_list args in
let t = add_help_subcommands t in
dispatch
t
Env.empty
~path
~args
~extend:None
~maybe_new_comp_cword:None
~version:Version_info.default_version
~build_info:Version_info.default_build_info
~verbose_on_parse_error:None
~when_parsing_succeeds:Fn.id
~complete_subcommands:None
;;
end
module Param = struct
module type S = sig
type +'a t
include Applicative.S with type 'a t := 'a t
val help : string Lazy.t t
val path : string list t
val args : string list t
val flag
: ?aliases:string list
-> ?full_flag_required:unit
-> string
-> 'a Flag.t
-> doc:string
-> 'a t
val flag_optional_with_default_doc
: ?aliases:string list
-> ?full_flag_required:unit
-> string
-> 'a Arg_type.t
-> ('a -> Sexp.t)
-> default:'a
-> doc:string
-> 'a t
val anon : 'a Anons.t -> 'a t
val escape_anon : final_anon:'a Anons.t -> ('a * string list) t
module If_nothing_chosen : sig
type (_, _) t =
| Default_to : 'a -> ('a, 'a) t
| Raise : ('a, 'a) t
| Return_none : ('a, 'a option) t
end
val choose_one
: 'a option t list
-> if_nothing_chosen:('a, 'b) If_nothing_chosen.t
-> 'b t
val choose_one_non_optional
: 'a t list
-> if_nothing_chosen:('a, 'b) If_nothing_chosen.t
-> 'b t
val and_arg_names : 'a t -> ('a * string list) t
val and_arg_name : 'a t -> ('a * string) t
val arg_names : 'a t -> string list
end
include Command_base.Param
let path = map ~f:Path.parts_exe_basename path
end
module Let_syntax = struct
include Param
module Let_syntax = struct
include Param
module Open_on_rhs = Param
end
end
type 'result basic_command =
summary:string -> ?readme:(unit -> string) -> (unit -> 'result) Param.t -> t
let basic ~summary ?readme param =
let readme = Option.map readme ~f:(fun f () -> String.strip (f ())) in
basic ~summary ?readme param
;;
let basic_or_error ~summary ?readme param =
basic
~summary
?readme
(let%map run = param in
fun () ->
match run () with
| Ok () -> ()
| Error e ->
Stdio.prerr_endline (Error.to_string_hum e);
exit 1)
;;
module For_telemetry = struct
let normalized_path () = Option.map !Command_base.normalized_path ~f:Path.parts
let normalized_args () = !Command_base.normalized_args
end
module Private = struct
let abs_path = abs_path
let word_wrap = Shape.Private.word_wrap
module Anons = Anons
module Cmdline = Cmdline
module For_unix = For_unix
module Path = Path
module Spec = struct
include Spec
let to_string_for_choose_one param =
Command_base.Param.Choose_one.Choice_name.(create_exn param |> to_string)
;;
end
end
let run = `Use_Command_unix
let shape = `Use_Command_unix