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
//! #### Project documentation
//!
//! See [official website](https://pacak.github.io/bpaf/bpaf/_documentation/index.html) for more up to date version.
//!
//! - [Introduction and design goals](_0_intro) - A quick intro. What, why and how
//! - [Tutorials](_1_tutorials) - practical, learning oriented guides
//! - [HOWTO - practical, oriented to solving problems guides](_2_howto)
//! - [Parsing cookbook](_3_cookbook) - How to parse less frequent combinations
//! - [Theory explanation](_4_explanation) - Theoretical information about abstractions used by the library, oriented for understanding
//!
pub mod _0_intro {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Project documentation ↑](super::super::_documentation)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Tutorials →](super::_1_tutorials)
//!
//! </td>
//! </tr></table>
//!
//! #### Introduction and design goals
//! A quick intro. What, why and how
//!
//! `bpaf` is a lightweight and flexible command line parser that uses both combinatoric and derive
//! style API
//!
//! Combinatoric API usually means a bit more typing but no dependency on proc macros and more help
//! from the IDE, derive API uses proc macro to save on typing but your IDE will be less likely to
//! help you. Picking one API style does not lock you out from using the other style, you can mix
//! and match both in a single parser
//!
//! # Examples of both styles
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/intro.md"))]
//!
//! # Design goals
//!
//! ## Parse, don't validate
//!
//! `bpaf` tries hard to let you move as many invariants about the user input you are
//! trying to parse into rust types: for mutually exclusive options you can get `enum` with
//! exclusive items going into separate branches, and you can collect results into types like
//! [`BTreeSet`](std::collections::BTreeSet), or whatever custom type you might have with
//! custom parsing. Ideas for
//! [making invalid states unrepresentable](https://geeklaunch.io/blog/make-invalid-states-unrepresentable/)
//! and [using parsing over validation](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/)
//! are not new.
//!
//! That said you can also validate your inputs if this fits your situation better. If you want to
//! ensure that the sum of every numeric field must be divisible by both 3 and 5, but only when it's
//! Thursday - you can do that too.
//!
//! ## Flexibility
//!
//! While aiming to be a general-purpose command line parser `bpaf` offers a few backdoors that
//! allow you to parse pretty much anything you want: chained commands, custom blocks of options,
//! DOS-style options (`/ofile.pas`), `dd` style options (`if=file of=out`), etc. A similar idea applies
//! to what the parser can produce - if your app operates with boxed string slices internally - `bpaf`
//! will give you `Box<str>` instead of `String` if you ask it to.
//!
//! The only restriction is that you cannot use information from items parsed earlier (but not
//! the fact that something was parsed successfully or not) to decide to how to parse further
//! options, and even then you can side step this restriction by passing some shared state as a
//! parameter to the parsers.
//!
//! ## Reusability
//!
//! Parsers in `bpaf` are not monolithic and you can share their parts across multiple binaries,
//! workspace members or even independent projects. Say you have multiple binaries in a workspace
//! that perform different operations on some input. You can declare a parser for the input
//! specifically, along with all the validations, help messages or shell dynamic completion
//! functions you need and use it across all the binaries alongside the arguments specific to
//! those binaries.
//!
//! ## Composition, transformation
//!
//! Parsers in `bpaf` are not finalized either, say you have a parser that describes a single input
//! for your program, it can take multiple arguments or perform extra validations, etc. You can
//! always compose this parser with any other parser to produce tuples of both results for example.
//! Or to make it so parser runs multiple times and collects results into a `Vec`.
//!
//! ## Performance
//!
//! While performance is an explicit non-goal - `bpaf` does nothing that would pessimize it either,
//! so performance is on par or better compared to other fully featured parsers.
//!
//! ## Correctness
//!
//! `bpaf` would parse only items it can represent and will reject anything it cannot represent
//! in the output. Say your parser accepts both `--intel` and `--att` flags, but encodes the result
//! into `enum Style { Intel, Att }`, `bpaf` will accept those flags separately, but not if they
//! are used both at once. If the parser later collects multiple styles into a `Vec<Style>` then it
//! will accept any combinationof those flags.
//!
//! ## User friendly
//!
//! `bpaf` tries to provide user-friendly error messages, and suggestions for typos but also scripts
//! for shell completion, `man` pages and markdown documentation for the web.
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Project documentation ↑](super::super::_documentation)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Tutorials →](super::_1_tutorials)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _1_tutorials {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Introduction and design goals](super::_0_intro)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Project documentation ↑](super::super::_documentation)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [HOWTO - practical, oriented to solving problems guides →](super::_2_howto)
//!
//! </td>
//! </tr></table>
//!
//! #### Tutorials
//! practical, learning oriented guides
//!
//! - [Types of arguments](_0_types_of_arguments) - common types of line options and conventions
//! - [Combinatoric API](_1_combinatoric_api) - Parse arguments without using proc macros
//! - [Derive API tutorial](_2_derive_api) - Create a parser by defining a structure
//! - [Designing a good datatype](_3_picking_type) - bpaf allows you to reduce the size of legal values to valid ones
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Introduction and design goals](super::_0_intro)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Project documentation ↑](super::super::_documentation)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [HOWTO - practical, oriented to solving problems guides →](super::_2_howto)
//!
//! </td>
//! </tr></table>
//!
pub mod _0_types_of_arguments {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Tutorials ↑](super::super::_1_tutorials)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Combinatoric API →](super::_1_combinatoric_api)
//!
//! </td>
//! </tr></table>
//!
//! #### Types of arguments
//! common types of line options and conventions
//!
//! This chapter serves as an introduction to available command line options and tries to set the
//! terminology. If you are familiar with command line argument parsers in general - feel free to
//! skip it.
//!
//! If you ever used any software from a command line (say `cargo`) you used command line options.
//! Let's recap how you might run tests for a crate in your rust project:
//!
//! <div class="code-wrap">
//! <pre>
//! $ cargo test -p my_project --verbose
//! </pre>
//! </div>
//!
//! `cargo` here is an executable name, everything to the right of it separated by spaces are the
//! options.
//!
//! Nowadays programs share mostly similar conventions about what a command line argument is, it
//! wasn't the case before though. Let's cover the basic types.
//!
//! - [Options, switches or flags](_0_switch)
//! - [Option arguments or arguments](_1_argument)
//! - [Operands or positional items](_2_positional)
//! - [Commands or subcommands](_3_command)
//! - [Exotic schemas](_4_exotic)
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Tutorials ↑](super::super::_1_tutorials)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Combinatoric API →](super::_1_combinatoric_api)
//!
//! </td>
//! </tr></table>
//!
pub mod _0_switch {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Types of arguments ↑](super::super::_0_types_of_arguments)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Option arguments or arguments →](super::_1_argument)
//!
//! </td>
//! </tr></table>
//!
//! #### Options, switches or flags
//!
//! Options or flags usually starts with a dash, a single dash for short options and a double dash for
//! long one. Several short options can usually be squashed together with a single dash in front of
//! them to save on typing: `-vvv` can be parsed the same as `-v -v -v`. Options don't have any
//! other information apart from being there or not. Relative position usually does not matter and
//! `--alpha --beta` should parse the same as `--beta --alpha`.
//!
//! <div class="code-wrap">
//! <pre>
//! $ cargo <span style="font-weight: bold">--help</span>
//! $ ls <span style="font-weight: bold">-la</span>
//! $ ls <span style="font-weight: bold">--time --reverse</span>
//! </pre>
//! </div>
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/switch.md"))]
//!
//! For more detailed info see [`NamedArg::switch`] and
//! [`NamedArg::flag`]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Types of arguments ↑](super::super::_0_types_of_arguments)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Option arguments or arguments →](super::_1_argument)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _1_argument {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Options, switches or flags](super::_0_switch)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Types of arguments ↑](super::super::_0_types_of_arguments)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Operands or positional items →](super::_2_positional)
//!
//! </td>
//! </tr></table>
//!
//! #### Option arguments or arguments
//!
//! Option arguments are similar to regular options but they come with an extra value attached.
//! Value can be separated by a space, `=` or directly adjacent to a short name. Same as with
//! options - their relative position usually doesn't matter.
//!
//! <div class="code-wrap">
//! <pre>
//! $ cargo build <span style="font-weight: bold">--package bpaf</span>
//! $ cargo test <span style="font-weight: bold">-j2</span>
//! $ cargo check <span style="font-weight: bold">--bin=megapotato</span>
//! </pre>
//! </div>
//!
//! In the generated help message or documentation they come with a placeholder metavariable,
//! usually a short, all-caps word describing what the value means: `NAME`, `AGE`, `SPEC`, and `CODE`
//! are all valid examples.
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/argument.md"))]
//!
//! For more detailed info see [`NamedArg::argument`]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Options, switches or flags](super::_0_switch)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Types of arguments ↑](super::super::_0_types_of_arguments)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Operands or positional items →](super::_2_positional)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _2_positional {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Option arguments or arguments](super::_1_argument)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Types of arguments ↑](super::super::_0_types_of_arguments)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Commands or subcommands →](super::_3_command)
//!
//! </td>
//! </tr></table>
//!
//! #### Operands or positional items
//!
//! Operands are usually items that are present on a command line and not prefixed by a short or
//! long name. They are usually used to represent the most important part of the operation:
//! `cat Cargo.toml` - display THIS file, `rm -rf target` - remove THIS folder and so on.
//!
//! <div class="code-wrap">
//! <pre>
//! $ cat <span style="font-weight: bold">/etc/passwd</span>
//! $ rm -rf <span style="font-weight: bold">target</span>
//! $ man <span style="font-weight: bold">gcc</span>
//! </pre>
//! </div>
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/positional.md"))]
//!
//! For more detailed info see [`positional`](crate::positional) and
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Option arguments or arguments](super::_1_argument)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Types of arguments ↑](super::super::_0_types_of_arguments)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Commands or subcommands →](super::_3_command)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _3_command {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Operands or positional items](super::_2_positional)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Types of arguments ↑](super::super::_0_types_of_arguments)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Exotic schemas →](super::_4_exotic)
//!
//! </td>
//! </tr></table>
//!
//! #### Commands or subcommands
//!
//! Commands are similar to positional items, but instead of representing an item they start
//! a whole new parser, usually with its help and other arguments. Commands allow a single
//! application to perform multiple different functions. The command parser will be able to parse all
//! the command line options to the right of the command name
//!
//! <div class="code-wrap">
//! <pre>
//! $ cargo <span style="font-weight: bold">build --release</span>
//! $ cargo <span style="font-weight: bold">clippy</span>
//! $ cargo <span style="font-weight: bold">asm --intel --everything</span>
//! </pre>
//! </div>
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/command.md"))]
//!
//! For more detailed info see [`OptionParser::command`]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Operands or positional items](super::_2_positional)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Types of arguments ↑](super::super::_0_types_of_arguments)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Exotic schemas →](super::_4_exotic)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _4_exotic {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Commands or subcommands](super::_3_command)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Types of arguments ↑](super::super::_0_types_of_arguments)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
//! #### Exotic schemas
//!
//! While modern software tends to use just the options listed above you can still encounter
//! programs created before those options became the norm and they use something completely different,
//! let me give a few examples, see [the parsing cookbook](crate::_documentation::_2_howto)
//! about actually parsing them
//!
//! `su` takes an option that consists of a single dash `-`
//!
//! <div class="code-wrap"><pre>
//! $ su <span style="font-weight: bold">-</span>
//! </pre></div>
//!
//! `find` considers everything between `--exec` and `;` to be a single item.
//! this example calls `ls -l` on every file `find` finds.
//!
//! <div class="code-wrap"><pre>
//! $ find /etc --exec ls -l '{}' \;
//! </pre></div>
//!
//! `Xorg` and related tools use flag-like items that start with a single `+` to enable a
//! feature and with `-` to disable it.
//!
//! <div class="code-wrap"><pre>
//! $ xorg -backing +xinerama
//! </pre></div>
//!
//! `dd` takes several key-value pairs, this would create a 100M file
//! <div class="code-wrap"><pre>
//! $ dd if=/dev/zero of=dummy.bin bs=1M count=100
//! </pre></div>
//!
//! Most of the command line arguments in Turbo C++ 3.0 start with `/`. For example, option
//! `/x` tells it to use all available extended memory, while `/x[=n]` limits it to n kilobytes
//! <div class="code-wrap"><pre>
//! C:\PROJECT>TC /x=200
//! </pre></div>
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Commands or subcommands](super::_3_command)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Types of arguments ↑](super::super::_0_types_of_arguments)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
use crate::*;
}
use crate::*;
}
pub mod _1_combinatoric_api {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Types of arguments](super::_0_types_of_arguments)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Tutorials ↑](super::super::_1_tutorials)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Derive API tutorial →](super::_2_derive_api)
//!
//! </td>
//! </tr></table>
//!
//! #### Combinatoric API
//! Parse arguments without using proc macros
//!
//! When making a parser in the Combinatoric style API you usually go through those steps
//!
//! 1. Design data type your application will receive
//! 2. Design command line options user will have to pass
//! 3. Create a set of simple parsers
//! 4. Combine and transform simple parsers to create the final data type
//! 5. Transform the resulting [`Parser`] into [`OptionParser`] and run it
//!
//! Let's go through some of them in more detail:
//!
//! - [Making a simple parser](_0_simple_parser)
//! - [Transforming parsers](_1_chaining)
//! - [Combining multiple simple parsers](_2_combining)
//! - [Subcommand parsers](_3_subcommands)
//! - [Improving the user experience](_4_decorating)
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Types of arguments](super::_0_types_of_arguments)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Tutorials ↑](super::super::_1_tutorials)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Derive API tutorial →](super::_2_derive_api)
//!
//! </td>
//! </tr></table>
//!
pub mod _0_simple_parser {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Combinatoric API ↑](super::super::_1_combinatoric_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Transforming parsers →](super::_1_chaining)
//!
//! </td>
//! </tr></table>
//!
//! #### Making a simple parser
//!
//! In this chapter we'll go over making a few simple parsers.
//!
//! - [Switch parser](_0_switch)
//! - [Argument parser](_1_argument)
//! - [Positional item parser](_2_positional)
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Combinatoric API ↑](super::super::_1_combinatoric_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Transforming parsers →](super::_1_chaining)
//!
//! </td>
//! </tr></table>
//!
pub mod _0_switch {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Making a simple parser ↑](super::super::_0_simple_parser)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Argument parser →](super::_1_argument)
//!
//! </td>
//! </tr></table>
//!
//! #### Switch parser
//!
//! Let's start with the simplest possible one - a simple switch that gets parsed into a `bool`.
//!
//! First of all - the switch needs a name - you can start with [`short`] or [`long`] and add more
//! names if you want: `long("simple")` or `short('s').long("simple")`. This gives something with
//! the type [`NamedArg`]:
//!
//! ```rust
//! # use bpaf::*;
//! use bpaf::parsers::NamedArg;
//! fn simple_switch() -> NamedArg {
//! short('s').long("simple")
//! }
//! ```
//!
//! From `NamedArg` you make a switch parser by calling [`NamedArg::switch`]. Usually, you do it
//! right away without assigning `NamedArg` to a variable.
//!
//! ```rust
//! # use bpaf::*;
//! fn simple_switch() -> impl Parser<bool> {
//! short('s').long("simple").switch()
//! }
//! ```
//!
//! The switch parser we just made implements trait [`Parser`] and to run it you convert it to [`OptionParser`] with
//! [`Parser::to_options`] and run it with [`OptionParser::run`]
//!
//! Full example with some sample inputs and outputs:
#![cfg_attr(not(doctest), doc = include_str!("docs2/compose_basic_switch.md"))]
//!
//!
//! With [`NamedArg::help`] you can attach a help message that will be used in `--help` output.
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Making a simple parser ↑](super::super::_0_simple_parser)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Argument parser →](super::_1_argument)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _1_argument {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Switch parser](super::_0_switch)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Making a simple parser ↑](super::super::_0_simple_parser)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Positional item parser →](super::_2_positional)
//!
//! </td>
//! </tr></table>
//!
//! #### Argument parser
//!
//! Next in complexity would be a parser to consume a named argument, such as `-p my_crate`. Same
//! as with the switch parser it starts from a `NamedArg` but the next method is [`NamedArg::argument`].
//! This method takes a metavariable name - a short description that will be used in the `--help`
//! output. `rustc` also needs to know the parameter type you are trying to parse, there are
//! several ways to do it:
//!
//! ```rust
//! # use bpaf::*;
//! # use std::path::PathBuf;
//! fn simple_argument_1() -> impl Parser<u32> {
//! // rustc figures out the type from returned value
//! long("number").argument("NUM")
//! }
//!
//! fn simple_argument_2() -> impl Parser<String> {
//! // type is specified explicitly with turbofish
//! long("name").argument::<String>("NAME")
//! }
//!
//! fn file_parser() -> OptionParser<PathBuf> {
//! // OptionParser is a type for finalized parser, at this point you can
//! // start adding extra information to the `--help` message
//! long("file").argument::<PathBuf>("FILE").to_options()
//! }
//! ```
//!
//! You can use any type for as long as it implements [`FromStr`]. To parse items that don't
//! implement it you can first parse a `String` or `OsString` and then use [`Parser::parse`], see
//! [the next chapter](super::super::_1_chaining) on how to do that.
//!
//! Full example with some sample inputs and outputs:
#![cfg_attr(not(doctest), doc = include_str!("docs2/compose_basic_argument.md"))]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Switch parser](super::_0_switch)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Making a simple parser ↑](super::super::_0_simple_parser)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Positional item parser →](super::_2_positional)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _2_positional {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Argument parser](super::_1_argument)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Making a simple parser ↑](super::super::_0_simple_parser)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
//! #### Positional item parser
//!
//! And the last simple option type is a parser for positional items. Since there's no name you use
//! the [`positional`] function directly. Similar to [`NamedArg::argument`] this method takes
//! a metavariable name and a type parameter in some form. You can also attach the help message
//! thanks to [`ParsePositional::help`]
//!
//! Full example:
#![cfg_attr(not(doctest), doc = include_str!("docs2/compose_basic_positional.md"))]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Argument parser](super::_1_argument)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Making a simple parser ↑](super::super::_0_simple_parser)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
use crate::*;
}
use crate::*;
}
pub mod _1_chaining {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Making a simple parser](super::_0_simple_parser)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Combinatoric API ↑](super::super::_1_combinatoric_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Combining multiple simple parsers →](super::_2_combining)
//!
//! </td>
//! </tr></table>
//!
//! #### Transforming parsers
//!
//! Once you have your primitive parsers done you might want to improve them a bit - add fallback
//! values, or change them to consume multiple items, etc. Every primitive (or composite) parser
//! implements [`Parser`] so most of the transformations are coming from this trait.
//!
//! Say you have a parser that takes a crate name as a required argument you want to use in your own
//! `cargo test` replacement
//!
//! ```rust
//! use bpaf::*;
//! fn krate() -> impl Parser<String> {
//! long("crate").help("Crate name to process").argument("CRATE")
//! }
//! ```
//!
//! You can turn it into, for example, an optional argument - something that returns
//! `Some("my_crate")` if specified or `None` if it wasn't. Or to let the user to pass a multiple
//! of them and collect them all into a `Vec`
//!
//!
//! ```rust
//! use bpaf::*;
//! fn maybe_krate() -> impl Parser<Option<String>> {
//! long("crate")
//! .help("Crate name to process")
//! .argument("CRATE")
//! .optional()
//! }
//!
//! fn krates() -> impl Parser<Vec<String>> {
//! long("crate")
//! .help("Crate name to process")
//! .argument("CRATE")
//! .many()
//! }
//! ```
//!
//! A complete example:
#![cfg_attr(not(doctest), doc = include_str!("docs2/compose_basic_many.md"))]
//!
//! Transforming a parser with a method from the `Parser` trait usually gives you a new parser back and
//! you can chain as many transformations as you need.
//!
//! Transformations available in the `Parser` trait things like adding fallback values, making
//! the parser optional, making it so it consumes many but at least one value, changing how it is
//! being shown in `--help` output, adding additional validation and parsing on top and so on.
//!
//! The order of those chained transformations matters and for some operations using the right order
//! makes code cleaner. For example, suppose you are trying to write a parser that takes an even
//! number and this parser should be optional. There are two ways to write it:
//!
//! Validation first:
//!
//! ```rust
//! # use bpaf::*;
//! fn even() -> impl Parser<Option<usize>> {
//! long("even")
//! .argument("N")
//! .guard(|&n| n % 2 == 0, "number must be even")
//! .optional()
//! }
//! ```
//!
//! Optional first:
//!
//! ```rust
//! # use bpaf::*;
//! fn even() -> impl Parser<Option<usize>> {
//! long("even")
//! .argument("N")
//! .optional()
//! .guard(|&n| n.map_or(true, |n| n % 2 == 0), "number must be even")
//! }
//! ```
//!
//! In later case validation function must deal with a possibility where a number is absent, for this
//! specific example it makes code less readable.
//!
//! One of the important types of transformations you can apply is a set of failing
//! transformations. Suppose your application operates with numbers and uses `newtype` pattern to
//! keep track of what numbers are odd or even. A parser that consumes an even number can use
//! [`Parser::parse`] and may look like this:
//!
//! ```rust
//! # use bpaf::*;
//! pub struct Even(usize);
//!
//! fn mk_even(n: usize) -> Result<Even, &'static str> {
//! if n % 2 == 0 {
//! Ok(Even(n))
//! } else {
//! Err("Not an even number")
//! }
//! }
//!
//! fn even() -> impl Parser<Even> {
//! long("even")
//! .argument::<usize>("N")
//! .parse(mk_even)
//! }
//! ```
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Making a simple parser](super::_0_simple_parser)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Combinatoric API ↑](super::super::_1_combinatoric_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Combining multiple simple parsers →](super::_2_combining)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _2_combining {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Transforming parsers](super::_1_chaining)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Combinatoric API ↑](super::super::_1_combinatoric_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Subcommand parsers →](super::_3_subcommands)
//!
//! </td>
//! </tr></table>
//!
//! #### Combining multiple simple parsers
//!
//! A single-item option parser can only get you so far. Fortunately, you can combine multiple
//! parsers with [`construct!`] macro.
//!
//! For sequential composition (all the fields must be present) you write your code as if you are
//! constructing a structure, enum variant or a tuple and wrap it with `construct!`. Both
//! a constructor and parsers must be present in the scope. If instead of a parser you have a function
//! that creates one - just add `()` after the name:
//!
//! ```rust
//! # use bpaf::*;
//! struct Options {
//! alpha: usize,
//! beta: usize
//! }
//!
//! fn alpha() -> impl Parser<usize> {
//! long("alpha").argument("ALPHA")
//! }
//!
//! fn both() -> impl Parser<Options> {
//! let beta = long("beta").argument("BETA");
//! // call `alpha` function, and use result to make parser
//! // for field `alpha`, use parser `beta` for field `beta`
//! construct!(Options { alpha(), beta })
//! }
//! ```
//!
//! Full example:
#![cfg_attr(not(doctest), doc = include_str!("docs2/compose_basic_construct.md"))]
//!
//! If you are using positional parsers - they must go to the right-most side and will run in
//! the order you specify them. For named parsers order affects only the `--help` message.
//!
//! The second type of composition `construct!` offers is a parallel composition. You pass multiple
//! parsers that produce the same result type in `[]` and `bpaf` selects one that fits best with
//! the data user gave.
//!
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/compose_basic_choice.md"))]
//!
//! If parsers inside parallel composition can parse the same object - the longest possible match
//! should go first since `bpaf` picks an earlier parser if everything else is equal, otherwise it
//! does not matter. In this example `construct!([miles, km])` produces the same results as
//! `construct!([km, miles])` and only `--help` message is going to be different.
//!
//! Parsers created with [`construct!`] still implement the [`Parser`] trait so you can apply more
//! transformation on top. For example same as you can make a simple parser optional - you can make
//! a composite parser optional. Parser transformed this way will succeed if both `--alpha` and
//! `--beta` are present or neither of them:
//!
//! ```rust
//! # use bpaf::*;
//! struct Options {
//! alpha: usize,
//! beta: usize
//! }
//!
//! fn parser() -> impl Parser<Option<Options>> {
//! let alpha = long("alpha").argument("ALPHA");
//! let beta = long("beta").argument("BETA");
//! construct!(Options { alpha, beta }).optional()
//! }
//! ```
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Transforming parsers](super::_1_chaining)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Combinatoric API ↑](super::super::_1_combinatoric_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Subcommand parsers →](super::_3_subcommands)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _3_subcommands {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Combining multiple simple parsers](super::_2_combining)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Combinatoric API ↑](super::super::_1_combinatoric_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Improving the user experience →](super::_4_decorating)
//!
//! </td>
//! </tr></table>
//!
//! #### Subcommand parsers
//!
//! To make a parser for a subcommand you make an `OptionParser` for that subcommand first as if it
//! was the only thing your application would parse then turn it into a regular [`Parser`]
//! you can further compose with [`OptionParser::command`].
//!
//! This gives [`ParseCommand`] back, you can add aliases or tweak the help message if you want to.
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/compose_basic_command.md"))]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Combining multiple simple parsers](super::_2_combining)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Combinatoric API ↑](super::super::_1_combinatoric_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Improving the user experience →](super::_4_decorating)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _4_decorating {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Subcommand parsers](super::_3_subcommands)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Combinatoric API ↑](super::super::_1_combinatoric_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
//! #### Improving the user experience
//!
//! Once you have the final parser done there are still a few ways you can improve user experience.
//! [`OptionParser`] comes equipped with a few methods that let you set version number,
//! description, help message header and footer and so on.
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/compose_basic_to_options.md"))]
//!
//! There are a few other things you can do:
//!
//! - group some of the primitive parsers into logical blocks for `--help` message with
//! [`Parser::group_help`]
//! - add tests to make sure important combinations are handled the way they are supposed to
//! after any future refactors with [`OptionParser::run_inner`]
//! - add a test to make sure that bpaf internal invariants are satisfied with
//! [`OptionParser::check_invariants`]
//! - generate user documentation in manpage and markdown formats with
//! [`OptionParser::render_manpage`] and [`OptionParser::render_markdown`]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Subcommand parsers](super::_3_subcommands)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Combinatoric API ↑](super::super::_1_combinatoric_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
use crate::*;
}
use crate::*;
}
pub mod _2_derive_api {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Combinatoric API](super::_1_combinatoric_api)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Tutorials ↑](super::super::_1_tutorials)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Designing a good datatype →](super::_3_picking_type)
//!
//! </td>
//! </tr></table>
//!
//! #### Derive API tutorial
//! Create a parser by defining a structure
//!
//!
//! When making a parser using Derive API you should go through approximately following steps:
//!
//! 1. Design data type your application will receive
//! 2. Design command line options user will have to pass
//! 3. Add `#[derive(Bpaf, Debug, Clone)]` on top of your type or types
//! 4. Add `#[bpaf(xxx)]` annotations on types and fields
//! 5. And `#[bpaf(options)]` to the top type
//! 6. Run the resulting parser
//!
//!
//! Let’s go through some of them in more detail:
//!
//! - [Getting started with derive macro](_0_intro)
//! - [Customizing flag and argument names](_1_custom_names)
//! - [Customizing the consumers](_2_custom_consumers)
//! - [Transforming parsed values](_3_postpr)
//! - [Parsing structs and enums](_4_enums_and_structs)
//! - [What gets generated](_5_generate)
//! - [Making nested parsers](_6_nesting)
//! - [Parsing subcommands](_7_commands)
//! - [Making a cargo command](_8_cargo)
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Combinatoric API](super::_1_combinatoric_api)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Tutorials ↑](super::super::_1_tutorials)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Designing a good datatype →](super::_3_picking_type)
//!
//! </td>
//! </tr></table>
//!
pub mod _0_intro {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Customizing flag and argument names →](super::_1_custom_names)
//!
//! </td>
//! </tr></table>
//!
//! #### Getting started with derive macro
//!
//! Let's take a look at a simple example
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/derive_basic_intro.md"))]
//!
//! `bpaf` is trying hard to guess what you are trying to achieve just from the types so it will
//! pick up types, doc comments, presence or absence of names, but it is possible to customize all
//! of it, add custom transformations, validations and more.
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Customizing flag and argument names →](super::_1_custom_names)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _1_custom_names {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Getting started with derive macro](super::_0_intro)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Customizing the consumers →](super::_2_custom_consumers)
//!
//! </td>
//! </tr></table>
//!
//! #### Customizing flag and argument names
//!
//! By default names for flag names are taken directly from the field names so usually you don't
//! have to do anything about it, but you can change it with annotations on the fields themselves:
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/derive_basic_custom_name.md"))]
//!
//! Rules for picking the name are:
//!
//! 1. With no annotations field name longer than a single character becomes a long name,
//! single character name becomes a short name
//! 2. Adding either `long` or `short` disables item 1, so adding `short` disables the long name
//! 3. `long` or `short` annotation without a parameter derives a value from a field name
//! 4. `long` or `short` with a parameter uses that instead
//! 5. You can have multiple `long` and `short` annotations, the first of each type becomes a
//! visible name, remaining are used as hidden aliases
//!
//! And if you decide to add names - they should go to the left side of the annotation list
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Getting started with derive macro](super::_0_intro)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Customizing the consumers →](super::_2_custom_consumers)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _2_custom_consumers {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Customizing flag and argument names](super::_1_custom_names)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Transforming parsed values →](super::_3_postpr)
//!
//! </td>
//! </tr></table>
//!
//! #### Customizing the consumers
//!
//! By default, `bpaf` picks parsers depending on a field type according to those rules:
//!
//! 1. `bool` fields are converted into switches: [`NamedArg::switch`](crate::parsers::NamedArg::switch)
//! 2. `()` (unit) fields, unit variants of an enum or unit structs themselves are handled as
//! [`NamedArg::req_flag`](crate::parsers::NamedArg::req_flag) and thus users must always specify
//! them for the parser to succeed
//! 3. All other types with no `Vec`/`Option` are parsed using [`FromStr`](std::str::FromStr), but
//! smartly, so non-utf8 `PathBuf`/`OsString` are working as expected.
//! 4. For values wrapped in `Option` or `Vec` bpaf derives the inner parser and then applies
//! applies logic from [`Parser::optional`] and [`Parser::many`] respectively.
//!
//! You can change it with annotations like `switch`, `argument` or `positional`
//!
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/derive_basic_custom_consumer.md"))]
//!
//! With arguments that consume a value you can specify its type using turbofish-line syntax
//!
//!
//! ```no_run
//! # use bpaf::*;
//! #[derive(Debug, Clone, Bpaf)]
//! #[bpaf(options)]
//! pub struct Options {
//! /// A custom argument
//! #[bpaf(positional::<usize>("LENGTH"))]
//! argument: usize,
//! }
//!
//! fn main() {
//! let opts = options().run();
//! println!("{:?}", opts)
//! }
//! ```
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Customizing flag and argument names](super::_1_custom_names)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Transforming parsed values →](super::_3_postpr)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _3_postpr {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Customizing the consumers](super::_2_custom_consumers)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Parsing structs and enums →](super::_4_enums_and_structs)
//!
//! </td>
//! </tr></table>
//!
//! #### Transforming parsed values
//!
//! Once the field has a consumer you can start applying transformations from the [`Parser`] trait.
//! Annotation share the same names and follow the same composition rules as in Combinatoric API.
//!
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/derive_basic_postpr.md"))]
//!
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Customizing the consumers](super::_2_custom_consumers)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Parsing structs and enums →](super::_4_enums_and_structs)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _4_enums_and_structs {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Transforming parsed values](super::_3_postpr)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [What gets generated →](super::_5_generate)
//!
//! </td>
//! </tr></table>
//!
//! #### Parsing structs and enums
//!
//! To produce a struct bpaf needs for all the field parsers to succeed. If you are planning to use
//! it for some other purpose as well and want to skip them during parsing you can use [`pure`] to
//! fill in values in member fields and `#[bpaf(skip)]` on enum variants you want to ignore, see
//! combinatoric example in [`Parser::last`].
//!
//! If you use `#[derive(Bpaf)]` on an enum parser will produce a variant for which all the parsers
//! succeed.
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/derive_basic_enum.md"))]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Transforming parsed values](super::_3_postpr)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [What gets generated →](super::_5_generate)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _5_generate {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Parsing structs and enums](super::_4_enums_and_structs)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Making nested parsers →](super::_6_nesting)
//!
//! </td>
//! </tr></table>
//!
//! #### What gets generated
//!
//! Usually calling derive macro on a type generates code to derive a trait implementation for this
//! type. With bpaf it's slightly different. It instead generates a function with a name that
//! depends on the name of the type and gives either a composable parser (`Parser`) or option parser
//! (`OptionParser`) back.
//!
//! You can customize the function name with `generate` annotation at the top level:
//!
//! ```no_run
//! # use bpaf::*;
//! #[derive(Debug, Clone, Bpaf)]
//! #[bpaf(options, generate(my_options))]
//! pub struct Options {
//! /// A simple switch
//! switch: bool
//! }
//!
//!
//! fn main() {
//! let opts = my_options().run();
//! println!("{:?}", opts);
//! }
//! ```
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Parsing structs and enums](super::_4_enums_and_structs)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Making nested parsers →](super::_6_nesting)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _6_nesting {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← What gets generated](super::_5_generate)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Parsing subcommands →](super::_7_commands)
//!
//! </td>
//! </tr></table>
//!
//! #### Making nested parsers
//!
//! Up to this point, we've been looking at cases where fields of a structure are all simple
//! parsers, possibly wrapped in `Option` or `Vec`, but it is also possible to nest derived parsers
//! too:
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/derive_basic_nesting.md"))]
//!
//!
//! `external` takes an optional function name and will call that function to make the parser for
//! the field. You can chain more transformations after the `external` and if the name is absent -
//! `bpaf` would use the field name instead, so you can also write the example above as
//!
//!
//! ```rust
//! # use bpaf::*;
//! #[derive(Debug, Clone, Bpaf)]
//! pub enum Format {
//! /// Produce output in HTML format
//! Html,
//! /// Produce output in Markdown format
//! Markdown,
//! /// Produce output in manpage format
//! Manpage,
//! }
//!
//! #[derive(Debug, Clone, Bpaf)]
//! #[bpaf(options)]
//! pub struct Options {
//! /// File to process
//! input: String,
//! #[bpaf(external)]
//! format: Format,
//! }
//! ```
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← What gets generated](super::_5_generate)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Parsing subcommands →](super::_7_commands)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _7_commands {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Making nested parsers](super::_6_nesting)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Making a cargo command →](super::_8_cargo)
//!
//! </td>
//! </tr></table>
//!
//! #### Parsing subcommands
//!
//! The easiest way to define a group of subcommands is to have them inside the same enum with variant
//! constructors annotated with `#[bpaf(command("name"))]` with or without the name
//!
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/derive_basic_commands.md"))]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Making nested parsers](super::_6_nesting)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Making a cargo command →](super::_8_cargo)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _8_cargo {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Parsing subcommands](super::_7_commands)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
//! #### Making a cargo command
//!
//! To make a cargo command you should pass its name as a parameter to `options`. In this example,
//! `bpaf` will parse extra parameter cargo passes and you will be able to use it either directly
//! with `cargo run` from the repository, running it by `cargo-asm` name or with `cargo asm` name.
//!
//! ```no_run
//! # use bpaf::*;
//! #[derive(Debug, Clone, Bpaf)]
//! #[bpaf(options("asm"))]
//! pub struct Options {
//! /// A simple switch
//! switch: bool
//! }
//!
//!
//! fn main() {
//! let opts = options().run();
//! println!("{:?}", opts);
//! }
//! ```
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Parsing subcommands](super::_7_commands)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Derive API tutorial ↑](super::super::_2_derive_api)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
use crate::*;
}
use crate::*;
}
pub mod _3_picking_type {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Derive API tutorial](super::_2_derive_api)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Tutorials ↑](super::super::_1_tutorials)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
//! #### Designing a good datatype
//! bpaf allows you to reduce the size of legal values to valid ones
//!
//! Parsing usually starts with deciding what kind of data your application wants to get from the user.
//! You should try to take advantage of the Rust type system, try to represent the result such that more
//! validation can be done during parsing.
//!
//! Data types can represent a set of *legal* states - for example, for u8 this is all the numbers
//! from 0 to 255, while your app logic may only operate correctly only on some set of *valid*
//! states: if this u8 represents a fill ratio for something in percents - only valid numbers are
//! from 0 to 100. You can try to narrow down the set of legal states to valid states with [newtype
//! pattern](https://doc.rust-lang.org/rust-by-example/generics/new_types.html). This newtype will
//! indicate through the type when you've already done validation. For the fill ratio example you can
//! implement a newtype along with `FromStr` implementation to get validation for free during
//! parsing.
//!
//!
//! ```no_run
//! # use std::str::FromStr;
//! # use bpaf::*;
//! #[derive(Debug, Clone, Copy)]
//! pub struct Ratio(u8);
//!
//! impl FromStr for Ratio {
//! type Err = &'static str;
//!
//! fn from_str(s: &str) -> Result<Self, Self::Err> {
//! match s.parse() {
//! Ok(n) if n <= 100 => Ok(Ratio(n)),
//! _ => Err("Invalid fill ratio")
//! }
//! }
//! }
//!
//! #[derive(Debug, Clone, Bpaf)]
//! #[bpaf(options)]
//! struct Options {
//! /// Fill ratio
//! ratio: Ratio
//! }
//!
//! fn main() {
//! println!("{:?}", options().run());
//! }
//! ```
//!
//!
//! Try using enums instead of structs for mutually exclusive options:
//!
//! ```no_check
//! /// Good format selection
//! #[derive(Debug, Clone, Bpaf)]
//! #[bpaf(options)]
//! enum OutputFormat {
//! Intel,
//! Att,
//! Llvm
//! }
//!
//! fn main() {
//! let format = output_format().run();
//!
//! // `rustc` ensures you handle each case, parser won't try to consume
//! // combinations of flags it can't represent. For example it won't accept
//! // both `--intel` and `--att` at once
//! // (unless it can collect multiple of them in a vector)
//! match format {
//! OutputFormat::Intel => ...,
//! OutputFormat::Att => ...,
//! OutputFormat::Llvm => ...,
//! }
//! }
//! ```
//!
//! While it's easy to see how flags like `--intel` and `--att` maps to each of those bools,
//! consuming inside your app is more fragile
//!
//! ```no_check
//! /// Bad format selection
//! #[derive(Debug, Clone, Bpaf)]
//! #[bpaf(options)]
//! struct OutputFormat {
//! intel: bool,
//! att: bool,
//! llvm: bool,
//! }
//!
//! fn main() {
//! let format = output_format().run();
//! // what happens when none matches? Or all of them?
//! // What happens when you add a new output format?
//! if format.intel {
//! ...
//! } else if format.att {
//! ...
//! } else if format.llvm {
//! ...
//! } else {
//! // can this branch be reached?
//! }
//! }
//! ```
//!
//! Mutually exclusive things are not limited to just flags. For example if your program can take
//! input from several different sources such as file, database or interactive input it's a good
//! idea to use enum as well:
//!
//! ```no_check
//! /// Good input selection
//! #[derive(Debug, Clone, Bpaf)]
//! enum Input {
//! File {
//! filepath: PathBuf,
//! }
//! Database {
//! user: String,
//! password: String.
//! }
//! Interactive,
//! }
//! ```
//!
//! If your codebase uses newtype pattern - it's a good idea to use it starting from the command
//! options:
//!
//! ```no_check
//! #[derive(Debug, Clone, Bpaf)]
//! struct Options {
//! // better than taking a String and parsing internally
//! date: NaiveDate,
//! // f64 might work too, but you can start from some basic sanity checks
//! speed: Speed
//! }
//! ```
//!
//!
//! # More reading
//!
//! - <https://fsharpforfunandprofit.com/posts/designing-with-types-making-illegal-states-unrepresentable/>
//! - <https://geeklaunch.io/blog/make-invalid-states-unrepresentable/>
//! - <https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/>
//! - <https://khalilstemmler.com/articles/typescript-domain-driven-design/make-illegal-states-unrepresentable/>
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Derive API tutorial](super::_2_derive_api)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Tutorials ↑](super::super::_1_tutorials)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
use crate::*;
}
use crate::*;
}
pub mod _2_howto {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Tutorials](super::_1_tutorials)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Project documentation ↑](super::super::_documentation)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Parsing cookbook →](super::_3_cookbook)
//!
//! </td>
//! </tr></table>
//!
//! #### HOWTO - practical, oriented to solving problems guides
//!
//! - [Testing your parsers](_0_testing)
//! - [Dynamic shell completion](_1_completion)
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Tutorials](super::_1_tutorials)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Project documentation ↑](super::super::_documentation)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Parsing cookbook →](super::_3_cookbook)
//!
//! </td>
//! </tr></table>
//!
pub mod _0_testing {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ HOWTO - practical, oriented to solving problems guides ↑](super::super::_2_howto)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Dynamic shell completion →](super::_1_completion)
//!
//! </td>
//! </tr></table>
//!
//! #### Testing your parsers
//!
//! You can test values your parser produces and expected output
//!
//! ```no_run
//! # use bpaf::*;
//! #[derive(Debug, Clone, Bpaf)]
//! #[bpaf(options)]
//! pub struct Options {
//! pub user: String
//! }
//!
//! #[test]
//! fn test_my_options() {
//! let help = options()
//! .run_inner(&["--help"])
//! .unwrap_err()
//! .unwrap_stdout();
//! let expected_help = "\
//! Usage --user=ARG
//! <skip>
//! ";
//!
//! assert_eq!(help, expected_help);
//! }
//!
//! #[test]
//! fn test_value() {
//! let value = options()
//! .run_inner(&["--user", "Bob"])
//! .unwrap();
//! assert_eq!(value.user, "Bob");
//! }
//! ```
//!
//! [`OptionParser::run_inner`] takes [`Args`] or anything that can be converted to it, in most
//! cases using a static slice with strings is enough.
//!
//! Easiest way to consume [`ParseFailure`] for testing purposes is with
//! [`ParseFailure::unwrap_stderr`] and [`ParseFailure::unwrap_stdout`] - result will lack any colors
//! even with them enabled which makes testing easier.
//!
//! Successful result parse produces a value, "failed" parse produces stdout or stderr outputs -
//! stdout to print help message or version number and stderr to print the error message.
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ HOWTO - practical, oriented to solving problems guides ↑](super::super::_2_howto)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Dynamic shell completion →](super::_1_completion)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _1_completion {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Testing your parsers](super::_0_testing)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ HOWTO - practical, oriented to solving problems guides ↑](super::super::_2_howto)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
//! #### Dynamic shell completion
//!
//! `bpaf` implements shell completion to allow to automatically fill in not only flag and command
//! names, but also argument and positional item values.
//!
//! 1. Enable `autocomplete` feature:
//!
//!
//! ```toml
//! bpaf = { version = "0.9", features = ["autocomplete"] }
//! ```
//!
//! 2. Decorate [`argument`](crate::parsers::NamedArg::argument) and [`positional`] parsers with
//! [`Parser::complete`] to provide completion functions for arguments
//!
//!
//! 3. Depending on your shell generate appropriate completion file and place it to whereever your
//! shell is going to look for it, name of the file should correspond in some way to name of
//! your program. Consult manual for your shell for the location and named conventions:
//!
//! 1. **bash**
//! ```console
//! $ your_program --bpaf-complete-style-bash >> ~/.bash_completion
//! ```
//!
//! 1. **zsh**: note `_` at the beginning of the filename
//! ```console
//! $ your_program --bpaf-complete-style-zsh > ~/.zsh/_your_program
//! ```
//!
//! 1. **fish**
//! ```console
//! $ your_program --bpaf-complete-style-fish > ~/.config/fish/completions/your_program.fish
//! ```
//!
//! 1. **elvish**
//! ```console
//! $ your_program --bpaf-complete-style-elvish >> ~/.config/elvish/rc.elv
//! ```
//!
//! 4. Restart your shell - you need to done it only once or optionally after bpaf major version
//! upgrade: generated completion files contain only instructions how to ask your program for
//! possible completions and don’t change even if options are different.
//!
//!
//! 5. Generated scripts rely on your program being accessible in $PATH
//!
//!
//!
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Testing your parsers](super::_0_testing)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ HOWTO - practical, oriented to solving problems guides ↑](super::super::_2_howto)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
use crate::*;
}
use crate::*;
}
pub mod _3_cookbook {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← HOWTO - practical, oriented to solving problems guides](super::_2_howto)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Project documentation ↑](super::super::_documentation)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Theory explanation →](super::_4_explanation)
//!
//! </td>
//! </tr></table>
//!
//! #### Parsing cookbook
//! How to parse less frequent combinations
//!
//! While `bpaf`'s design tries to cover the most common use cases, mostly
//! [posix conventions](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/basedefs/V1_chap12.html),
//! it can also handle some more unusual requirements. It might come at the cost of having to write
//! more code, more confusing error messages or worse performance, but it will get the job done.
//!
//! - [`find(1)`: `find -exec commands -flags terminated by \;`](_00_find)
//! - [`dd(1)`: `dd if=/dev/zero of=/dev/null bs=1000`](_01_dd)
//! - [`Xorg(1)`: `Xorg +xinerama +extension name`](_02_xorg)
//! - [Command chaining](_03_command_chaining) - Lets you do things like `setup.py sdist bdist`: [command chaining](https://click.palletsprojects.com/en/7.x/commands/#multi-command-chaining)
//! - [Multi-value arguments: `--foo ARG1 ARG2 ARG3`](_04_multi_value)
//! - [Structure groups: `--foo --foo-1 ARG1 --foo-2 ARG2 --foo-3 ARG3`](_05_struct_groups)
//! - [Multi-value arguments with optional flags: `--foo ARG1 --flag --inner ARG2`](_06_multi_flag)
//! - [Skipping optional positional items if parsing or validation fails](_07_skip_positional)
//! - [Implementing cargo commands](_08_cargo_helper)
//! - [Numeric flags - compression levels like in zip](_09_numeric_flags)
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← HOWTO - practical, oriented to solving problems guides](super::_2_howto)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Project documentation ↑](super::super::_documentation)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Theory explanation →](super::_4_explanation)
//!
//! </td>
//! </tr></table>
//!
pub mod _00_find {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [`dd(1)`: `dd if=/dev/zero of=/dev/null bs=1000` →](super::_01_dd)
//!
//! </td>
//! </tr></table>
//!
//! #### `find(1)`: `find -exec commands -flags terminated by \;`
//!
//! An Example for `find` shows how to implement 3 different unusual options:
//!
//! - an option with a long name but a single dash as a prefix: `-user bob`
//! - an option that captures everything until the next fixed character
//! - an option that takes a set of characters: `-mode -rw`, `mode /rw`
//!
//! In all cases, long name with a single dash is implemented by the [`literal`] with
//! [`ParseAny::anywhere`](crate::parsers::ParseAny::anywhere) with some items made `adjacent` to it.
//!
//! To parse `-user bob` this is simply literal `-user` adjacent to a positional item with `map` to
//! focus on the interesting part.
//!
//! For `-exec more things here ;` this is a combination of literal `-exec`, followed by `many`
//! items that are not `;` parsed positionally with `any` followed by `;` - again with `any`, but
//! `literal` works too.
//!
//! And lastly to parse mode - after the tag, we accept `any` to be able to handle a combination of
//! modes that may or may not start with `-` and use [`Parser::parse`] to parse them or fail.
//!
//! All special cases are made optional with [`Parser::optional`], but [`Parser::fallback`] also
//! works.
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/find.md"))]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [`dd(1)`: `dd if=/dev/zero of=/dev/null bs=1000` →](super::_01_dd)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _01_dd {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← `find(1)`: `find -exec commands -flags terminated by \;`](super::_00_find)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [`Xorg(1)`: `Xorg +xinerama +extension name` →](super::_02_xorg)
//!
//! </td>
//! </tr></table>
//!
//! #### `dd(1)`: `dd if=/dev/zero of=/dev/null bs=1000`
//!
//! This example implements syntax similar to `dd` command. The main idea is to implement something to
//! make it simple to make parsers for `PREFIX=SUFFIX`, where prefix is fixed for each parser - for
//! example `if=` or `of=` and suffix is parsed with usual [`FromStr`](std::str::FromStr) trait.
//!
//! The function `tag` serves this purpose. It performs the following steps:
//!
//! - consume any item that starts with a prefix at any argument position with [`any`] and
//! [`ParseAny::anywhere`]
//! - attaches help message and custom metadata to make `--help` friendlier
//! - parses suffix with [`Parser::parse`]
//!
//! The rest of the parser simply uses `tag` to parse a few of `dd` arguments
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/dd.md"))]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← `find(1)`: `find -exec commands -flags terminated by \;`](super::_00_find)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [`Xorg(1)`: `Xorg +xinerama +extension name` →](super::_02_xorg)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _02_xorg {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← `dd(1)`: `dd if=/dev/zero of=/dev/null bs=1000`](super::_01_dd)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Command chaining →](super::_03_command_chaining)
//!
//! </td>
//! </tr></table>
//!
//! #### `Xorg(1)`: `Xorg +xinerama +extension name`
//!
//! This example implements syntax similar to used by `Xorg` or similar programs. As usual with
//! strange examples [`any`] serves an important role.
//!
//! The example implements the following parsers:
//!
//! - enable or disable an extension using `+ext name` and `-ext name` like syntax
//! - enable or disable specific extensions with syntax like `-xinerama` or `+backing`
//!
//! Both parsers use [`any`] with [`ParseAny::anywhere`]
//!
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/xorg.md"))]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← `dd(1)`: `dd if=/dev/zero of=/dev/null bs=1000`](super::_01_dd)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Command chaining →](super::_03_command_chaining)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _03_command_chaining {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← `Xorg(1)`: `Xorg +xinerama +extension name`](super::_02_xorg)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Multi-value arguments: `--foo ARG1 ARG2 ARG3` →](super::_04_multi_value)
//!
//! </td>
//! </tr></table>
//!
//! #### Command chaining
//! Lets you do things like `setup.py sdist bdist`: [command chaining](https://click.palletsprojects.com/en/7.x/commands/#multi-command-chaining)
//!
//! With [`adjacent`](crate::parsers::ParseCommand::adjacent)
//! `bpaf` allows you to have several commands side by side instead of being nested.
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/adjacent_command.md"))]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← `Xorg(1)`: `Xorg +xinerama +extension name`](super::_02_xorg)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Multi-value arguments: `--foo ARG1 ARG2 ARG3` →](super::_04_multi_value)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _04_multi_value {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Command chaining](super::_03_command_chaining)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Structure groups: `--foo --foo-1 ARG1 --foo-2 ARG2 --foo-3 ARG3` →](super::_05_struct_groups)
//!
//! </td>
//! </tr></table>
//!
//! #### Multi-value arguments: `--foo ARG1 ARG2 ARG3`
//!
//! By default arguments take at most one value, you can create multi value options by using
//! [`adjacent`](crate::parsers::ParseCon::adjacent) modifier
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/adjacent_struct_0.md"))]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Command chaining](super::_03_command_chaining)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Structure groups: `--foo --foo-1 ARG1 --foo-2 ARG2 --foo-3 ARG3` →](super::_05_struct_groups)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _05_struct_groups {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Multi-value arguments: `--foo ARG1 ARG2 ARG3`](super::_04_multi_value)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Multi-value arguments with optional flags: `--foo ARG1 --flag --inner ARG2` →](super::_06_multi_flag)
//!
//! </td>
//! </tr></table>
//!
//! #### Structure groups: `--foo --foo-1 ARG1 --foo-2 ARG2 --foo-3 ARG3`
//!
//! Groups of options that can be specified multiple times. All such groups should be kept without
//! overwriting previous one.
//!
//! ```console
//! $ prometheus_sensors_exporter \
//! \
//! `# 2 physical sensors located on physycial different i2c bus or address` \
//! --sensor \
//! --sensor-device=tmp102 \
//! --sensor-name="temperature_tmp102_outdoor" \
//! --sensor-i2c-bus=0 \
//! --sensor-i2c-address=0x48 \
//! --sensor \
//! --sensor-device=tmp102 \
//! --sensor-name="temperature_tmp102_indoor" \
//! --sensor-i2c-bus=1 \
//! --sensor-i2c-address=0x49 \
//! ```
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/adjacent_struct_1.md"))]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Multi-value arguments: `--foo ARG1 ARG2 ARG3`](super::_04_multi_value)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Multi-value arguments with optional flags: `--foo ARG1 --flag --inner ARG2` →](super::_06_multi_flag)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _06_multi_flag {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Structure groups: `--foo --foo-1 ARG1 --foo-2 ARG2 --foo-3 ARG3`](super::_05_struct_groups)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Skipping optional positional items if parsing or validation fails →](super::_07_skip_positional)
//!
//! </td>
//! </tr></table>
//!
//! #### Multi-value arguments with optional flags: `--foo ARG1 --flag --inner ARG2`
//!
//! So you can parse things while parsing things. Not sure why you might need this, but you can
//! :)
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/adjacent_struct_4.md"))]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Structure groups: `--foo --foo-1 ARG1 --foo-2 ARG2 --foo-3 ARG3`](super::_05_struct_groups)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Skipping optional positional items if parsing or validation fails →](super::_07_skip_positional)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _07_skip_positional {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Multi-value arguments with optional flags: `--foo ARG1 --flag --inner ARG2`](super::_06_multi_flag)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Implementing cargo commands →](super::_08_cargo_helper)
//!
//! </td>
//! </tr></table>
//!
//! #### Skipping optional positional items if parsing or validation fails
//!
//! Combinations like [`Parser::optional`] and
//! [`ParseOptional::catch`](crate::parsers::ParseOptional::catch) allow to try to parse something
//! and then handle the error as if pase attempt never existed
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/numeric_prefix.md"))]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Multi-value arguments with optional flags: `--foo ARG1 --flag --inner ARG2`](super::_06_multi_flag)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Implementing cargo commands →](super::_08_cargo_helper)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _08_cargo_helper {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Skipping optional positional items if parsing or validation fails](super::_07_skip_positional)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Numeric flags - compression levels like in zip →](super::_09_numeric_flags)
//!
//! </td>
//! </tr></table>
//!
//! #### Implementing cargo commands
//!
//! With [`cargo_helper`](crate::batteries::cargo_helper) you can use your application as a `cargo` command.
//! You will need to enable `batteries` feature while importing `bpaf`.
//!
#![cfg_attr(not(doctest), doc = include_str!("docs2/cargo_helper.md"))]
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Skipping optional positional items if parsing or validation fails](super::_07_skip_positional)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//!
//! [Numeric flags - compression levels like in zip →](super::_09_numeric_flags)
//!
//! </td>
//! </tr></table>
//!
use crate::*;
}
pub mod _09_numeric_flags {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Implementing cargo commands](super::_08_cargo_helper)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
//! #### Numeric flags - compression levels like in zip
//!
//! While you can add flags in a usual way for compression levels using `short(1)`, `short(2)`, etc
//! combined with `req_flag`, you can also parse all of then using [`any`]
//!
//! ```no_run
//! use bpaf::{doc::Style, *};
//!
//! fn compression() -> impl Parser<usize> {
//! any::<isize, _, _>("COMP", |x: isize| {
//! if (-9..=-1).contains(&x) {
//! Some(x.abs().try_into().unwrap())
//! } else {
//! None
//! }
//! })
//! .metavar(&[
//! ("-1", Style::Literal),
//! (" to ", Style::Text),
//! ("-9", Style::Literal),
//! ])
//! .help("Compression level")
//! .anywhere()
//! }
//!
//! fn main() {
//! let opts = compression().to_options().run();
//!
//! println!("{:?}", opts);
//! }
//! ```
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Implementing cargo commands](super::_08_cargo_helper)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Parsing cookbook ↑](super::super::_3_cookbook)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
use crate::*;
}
use crate::*;
}
pub mod _4_explanation {
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Parsing cookbook](super::_3_cookbook)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Project documentation ↑](super::super::_documentation)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
//! #### Theory explanation
//! Theoretical information about abstractions used by the library, oriented for understanding
//!
//!
//! # Applicative functors, Category Theory? What is it about?
//!
//! You don't need to read/understand this chapter in order to use the library but it might
//! help to understand what makes it tick.
//!
//! `bpaf` uses ideas from functional proggramming, specifically Functor, Applicative and
//! Alternative to create a composable interface. Exposed API and the fact that individual
//! components obey certain laws ensures that any composition of parsers is valid even if it
//! doesn't make any sense.
//!
//! ## Category theory
//!
//! Category theory, also called Abstract Nonsense, is a general theory about mathematical
//! structures and their relations. *Category* in CT constists of two sorts of abstractions:
//! *objects* and *morphisms* along with some extra rules:
//! - objects don't expose any information other than the name and only serve as start and end points for morphisms
//! - morphisms must compose with associative composition
//! - there must be an *identity morphism* for every object that maps the object to itself
//!
//! A simple example of a category would be a category where objects are Rust types (here: `u8` ..
//! `u64`) and morphisms are functions between those types (here: `a`, `b` and `c`):
//!
//! ```rust
//! fn a(i: u8) -> u16 {
//! 3000 + i as u16
//! }
//!
//! fn b(i: u16) -> u32 {
//! 40000 + i as u32
//! }
//!
//! fn c(i: u32) -> u64 {
//! 40000 + i as u64
//! }
//!
//! /// Identity morphism
//! fn id<T>(i: T) -> T {
//! i
//! }
//!
//! /// morphism composition:
//! /// `comp (a, comp(b, c))` gives the same results as `comp(comp(a, b), c)`
//! fn comp<F, G, A, B, C>(f: F, g: G) -> impl Fn(A) -> C
//! where
//! F: Fn(A) -> B,
//! G: Fn(B) -> C,
//! {
//! move |i| g(f(i))
//! }
//! ```
//!
//! ## Composition and decomposition
//!
//! Decomposition is one of the keys to solving big problems - you break down big problem into a
//! bunch of small problems, solve them separately and compose back a solution. Decomposition is
//! not required by computers but makes it easier to think about a problem: magical number for
//! human short term memory is 7 plus minus 2 objects. Category theory, studies relations and
//! composition can be a valuable tool: after all decomposition only makes sense when you can
//! combine components back into a solution. Imperative algorithms that operate in terms of
//! mutating variables are harder decompose - individual pieces need to be aware of the variables,
//! functional and declarative approaches make it easier: calculating a sum of all the numbers in a
//! vector can be decomposed into running an iterator over it and applying `fold` to it: `fold`
//! doesn't need to know about iteration shape, iterator doesn't need to know about how values are
//! used.
//!
//! In category theory you are not allowed to look inside the objects at all and can distinguish
//! between them only by means of the composition so as long as implemented API obeys the
//! restrictions set by category theory - it should be very composable.
//!
//! ## Functors
//!
//! Let's start by talking about what a `Functor` is. Wikipedia defines it as a "design pattern
//! that allows for a generic type to apply a function inside without changing the structure of
//! the generic type". Sounds scary, but in Rust terms it's a trait that takes a value or values
//! in a container (or more general *value in a context* ) such as `Option<A>` and a function
//! `fn(A) -> B` and gives you `Option<B>` back.
//!
//! Closest analogy in a real code you can write in Rust right now would be modifying an `Option`
//! using only `Option::map`:
//! ```rust
//! fn plus_one(input: Option<u32>) -> Option<u32> {
//! input.map(|i| i + 1)
//! }
//!
//! let present = Some(10);
//! let absent = None;
//!
//! assert_eq!(plus_one(present), Some(11));
//! assert_eq!(plus_one(absent), None);
//! ```
//!
//! `Vec`, `Result` and other types that implement `map` are `Functors` as well, but `Functor`
//! is not limited just to containers - you don't have to have a value inside to be able to
//! manipulate it. In fact a regular rust function is also a `Functor` if you squint hard enough.
//! Consider `Reader` that allows you to perform transformations on a *value in a context* `T`
//! without having any value until it the execution time:
//!
//! ```rust
//! struct Reader<T>(Box<dyn Fn(T) -> T>);
//! impl<T: 'static> Reader<T> {
//! /// Initialize an new value in a context
//! fn new() -> Self {
//! Self(Box::new(|x| x))
//! }
//!
//! /// Modify a value in a context
//! fn map<F: Fn(T) -> T + 'static>(self, f: F) -> Self {
//! Self(Box::new(move |x| f((self.0)(x))))
//! }
//!
//! /// Apply the changes by giving it the initial value
//! fn run(self, input: T) -> T {
//! (self.0)(input)
//! }
//! }
//!
//! let val = Reader::<u32>::new();
//! let val = val.map(|x| x + 1);
//! let res = val.run(10);
//! assert_eq!(res, 11);
//! ```
//!
//! Not all the collections are `Functors` - by `Functor` laws mapping the *value in context*
//! shouldn't change the shape so any collections where shape depends on a value, such as `HashSet`
//! or `BTreeSet` are out.
//!
//! ## Applicative Functors
//!
//! `map` in `Functor` is limited to a single *value in a context*, `Applicative Functor` extends it
//! to operations combining multiple values, closest Rust analogy would be doing computations on
//! `Option` or `Result` using only `?`, having `Some`/`Ok` around the whole expression and not using `return`.
//! ```rust
//! fn add_numbers(input_a: Option<u32>, input_b: Option<u32>) -> Option<u32> {
//! Some(input_a? + input_b?)
//! }
//!
//! let present_1 = Some(10);
//! let present_2 = Some(20);
//! let absent = None;
//!
//! assert_eq!(add_numbers(present_1, present_2), Some(30));
//! assert_eq!(add_numbers(present_1, absent), None);
//! assert_eq!(add_numbers(absent, absent), None);
//! ```
//!
//! Similarly to `Functors`, `Applicative Functors` are not limited to containers and can
//! represent *a value in an arbitrary context*.
//!
//! `Try` trait (`?`) for `Option` and `Result` short circuits when it finds a missing value,
//! but `Applicative Functors` in general don't have to - in fact to implement dynamic completion
//! `bpaf` needs to check items past the first failure point to collect all the possible
//! completions.
//!
//! ## Alternative Functors
//!
//! So far `Applicative Functors` allow us to create structs containing multiple fields out of
//! individual parsers for each field. `Alternative` extends `Applicative` with two extra
//! things: one for combining two *values in a context* into one and and an idenity element
//! for this operation. In Rust a closest analogy would be `Option::or` and `Option::None`:
//!
//! ```rust
//! fn pick_number(a: Option<u32>, b: Option<u32>) -> Option<u32> {
//! a.or(b)
//! }
//!
//! let present_1 = Some(10);
//! let present_2 = Some(20);
//! let empty = None;
//! assert_eq!(pick_number(present_1, present_2), present_1);
//! assert_eq!(pick_number(present_1, empty), present_1);
//! assert_eq!(pick_number(empty, present_1), present_1);
//! assert_eq!(pick_number(empty, empty), empty);
//! ```
//!
//! ## `Parser` trait and `construct!` macro
//!
//! [`Parser`] trait defines a context for values and gives access to `Functor` laws and [`construct!`]
//! macro allows to compose several values according to `Applicative` and `Alternative` laws.
//!
//! ## So why use `Applicative Functors` then?
//!
//! As a user I want to be able to express requirements using full power of Rust algebraic
//! datatypes: `struct` for product types and `enum` for sum types. To give an example -
//! `cargo-show-asm` asks user to specify what to output - Intel or AT&T asm, LLVM or Rust's MIR
//! and opts to represent it as one of four flags: `--intel`, `--att`, `--llvm` and `--mir`. While
//! each flag can be though of a boolean value - present/absent - consuming it as an `enum` with four
//! possible values is much more convenient compared to a struct-like thing that can have any
//! combination of the flags inside:
//!
//! ```no_check
//! /// Format selection as enum - program needs to deal with just one format
//! enum Format {
//! Intel,
//! Att,
//! Llvm,
//! Mir
//! }
//!
//! /// Format selection as struct - can represent any possible combination of formats
//! struct Formats {
//! intel: bool,
//! att: bool,
//! llvm: bool,
//! mir: bool,
//! }
//! ```
//!
//! `Applicative` interface gives just enough power to compose simple parsers as an arbitrary tree
//! ready for consumption.
//!
//! As a library author I need to be able to extract information from the tree constructed by user
//! to generate `--help` information and do command line completion. As long as the tree uses only
//! `Applicative` powers - it is possible to evaluate it without giving it any input.
//! Adding `Monadic` powers (deciding what to parse next depending on the previous input) would
//! make this impossible.
//!
//! So `Applicative Functors` sits right in the middle between what users want to express and
//! library can consume.
//!
//! To recap - all sorts of Functors listed here only define laws to how individual parts are
//! composed, how values in context can be transformed and how pure values can be turned into a
//! functor, but not how the values are parsed or how they can be extracted.
//!
//! ## Putting the values into a context
//!
//! Similarly to how `Reader` defined above `bpaf`'s `Parsers` don't actually have values inside
//! until they are executed. Instead starting points ([`flag`](NamedArg::flag), [`positional`],
//! [`argument`](NamedArg::argument), etc) define what exactly needs to be consumed, various mapping
//! functions define transformations, [`construct!`] composes them and defines the relative order
//! values should be consumed. Not everything present inside [`Parser`] can be repesented in terms
//! of plain applicative functors - specifically [`parse`](Parser::parse) is not and it is best
//! though of as a function that takes one applicative and gives a different applicative back.
//! The actual values will show up inside once `bpaf` starts running the [`OptionParser`] with
//! [`run`](OptionParser::run).
//!
//! ## Taking the results out
//!
//! The rest of the execution is relatively simple: getting console arguments from OS, doing the
//! initial split into short/long flags and standalone words, disambiguating groups of short
//! options from short options with attached values and applying all the transformations like
//! `Reader::run` above would do.
//!
//!
//!
//!
//! <table width='100%' cellspacing='0' style='border: hidden;'><tr>
//! <td style='width: 33%; text-align: left;'>
//!
//! [← Parsing cookbook](super::_3_cookbook)
//!
//! </td>
//! <td style='width: 34%; text-align: center;'>
//!
//! [↑ Project documentation ↑](super::super::_documentation)
//!
//! </td>
//! <td style='width: 33%; text-align: right;'>
//! </td>
//! </tr></table>
//!
use crate::*;
}
use crate::*;