1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675
|
/*!
Defines all of the flags available in ripgrep.
Each flag corresponds to a unit struct with a corresponding implementation
of `Flag`. Note that each implementation of `Flag` might actually have many
possible manifestations of the same "flag." That is, each implementation of
`Flag` can have the following flags available to an end user of ripgrep:
* The long flag name.
* An optional short flag name.
* An optional negated long flag name.
* An arbitrarily long list of aliases.
The idea is that even though there are multiple flags that a user can type,
one implementation of `Flag` corresponds to a single _logical_ flag inside of
ripgrep. For example, `-E`, `--encoding` and `--no-encoding` all manipulate the
same encoding state in ripgrep.
*/
use std::path::PathBuf;
use {anyhow::Context as AnyhowContext, bstr::ByteVec};
use crate::flags::{
lowargs::{
BinaryMode, BoundaryMode, BufferMode, CaseMode, ColorChoice,
ContextMode, EncodingMode, EngineChoice, GenerateMode, LoggingMode,
LowArgs, MmapMode, Mode, PatternSource, SearchMode, SortMode,
SortModeKind, SpecialMode, TypeChange,
},
Category, Flag, FlagValue,
};
#[cfg(test)]
use crate::flags::parse::parse_low_raw;
use super::CompletionType;
/// A list of all flags in ripgrep via implementations of `Flag`.
///
/// The order of these flags matter. It determines the order of the flags in
/// the generated documentation (`-h`, `--help` and the man page) within each
/// category. (This is why the deprecated flags are last.)
pub(super) const FLAGS: &[&dyn Flag] = &[
// -e/--regexp and -f/--file should come before anything else in the
// same category.
&Regexp,
&File,
&AfterContext,
&BeforeContext,
&Binary,
&BlockBuffered,
&ByteOffset,
&CaseSensitive,
&Color,
&Colors,
&Column,
&Context,
&ContextSeparator,
&Count,
&CountMatches,
&Crlf,
&Debug,
&DfaSizeLimit,
&Encoding,
&Engine,
&FieldContextSeparator,
&FieldMatchSeparator,
&Files,
&FilesWithMatches,
&FilesWithoutMatch,
&FixedStrings,
&Follow,
&Generate,
&Glob,
&GlobCaseInsensitive,
&Heading,
&Help,
&Hidden,
&HostnameBin,
&HyperlinkFormat,
&IGlob,
&IgnoreCase,
&IgnoreFile,
&IgnoreFileCaseInsensitive,
&IncludeZero,
&InvertMatch,
&JSON,
&LineBuffered,
&LineNumber,
&LineNumberNo,
&LineRegexp,
&MaxColumns,
&MaxColumnsPreview,
&MaxCount,
&MaxDepth,
&MaxFilesize,
&Mmap,
&Multiline,
&MultilineDotall,
&NoConfig,
&NoIgnore,
&NoIgnoreDot,
&NoIgnoreExclude,
&NoIgnoreFiles,
&NoIgnoreGlobal,
&NoIgnoreMessages,
&NoIgnoreParent,
&NoIgnoreVcs,
&NoMessages,
&NoRequireGit,
&NoUnicode,
&Null,
&NullData,
&OneFileSystem,
&OnlyMatching,
&PathSeparator,
&Passthru,
&PCRE2,
&PCRE2Version,
&Pre,
&PreGlob,
&Pretty,
&Quiet,
&RegexSizeLimit,
&Replace,
&SearchZip,
&SmartCase,
&Sort,
&Sortr,
&Stats,
&StopOnNonmatch,
&Text,
&Threads,
&Trace,
&Trim,
&Type,
&TypeNot,
&TypeAdd,
&TypeClear,
&TypeList,
&Unrestricted,
&Version,
&Vimgrep,
&WithFilename,
&WithFilenameNo,
&WordRegexp,
// DEPRECATED (make them show up last in their respective categories)
&AutoHybridRegex,
&NoPcre2Unicode,
&SortFiles,
];
/// -A/--after-context
#[derive(Debug)]
struct AfterContext;
impl Flag for AfterContext {
fn is_switch(&self) -> bool {
false
}
fn name_short(&self) -> Option<u8> {
Some(b'A')
}
fn name_long(&self) -> &'static str {
"after-context"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("NUM")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
"Show NUM lines after each match."
}
fn doc_long(&self) -> &'static str {
r"
Show \fINUM\fP lines after each match.
.sp
This overrides the \flag{passthru} flag and partially overrides the
\flag{context} flag.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.context.set_after(convert::usize(&v.unwrap_value())?);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_after_context() {
let mkctx = |lines| {
let mut mode = ContextMode::default();
mode.set_after(lines);
mode
};
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(ContextMode::default(), args.context);
let args = parse_low_raw(["--after-context", "5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let args = parse_low_raw(["--after-context=5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let args = parse_low_raw(["-A", "5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let args = parse_low_raw(["-A5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let args = parse_low_raw(["-A5", "-A10"]).unwrap();
assert_eq!(mkctx(10), args.context);
let args = parse_low_raw(["-A5", "-A0"]).unwrap();
assert_eq!(mkctx(0), args.context);
let args = parse_low_raw(["-A5", "--passthru"]).unwrap();
assert_eq!(ContextMode::Passthru, args.context);
let args = parse_low_raw(["--passthru", "-A5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let n = usize::MAX.to_string();
let args = parse_low_raw(["--after-context", n.as_str()]).unwrap();
assert_eq!(mkctx(usize::MAX), args.context);
#[cfg(target_pointer_width = "64")]
{
let n = (u128::from(u64::MAX) + 1).to_string();
let result = parse_low_raw(["--after-context", n.as_str()]);
assert!(result.is_err(), "{result:?}");
}
}
/// --auto-hybrid-regex
#[derive(Debug)]
struct AutoHybridRegex;
impl Flag for AutoHybridRegex {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"auto-hybrid-regex"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-auto-hybrid-regex")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
"(DEPRECATED) Use PCRE2 if appropriate."
}
fn doc_long(&self) -> &'static str {
r"
DEPRECATED. Use \flag{engine} instead.
.sp
When this flag is used, ripgrep will dynamically choose between supported regex
engines depending on the features used in a pattern. When ripgrep chooses a
regex engine, it applies that choice for every regex provided to ripgrep (e.g.,
via multiple \flag{regexp} or \flag{file} flags).
.sp
As an example of how this flag might behave, ripgrep will attempt to use
its default finite automata based regex engine whenever the pattern can be
successfully compiled with that regex engine. If PCRE2 is enabled and if the
pattern given could not be compiled with the default regex engine, then PCRE2
will be automatically used for searching. If PCRE2 isn't available, then this
flag has no effect because there is only one regex engine to choose from.
.sp
In the future, ripgrep may adjust its heuristics for how it decides which
regex engine to use. In general, the heuristics will be limited to a static
analysis of the patterns, and not to any specific runtime behavior observed
while searching files.
.sp
The primary downside of using this flag is that it may not always be obvious
which regex engine ripgrep uses, and thus, the match semantics or performance
profile of ripgrep may subtly and unexpectedly change. However, in many cases,
all regex engines will agree on what constitutes a match and it can be nice
to transparently support more advanced regex features like look-around and
backreferences without explicitly needing to enable them.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let mode = if v.unwrap_switch() {
EngineChoice::Auto
} else {
EngineChoice::Default
};
args.engine = mode;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_auto_hybrid_regex() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(EngineChoice::Default, args.engine);
let args = parse_low_raw(["--auto-hybrid-regex"]).unwrap();
assert_eq!(EngineChoice::Auto, args.engine);
let args =
parse_low_raw(["--auto-hybrid-regex", "--no-auto-hybrid-regex"])
.unwrap();
assert_eq!(EngineChoice::Default, args.engine);
let args =
parse_low_raw(["--no-auto-hybrid-regex", "--auto-hybrid-regex"])
.unwrap();
assert_eq!(EngineChoice::Auto, args.engine);
let args = parse_low_raw(["--auto-hybrid-regex", "-P"]).unwrap();
assert_eq!(EngineChoice::PCRE2, args.engine);
let args = parse_low_raw(["-P", "--auto-hybrid-regex"]).unwrap();
assert_eq!(EngineChoice::Auto, args.engine);
let args =
parse_low_raw(["--engine=auto", "--auto-hybrid-regex"]).unwrap();
assert_eq!(EngineChoice::Auto, args.engine);
let args =
parse_low_raw(["--engine=default", "--auto-hybrid-regex"]).unwrap();
assert_eq!(EngineChoice::Auto, args.engine);
let args =
parse_low_raw(["--auto-hybrid-regex", "--engine=default"]).unwrap();
assert_eq!(EngineChoice::Default, args.engine);
}
/// -B/--before-context
#[derive(Debug)]
struct BeforeContext;
impl Flag for BeforeContext {
fn is_switch(&self) -> bool {
false
}
fn name_short(&self) -> Option<u8> {
Some(b'B')
}
fn name_long(&self) -> &'static str {
"before-context"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("NUM")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
"Show NUM lines before each match."
}
fn doc_long(&self) -> &'static str {
r"
Show \fINUM\fP lines before each match.
.sp
This overrides the \flag{passthru} flag and partially overrides the
\flag{context} flag.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.context.set_before(convert::usize(&v.unwrap_value())?);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_before_context() {
let mkctx = |lines| {
let mut mode = ContextMode::default();
mode.set_before(lines);
mode
};
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(ContextMode::default(), args.context);
let args = parse_low_raw(["--before-context", "5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let args = parse_low_raw(["--before-context=5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let args = parse_low_raw(["-B", "5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let args = parse_low_raw(["-B5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let args = parse_low_raw(["-B5", "-B10"]).unwrap();
assert_eq!(mkctx(10), args.context);
let args = parse_low_raw(["-B5", "-B0"]).unwrap();
assert_eq!(mkctx(0), args.context);
let args = parse_low_raw(["-B5", "--passthru"]).unwrap();
assert_eq!(ContextMode::Passthru, args.context);
let args = parse_low_raw(["--passthru", "-B5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let n = usize::MAX.to_string();
let args = parse_low_raw(["--before-context", n.as_str()]).unwrap();
assert_eq!(mkctx(usize::MAX), args.context);
#[cfg(target_pointer_width = "64")]
{
let n = (u128::from(u64::MAX) + 1).to_string();
let result = parse_low_raw(["--before-context", n.as_str()]);
assert!(result.is_err(), "{result:?}");
}
}
/// --binary
#[derive(Debug)]
struct Binary;
impl Flag for Binary {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"binary"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-binary")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
"Search binary files."
}
fn doc_long(&self) -> &'static str {
r"
Enabling this flag will cause ripgrep to search binary files. By default,
ripgrep attempts to automatically skip binary files in order to improve the
relevance of results and make the search faster.
.sp
Binary files are heuristically detected based on whether they contain a
\fBNUL\fP byte or not. By default (without this flag set), once a \fBNUL\fP
byte is seen, ripgrep will stop searching the file. Usually, \fBNUL\fP bytes
occur in the beginning of most binary files. If a \fBNUL\fP byte occurs after
a match, then ripgrep will not print the match, stop searching that file, and
emit a warning that some matches are being suppressed.
.sp
In contrast, when this flag is provided, ripgrep will continue searching a
file even if a \fBNUL\fP byte is found. In particular, if a \fBNUL\fP byte is
found then ripgrep will continue searching until either a match is found or
the end of the file is reached, whichever comes sooner. If a match is found,
then ripgrep will stop and print a warning saying that the search stopped
prematurely.
.sp
If you want ripgrep to search a file without any special \fBNUL\fP byte
handling at all (and potentially print binary data to stdout), then you should
use the \flag{text} flag.
.sp
The \flag{binary} flag is a flag for controlling ripgrep's automatic filtering
mechanism. As such, it does not need to be used when searching a file
explicitly or when searching stdin. That is, it is only applicable when
recursively searching a directory.
.sp
When the \flag{unrestricted} flag is provided for a third time, then this flag
is automatically enabled.
.sp
This flag overrides the \flag{text} flag.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.binary = if v.unwrap_switch() {
BinaryMode::SearchAndSuppress
} else {
BinaryMode::Auto
};
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_binary() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(BinaryMode::Auto, args.binary);
let args = parse_low_raw(["--binary"]).unwrap();
assert_eq!(BinaryMode::SearchAndSuppress, args.binary);
let args = parse_low_raw(["--binary", "--no-binary"]).unwrap();
assert_eq!(BinaryMode::Auto, args.binary);
let args = parse_low_raw(["--no-binary", "--binary"]).unwrap();
assert_eq!(BinaryMode::SearchAndSuppress, args.binary);
let args = parse_low_raw(["--binary", "-a"]).unwrap();
assert_eq!(BinaryMode::AsText, args.binary);
let args = parse_low_raw(["-a", "--binary"]).unwrap();
assert_eq!(BinaryMode::SearchAndSuppress, args.binary);
let args = parse_low_raw(["-a", "--no-binary"]).unwrap();
assert_eq!(BinaryMode::Auto, args.binary);
}
/// --block-buffered
#[derive(Debug)]
struct BlockBuffered;
impl Flag for BlockBuffered {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"block-buffered"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-block-buffered")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
"Force block buffering."
}
fn doc_long(&self) -> &'static str {
r"
When enabled, ripgrep will use block buffering. That is, whenever a matching
line is found, it will be written to an in-memory buffer and will not be
written to stdout until the buffer reaches a certain size. This is the default
when ripgrep's stdout is redirected to a pipeline or a file. When ripgrep's
stdout is connected to a tty, line buffering will be used by default. Forcing
block buffering can be useful when dumping a large amount of contents to a tty.
.sp
This overrides the \flag{line-buffered} flag.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.buffer = if v.unwrap_switch() {
BufferMode::Block
} else {
BufferMode::Auto
};
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_block_buffered() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(BufferMode::Auto, args.buffer);
let args = parse_low_raw(["--block-buffered"]).unwrap();
assert_eq!(BufferMode::Block, args.buffer);
let args =
parse_low_raw(["--block-buffered", "--no-block-buffered"]).unwrap();
assert_eq!(BufferMode::Auto, args.buffer);
let args = parse_low_raw(["--block-buffered", "--line-buffered"]).unwrap();
assert_eq!(BufferMode::Line, args.buffer);
}
/// --byte-offset
#[derive(Debug)]
struct ByteOffset;
impl Flag for ByteOffset {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'b')
}
fn name_long(&self) -> &'static str {
"byte-offset"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-byte-offset")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
"Print the byte offset for each matching line."
}
fn doc_long(&self) -> &'static str {
r"
Print the 0-based byte offset within the input file before each line of output.
If \flag{only-matching} is specified, print the offset of the matched text
itself.
.sp
If ripgrep does transcoding, then the byte offset is in terms of the result
of transcoding and not the original data. This applies similarly to other
transformations on the data, such as decompression or a \flag{pre} filter.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.byte_offset = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_byte_offset() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.byte_offset);
let args = parse_low_raw(["--byte-offset"]).unwrap();
assert_eq!(true, args.byte_offset);
let args = parse_low_raw(["-b"]).unwrap();
assert_eq!(true, args.byte_offset);
let args = parse_low_raw(["--byte-offset", "--no-byte-offset"]).unwrap();
assert_eq!(false, args.byte_offset);
let args = parse_low_raw(["--no-byte-offset", "-b"]).unwrap();
assert_eq!(true, args.byte_offset);
}
/// -s/--case-sensitive
#[derive(Debug)]
struct CaseSensitive;
impl Flag for CaseSensitive {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b's')
}
fn name_long(&self) -> &'static str {
"case-sensitive"
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Search case sensitively (default)."
}
fn doc_long(&self) -> &'static str {
r"
Execute the search case sensitively. This is the default mode.
.sp
This is a global option that applies to all patterns given to ripgrep.
Individual patterns can still be matched case insensitively by using inline
regex flags. For example, \fB(?i)abc\fP will match \fBabc\fP case insensitively
even when this flag is used.
.sp
This flag overrides the \flag{ignore-case} and \flag{smart-case} flags.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "flag has no negation");
args.case = CaseMode::Sensitive;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_case_sensitive() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(CaseMode::Sensitive, args.case);
let args = parse_low_raw(["--case-sensitive"]).unwrap();
assert_eq!(CaseMode::Sensitive, args.case);
let args = parse_low_raw(["-s"]).unwrap();
assert_eq!(CaseMode::Sensitive, args.case);
}
/// --color
#[derive(Debug)]
struct Color;
impl Flag for Color {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"color"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("WHEN")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
"When to use color."
}
fn doc_long(&self) -> &'static str {
r"
This flag controls when to use colors. The default setting is \fBauto\fP, which
means ripgrep will try to guess when to use colors. For example, if ripgrep is
printing to a tty, then it will use colors, but if it is redirected to a file
or a pipe, then it will suppress color output.
.sp
ripgrep will suppress color output by default in some other circumstances as
well. These include, but are not limited to:
.sp
.IP \(bu 3n
When the \fBTERM\fP environment variable is not set or set to \fBdumb\fP.
.sp
.IP \(bu 3n
When the \fBNO_COLOR\fP environment variable is set (regardless of value).
.sp
.IP \(bu 3n
When flags that imply no use for colors are given. For example,
\flag{vimgrep} and \flag{json}.
.
.PP
The possible values for this flag are:
.sp
.IP \fBnever\fP 10n
Colors will never be used.
.sp
.IP \fBauto\fP 10n
The default. ripgrep tries to be smart.
.sp
.IP \fBalways\fP 10n
Colors will always be used regardless of where output is sent.
.sp
.IP \fBansi\fP 10n
Like 'always', but emits ANSI escapes (even in a Windows console).
.
.PP
This flag also controls whether hyperlinks are emitted. For example, when
a hyperlink format is specified, hyperlinks won't be used when color is
suppressed. If one wants to emit hyperlinks but no colors, then one must use
the \flag{colors} flag to manually set all color styles to \fBnone\fP:
.sp
.EX
\-\-colors 'path:none' \\
\-\-colors 'line:none' \\
\-\-colors 'column:none' \\
\-\-colors 'match:none'
.EE
.sp
"
}
fn doc_choices(&self) -> &'static [&'static str] {
&["never", "auto", "always", "ansi"]
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.color = match convert::str(&v.unwrap_value())? {
"never" => ColorChoice::Never,
"auto" => ColorChoice::Auto,
"always" => ColorChoice::Always,
"ansi" => ColorChoice::Ansi,
unk => anyhow::bail!("choice '{unk}' is unrecognized"),
};
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_color() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(ColorChoice::Auto, args.color);
let args = parse_low_raw(["--color", "never"]).unwrap();
assert_eq!(ColorChoice::Never, args.color);
let args = parse_low_raw(["--color", "auto"]).unwrap();
assert_eq!(ColorChoice::Auto, args.color);
let args = parse_low_raw(["--color", "always"]).unwrap();
assert_eq!(ColorChoice::Always, args.color);
let args = parse_low_raw(["--color", "ansi"]).unwrap();
assert_eq!(ColorChoice::Ansi, args.color);
let args = parse_low_raw(["--color=never"]).unwrap();
assert_eq!(ColorChoice::Never, args.color);
let args =
parse_low_raw(["--color", "always", "--color", "never"]).unwrap();
assert_eq!(ColorChoice::Never, args.color);
let args =
parse_low_raw(["--color", "never", "--color", "always"]).unwrap();
assert_eq!(ColorChoice::Always, args.color);
let result = parse_low_raw(["--color", "foofoo"]);
assert!(result.is_err(), "{result:?}");
let result = parse_low_raw(["--color", "Always"]);
assert!(result.is_err(), "{result:?}");
}
/// --colors
#[derive(Debug)]
struct Colors;
impl Flag for Colors {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"colors"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("COLOR_SPEC")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
"Configure color settings and styles."
}
fn doc_long(&self) -> &'static str {
r"
This flag specifies color settings for use in the output. This flag may be
provided multiple times. Settings are applied iteratively. Pre-existing color
labels are limited to one of eight choices: \fBred\fP, \fBblue\fP, \fBgreen\fP,
\fBcyan\fP, \fBmagenta\fP, \fByellow\fP, \fBwhite\fP and \fBblack\fP. Styles
are limited to \fBnobold\fP, \fBbold\fP, \fBnointense\fP, \fBintense\fP,
\fBnounderline\fP or \fBunderline\fP.
.sp
The format of the flag is
\fB{\fP\fItype\fP\fB}:{\fP\fIattribute\fP\fB}:{\fP\fIvalue\fP\fB}\fP.
\fItype\fP should be one of \fBpath\fP, \fBline\fP, \fBcolumn\fP or
\fBmatch\fP. \fIattribute\fP can be \fBfg\fP, \fBbg\fP or \fBstyle\fP.
\fIvalue\fP is either a color (for \fBfg\fP and \fBbg\fP) or a text style. A
special format, \fB{\fP\fItype\fP\fB}:none\fP, will clear all color settings
for \fItype\fP.
.sp
For example, the following command will change the match color to magenta and
the background color for line numbers to yellow:
.sp
.EX
rg \-\-colors 'match:fg:magenta' \-\-colors 'line:bg:yellow'
.EE
.sp
Extended colors can be used for \fIvalue\fP when the tty supports ANSI color
sequences. These are specified as either \fIx\fP (256-color) or
.IB x , x , x
(24-bit truecolor) where \fIx\fP is a number between \fB0\fP and \fB255\fP
inclusive. \fIx\fP may be given as a normal decimal number or a hexadecimal
number, which is prefixed by \fB0x\fP.
.sp
For example, the following command will change the match background color to
that represented by the rgb value (0,128,255):
.sp
.EX
rg \-\-colors 'match:bg:0,128,255'
.EE
.sp
or, equivalently,
.sp
.EX
rg \-\-colors 'match:bg:0x0,0x80,0xFF'
.EE
.sp
Note that the \fBintense\fP and \fBnointense\fP styles will have no effect when
used alongside these extended color codes.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let v = v.unwrap_value();
let v = convert::str(&v)?;
args.colors.push(v.parse()?);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_colors() {
let args = parse_low_raw(None::<&str>).unwrap();
assert!(args.colors.is_empty());
let args = parse_low_raw(["--colors", "match:fg:magenta"]).unwrap();
assert_eq!(args.colors, vec!["match:fg:magenta".parse().unwrap()]);
let args = parse_low_raw([
"--colors",
"match:fg:magenta",
"--colors",
"line:bg:yellow",
])
.unwrap();
assert_eq!(
args.colors,
vec![
"match:fg:magenta".parse().unwrap(),
"line:bg:yellow".parse().unwrap()
]
);
}
/// --column
#[derive(Debug)]
struct Column;
impl Flag for Column {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"column"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-column")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
"Show column numbers."
}
fn doc_long(&self) -> &'static str {
r"
Show column numbers (1-based). This only shows the column numbers for the first
match on each line. This does not try to account for Unicode. One byte is equal
to one column. This implies \flag{line-number}.
.sp
When \flag{only-matching} is used, then the column numbers written correspond
to the start of each match.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.column = Some(v.unwrap_switch());
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_column() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.column);
let args = parse_low_raw(["--column"]).unwrap();
assert_eq!(Some(true), args.column);
let args = parse_low_raw(["--column", "--no-column"]).unwrap();
assert_eq!(Some(false), args.column);
let args = parse_low_raw(["--no-column", "--column"]).unwrap();
assert_eq!(Some(true), args.column);
}
/// -C/--context
#[derive(Debug)]
struct Context;
impl Flag for Context {
fn is_switch(&self) -> bool {
false
}
fn name_short(&self) -> Option<u8> {
Some(b'C')
}
fn name_long(&self) -> &'static str {
"context"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("NUM")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Show NUM lines before and after each match."
}
fn doc_long(&self) -> &'static str {
r"
Show \fINUM\fP lines before and after each match. This is equivalent to
providing both the \flag{before-context} and \flag{after-context} flags with
the same value.
.sp
This overrides the \flag{passthru} flag. The \flag{after-context} and
\flag{before-context} flags both partially override this flag, regardless of
the order. For example, \fB\-A2 \-C1\fP is equivalent to \fB\-A2 \-B1\fP.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.context.set_both(convert::usize(&v.unwrap_value())?);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_context() {
let mkctx = |lines| {
let mut mode = ContextMode::default();
mode.set_both(lines);
mode
};
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(ContextMode::default(), args.context);
let args = parse_low_raw(["--context", "5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let args = parse_low_raw(["--context=5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let args = parse_low_raw(["-C", "5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let args = parse_low_raw(["-C5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let args = parse_low_raw(["-C5", "-C10"]).unwrap();
assert_eq!(mkctx(10), args.context);
let args = parse_low_raw(["-C5", "-C0"]).unwrap();
assert_eq!(mkctx(0), args.context);
let args = parse_low_raw(["-C5", "--passthru"]).unwrap();
assert_eq!(ContextMode::Passthru, args.context);
let args = parse_low_raw(["--passthru", "-C5"]).unwrap();
assert_eq!(mkctx(5), args.context);
let n = usize::MAX.to_string();
let args = parse_low_raw(["--context", n.as_str()]).unwrap();
assert_eq!(mkctx(usize::MAX), args.context);
#[cfg(target_pointer_width = "64")]
{
let n = (u128::from(u64::MAX) + 1).to_string();
let result = parse_low_raw(["--context", n.as_str()]);
assert!(result.is_err(), "{result:?}");
}
// Test the interaction between -A/-B and -C. Basically, -A/-B always
// partially overrides -C, regardless of where they appear relative to
// each other. This behavior is also how GNU grep works, and it also makes
// logical sense to me: -A/-B are the more specific flags.
let args = parse_low_raw(["-A1", "-C5"]).unwrap();
let mut mode = ContextMode::default();
mode.set_after(1);
mode.set_both(5);
assert_eq!(mode, args.context);
assert_eq!((5, 1), args.context.get_limited());
let args = parse_low_raw(["-B1", "-C5"]).unwrap();
let mut mode = ContextMode::default();
mode.set_before(1);
mode.set_both(5);
assert_eq!(mode, args.context);
assert_eq!((1, 5), args.context.get_limited());
let args = parse_low_raw(["-A1", "-B2", "-C5"]).unwrap();
let mut mode = ContextMode::default();
mode.set_before(2);
mode.set_after(1);
mode.set_both(5);
assert_eq!(mode, args.context);
assert_eq!((2, 1), args.context.get_limited());
// These next three are like the ones above, but with -C before -A/-B. This
// tests that -A and -B only partially override -C. That is, -C1 -A2 is
// equivalent to -B1 -A2.
let args = parse_low_raw(["-C5", "-A1"]).unwrap();
let mut mode = ContextMode::default();
mode.set_after(1);
mode.set_both(5);
assert_eq!(mode, args.context);
assert_eq!((5, 1), args.context.get_limited());
let args = parse_low_raw(["-C5", "-B1"]).unwrap();
let mut mode = ContextMode::default();
mode.set_before(1);
mode.set_both(5);
assert_eq!(mode, args.context);
assert_eq!((1, 5), args.context.get_limited());
let args = parse_low_raw(["-C5", "-A1", "-B2"]).unwrap();
let mut mode = ContextMode::default();
mode.set_before(2);
mode.set_after(1);
mode.set_both(5);
assert_eq!(mode, args.context);
assert_eq!((2, 1), args.context.get_limited());
}
/// --context-separator
#[derive(Debug)]
struct ContextSeparator;
impl Flag for ContextSeparator {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"context-separator"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-context-separator")
}
fn doc_variable(&self) -> Option<&'static str> {
Some("SEPARATOR")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Set the separator for contextual chunks."
}
fn doc_long(&self) -> &'static str {
r"
The string used to separate non-contiguous context lines in the output. This is
only used when one of the context flags is used (that is, \flag{after-context},
\flag{before-context} or \flag{context}). Escape sequences like \fB\\x7F\fP or
\fB\\t\fP may be used. The default value is \fB\-\-\fP.
.sp
When the context separator is set to an empty string, then a line break
is still inserted. To completely disable context separators, use the
\flag-negate{context-separator} flag.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
use crate::flags::lowargs::ContextSeparator as Separator;
args.context_separator = match v {
FlagValue::Switch(true) => {
unreachable!("flag can only be disabled")
}
FlagValue::Switch(false) => Separator::disabled(),
FlagValue::Value(v) => Separator::new(&v)?,
};
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_context_separator() {
use bstr::BString;
use crate::flags::lowargs::ContextSeparator as Separator;
let getbytes = |ctxsep: Separator| ctxsep.into_bytes().map(BString::from);
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Some(BString::from("--")), getbytes(args.context_separator));
let args = parse_low_raw(["--context-separator", "XYZ"]).unwrap();
assert_eq!(Some(BString::from("XYZ")), getbytes(args.context_separator));
let args = parse_low_raw(["--no-context-separator"]).unwrap();
assert_eq!(None, getbytes(args.context_separator));
let args = parse_low_raw([
"--context-separator",
"XYZ",
"--no-context-separator",
])
.unwrap();
assert_eq!(None, getbytes(args.context_separator));
let args = parse_low_raw([
"--no-context-separator",
"--context-separator",
"XYZ",
])
.unwrap();
assert_eq!(Some(BString::from("XYZ")), getbytes(args.context_separator));
// This checks that invalid UTF-8 can be used. This case isn't too tricky
// to handle, because it passes the invalid UTF-8 as an escape sequence
// that is itself valid UTF-8. It doesn't become invalid UTF-8 until after
// the argument is parsed and then unescaped.
let args = parse_low_raw(["--context-separator", r"\xFF"]).unwrap();
assert_eq!(Some(BString::from(b"\xFF")), getbytes(args.context_separator));
// In this case, we specifically try to pass an invalid UTF-8 argument to
// the flag. In theory we might be able to support this, but because we do
// unescaping and because unescaping wants valid UTF-8, we do a UTF-8 check
// on the value. Since we pass invalid UTF-8, it fails. This demonstrates
// that the only way to use an invalid UTF-8 separator is by specifying an
// escape sequence that is itself valid UTF-8.
#[cfg(unix)]
{
use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
let result = parse_low_raw([
OsStr::from_bytes(b"--context-separator"),
OsStr::from_bytes(&[0xFF]),
]);
assert!(result.is_err(), "{result:?}");
}
}
/// -c/--count
#[derive(Debug)]
struct Count;
impl Flag for Count {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'c')
}
fn name_long(&self) -> &'static str {
"count"
}
fn doc_category(&self) -> Category {
Category::OutputModes
}
fn doc_short(&self) -> &'static str {
r"Show count of matching lines for each file."
}
fn doc_long(&self) -> &'static str {
r"
This flag suppresses normal output and shows the number of lines that match the
given patterns for each file searched. Each file containing a match has its
path and count printed on each line. Note that unless \flag{multiline}
is enabled, this reports the number of lines that match and not the total
number of matches. In multiline mode, \flag{count} is equivalent to
\flag{count-matches}.
.sp
If only one file is given to ripgrep, then only the count is printed if there
is a match. The \flag{with-filename} flag can be used to force printing the
file path in this case. If you need a count to be printed regardless of whether
there is a match, then use \flag{include-zero}.
.sp
This overrides the \flag{count-matches} flag. Note that when \flag{count}
is combined with \flag{only-matching}, then ripgrep behaves as if
\flag{count-matches} was given.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--count can only be enabled");
args.mode.update(Mode::Search(SearchMode::Count));
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_count() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Mode::Search(SearchMode::Standard), args.mode);
let args = parse_low_raw(["--count"]).unwrap();
assert_eq!(Mode::Search(SearchMode::Count), args.mode);
let args = parse_low_raw(["-c"]).unwrap();
assert_eq!(Mode::Search(SearchMode::Count), args.mode);
let args = parse_low_raw(["--count-matches", "--count"]).unwrap();
assert_eq!(Mode::Search(SearchMode::Count), args.mode);
let args = parse_low_raw(["--count-matches", "-c"]).unwrap();
assert_eq!(Mode::Search(SearchMode::Count), args.mode);
}
/// --count-matches
#[derive(Debug)]
struct CountMatches;
impl Flag for CountMatches {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"count-matches"
}
fn doc_variable(&self) -> Option<&'static str> {
None
}
fn doc_category(&self) -> Category {
Category::OutputModes
}
fn doc_short(&self) -> &'static str {
r"Show count of every match for each file."
}
fn doc_long(&self) -> &'static str {
r"
This flag suppresses normal output and shows the number of individual matches
of the given patterns for each file searched. Each file containing matches has
its path and match count printed on each line. Note that this reports the total
number of individual matches and not the number of lines that match.
.sp
If only one file is given to ripgrep, then only the count is printed if there
is a match. The \flag{with-filename} flag can be used to force printing the
file path in this case.
.sp
This overrides the \flag{count} flag. Note that when \flag{count} is combined
with \flag{only-matching}, then ripgrep behaves as if \flag{count-matches} was
given.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--count-matches can only be enabled");
args.mode.update(Mode::Search(SearchMode::CountMatches));
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_count_matches() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Mode::Search(SearchMode::Standard), args.mode);
let args = parse_low_raw(["--count-matches"]).unwrap();
assert_eq!(Mode::Search(SearchMode::CountMatches), args.mode);
let args = parse_low_raw(["--count", "--count-matches"]).unwrap();
assert_eq!(Mode::Search(SearchMode::CountMatches), args.mode);
let args = parse_low_raw(["-c", "--count-matches"]).unwrap();
assert_eq!(Mode::Search(SearchMode::CountMatches), args.mode);
}
/// --crlf
#[derive(Debug)]
struct Crlf;
impl Flag for Crlf {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"crlf"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-crlf")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Use CRLF line terminators (nice for Windows)."
}
fn doc_long(&self) -> &'static str {
r"
When enabled, ripgrep will treat CRLF (\fB\\r\\n\fP) as a line terminator
instead of just \fB\\n\fP.
.sp
Principally, this permits the line anchor assertions \fB^\fP and \fB$\fP in
regex patterns to treat CRLF, CR or LF as line terminators instead of just LF.
Note that they will never match between a CR and a LF. CRLF is treated as one
single line terminator.
.sp
When using the default regex engine, CRLF support can also be enabled inside
the pattern with the \fBR\fP flag. For example, \fB(?R:$)\fP will match just
before either CR or LF, but never between CR and LF.
.sp
This flag overrides \flag{null-data}.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.crlf = v.unwrap_switch();
if args.crlf {
args.null_data = false;
}
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_crlf() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.crlf);
let args = parse_low_raw(["--crlf"]).unwrap();
assert_eq!(true, args.crlf);
assert_eq!(false, args.null_data);
let args = parse_low_raw(["--crlf", "--null-data"]).unwrap();
assert_eq!(false, args.crlf);
assert_eq!(true, args.null_data);
let args = parse_low_raw(["--null-data", "--crlf"]).unwrap();
assert_eq!(true, args.crlf);
assert_eq!(false, args.null_data);
let args = parse_low_raw(["--null-data", "--no-crlf"]).unwrap();
assert_eq!(false, args.crlf);
assert_eq!(true, args.null_data);
let args = parse_low_raw(["--null-data", "--crlf", "--no-crlf"]).unwrap();
assert_eq!(false, args.crlf);
assert_eq!(false, args.null_data);
}
/// --debug
#[derive(Debug)]
struct Debug;
impl Flag for Debug {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"debug"
}
fn doc_category(&self) -> Category {
Category::Logging
}
fn doc_short(&self) -> &'static str {
r"Show debug messages."
}
fn doc_long(&self) -> &'static str {
r"
Show debug messages. Please use this when filing a bug report.
.sp
The \flag{debug} flag is generally useful for figuring out why ripgrep skipped
searching a particular file. The debug messages should mention all files
skipped and why they were skipped.
.sp
To get even more debug output, use the \flag{trace} flag, which implies
\flag{debug} along with additional trace data.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--debug can only be enabled");
args.logging = Some(LoggingMode::Debug);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_debug() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.logging);
let args = parse_low_raw(["--debug"]).unwrap();
assert_eq!(Some(LoggingMode::Debug), args.logging);
let args = parse_low_raw(["--trace", "--debug"]).unwrap();
assert_eq!(Some(LoggingMode::Debug), args.logging);
}
/// --dfa-size-limit
#[derive(Debug)]
struct DfaSizeLimit;
impl Flag for DfaSizeLimit {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"dfa-size-limit"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("NUM+SUFFIX?")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"The upper size limit of the regex DFA."
}
fn doc_long(&self) -> &'static str {
r"
The upper size limit of the regex DFA. The default limit is something generous
for any single pattern or for many smallish patterns. This should only be
changed on very large regex inputs where the (slower) fallback regex engine may
otherwise be used if the limit is reached.
.sp
The input format accepts suffixes of \fBK\fP, \fBM\fP or \fBG\fP which
correspond to kilobytes, megabytes and gigabytes, respectively. If no suffix is
provided the input is treated as bytes.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let v = v.unwrap_value();
args.dfa_size_limit = Some(convert::human_readable_usize(&v)?);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_dfa_size_limit() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.dfa_size_limit);
#[cfg(target_pointer_width = "64")]
{
let args = parse_low_raw(["--dfa-size-limit", "9G"]).unwrap();
assert_eq!(Some(9 * (1 << 30)), args.dfa_size_limit);
let args = parse_low_raw(["--dfa-size-limit=9G"]).unwrap();
assert_eq!(Some(9 * (1 << 30)), args.dfa_size_limit);
let args =
parse_low_raw(["--dfa-size-limit=9G", "--dfa-size-limit=0"])
.unwrap();
assert_eq!(Some(0), args.dfa_size_limit);
}
let args = parse_low_raw(["--dfa-size-limit=0K"]).unwrap();
assert_eq!(Some(0), args.dfa_size_limit);
let args = parse_low_raw(["--dfa-size-limit=0M"]).unwrap();
assert_eq!(Some(0), args.dfa_size_limit);
let args = parse_low_raw(["--dfa-size-limit=0G"]).unwrap();
assert_eq!(Some(0), args.dfa_size_limit);
let result = parse_low_raw(["--dfa-size-limit", "9999999999999999999999"]);
assert!(result.is_err(), "{result:?}");
let result = parse_low_raw(["--dfa-size-limit", "9999999999999999G"]);
assert!(result.is_err(), "{result:?}");
}
/// -E/--encoding
#[derive(Debug)]
struct Encoding;
impl Flag for Encoding {
fn is_switch(&self) -> bool {
false
}
fn name_short(&self) -> Option<u8> {
Some(b'E')
}
fn name_long(&self) -> &'static str {
"encoding"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-encoding")
}
fn doc_variable(&self) -> Option<&'static str> {
Some("ENCODING")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Specify the text encoding of files to search."
}
fn doc_long(&self) -> &'static str {
r"
Specify the text encoding that ripgrep will use on all files searched. The
default value is \fBauto\fP, which will cause ripgrep to do a best effort
automatic detection of encoding on a per-file basis. Automatic detection in
this case only applies to files that begin with a UTF-8 or UTF-16 byte-order
mark (BOM). No other automatic detection is performed. One can also specify
\fBnone\fP which will then completely disable BOM sniffing and always result
in searching the raw bytes, including a BOM if it's present, regardless of its
encoding.
.sp
Other supported values can be found in the list of labels here:
\fIhttps://encoding.spec.whatwg.org/#concept-encoding-get\fP.
.sp
For more details on encoding and how ripgrep deals with it, see \fBGUIDE.md\fP.
.sp
The encoding detection that ripgrep uses can be reverted to its automatic mode
via the \flag-negate{encoding} flag.
"
}
fn completion_type(&self) -> CompletionType {
CompletionType::Encoding
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let value = match v {
FlagValue::Value(v) => v,
FlagValue::Switch(true) => {
unreachable!("--encoding must accept a value")
}
FlagValue::Switch(false) => {
args.encoding = EncodingMode::Auto;
return Ok(());
}
};
let label = convert::str(&value)?;
args.encoding = match label {
"auto" => EncodingMode::Auto,
"none" => EncodingMode::Disabled,
_ => EncodingMode::Some(grep::searcher::Encoding::new(label)?),
};
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_encoding() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(EncodingMode::Auto, args.encoding);
let args = parse_low_raw(["--encoding", "auto"]).unwrap();
assert_eq!(EncodingMode::Auto, args.encoding);
let args = parse_low_raw(["--encoding", "none"]).unwrap();
assert_eq!(EncodingMode::Disabled, args.encoding);
let args = parse_low_raw(["--encoding=none"]).unwrap();
assert_eq!(EncodingMode::Disabled, args.encoding);
let args = parse_low_raw(["-E", "none"]).unwrap();
assert_eq!(EncodingMode::Disabled, args.encoding);
let args = parse_low_raw(["-Enone"]).unwrap();
assert_eq!(EncodingMode::Disabled, args.encoding);
let args = parse_low_raw(["-E", "none", "--no-encoding"]).unwrap();
assert_eq!(EncodingMode::Auto, args.encoding);
let args = parse_low_raw(["--no-encoding", "-E", "none"]).unwrap();
assert_eq!(EncodingMode::Disabled, args.encoding);
let args = parse_low_raw(["-E", "utf-16"]).unwrap();
let enc = grep::searcher::Encoding::new("utf-16").unwrap();
assert_eq!(EncodingMode::Some(enc), args.encoding);
let args = parse_low_raw(["-E", "utf-16", "--no-encoding"]).unwrap();
assert_eq!(EncodingMode::Auto, args.encoding);
let result = parse_low_raw(["-E", "foo"]);
assert!(result.is_err(), "{result:?}");
}
/// --engine
#[derive(Debug)]
struct Engine;
impl Flag for Engine {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"engine"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("ENGINE")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Specify which regex engine to use."
}
fn doc_long(&self) -> &'static str {
r"
Specify which regular expression engine to use. When you choose a regex engine,
it applies that choice for every regex provided to ripgrep (e.g., via multiple
\flag{regexp} or \flag{file} flags).
.sp
Accepted values are \fBdefault\fP, \fBpcre2\fP, or \fBauto\fP.
.sp
The default value is \fBdefault\fP, which is usually the fastest and should be
good for most use cases. The \fBpcre2\fP engine is generally useful when you
want to use features such as look-around or backreferences. \fBauto\fP will
dynamically choose between supported regex engines depending on the features
used in a pattern on a best effort basis.
.sp
Note that the \fBpcre2\fP engine is an optional ripgrep feature. If PCRE2
wasn't included in your build of ripgrep, then using this flag will result in
ripgrep printing an error message and exiting.
.sp
This overrides previous uses of the \flag{pcre2} and \flag{auto-hybrid-regex}
flags.
"
}
fn doc_choices(&self) -> &'static [&'static str] {
&["default", "pcre2", "auto"]
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let v = v.unwrap_value();
let string = convert::str(&v)?;
args.engine = match string {
"default" => EngineChoice::Default,
"pcre2" => EngineChoice::PCRE2,
"auto" => EngineChoice::Auto,
_ => anyhow::bail!("unrecognized regex engine '{string}'"),
};
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_engine() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(EngineChoice::Default, args.engine);
let args = parse_low_raw(["--engine", "pcre2"]).unwrap();
assert_eq!(EngineChoice::PCRE2, args.engine);
let args = parse_low_raw(["--engine=pcre2"]).unwrap();
assert_eq!(EngineChoice::PCRE2, args.engine);
let args =
parse_low_raw(["--auto-hybrid-regex", "--engine=pcre2"]).unwrap();
assert_eq!(EngineChoice::PCRE2, args.engine);
let args =
parse_low_raw(["--engine=pcre2", "--auto-hybrid-regex"]).unwrap();
assert_eq!(EngineChoice::Auto, args.engine);
let args =
parse_low_raw(["--auto-hybrid-regex", "--engine=auto"]).unwrap();
assert_eq!(EngineChoice::Auto, args.engine);
let args =
parse_low_raw(["--auto-hybrid-regex", "--engine=default"]).unwrap();
assert_eq!(EngineChoice::Default, args.engine);
let args =
parse_low_raw(["--engine=pcre2", "--no-auto-hybrid-regex"]).unwrap();
assert_eq!(EngineChoice::Default, args.engine);
}
/// --field-context-separator
#[derive(Debug)]
struct FieldContextSeparator;
impl Flag for FieldContextSeparator {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"field-context-separator"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("SEPARATOR")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Set the field context separator."
}
fn doc_long(&self) -> &'static str {
r"
Set the field context separator. This separator is only used when printing
contextual lines. It is used to delimit file paths, line numbers, columns and
the contextual line itself. The separator may be any number of bytes, including
zero. Escape sequences like \fB\\x7F\fP or \fB\\t\fP may be used.
.sp
The \fB-\fP character is the default value.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
use crate::flags::lowargs::FieldContextSeparator as Separator;
args.field_context_separator = Separator::new(&v.unwrap_value())?;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_field_context_separator() {
use bstr::BString;
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(BString::from("-"), args.field_context_separator.into_bytes());
let args = parse_low_raw(["--field-context-separator", "XYZ"]).unwrap();
assert_eq!(
BString::from("XYZ"),
args.field_context_separator.into_bytes()
);
let args = parse_low_raw(["--field-context-separator=XYZ"]).unwrap();
assert_eq!(
BString::from("XYZ"),
args.field_context_separator.into_bytes()
);
let args = parse_low_raw([
"--field-context-separator",
"XYZ",
"--field-context-separator",
"ABC",
])
.unwrap();
assert_eq!(
BString::from("ABC"),
args.field_context_separator.into_bytes()
);
let args = parse_low_raw(["--field-context-separator", r"\t"]).unwrap();
assert_eq!(BString::from("\t"), args.field_context_separator.into_bytes());
let args = parse_low_raw(["--field-context-separator", r"\x00"]).unwrap();
assert_eq!(
BString::from("\x00"),
args.field_context_separator.into_bytes()
);
// This checks that invalid UTF-8 can be used. This case isn't too tricky
// to handle, because it passes the invalid UTF-8 as an escape sequence
// that is itself valid UTF-8. It doesn't become invalid UTF-8 until after
// the argument is parsed and then unescaped.
let args = parse_low_raw(["--field-context-separator", r"\xFF"]).unwrap();
assert_eq!(
BString::from(b"\xFF"),
args.field_context_separator.into_bytes()
);
// In this case, we specifically try to pass an invalid UTF-8 argument to
// the flag. In theory we might be able to support this, but because we do
// unescaping and because unescaping wants valid UTF-8, we do a UTF-8 check
// on the value. Since we pass invalid UTF-8, it fails. This demonstrates
// that the only way to use an invalid UTF-8 separator is by specifying an
// escape sequence that is itself valid UTF-8.
#[cfg(unix)]
{
use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
let result = parse_low_raw([
OsStr::from_bytes(b"--field-context-separator"),
OsStr::from_bytes(&[0xFF]),
]);
assert!(result.is_err(), "{result:?}");
}
}
/// --field-match-separator
#[derive(Debug)]
struct FieldMatchSeparator;
impl Flag for FieldMatchSeparator {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"field-match-separator"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("SEPARATOR")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Set the field match separator."
}
fn doc_long(&self) -> &'static str {
r"
Set the field match separator. This separator is only used when printing
matching lines. It is used to delimit file paths, line numbers, columns and the
matching line itself. The separator may be any number of bytes, including zero.
Escape sequences like \fB\\x7F\fP or \fB\\t\fP may be used.
.sp
The \fB:\fP character is the default value.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
use crate::flags::lowargs::FieldMatchSeparator as Separator;
args.field_match_separator = Separator::new(&v.unwrap_value())?;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_field_match_separator() {
use bstr::BString;
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(BString::from(":"), args.field_match_separator.into_bytes());
let args = parse_low_raw(["--field-match-separator", "XYZ"]).unwrap();
assert_eq!(BString::from("XYZ"), args.field_match_separator.into_bytes());
let args = parse_low_raw(["--field-match-separator=XYZ"]).unwrap();
assert_eq!(BString::from("XYZ"), args.field_match_separator.into_bytes());
let args = parse_low_raw([
"--field-match-separator",
"XYZ",
"--field-match-separator",
"ABC",
])
.unwrap();
assert_eq!(BString::from("ABC"), args.field_match_separator.into_bytes());
let args = parse_low_raw(["--field-match-separator", r"\t"]).unwrap();
assert_eq!(BString::from("\t"), args.field_match_separator.into_bytes());
let args = parse_low_raw(["--field-match-separator", r"\x00"]).unwrap();
assert_eq!(BString::from("\x00"), args.field_match_separator.into_bytes());
// This checks that invalid UTF-8 can be used. This case isn't too tricky
// to handle, because it passes the invalid UTF-8 as an escape sequence
// that is itself valid UTF-8. It doesn't become invalid UTF-8 until after
// the argument is parsed and then unescaped.
let args = parse_low_raw(["--field-match-separator", r"\xFF"]).unwrap();
assert_eq!(
BString::from(b"\xFF"),
args.field_match_separator.into_bytes()
);
// In this case, we specifically try to pass an invalid UTF-8 argument to
// the flag. In theory we might be able to support this, but because we do
// unescaping and because unescaping wants valid UTF-8, we do a UTF-8 check
// on the value. Since we pass invalid UTF-8, it fails. This demonstrates
// that the only way to use an invalid UTF-8 separator is by specifying an
// escape sequence that is itself valid UTF-8.
#[cfg(unix)]
{
use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
let result = parse_low_raw([
OsStr::from_bytes(b"--field-match-separator"),
OsStr::from_bytes(&[0xFF]),
]);
assert!(result.is_err(), "{result:?}");
}
}
/// -f/--file
#[derive(Debug)]
struct File;
impl Flag for File {
fn is_switch(&self) -> bool {
false
}
fn name_short(&self) -> Option<u8> {
Some(b'f')
}
fn name_long(&self) -> &'static str {
"file"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("PATTERNFILE")
}
fn doc_category(&self) -> Category {
Category::Input
}
fn doc_short(&self) -> &'static str {
r"Search for patterns from the given file."
}
fn doc_long(&self) -> &'static str {
r"
Search for patterns from the given file, with one pattern per line. When this
flag is used multiple times or in combination with the \flag{regexp} flag, then
all patterns provided are searched. Empty pattern lines will match all input
lines, and the newline is not counted as part of the pattern.
.sp
A line is printed if and only if it matches at least one of the patterns.
.sp
When \fIPATTERNFILE\fP is \fB-\fP, then \fBstdin\fP will be read for the
patterns.
.sp
When \flag{file} or \flag{regexp} is used, then ripgrep treats all positional
arguments as files or directories to search.
"
}
fn completion_type(&self) -> CompletionType {
CompletionType::Filename
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let path = PathBuf::from(v.unwrap_value());
args.patterns.push(PatternSource::File(path));
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_file() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Vec::<PatternSource>::new(), args.patterns);
let args = parse_low_raw(["--file", "foo"]).unwrap();
assert_eq!(vec![PatternSource::File(PathBuf::from("foo"))], args.patterns);
let args = parse_low_raw(["--file=foo"]).unwrap();
assert_eq!(vec![PatternSource::File(PathBuf::from("foo"))], args.patterns);
let args = parse_low_raw(["-f", "foo"]).unwrap();
assert_eq!(vec![PatternSource::File(PathBuf::from("foo"))], args.patterns);
let args = parse_low_raw(["-ffoo"]).unwrap();
assert_eq!(vec![PatternSource::File(PathBuf::from("foo"))], args.patterns);
let args = parse_low_raw(["--file", "-foo"]).unwrap();
assert_eq!(
vec![PatternSource::File(PathBuf::from("-foo"))],
args.patterns
);
let args = parse_low_raw(["--file=-foo"]).unwrap();
assert_eq!(
vec![PatternSource::File(PathBuf::from("-foo"))],
args.patterns
);
let args = parse_low_raw(["-f", "-foo"]).unwrap();
assert_eq!(
vec![PatternSource::File(PathBuf::from("-foo"))],
args.patterns
);
let args = parse_low_raw(["-f-foo"]).unwrap();
assert_eq!(
vec![PatternSource::File(PathBuf::from("-foo"))],
args.patterns
);
let args = parse_low_raw(["--file=foo", "--file", "bar"]).unwrap();
assert_eq!(
vec![
PatternSource::File(PathBuf::from("foo")),
PatternSource::File(PathBuf::from("bar"))
],
args.patterns
);
// We permit path arguments to be invalid UTF-8. So test that. Some of
// these cases are tricky and depend on lexopt doing the right thing.
//
// We probably should add tests for this handling on Windows too, but paths
// that are invalid UTF-16 appear incredibly rare in the Windows world.
#[cfg(unix)]
{
use std::{
ffi::{OsStr, OsString},
os::unix::ffi::{OsStrExt, OsStringExt},
};
let bytes = &[b'A', 0xFF, b'Z'][..];
let path = PathBuf::from(OsString::from_vec(bytes.to_vec()));
let args = parse_low_raw([
OsStr::from_bytes(b"--file"),
OsStr::from_bytes(bytes),
])
.unwrap();
assert_eq!(vec![PatternSource::File(path.clone())], args.patterns);
let args = parse_low_raw([
OsStr::from_bytes(b"-f"),
OsStr::from_bytes(bytes),
])
.unwrap();
assert_eq!(vec![PatternSource::File(path.clone())], args.patterns);
let mut bytes = b"--file=A".to_vec();
bytes.push(0xFF);
bytes.push(b'Z');
let args = parse_low_raw([OsStr::from_bytes(&bytes)]).unwrap();
assert_eq!(vec![PatternSource::File(path.clone())], args.patterns);
let mut bytes = b"-fA".to_vec();
bytes.push(0xFF);
bytes.push(b'Z');
let args = parse_low_raw([OsStr::from_bytes(&bytes)]).unwrap();
assert_eq!(vec![PatternSource::File(path.clone())], args.patterns);
}
}
/// --files
#[derive(Debug)]
struct Files;
impl Flag for Files {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"files"
}
fn doc_category(&self) -> Category {
Category::OtherBehaviors
}
fn doc_short(&self) -> &'static str {
r"Print each file that would be searched."
}
fn doc_long(&self) -> &'static str {
r"
Print each file that would be searched without actually performing the search.
This is useful to determine whether a particular file is being searched or not.
.sp
This overrides \flag{type-list}.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch());
args.mode.update(Mode::Files);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_files() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Mode::Search(SearchMode::Standard), args.mode);
let args = parse_low_raw(["--files"]).unwrap();
assert_eq!(Mode::Files, args.mode);
}
/// -l/--files-with-matches
#[derive(Debug)]
struct FilesWithMatches;
impl Flag for FilesWithMatches {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'l')
}
fn name_long(&self) -> &'static str {
"files-with-matches"
}
fn doc_category(&self) -> Category {
Category::OutputModes
}
fn doc_short(&self) -> &'static str {
r"Print the paths with at least one match."
}
fn doc_long(&self) -> &'static str {
r"
Print only the paths with at least one match and suppress match contents.
.sp
This overrides \flag{files-without-match}.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--files-with-matches can only be enabled");
args.mode.update(Mode::Search(SearchMode::FilesWithMatches));
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_files_with_matches() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Mode::Search(SearchMode::Standard), args.mode);
let args = parse_low_raw(["--files-with-matches"]).unwrap();
assert_eq!(Mode::Search(SearchMode::FilesWithMatches), args.mode);
let args = parse_low_raw(["-l"]).unwrap();
assert_eq!(Mode::Search(SearchMode::FilesWithMatches), args.mode);
}
/// -l/--files-without-match
#[derive(Debug)]
struct FilesWithoutMatch;
impl Flag for FilesWithoutMatch {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"files-without-match"
}
fn doc_category(&self) -> Category {
Category::OutputModes
}
fn doc_short(&self) -> &'static str {
r"Print the paths that contain zero matches."
}
fn doc_long(&self) -> &'static str {
r"
Print the paths that contain zero matches and suppress match contents.
.sp
This overrides \flag{files-with-matches}.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(
v.unwrap_switch(),
"--files-without-match can only be enabled"
);
args.mode.update(Mode::Search(SearchMode::FilesWithoutMatch));
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_files_without_match() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Mode::Search(SearchMode::Standard), args.mode);
let args = parse_low_raw(["--files-without-match"]).unwrap();
assert_eq!(Mode::Search(SearchMode::FilesWithoutMatch), args.mode);
let args =
parse_low_raw(["--files-with-matches", "--files-without-match"])
.unwrap();
assert_eq!(Mode::Search(SearchMode::FilesWithoutMatch), args.mode);
let args =
parse_low_raw(["--files-without-match", "--files-with-matches"])
.unwrap();
assert_eq!(Mode::Search(SearchMode::FilesWithMatches), args.mode);
}
/// -F/--fixed-strings
#[derive(Debug)]
struct FixedStrings;
impl Flag for FixedStrings {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'F')
}
fn name_long(&self) -> &'static str {
"fixed-strings"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-fixed-strings")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Treat all patterns as literals."
}
fn doc_long(&self) -> &'static str {
r"
Treat all patterns as literals instead of as regular expressions. When this
flag is used, special regular expression meta characters such as \fB.(){}*+\fP
should not need be escaped.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.fixed_strings = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_fixed_strings() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.fixed_strings);
let args = parse_low_raw(["--fixed-strings"]).unwrap();
assert_eq!(true, args.fixed_strings);
let args = parse_low_raw(["-F"]).unwrap();
assert_eq!(true, args.fixed_strings);
let args = parse_low_raw(["-F", "--no-fixed-strings"]).unwrap();
assert_eq!(false, args.fixed_strings);
let args = parse_low_raw(["--no-fixed-strings", "-F"]).unwrap();
assert_eq!(true, args.fixed_strings);
}
/// -L/--follow
#[derive(Debug)]
struct Follow;
impl Flag for Follow {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'L')
}
fn name_long(&self) -> &'static str {
"follow"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-follow")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Follow symbolic links."
}
fn doc_long(&self) -> &'static str {
r"
This flag instructs ripgrep to follow symbolic links while traversing
directories. This behavior is disabled by default. Note that ripgrep will
check for symbolic link loops and report errors if it finds one. ripgrep will
also report errors for broken links. To suppress error messages, use the
\flag{no-messages} flag.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.follow = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_follow() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.follow);
let args = parse_low_raw(["--follow"]).unwrap();
assert_eq!(true, args.follow);
let args = parse_low_raw(["-L"]).unwrap();
assert_eq!(true, args.follow);
let args = parse_low_raw(["-L", "--no-follow"]).unwrap();
assert_eq!(false, args.follow);
let args = parse_low_raw(["--no-follow", "-L"]).unwrap();
assert_eq!(true, args.follow);
}
/// --generate
#[derive(Debug)]
struct Generate;
impl Flag for Generate {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"generate"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("KIND")
}
fn doc_category(&self) -> Category {
Category::OtherBehaviors
}
fn doc_short(&self) -> &'static str {
r"Generate man pages and completion scripts."
}
fn doc_long(&self) -> &'static str {
r"
This flag instructs ripgrep to generate some special kind of output identified
by \fIKIND\fP and then quit without searching. \fIKIND\fP can be one of the
following values:
.sp
.TP 15
\fBman\fP
Generates a manual page for ripgrep in the \fBroff\fP format.
.TP 15
\fBcomplete\-bash\fP
Generates a completion script for the \fBbash\fP shell.
.TP 15
\fBcomplete\-zsh\fP
Generates a completion script for the \fBzsh\fP shell.
.TP 15
\fBcomplete\-fish\fP
Generates a completion script for the \fBfish\fP shell.
.TP 15
\fBcomplete\-powershell\fP
Generates a completion script for PowerShell.
.PP
The output is written to \fBstdout\fP. The list above may expand over time.
"
}
fn doc_choices(&self) -> &'static [&'static str] {
&[
"man",
"complete-bash",
"complete-zsh",
"complete-fish",
"complete-powershell",
]
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let genmode = match convert::str(&v.unwrap_value())? {
"man" => GenerateMode::Man,
"complete-bash" => GenerateMode::CompleteBash,
"complete-zsh" => GenerateMode::CompleteZsh,
"complete-fish" => GenerateMode::CompleteFish,
"complete-powershell" => GenerateMode::CompletePowerShell,
unk => anyhow::bail!("choice '{unk}' is unrecognized"),
};
args.mode.update(Mode::Generate(genmode));
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_generate() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Mode::Search(SearchMode::Standard), args.mode);
let args = parse_low_raw(["--generate", "man"]).unwrap();
assert_eq!(Mode::Generate(GenerateMode::Man), args.mode);
let args = parse_low_raw(["--generate", "complete-bash"]).unwrap();
assert_eq!(Mode::Generate(GenerateMode::CompleteBash), args.mode);
let args = parse_low_raw(["--generate", "complete-zsh"]).unwrap();
assert_eq!(Mode::Generate(GenerateMode::CompleteZsh), args.mode);
let args = parse_low_raw(["--generate", "complete-fish"]).unwrap();
assert_eq!(Mode::Generate(GenerateMode::CompleteFish), args.mode);
let args = parse_low_raw(["--generate", "complete-powershell"]).unwrap();
assert_eq!(Mode::Generate(GenerateMode::CompletePowerShell), args.mode);
let args =
parse_low_raw(["--generate", "complete-bash", "--generate=man"])
.unwrap();
assert_eq!(Mode::Generate(GenerateMode::Man), args.mode);
let args = parse_low_raw(["--generate", "man", "-l"]).unwrap();
assert_eq!(Mode::Search(SearchMode::FilesWithMatches), args.mode);
// An interesting quirk of how the modes override each other that lets
// you get back to the "default" mode of searching.
let args =
parse_low_raw(["--generate", "man", "--json", "--no-json"]).unwrap();
assert_eq!(Mode::Search(SearchMode::Standard), args.mode);
}
/// -g/--glob
#[derive(Debug)]
struct Glob;
impl Flag for Glob {
fn is_switch(&self) -> bool {
false
}
fn name_short(&self) -> Option<u8> {
Some(b'g')
}
fn name_long(&self) -> &'static str {
"glob"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("GLOB")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Include or exclude file paths."
}
fn doc_long(&self) -> &'static str {
r#"
Include or exclude files and directories for searching that match the given
glob. This always overrides any other ignore logic. Multiple glob flags may
be used. Globbing rules match \fB.gitignore\fP globs. Precede a glob with a
\fB!\fP to exclude it. If multiple globs match a file or directory, the glob
given later in the command line takes precedence.
.sp
As an extension, globs support specifying alternatives:
.BI "\-g '" ab{c,d}* '
is equivalent to
.BI "\-g " "abc " "\-g " abd.
Empty alternatives like
.BI "\-g '" ab{,c} '
are not currently supported. Note that this syntax extension is also currently
enabled in \fBgitignore\fP files, even though this syntax isn't supported by
git itself. ripgrep may disable this syntax extension in gitignore files, but
it will always remain available via the \flag{glob} flag.
.sp
When this flag is set, every file and directory is applied to it to test for
a match. For example, if you only want to search in a particular directory
\fIfoo\fP, then
.BI "\-g " foo
is incorrect because \fIfoo/bar\fP does not match
the glob \fIfoo\fP. Instead, you should use
.BI "\-g '" foo/** '.
"#
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let glob = convert::string(v.unwrap_value())?;
args.globs.push(glob);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_glob() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Vec::<String>::new(), args.globs);
let args = parse_low_raw(["--glob", "foo"]).unwrap();
assert_eq!(vec!["foo".to_string()], args.globs);
let args = parse_low_raw(["--glob=foo"]).unwrap();
assert_eq!(vec!["foo".to_string()], args.globs);
let args = parse_low_raw(["-g", "foo"]).unwrap();
assert_eq!(vec!["foo".to_string()], args.globs);
let args = parse_low_raw(["-gfoo"]).unwrap();
assert_eq!(vec!["foo".to_string()], args.globs);
let args = parse_low_raw(["--glob", "-foo"]).unwrap();
assert_eq!(vec!["-foo".to_string()], args.globs);
let args = parse_low_raw(["--glob=-foo"]).unwrap();
assert_eq!(vec!["-foo".to_string()], args.globs);
let args = parse_low_raw(["-g", "-foo"]).unwrap();
assert_eq!(vec!["-foo".to_string()], args.globs);
let args = parse_low_raw(["-g-foo"]).unwrap();
assert_eq!(vec!["-foo".to_string()], args.globs);
}
/// --glob-case-insensitive
#[derive(Debug)]
struct GlobCaseInsensitive;
impl Flag for GlobCaseInsensitive {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"glob-case-insensitive"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-glob-case-insensitive")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Process all glob patterns case insensitively."
}
fn doc_long(&self) -> &'static str {
r"
Process all glob patterns given with the \flag{glob} flag case insensitively.
This effectively treats \flag{glob} as \flag{iglob}.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.glob_case_insensitive = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_glob_case_insensitive() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.glob_case_insensitive);
let args = parse_low_raw(["--glob-case-insensitive"]).unwrap();
assert_eq!(true, args.glob_case_insensitive);
let args = parse_low_raw([
"--glob-case-insensitive",
"--no-glob-case-insensitive",
])
.unwrap();
assert_eq!(false, args.glob_case_insensitive);
let args = parse_low_raw([
"--no-glob-case-insensitive",
"--glob-case-insensitive",
])
.unwrap();
assert_eq!(true, args.glob_case_insensitive);
}
/// --heading
#[derive(Debug)]
struct Heading;
impl Flag for Heading {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"heading"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-heading")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Print matches grouped by each file."
}
fn doc_long(&self) -> &'static str {
r"
This flag prints the file path above clusters of matches from each file instead
of printing the file path as a prefix for each matched line.
.sp
This is the default mode when printing to a tty.
.sp
When \fBstdout\fP is not a tty, then ripgrep will default to the standard
grep-like format. One can force this format in Unix-like environments by
piping the output of ripgrep to \fBcat\fP. For example, \fBrg\fP \fIfoo\fP \fB|
cat\fP.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.heading = Some(v.unwrap_switch());
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_heading() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.heading);
let args = parse_low_raw(["--heading"]).unwrap();
assert_eq!(Some(true), args.heading);
let args = parse_low_raw(["--no-heading"]).unwrap();
assert_eq!(Some(false), args.heading);
let args = parse_low_raw(["--heading", "--no-heading"]).unwrap();
assert_eq!(Some(false), args.heading);
let args = parse_low_raw(["--no-heading", "--heading"]).unwrap();
assert_eq!(Some(true), args.heading);
}
/// -h/--help
#[derive(Debug)]
struct Help;
impl Flag for Help {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"help"
}
fn name_short(&self) -> Option<u8> {
Some(b'h')
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Show help output."
}
fn doc_long(&self) -> &'static str {
r"
This flag prints the help output for ripgrep.
.sp
Unlike most other flags, the behavior of the short flag, \fB\-h\fP, and the
long flag, \fB\-\-help\fP, is different. The short flag will show a condensed
help output while the long flag will show a verbose help output. The verbose
help output has complete documentation, where as the condensed help output will
show only a single line for every flag.
"
}
fn update(&self, v: FlagValue, _: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--help has no negation");
// Since this flag has different semantics for -h and --help and the
// Flag trait doesn't support encoding this sort of thing, we handle it
// as a special case in the parser.
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_help() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.special);
let args = parse_low_raw(["-h"]).unwrap();
assert_eq!(Some(SpecialMode::HelpShort), args.special);
let args = parse_low_raw(["--help"]).unwrap();
assert_eq!(Some(SpecialMode::HelpLong), args.special);
let args = parse_low_raw(["-h", "--help"]).unwrap();
assert_eq!(Some(SpecialMode::HelpLong), args.special);
let args = parse_low_raw(["--help", "-h"]).unwrap();
assert_eq!(Some(SpecialMode::HelpShort), args.special);
}
/// -./--hidden
#[derive(Debug)]
struct Hidden;
impl Flag for Hidden {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'.')
}
fn name_long(&self) -> &'static str {
"hidden"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-hidden")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Search hidden files and directories."
}
fn doc_long(&self) -> &'static str {
r#"
Search hidden files and directories. By default, hidden files and directories
are skipped. Note that if a hidden file or a directory is whitelisted in
an ignore file, then it will be searched even if this flag isn't provided.
Similarly if a hidden file or directory is given explicitly as an argument to
ripgrep.
.sp
A file or directory is considered hidden if its base name starts with a dot
character (\fB.\fP). On operating systems which support a "hidden" file
attribute, like Windows, files with this attribute are also considered hidden.
"#
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.hidden = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_hidden() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.hidden);
let args = parse_low_raw(["--hidden"]).unwrap();
assert_eq!(true, args.hidden);
let args = parse_low_raw(["-."]).unwrap();
assert_eq!(true, args.hidden);
let args = parse_low_raw(["-.", "--no-hidden"]).unwrap();
assert_eq!(false, args.hidden);
let args = parse_low_raw(["--no-hidden", "-."]).unwrap();
assert_eq!(true, args.hidden);
}
/// --hostname-bin
#[derive(Debug)]
struct HostnameBin;
impl Flag for HostnameBin {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"hostname-bin"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("COMMAND")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Run a program to get this system's hostname."
}
fn doc_long(&self) -> &'static str {
r#"
This flag controls how ripgrep determines this system's hostname. The flag's
value should correspond to an executable (either a path or something that can
be found via your system's \fBPATH\fP environment variable). When set, ripgrep
will run this executable, with no arguments, and treat its output (with leading
and trailing whitespace stripped) as your system's hostname.
.sp
When not set (the default, or the empty string), ripgrep will try to
automatically detect your system's hostname. On Unix, this corresponds
to calling \fBgethostname\fP. On Windows, this corresponds to calling
\fBGetComputerNameExW\fP to fetch the system's "physical DNS hostname."
.sp
ripgrep uses your system's hostname for producing hyperlinks.
"#
}
fn completion_type(&self) -> CompletionType {
CompletionType::Executable
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let path = PathBuf::from(v.unwrap_value());
args.hostname_bin =
if path.as_os_str().is_empty() { None } else { Some(path) };
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_hostname_bin() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.hostname_bin);
let args = parse_low_raw(["--hostname-bin", "foo"]).unwrap();
assert_eq!(Some(PathBuf::from("foo")), args.hostname_bin);
let args = parse_low_raw(["--hostname-bin=foo"]).unwrap();
assert_eq!(Some(PathBuf::from("foo")), args.hostname_bin);
}
/// --hyperlink-format
#[derive(Debug)]
struct HyperlinkFormat;
impl Flag for HyperlinkFormat {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"hyperlink-format"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("FORMAT")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Set the format of hyperlinks."
}
fn doc_long(&self) -> &'static str {
r#"
Set the format of hyperlinks to use when printing results. Hyperlinks make
certain elements of ripgrep's output, such as file paths, clickable. This
generally only works in terminal emulators that support OSC-8 hyperlinks. For
example, the format \fBfile://{host}{path}\fP will emit an RFC 8089 hyperlink.
To see the format that ripgrep is using, pass the \flag{debug} flag.
.sp
Alternatively, a format string may correspond to one of the following aliases:
\fBdefault\fP, \fBnone\fP, \fBfile\fP, \fBgrep+\fP, \fBkitty\fP, \fBmacvim\fP,
\fBtextmate\fP, \fBvscode\fP, \fBvscode-insiders\fP, \fBvscodium\fP. The
alias will be replaced with a format string that is intended to work for the
corresponding application.
.sp
The following variables are available in the format string:
.sp
.TP 12
\fB{path}\fP
Required. This is replaced with a path to a matching file. The path is
guaranteed to be absolute and percent encoded such that it is valid to put into
a URI. Note that a path is guaranteed to start with a /.
.TP 12
\fB{host}\fP
Optional. This is replaced with your system's hostname. On Unix, this
corresponds to calling \fBgethostname\fP. On Windows, this corresponds to
calling \fBGetComputerNameExW\fP to fetch the system's "physical DNS hostname."
Alternatively, if \flag{hostname-bin} was provided, then the hostname returned
from the output of that program will be returned. If no hostname could be
found, then this variable is replaced with the empty string.
.TP 12
\fB{line}\fP
Optional. If appropriate, this is replaced with the line number of a match. If
no line number is available (for example, if \fB\-\-no\-line\-number\fP was
given), then it is automatically replaced with the value 1.
.TP 12
\fB{column}\fP
Optional, but requires the presence of \fB{line}\fP. If appropriate, this is
replaced with the column number of a match. If no column number is available
(for example, if \fB\-\-no\-column\fP was given), then it is automatically
replaced with the value 1.
.TP 12
\fB{wslprefix}\fP
Optional. This is a special value that is set to
\fBwsl$/\fP\fIWSL_DISTRO_NAME\fP, where \fIWSL_DISTRO_NAME\fP corresponds to
the value of the equivalent environment variable. If the system is not Unix
or if the \fIWSL_DISTRO_NAME\fP environment variable is not set, then this is
replaced with the empty string.
.PP
A format string may be empty. An empty format string is equivalent to the
\fBnone\fP alias. In this case, hyperlinks will be disabled.
.sp
At present, ripgrep does not enable hyperlinks by default. Users must opt into
them. If you aren't sure what format to use, try \fBdefault\fP.
.sp
Like colors, when ripgrep detects that stdout is not connected to a tty, then
hyperlinks are automatically disabled, regardless of the value of this flag.
Users can pass \fB\-\-color=always\fP to forcefully emit hyperlinks.
.sp
Note that hyperlinks are only written when a path is also in the output
and colors are enabled. To write hyperlinks without colors, you'll need to
configure ripgrep to not colorize anything without actually disabling all ANSI
escape codes completely:
.sp
.EX
\-\-colors 'path:none' \\
\-\-colors 'line:none' \\
\-\-colors 'column:none' \\
\-\-colors 'match:none'
.EE
.sp
ripgrep works this way because it treats the \flag{color} flag as a proxy for
whether ANSI escape codes should be used at all. This means that environment
variables like \fBNO_COLOR=1\fP and \fBTERM=dumb\fP not only disable colors,
but hyperlinks as well. Similarly, colors and hyperlinks are disabled when
ripgrep is not writing to a tty. (Unless one forces the issue by setting
\fB\-\-color=always\fP.)
.sp
If you're searching a file directly, for example:
.sp
.EX
rg foo path/to/file
.EE
.sp
then hyperlinks will not be emitted since the path given does not appear
in the output. To make the path appear, and thus also a hyperlink, use the
\flag{with-filename} flag.
.sp
For more information on hyperlinks in terminal emulators, see:
https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
"#
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let v = v.unwrap_value();
let string = convert::str(&v)?;
let format = string.parse().context("invalid hyperlink format")?;
args.hyperlink_format = format;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_hyperlink_format() {
let parseformat = |format: &str| {
format.parse::<grep::printer::HyperlinkFormat>().unwrap()
};
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(parseformat("none"), args.hyperlink_format);
let args = parse_low_raw(["--hyperlink-format", "default"]).unwrap();
#[cfg(windows)]
assert_eq!(parseformat("file://{path}"), args.hyperlink_format);
#[cfg(not(windows))]
assert_eq!(parseformat("file://{host}{path}"), args.hyperlink_format);
let args = parse_low_raw(["--hyperlink-format", "file"]).unwrap();
assert_eq!(parseformat("file://{host}{path}"), args.hyperlink_format);
let args = parse_low_raw([
"--hyperlink-format",
"file",
"--hyperlink-format=grep+",
])
.unwrap();
assert_eq!(parseformat("grep+://{path}:{line}"), args.hyperlink_format);
let args =
parse_low_raw(["--hyperlink-format", "file://{host}{path}#{line}"])
.unwrap();
assert_eq!(
parseformat("file://{host}{path}#{line}"),
args.hyperlink_format
);
let result = parse_low_raw(["--hyperlink-format", "file://heythere"]);
assert!(result.is_err(), "{result:?}");
}
/// --iglob
#[derive(Debug)]
struct IGlob;
impl Flag for IGlob {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"iglob"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("GLOB")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Include/exclude paths case insensitively."
}
fn doc_long(&self) -> &'static str {
r"
Include or exclude files and directories for searching that match the given
glob. This always overrides any other ignore logic. Multiple glob flags may
be used. Globbing rules match \fB.gitignore\fP globs. Precede a glob with a
\fB!\fP to exclude it. If multiple globs match a file or directory, the glob
given later in the command line takes precedence. Globs used via this flag are
matched case insensitively.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let glob = convert::string(v.unwrap_value())?;
args.iglobs.push(glob);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_iglob() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Vec::<String>::new(), args.iglobs);
let args = parse_low_raw(["--iglob", "foo"]).unwrap();
assert_eq!(vec!["foo".to_string()], args.iglobs);
let args = parse_low_raw(["--iglob=foo"]).unwrap();
assert_eq!(vec!["foo".to_string()], args.iglobs);
let args = parse_low_raw(["--iglob", "-foo"]).unwrap();
assert_eq!(vec!["-foo".to_string()], args.iglobs);
let args = parse_low_raw(["--iglob=-foo"]).unwrap();
assert_eq!(vec!["-foo".to_string()], args.iglobs);
}
/// -i/--ignore-case
#[derive(Debug)]
struct IgnoreCase;
impl Flag for IgnoreCase {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'i')
}
fn name_long(&self) -> &'static str {
"ignore-case"
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Case insensitive search."
}
fn doc_long(&self) -> &'static str {
r#"
When this flag is provided, all patterns will be searched case insensitively.
The case insensitivity rules used by ripgrep's default regex engine conform to
Unicode's "simple" case folding rules.
.sp
This is a global option that applies to all patterns given to ripgrep.
Individual patterns can still be matched case sensitively by using
inline regex flags. For example, \fB(?\-i)abc\fP will match \fBabc\fP
case sensitively even when this flag is used.
.sp
This flag overrides \flag{case-sensitive} and \flag{smart-case}.
"#
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "flag has no negation");
args.case = CaseMode::Insensitive;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_ignore_case() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(CaseMode::Sensitive, args.case);
let args = parse_low_raw(["--ignore-case"]).unwrap();
assert_eq!(CaseMode::Insensitive, args.case);
let args = parse_low_raw(["-i"]).unwrap();
assert_eq!(CaseMode::Insensitive, args.case);
let args = parse_low_raw(["-i", "-s"]).unwrap();
assert_eq!(CaseMode::Sensitive, args.case);
let args = parse_low_raw(["-s", "-i"]).unwrap();
assert_eq!(CaseMode::Insensitive, args.case);
}
/// --ignore-file
#[derive(Debug)]
struct IgnoreFile;
impl Flag for IgnoreFile {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"ignore-file"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("PATH")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Specify additional ignore files."
}
fn doc_long(&self) -> &'static str {
r"
Specifies a path to one or more \fBgitignore\fP formatted rules files.
These patterns are applied after the patterns found in \fB.gitignore\fP,
\fB.rgignore\fP and \fB.ignore\fP are applied and are matched relative to the
current working directory. Multiple additional ignore files can be specified
by using this flag repeatedly. When specifying multiple ignore files, earlier
files have lower precedence than later files.
.sp
If you are looking for a way to include or exclude files and directories
directly on the command line, then use \flag{glob} instead.
"
}
fn completion_type(&self) -> CompletionType {
CompletionType::Filename
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let path = PathBuf::from(v.unwrap_value());
args.ignore_file.push(path);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_ignore_file() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Vec::<PathBuf>::new(), args.ignore_file);
let args = parse_low_raw(["--ignore-file", "foo"]).unwrap();
assert_eq!(vec![PathBuf::from("foo")], args.ignore_file);
let args = parse_low_raw(["--ignore-file", "foo", "--ignore-file", "bar"])
.unwrap();
assert_eq!(
vec![PathBuf::from("foo"), PathBuf::from("bar")],
args.ignore_file
);
}
/// --ignore-file-case-insensitive
#[derive(Debug)]
struct IgnoreFileCaseInsensitive;
impl Flag for IgnoreFileCaseInsensitive {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"ignore-file-case-insensitive"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-ignore-file-case-insensitive")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Process ignore files case insensitively."
}
fn doc_long(&self) -> &'static str {
r"
Process ignore files (\fB.gitignore\fP, \fB.ignore\fP, etc.) case
insensitively. Note that this comes with a performance penalty and is most
useful on case insensitive file systems (such as Windows).
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.ignore_file_case_insensitive = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_ignore_file_case_insensitive() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.ignore_file_case_insensitive);
let args = parse_low_raw(["--ignore-file-case-insensitive"]).unwrap();
assert_eq!(true, args.ignore_file_case_insensitive);
let args = parse_low_raw([
"--ignore-file-case-insensitive",
"--no-ignore-file-case-insensitive",
])
.unwrap();
assert_eq!(false, args.ignore_file_case_insensitive);
let args = parse_low_raw([
"--no-ignore-file-case-insensitive",
"--ignore-file-case-insensitive",
])
.unwrap();
assert_eq!(true, args.ignore_file_case_insensitive);
}
/// --include-zero
#[derive(Debug)]
struct IncludeZero;
impl Flag for IncludeZero {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"include-zero"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-include-zero")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Include zero matches in summary output."
}
fn doc_long(&self) -> &'static str {
r"
When used with \flag{count} or \flag{count-matches}, this causes ripgrep to
print the number of matches for each file even if there were zero matches. This
is disabled by default but can be enabled to make ripgrep behave more like
grep.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.include_zero = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_include_zero() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.include_zero);
let args = parse_low_raw(["--include-zero"]).unwrap();
assert_eq!(true, args.include_zero);
let args = parse_low_raw(["--include-zero", "--no-include-zero"]).unwrap();
assert_eq!(false, args.include_zero);
}
/// -v/--invert-match
#[derive(Debug)]
struct InvertMatch;
impl Flag for InvertMatch {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'v')
}
fn name_long(&self) -> &'static str {
"invert-match"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-invert-match")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Invert matching."
}
fn doc_long(&self) -> &'static str {
r"
This flag inverts matching. That is, instead of printing lines that match,
ripgrep will print lines that don't match.
.sp
Note that this only inverts line-by-line matching. For example, combining this
flag with \flag{files-with-matches} will emit files that contain any lines
that do not match the patterns given. That's not the same as, for example,
\flag{files-without-match}, which will emit files that do not contain any
matching lines.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.invert_match = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_invert_match() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.invert_match);
let args = parse_low_raw(["--invert-match"]).unwrap();
assert_eq!(true, args.invert_match);
let args = parse_low_raw(["-v"]).unwrap();
assert_eq!(true, args.invert_match);
let args = parse_low_raw(["-v", "--no-invert-match"]).unwrap();
assert_eq!(false, args.invert_match);
}
/// --json
#[derive(Debug)]
struct JSON;
impl Flag for JSON {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"json"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-json")
}
fn doc_category(&self) -> Category {
Category::OutputModes
}
fn doc_short(&self) -> &'static str {
r"Show search results in a JSON Lines format."
}
fn doc_long(&self) -> &'static str {
r"
Enable printing results in a JSON Lines format.
.sp
When this flag is provided, ripgrep will emit a sequence of messages, each
encoded as a JSON object, where there are five different message types:
.sp
.TP 12
\fBbegin\fP
A message that indicates a file is being searched and contains at least one
match.
.TP 12
\fBend\fP
A message the indicates a file is done being searched. This message also
include summary statistics about the search for a particular file.
.TP 12
\fBmatch\fP
A message that indicates a match was found. This includes the text and offsets
of the match.
.TP 12
\fBcontext\fP
A message that indicates a contextual line was found. This includes the text of
the line, along with any match information if the search was inverted.
.TP 12
\fBsummary\fP
The final message emitted by ripgrep that contains summary statistics about the
search across all files.
.PP
Since file paths or the contents of files are not guaranteed to be valid
UTF-8 and JSON itself must be representable by a Unicode encoding, ripgrep
will emit all data elements as objects with one of two keys: \fBtext\fP or
\fBbytes\fP. \fBtext\fP is a normal JSON string when the data is valid UTF-8
while \fBbytes\fP is the base64 encoded contents of the data.
.sp
The JSON Lines format is only supported for showing search results. It cannot
be used with other flags that emit other types of output, such as \flag{files},
\flag{files-with-matches}, \flag{files-without-match}, \flag{count} or
\flag{count-matches}. ripgrep will report an error if any of the aforementioned
flags are used in concert with \flag{json}.
.sp
Other flags that control aspects of the standard output such as
\flag{only-matching}, \flag{heading}, \flag{replace}, \flag{max-columns}, etc.,
have no effect when \flag{json} is set. However, enabling JSON output will
always implicitly and unconditionally enable \flag{stats}.
.sp
A more complete description of the JSON format used can be found here:
\fIhttps://docs.rs/grep-printer/*/grep_printer/struct.JSON.html\fP.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
if v.unwrap_switch() {
args.mode.update(Mode::Search(SearchMode::JSON));
} else if matches!(args.mode, Mode::Search(SearchMode::JSON)) {
// --no-json only reverts to the default mode if the mode is
// JSON, otherwise it's a no-op.
args.mode.update(Mode::Search(SearchMode::Standard));
}
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_json() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Mode::Search(SearchMode::Standard), args.mode);
let args = parse_low_raw(["--json"]).unwrap();
assert_eq!(Mode::Search(SearchMode::JSON), args.mode);
let args = parse_low_raw(["--json", "--no-json"]).unwrap();
assert_eq!(Mode::Search(SearchMode::Standard), args.mode);
let args = parse_low_raw(["--json", "--files", "--no-json"]).unwrap();
assert_eq!(Mode::Files, args.mode);
let args = parse_low_raw(["--json", "-l", "--no-json"]).unwrap();
assert_eq!(Mode::Search(SearchMode::FilesWithMatches), args.mode);
}
/// --line-buffered
#[derive(Debug)]
struct LineBuffered;
impl Flag for LineBuffered {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"line-buffered"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-line-buffered")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Force line buffering."
}
fn doc_long(&self) -> &'static str {
r"
When enabled, ripgrep will always use line buffering. That is, whenever a
matching line is found, it will be flushed to stdout immediately. This is the
default when ripgrep's stdout is connected to a tty, but otherwise, ripgrep
will use block buffering, which is typically faster. This flag forces ripgrep
to use line buffering even if it would otherwise use block buffering. This is
typically useful in shell pipelines, for example:
.sp
.EX
tail -f something.log | rg foo --line-buffered | rg bar
.EE
.sp
This overrides the \flag{block-buffered} flag.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.buffer = if v.unwrap_switch() {
BufferMode::Line
} else {
BufferMode::Auto
};
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_line_buffered() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(BufferMode::Auto, args.buffer);
let args = parse_low_raw(["--line-buffered"]).unwrap();
assert_eq!(BufferMode::Line, args.buffer);
let args =
parse_low_raw(["--line-buffered", "--no-line-buffered"]).unwrap();
assert_eq!(BufferMode::Auto, args.buffer);
let args = parse_low_raw(["--line-buffered", "--block-buffered"]).unwrap();
assert_eq!(BufferMode::Block, args.buffer);
}
/// -n/--line-number
#[derive(Debug)]
struct LineNumber;
impl Flag for LineNumber {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'n')
}
fn name_long(&self) -> &'static str {
"line-number"
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Show line numbers."
}
fn doc_long(&self) -> &'static str {
r"
Show line numbers (1-based).
.sp
This is enabled by default when stdout is connected to a tty.
.sp
This flag can be disabled by \flag{no-line-number}.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--line-number has no automatic negation");
args.line_number = Some(true);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_line_number() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.line_number);
let args = parse_low_raw(["--line-number"]).unwrap();
assert_eq!(Some(true), args.line_number);
let args = parse_low_raw(["-n"]).unwrap();
assert_eq!(Some(true), args.line_number);
let args = parse_low_raw(["-n", "--no-line-number"]).unwrap();
assert_eq!(Some(false), args.line_number);
}
/// -N/--no-line-number
#[derive(Debug)]
struct LineNumberNo;
impl Flag for LineNumberNo {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'N')
}
fn name_long(&self) -> &'static str {
"no-line-number"
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Suppress line numbers."
}
fn doc_long(&self) -> &'static str {
r"
Suppress line numbers.
.sp
Line numbers are off by default when stdout is not connected to a tty.
.sp
Line numbers can be forcefully turned on by \flag{line-number}.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(
v.unwrap_switch(),
"--no-line-number has no automatic negation"
);
args.line_number = Some(false);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_no_line_number() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.line_number);
let args = parse_low_raw(["--no-line-number"]).unwrap();
assert_eq!(Some(false), args.line_number);
let args = parse_low_raw(["-N"]).unwrap();
assert_eq!(Some(false), args.line_number);
let args = parse_low_raw(["-N", "--line-number"]).unwrap();
assert_eq!(Some(true), args.line_number);
}
/// -x/--line-regexp
#[derive(Debug)]
struct LineRegexp;
impl Flag for LineRegexp {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'x')
}
fn name_long(&self) -> &'static str {
"line-regexp"
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Show matches surrounded by line boundaries."
}
fn doc_long(&self) -> &'static str {
r"
When enabled, ripgrep will only show matches surrounded by line boundaries.
This is equivalent to surrounding every pattern with \fB^\fP and \fB$\fP. In
other words, this only prints lines where the entire line participates in a
match.
.sp
This overrides the \flag{word-regexp} flag.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--line-regexp has no negation");
args.boundary = Some(BoundaryMode::Line);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_line_regexp() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.boundary);
let args = parse_low_raw(["--line-regexp"]).unwrap();
assert_eq!(Some(BoundaryMode::Line), args.boundary);
let args = parse_low_raw(["-x"]).unwrap();
assert_eq!(Some(BoundaryMode::Line), args.boundary);
}
/// -M/--max-columns
#[derive(Debug)]
struct MaxColumns;
impl Flag for MaxColumns {
fn is_switch(&self) -> bool {
false
}
fn name_short(&self) -> Option<u8> {
Some(b'M')
}
fn name_long(&self) -> &'static str {
"max-columns"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("NUM")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Omit lines longer than this limit."
}
fn doc_long(&self) -> &'static str {
r"
When given, ripgrep will omit lines longer than this limit in bytes. Instead of
printing long lines, only the number of matches in that line is printed.
.sp
When this flag is omitted or is set to \fB0\fP, then it has no effect.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let max = convert::u64(&v.unwrap_value())?;
args.max_columns = if max == 0 { None } else { Some(max) };
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_max_columns() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.max_columns);
let args = parse_low_raw(["--max-columns", "5"]).unwrap();
assert_eq!(Some(5), args.max_columns);
let args = parse_low_raw(["-M", "5"]).unwrap();
assert_eq!(Some(5), args.max_columns);
let args = parse_low_raw(["-M5"]).unwrap();
assert_eq!(Some(5), args.max_columns);
let args = parse_low_raw(["--max-columns", "5", "-M0"]).unwrap();
assert_eq!(None, args.max_columns);
}
/// --max-columns-preview
#[derive(Debug)]
struct MaxColumnsPreview;
impl Flag for MaxColumnsPreview {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"max-columns-preview"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-max-columns-preview")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Show preview for lines exceeding the limit."
}
fn doc_long(&self) -> &'static str {
r"
Prints a preview for lines exceeding the configured max column limit.
.sp
When the \flag{max-columns} flag is used, ripgrep will by default completely
replace any line that is too long with a message indicating that a matching
line was removed. When this flag is combined with \flag{max-columns}, a preview
of the line (corresponding to the limit size) is shown instead, where the part
of the line exceeding the limit is not shown.
.sp
If the \flag{max-columns} flag is not set, then this has no effect.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.max_columns_preview = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_max_columns_preview() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.max_columns_preview);
let args = parse_low_raw(["--max-columns-preview"]).unwrap();
assert_eq!(true, args.max_columns_preview);
let args =
parse_low_raw(["--max-columns-preview", "--no-max-columns-preview"])
.unwrap();
assert_eq!(false, args.max_columns_preview);
}
/// -m/--max-count
#[derive(Debug)]
struct MaxCount;
impl Flag for MaxCount {
fn is_switch(&self) -> bool {
false
}
fn name_short(&self) -> Option<u8> {
Some(b'm')
}
fn name_long(&self) -> &'static str {
"max-count"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("NUM")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Limit the number of matching lines."
}
fn doc_long(&self) -> &'static str {
r"
Limit the number of matching lines per file searched to \fINUM\fP.
.sp
Note that \fB0\fP is a legal value but not likely to be useful. When used,
ripgrep won't search anything.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.max_count = Some(convert::u64(&v.unwrap_value())?);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_max_count() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.max_count);
let args = parse_low_raw(["--max-count", "5"]).unwrap();
assert_eq!(Some(5), args.max_count);
let args = parse_low_raw(["-m", "5"]).unwrap();
assert_eq!(Some(5), args.max_count);
let args = parse_low_raw(["-m", "5", "--max-count=10"]).unwrap();
assert_eq!(Some(10), args.max_count);
let args = parse_low_raw(["-m0"]).unwrap();
assert_eq!(Some(0), args.max_count);
}
/// --max-depth
#[derive(Debug)]
struct MaxDepth;
impl Flag for MaxDepth {
fn is_switch(&self) -> bool {
false
}
fn name_short(&self) -> Option<u8> {
Some(b'd')
}
fn name_long(&self) -> &'static str {
"max-depth"
}
fn aliases(&self) -> &'static [&'static str] {
&["maxdepth"]
}
fn doc_variable(&self) -> Option<&'static str> {
Some("NUM")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Descend at most NUM directories."
}
fn doc_long(&self) -> &'static str {
r"
This flag limits the depth of directory traversal to \fINUM\fP levels beyond
the paths given. A value of \fB0\fP only searches the explicitly given paths
themselves.
.sp
For example, \fBrg --max-depth 0 \fP\fIdir/\fP is a no-op because \fIdir/\fP
will not be descended into. \fBrg --max-depth 1 \fP\fIdir/\fP will search only
the direct children of \fIdir\fP.
.sp
An alternative spelling for this flag is \fB\-\-maxdepth\fP.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.max_depth = Some(convert::usize(&v.unwrap_value())?);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_max_depth() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.max_depth);
let args = parse_low_raw(["--max-depth", "5"]).unwrap();
assert_eq!(Some(5), args.max_depth);
let args = parse_low_raw(["-d", "5"]).unwrap();
assert_eq!(Some(5), args.max_depth);
let args = parse_low_raw(["--max-depth", "5", "--max-depth=10"]).unwrap();
assert_eq!(Some(10), args.max_depth);
let args = parse_low_raw(["--max-depth", "0"]).unwrap();
assert_eq!(Some(0), args.max_depth);
let args = parse_low_raw(["--maxdepth", "5"]).unwrap();
assert_eq!(Some(5), args.max_depth);
}
/// --max-filesize
#[derive(Debug)]
struct MaxFilesize;
impl Flag for MaxFilesize {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"max-filesize"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("NUM+SUFFIX?")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Ignore files larger than NUM in size."
}
fn doc_long(&self) -> &'static str {
r"
Ignore files larger than \fINUM\fP in size. This does not apply to directories.
.sp
The input format accepts suffixes of \fBK\fP, \fBM\fP or \fBG\fP which
correspond to kilobytes, megabytes and gigabytes, respectively. If no suffix is
provided the input is treated as bytes.
.sp
Examples: \fB\-\-max-filesize 50K\fP or \fB\-\-max\-filesize 80M\fP.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let v = v.unwrap_value();
args.max_filesize = Some(convert::human_readable_u64(&v)?);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_max_filesize() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.max_filesize);
let args = parse_low_raw(["--max-filesize", "1024"]).unwrap();
assert_eq!(Some(1024), args.max_filesize);
let args = parse_low_raw(["--max-filesize", "1K"]).unwrap();
assert_eq!(Some(1024), args.max_filesize);
let args =
parse_low_raw(["--max-filesize", "1K", "--max-filesize=1M"]).unwrap();
assert_eq!(Some(1024 * 1024), args.max_filesize);
}
/// --mmap
#[derive(Debug)]
struct Mmap;
impl Flag for Mmap {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"mmap"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-mmap")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Search with memory maps when possible."
}
fn doc_long(&self) -> &'static str {
r"
When enabled, ripgrep will search using memory maps when possible. This is
enabled by default when ripgrep thinks it will be faster.
.sp
Memory map searching cannot be used in all circumstances. For example, when
searching virtual files or streams likes \fBstdin\fP. In such cases, memory
maps will not be used even when this flag is enabled.
.sp
Note that ripgrep may abort unexpectedly when memory maps are used if it
searches a file that is simultaneously truncated. Users can opt out of this
possibility by disabling memory maps.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.mmap = if v.unwrap_switch() {
MmapMode::AlwaysTryMmap
} else {
MmapMode::Never
};
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_mmap() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(MmapMode::Auto, args.mmap);
let args = parse_low_raw(["--mmap"]).unwrap();
assert_eq!(MmapMode::AlwaysTryMmap, args.mmap);
let args = parse_low_raw(["--no-mmap"]).unwrap();
assert_eq!(MmapMode::Never, args.mmap);
let args = parse_low_raw(["--mmap", "--no-mmap"]).unwrap();
assert_eq!(MmapMode::Never, args.mmap);
let args = parse_low_raw(["--no-mmap", "--mmap"]).unwrap();
assert_eq!(MmapMode::AlwaysTryMmap, args.mmap);
}
/// -U/--multiline
#[derive(Debug)]
struct Multiline;
impl Flag for Multiline {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'U')
}
fn name_long(&self) -> &'static str {
"multiline"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-multiline")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Enable searching across multiple lines."
}
fn doc_long(&self) -> &'static str {
r#"
This flag enable searching across multiple lines.
.sp
When multiline mode is enabled, ripgrep will lift the restriction that a
match cannot include a line terminator. For example, when multiline mode
is not enabled (the default), then the regex \fB\\p{any}\fP will match any
Unicode codepoint other than \fB\\n\fP. Similarly, the regex \fB\\n\fP is
explicitly forbidden, and if you try to use it, ripgrep will return an error.
However, when multiline mode is enabled, \fB\\p{any}\fP will match any Unicode
codepoint, including \fB\\n\fP, and regexes like \fB\\n\fP are permitted.
.sp
An important caveat is that multiline mode does not change the match semantics
of \fB.\fP. Namely, in most regex matchers, a \fB.\fP will by default match any
character other than \fB\\n\fP, and this is true in ripgrep as well. In order
to make \fB.\fP match \fB\\n\fP, you must enable the "dot all" flag inside the
regex. For example, both \fB(?s).\fP and \fB(?s:.)\fP have the same semantics,
where \fB.\fP will match any character, including \fB\\n\fP. Alternatively, the
\flag{multiline-dotall} flag may be passed to make the "dot all" behavior the
default. This flag only applies when multiline search is enabled.
.sp
There is no limit on the number of the lines that a single match can span.
.sp
\fBWARNING\fP: Because of how the underlying regex engine works, multiline
searches may be slower than normal line-oriented searches, and they may also
use more memory. In particular, when multiline mode is enabled, ripgrep
requires that each file it searches is laid out contiguously in memory (either
by reading it onto the heap or by memory-mapping it). Things that cannot be
memory-mapped (such as \fBstdin\fP) will be consumed until EOF before searching
can begin. In general, ripgrep will only do these things when necessary.
Specifically, if the \flag{multiline} flag is provided but the regex does
not contain patterns that would match \fB\\n\fP characters, then ripgrep
will automatically avoid reading each file into memory before searching it.
Nevertheless, if you only care about matches spanning at most one line, then it
is always better to disable multiline mode.
.sp
This overrides the \flag{stop-on-nonmatch} flag.
"#
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.multiline = v.unwrap_switch();
if args.multiline {
args.stop_on_nonmatch = false;
}
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_multiline() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.multiline);
let args = parse_low_raw(["--multiline"]).unwrap();
assert_eq!(true, args.multiline);
let args = parse_low_raw(["-U"]).unwrap();
assert_eq!(true, args.multiline);
let args = parse_low_raw(["-U", "--no-multiline"]).unwrap();
assert_eq!(false, args.multiline);
}
/// --multiline-dotall
#[derive(Debug)]
struct MultilineDotall;
impl Flag for MultilineDotall {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"multiline-dotall"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-multiline-dotall")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Make '.' match line terminators."
}
fn doc_long(&self) -> &'static str {
r#"
This flag enables "dot all" mode in all regex patterns. This causes \fB.\fP to
match line terminators when multiline searching is enabled. This flag has no
effect if multiline searching isn't enabled with the \flag{multiline} flag.
.sp
Normally, a \fB.\fP will match any character except line terminators. While
this behavior typically isn't relevant for line-oriented matching (since
matches can span at most one line), this can be useful when searching with the
\flag{multiline} flag. By default, multiline mode runs without "dot all" mode
enabled.
.sp
This flag is generally intended to be used in an alias or your ripgrep config
file if you prefer "dot all" semantics by default. Note that regardless of
whether this flag is used, "dot all" semantics can still be controlled via
inline flags in the regex pattern itself, e.g., \fB(?s:.)\fP always enables
"dot all" whereas \fB(?-s:.)\fP always disables "dot all". Moreover, you
can use character classes like \fB\\p{any}\fP to match any Unicode codepoint
regardless of whether "dot all" mode is enabled or not.
"#
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.multiline_dotall = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_multiline_dotall() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.multiline_dotall);
let args = parse_low_raw(["--multiline-dotall"]).unwrap();
assert_eq!(true, args.multiline_dotall);
let args = parse_low_raw(["--multiline-dotall", "--no-multiline-dotall"])
.unwrap();
assert_eq!(false, args.multiline_dotall);
}
/// --no-config
#[derive(Debug)]
struct NoConfig;
impl Flag for NoConfig {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"no-config"
}
fn doc_category(&self) -> Category {
Category::OtherBehaviors
}
fn doc_short(&self) -> &'static str {
r"Never read configuration files."
}
fn doc_long(&self) -> &'static str {
r"
When set, ripgrep will never read configuration files. When this flag is
present, ripgrep will not respect the \fBRIPGREP_CONFIG_PATH\fP environment
variable.
.sp
If ripgrep ever grows a feature to automatically read configuration files in
pre-defined locations, then this flag will also disable that behavior as well.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--no-config has no negation");
args.no_config = true;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_no_config() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.no_config);
let args = parse_low_raw(["--no-config"]).unwrap();
assert_eq!(true, args.no_config);
}
/// --no-ignore
#[derive(Debug)]
struct NoIgnore;
impl Flag for NoIgnore {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"no-ignore"
}
fn name_negated(&self) -> Option<&'static str> {
Some("ignore")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Don't use ignore files."
}
fn doc_long(&self) -> &'static str {
r"
When set, ignore files such as \fB.gitignore\fP, \fB.ignore\fP and
\fB.rgignore\fP will not be respected. This implies \flag{no-ignore-dot},
\flag{no-ignore-exclude}, \flag{no-ignore-global}, \flag{no-ignore-parent} and
\flag{no-ignore-vcs}.
.sp
This does not imply \flag{no-ignore-files}, since \flag{ignore-file} is
specified explicitly as a command line argument.
.sp
When given only once, the \flag{unrestricted} flag is identical in
behavior to this flag and can be considered an alias. However, subsequent
\flag{unrestricted} flags have additional effects.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let yes = v.unwrap_switch();
args.no_ignore_dot = yes;
args.no_ignore_exclude = yes;
args.no_ignore_global = yes;
args.no_ignore_parent = yes;
args.no_ignore_vcs = yes;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_no_ignore() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.no_ignore_dot);
assert_eq!(false, args.no_ignore_exclude);
assert_eq!(false, args.no_ignore_global);
assert_eq!(false, args.no_ignore_parent);
assert_eq!(false, args.no_ignore_vcs);
let args = parse_low_raw(["--no-ignore"]).unwrap();
assert_eq!(true, args.no_ignore_dot);
assert_eq!(true, args.no_ignore_exclude);
assert_eq!(true, args.no_ignore_global);
assert_eq!(true, args.no_ignore_parent);
assert_eq!(true, args.no_ignore_vcs);
let args = parse_low_raw(["--no-ignore", "--ignore"]).unwrap();
assert_eq!(false, args.no_ignore_dot);
assert_eq!(false, args.no_ignore_exclude);
assert_eq!(false, args.no_ignore_global);
assert_eq!(false, args.no_ignore_parent);
assert_eq!(false, args.no_ignore_vcs);
}
/// --no-ignore-dot
#[derive(Debug)]
struct NoIgnoreDot;
impl Flag for NoIgnoreDot {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"no-ignore-dot"
}
fn name_negated(&self) -> Option<&'static str> {
Some("ignore-dot")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Don't use .ignore or .rgignore files."
}
fn doc_long(&self) -> &'static str {
r"
Don't respect filter rules from \fB.ignore\fP or \fB.rgignore\fP files.
.sp
This does not impact whether ripgrep will ignore files and directories whose
names begin with a dot. For that, see the \flag{hidden} flag. This flag also
does not impact whether filter rules from \fB.gitignore\fP files are respected.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.no_ignore_dot = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_no_ignore_dot() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.no_ignore_dot);
let args = parse_low_raw(["--no-ignore-dot"]).unwrap();
assert_eq!(true, args.no_ignore_dot);
let args = parse_low_raw(["--no-ignore-dot", "--ignore-dot"]).unwrap();
assert_eq!(false, args.no_ignore_dot);
}
/// --no-ignore-exclude
#[derive(Debug)]
struct NoIgnoreExclude;
impl Flag for NoIgnoreExclude {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"no-ignore-exclude"
}
fn name_negated(&self) -> Option<&'static str> {
Some("ignore-exclude")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Don't use local exclusion files."
}
fn doc_long(&self) -> &'static str {
r"
Don't respect filter rules from files that are manually configured for the repository.
For example, this includes \fBgit\fP's \fB.git/info/exclude\fP.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.no_ignore_exclude = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_no_ignore_exclude() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.no_ignore_exclude);
let args = parse_low_raw(["--no-ignore-exclude"]).unwrap();
assert_eq!(true, args.no_ignore_exclude);
let args =
parse_low_raw(["--no-ignore-exclude", "--ignore-exclude"]).unwrap();
assert_eq!(false, args.no_ignore_exclude);
}
/// --no-ignore-files
#[derive(Debug)]
struct NoIgnoreFiles;
impl Flag for NoIgnoreFiles {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"no-ignore-files"
}
fn name_negated(&self) -> Option<&'static str> {
Some("ignore-files")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Don't use --ignore-file arguments."
}
fn doc_long(&self) -> &'static str {
r"
When set, any \flag{ignore-file} flags, even ones that come after this flag,
are ignored.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.no_ignore_files = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_no_ignore_files() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.no_ignore_files);
let args = parse_low_raw(["--no-ignore-files"]).unwrap();
assert_eq!(true, args.no_ignore_files);
let args = parse_low_raw(["--no-ignore-files", "--ignore-files"]).unwrap();
assert_eq!(false, args.no_ignore_files);
}
/// --no-ignore-global
#[derive(Debug)]
struct NoIgnoreGlobal;
impl Flag for NoIgnoreGlobal {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"no-ignore-global"
}
fn name_negated(&self) -> Option<&'static str> {
Some("ignore-global")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Don't use global ignore files."
}
fn doc_long(&self) -> &'static str {
r#"
Don't respect filter rules from ignore files that come from "global" sources
such as \fBgit\fP's \fBcore.excludesFile\fP configuration option (which
defaults to \fB$HOME/.config/git/ignore\fP).
"#
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.no_ignore_global = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_no_ignore_global() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.no_ignore_global);
let args = parse_low_raw(["--no-ignore-global"]).unwrap();
assert_eq!(true, args.no_ignore_global);
let args =
parse_low_raw(["--no-ignore-global", "--ignore-global"]).unwrap();
assert_eq!(false, args.no_ignore_global);
}
/// --no-ignore-messages
#[derive(Debug)]
struct NoIgnoreMessages;
impl Flag for NoIgnoreMessages {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"no-ignore-messages"
}
fn name_negated(&self) -> Option<&'static str> {
Some("ignore-messages")
}
fn doc_category(&self) -> Category {
Category::Logging
}
fn doc_short(&self) -> &'static str {
r"Suppress gitignore parse error messages."
}
fn doc_long(&self) -> &'static str {
r"
When this flag is enabled, all error messages related to parsing ignore files
are suppressed. By default, error messages are printed to stderr. In cases
where these errors are expected, this flag can be used to avoid seeing the
noise produced by the messages.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.no_ignore_messages = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_no_ignore_messages() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.no_ignore_messages);
let args = parse_low_raw(["--no-ignore-messages"]).unwrap();
assert_eq!(true, args.no_ignore_messages);
let args =
parse_low_raw(["--no-ignore-messages", "--ignore-messages"]).unwrap();
assert_eq!(false, args.no_ignore_messages);
}
/// --no-ignore-parent
#[derive(Debug)]
struct NoIgnoreParent;
impl Flag for NoIgnoreParent {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"no-ignore-parent"
}
fn name_negated(&self) -> Option<&'static str> {
Some("ignore-parent")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Don't use ignore files in parent directories."
}
fn doc_long(&self) -> &'static str {
r"
When this flag is set, filter rules from ignore files found in parent
directories are not respected. By default, ripgrep will ascend the parent
directories of the current working directory to look for any applicable ignore
files that should be applied. In some cases this may not be desirable.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.no_ignore_parent = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_no_ignore_parent() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.no_ignore_parent);
let args = parse_low_raw(["--no-ignore-parent"]).unwrap();
assert_eq!(true, args.no_ignore_parent);
let args =
parse_low_raw(["--no-ignore-parent", "--ignore-parent"]).unwrap();
assert_eq!(false, args.no_ignore_parent);
}
/// --no-ignore-vcs
#[derive(Debug)]
struct NoIgnoreVcs;
impl Flag for NoIgnoreVcs {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"no-ignore-vcs"
}
fn name_negated(&self) -> Option<&'static str> {
Some("ignore-vcs")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Don't use ignore files from source control."
}
fn doc_long(&self) -> &'static str {
r"
When given, filter rules from source control ignore files (e.g., \fB.gitignore\fP)
are not respected. By default, ripgrep respects \fBgit\fP's ignore rules for
automatic filtering. In some cases, it may not be desirable to respect the
source control's ignore rules and instead only respect rules in \fB.ignore\fP
or \fB.rgignore\fP.
.sp
This flag implies \flag{no-ignore-parent} for source control ignore files as
well.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.no_ignore_vcs = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_no_ignore_vcs() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.no_ignore_vcs);
let args = parse_low_raw(["--no-ignore-vcs"]).unwrap();
assert_eq!(true, args.no_ignore_vcs);
let args = parse_low_raw(["--no-ignore-vcs", "--ignore-vcs"]).unwrap();
assert_eq!(false, args.no_ignore_vcs);
}
/// --no-messages
#[derive(Debug)]
struct NoMessages;
impl Flag for NoMessages {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"no-messages"
}
fn name_negated(&self) -> Option<&'static str> {
Some("messages")
}
fn doc_category(&self) -> Category {
Category::Logging
}
fn doc_short(&self) -> &'static str {
r"Suppress some error messages."
}
fn doc_long(&self) -> &'static str {
r"
This flag suppresses some error messages. Specifically, messages related to
the failed opening and reading of files. Error messages related to the syntax
of the pattern are still shown.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.no_messages = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_no_messages() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.no_messages);
let args = parse_low_raw(["--no-messages"]).unwrap();
assert_eq!(true, args.no_messages);
let args = parse_low_raw(["--no-messages", "--messages"]).unwrap();
assert_eq!(false, args.no_messages);
}
/// --no-pcre2-unicode
#[derive(Debug)]
struct NoPcre2Unicode;
impl Flag for NoPcre2Unicode {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"no-pcre2-unicode"
}
fn name_negated(&self) -> Option<&'static str> {
Some("pcre2-unicode")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"(DEPRECATED) Disable Unicode mode for PCRE2."
}
fn doc_long(&self) -> &'static str {
r"
DEPRECATED. Use \flag{no-unicode} instead.
.sp
Note that Unicode mode is enabled by default.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.no_unicode = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_no_pcre2_unicode() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.no_unicode);
let args = parse_low_raw(["--no-pcre2-unicode"]).unwrap();
assert_eq!(true, args.no_unicode);
let args =
parse_low_raw(["--no-pcre2-unicode", "--pcre2-unicode"]).unwrap();
assert_eq!(false, args.no_unicode);
}
/// --no-require-git
#[derive(Debug)]
struct NoRequireGit;
impl Flag for NoRequireGit {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"no-require-git"
}
fn name_negated(&self) -> Option<&'static str> {
Some("require-git")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Use .gitignore outside of git repositories."
}
fn doc_long(&self) -> &'static str {
r"
When this flag is given, source control ignore files such as \fB.gitignore\fP
are respected even if no \fBgit\fP repository is present.
.sp
By default, ripgrep will only respect filter rules from source control ignore
files when ripgrep detects that the search is executed inside a source control
repository. For example, when a \fB.git\fP directory is observed.
.sp
This flag relaxes the default restriction. For example, it might be useful when
the contents of a \fBgit\fP repository are stored or copied somewhere, but
where the repository state is absent.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.no_require_git = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_no_require_git() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.no_require_git);
let args = parse_low_raw(["--no-require-git"]).unwrap();
assert_eq!(true, args.no_require_git);
let args = parse_low_raw(["--no-require-git", "--require-git"]).unwrap();
assert_eq!(false, args.no_require_git);
}
/// --no-unicode
#[derive(Debug)]
struct NoUnicode;
impl Flag for NoUnicode {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"no-unicode"
}
fn name_negated(&self) -> Option<&'static str> {
Some("unicode")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Disable Unicode mode."
}
fn doc_long(&self) -> &'static str {
r#"
This flag disables Unicode mode for all patterns given to ripgrep.
.sp
By default, ripgrep will enable "Unicode mode" in all of its regexes. This has
a number of consequences:
.sp
.IP \(bu 3n
\fB.\fP will only match valid UTF-8 encoded Unicode scalar values.
.sp
.IP \(bu 3n
Classes like \fB\\w\fP, \fB\\s\fP, \fB\\d\fP are all Unicode aware and much
bigger than their ASCII only versions.
.sp
.IP \(bu 3n
Case insensitive matching will use Unicode case folding.
.sp
.IP \(bu 3n
A large array of classes like \fB\\p{Emoji}\fP are available. (Although the
specific set of classes available varies based on the regex engine. In general,
the default regex engine has more classes available to it.)
.sp
.IP \(bu 3n
Word boundaries (\fB\\b\fP and \fB\\B\fP) use the Unicode definition of a word
character.
.PP
In some cases it can be desirable to turn these things off. This flag will do
exactly that. For example, Unicode mode can sometimes have a negative impact
on performance, especially when things like \fB\\w\fP are used frequently
(including via bounded repetitions like \fB\\w{100}\fP) when only their ASCII
interpretation is needed.
"#
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.no_unicode = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_no_unicode() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.no_unicode);
let args = parse_low_raw(["--no-unicode"]).unwrap();
assert_eq!(true, args.no_unicode);
let args = parse_low_raw(["--no-unicode", "--unicode"]).unwrap();
assert_eq!(false, args.no_unicode);
let args = parse_low_raw(["--no-unicode", "--pcre2-unicode"]).unwrap();
assert_eq!(false, args.no_unicode);
let args = parse_low_raw(["--no-pcre2-unicode", "--unicode"]).unwrap();
assert_eq!(false, args.no_unicode);
}
/// -0/--null
#[derive(Debug)]
struct Null;
impl Flag for Null {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'0')
}
fn name_long(&self) -> &'static str {
"null"
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Print a NUL byte after file paths."
}
fn doc_long(&self) -> &'static str {
r"
Whenever a file path is printed, follow it with a \fBNUL\fP byte. This includes
printing file paths before matches, and when printing a list of matching files
such as with \flag{count}, \flag{files-with-matches} and \flag{files}. This
option is useful for use with \fBxargs\fP.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--null has no negation");
args.null = true;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_null() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.null);
let args = parse_low_raw(["--null"]).unwrap();
assert_eq!(true, args.null);
let args = parse_low_raw(["-0"]).unwrap();
assert_eq!(true, args.null);
}
/// --null-data
#[derive(Debug)]
struct NullData;
impl Flag for NullData {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"null-data"
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Use NUL as a line terminator."
}
fn doc_long(&self) -> &'static str {
r"
Enabling this flag causes ripgrep to use \fBNUL\fP as a line terminator instead
of the default of \fP\\n\fP.
.sp
This is useful when searching large binary files that would otherwise have
very long lines if \fB\\n\fP were used as the line terminator. In particular,
ripgrep requires that, at a minimum, each line must fit into memory. Using
\fBNUL\fP instead can be a useful stopgap to keep memory requirements low and
avoid OOM (out of memory) conditions.
.sp
This is also useful for processing NUL delimited data, such as that emitted
when using ripgrep's \flag{null} flag or \fBfind\fP's \fB\-\-print0\fP flag.
.sp
Using this flag implies \flag{text}. It also overrides \flag{crlf}.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--null-data has no negation");
args.crlf = false;
args.null_data = true;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_null_data() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.null_data);
let args = parse_low_raw(["--null-data"]).unwrap();
assert_eq!(true, args.null_data);
let args = parse_low_raw(["--null-data", "--crlf"]).unwrap();
assert_eq!(false, args.null_data);
assert_eq!(true, args.crlf);
let args = parse_low_raw(["--crlf", "--null-data"]).unwrap();
assert_eq!(true, args.null_data);
assert_eq!(false, args.crlf);
let args = parse_low_raw(["--null-data", "--no-crlf"]).unwrap();
assert_eq!(true, args.null_data);
assert_eq!(false, args.crlf);
}
/// --one-file-system
#[derive(Debug)]
struct OneFileSystem;
impl Flag for OneFileSystem {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"one-file-system"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-one-file-system")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Skip directories on other file systems."
}
fn doc_long(&self) -> &'static str {
r"
When enabled, ripgrep will not cross file system boundaries relative to where
the search started from.
.sp
Note that this applies to each path argument given to ripgrep. For example, in
the command
.sp
.EX
rg \-\-one\-file\-system /foo/bar /quux/baz
.EE
.sp
ripgrep will search both \fI/foo/bar\fP and \fI/quux/baz\fP even if they are
on different file systems, but will not cross a file system boundary when
traversing each path's directory tree.
.sp
This is similar to \fBfind\fP's \fB\-xdev\fP or \fB\-mount\fP flag.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.one_file_system = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_one_file_system() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.one_file_system);
let args = parse_low_raw(["--one-file-system"]).unwrap();
assert_eq!(true, args.one_file_system);
let args =
parse_low_raw(["--one-file-system", "--no-one-file-system"]).unwrap();
assert_eq!(false, args.one_file_system);
}
/// -o/--only-matching
#[derive(Debug)]
struct OnlyMatching;
impl Flag for OnlyMatching {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'o')
}
fn name_long(&self) -> &'static str {
"only-matching"
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Print only matched parts of a line."
}
fn doc_long(&self) -> &'static str {
r"
Print only the matched (non-empty) parts of a matching line, with each such
part on a separate output line.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--only-matching does not have a negation");
args.only_matching = true;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_only_matching() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.only_matching);
let args = parse_low_raw(["--only-matching"]).unwrap();
assert_eq!(true, args.only_matching);
let args = parse_low_raw(["-o"]).unwrap();
assert_eq!(true, args.only_matching);
}
/// --path-separator
#[derive(Debug)]
struct PathSeparator;
impl Flag for PathSeparator {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"path-separator"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("SEPARATOR")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Set the path separator for printing paths."
}
fn doc_long(&self) -> &'static str {
r"
Set the path separator to use when printing file paths. This defaults to your
platform's path separator, which is \fB/\fP on Unix and \fB\\\fP on Windows.
This flag is intended for overriding the default when the environment demands
it (e.g., cygwin). A path separator is limited to a single byte.
.sp
Setting this flag to an empty string reverts it to its default behavior. That
is, the path separator is automatically chosen based on the environment.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let s = convert::string(v.unwrap_value())?;
let raw = Vec::unescape_bytes(&s);
args.path_separator = if raw.is_empty() {
None
} else if raw.len() == 1 {
Some(raw[0])
} else {
anyhow::bail!(
"A path separator must be exactly one byte, but \
the given separator is {len} bytes: {sep}\n\
In some shells on Windows '/' is automatically \
expanded. Use '//' instead.",
len = raw.len(),
sep = s,
)
};
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_path_separator() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.path_separator);
let args = parse_low_raw(["--path-separator", "/"]).unwrap();
assert_eq!(Some(b'/'), args.path_separator);
let args = parse_low_raw(["--path-separator", r"\"]).unwrap();
assert_eq!(Some(b'\\'), args.path_separator);
let args = parse_low_raw(["--path-separator", r"\x00"]).unwrap();
assert_eq!(Some(0), args.path_separator);
let args = parse_low_raw(["--path-separator", r"\0"]).unwrap();
assert_eq!(Some(0), args.path_separator);
let args = parse_low_raw(["--path-separator", "\x00"]).unwrap();
assert_eq!(Some(0), args.path_separator);
let args = parse_low_raw(["--path-separator", "\0"]).unwrap();
assert_eq!(Some(0), args.path_separator);
let args =
parse_low_raw(["--path-separator", r"\x00", "--path-separator=/"])
.unwrap();
assert_eq!(Some(b'/'), args.path_separator);
let result = parse_low_raw(["--path-separator", "foo"]);
assert!(result.is_err(), "{result:?}");
let result = parse_low_raw(["--path-separator", r"\\x00"]);
assert!(result.is_err(), "{result:?}");
}
/// --passthru
#[derive(Debug)]
struct Passthru;
impl Flag for Passthru {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"passthru"
}
fn aliases(&self) -> &'static [&'static str] {
&["passthrough"]
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Print both matching and non-matching lines."
}
fn doc_long(&self) -> &'static str {
r#"
Print both matching and non-matching lines.
.sp
Another way to achieve a similar effect is by modifying your pattern to match
the empty string. For example, if you are searching using \fBrg\fP \fIfoo\fP,
then using \fBrg\fP \fB'^|\fP\fIfoo\fP\fB'\fP instead will emit every line in
every file searched, but only occurrences of \fIfoo\fP will be highlighted.
This flag enables the same behavior without needing to modify the pattern.
.sp
An alternative spelling for this flag is \fB\-\-passthrough\fP.
.sp
This overrides the \flag{context}, \flag{after-context} and
\flag{before-context} flags.
"#
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--passthru has no negation");
args.context = ContextMode::Passthru;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_passthru() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(ContextMode::default(), args.context);
let args = parse_low_raw(["--passthru"]).unwrap();
assert_eq!(ContextMode::Passthru, args.context);
let args = parse_low_raw(["--passthrough"]).unwrap();
assert_eq!(ContextMode::Passthru, args.context);
}
/// -P/--pcre2
#[derive(Debug)]
struct PCRE2;
impl Flag for PCRE2 {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'P')
}
fn name_long(&self) -> &'static str {
"pcre2"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-pcre2")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Enable PCRE2 matching."
}
fn doc_long(&self) -> &'static str {
r"
When this flag is present, ripgrep will use the PCRE2 regex engine instead of
its default regex engine.
.sp
This is generally useful when you want to use features such as look-around
or backreferences.
.sp
Using this flag is the same as passing \fB\-\-engine=pcre2\fP. Users may
instead elect to use \fB\-\-engine=auto\fP to ask ripgrep to automatically
select the right regex engine based on the patterns given. This flag and the
\flag{engine} flag override one another.
.sp
Note that PCRE2 is an optional ripgrep feature. If PCRE2 wasn't included in
your build of ripgrep, then using this flag will result in ripgrep printing
an error message and exiting. PCRE2 may also have worse user experience in
some cases, since it has fewer introspection APIs than ripgrep's default
regex engine. For example, if you use a \fB\\n\fP in a PCRE2 regex without
the \flag{multiline} flag, then ripgrep will silently fail to match anything
instead of reporting an error immediately (like it does with the default regex
engine).
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.engine = if v.unwrap_switch() {
EngineChoice::PCRE2
} else {
EngineChoice::Default
};
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_pcre2() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(EngineChoice::Default, args.engine);
let args = parse_low_raw(["--pcre2"]).unwrap();
assert_eq!(EngineChoice::PCRE2, args.engine);
let args = parse_low_raw(["-P"]).unwrap();
assert_eq!(EngineChoice::PCRE2, args.engine);
let args = parse_low_raw(["-P", "--no-pcre2"]).unwrap();
assert_eq!(EngineChoice::Default, args.engine);
let args = parse_low_raw(["--engine=auto", "-P", "--no-pcre2"]).unwrap();
assert_eq!(EngineChoice::Default, args.engine);
let args = parse_low_raw(["-P", "--engine=auto"]).unwrap();
assert_eq!(EngineChoice::Auto, args.engine);
}
/// --pcre2-version
#[derive(Debug)]
struct PCRE2Version;
impl Flag for PCRE2Version {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"pcre2-version"
}
fn doc_category(&self) -> Category {
Category::OtherBehaviors
}
fn doc_short(&self) -> &'static str {
r"Print the version of PCRE2 that ripgrep uses."
}
fn doc_long(&self) -> &'static str {
r"
When this flag is present, ripgrep will print the version of PCRE2 in use,
along with other information, and then exit. If PCRE2 is not available, then
ripgrep will print an error message and exit with an error code.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--pcre2-version has no negation");
args.special = Some(SpecialMode::VersionPCRE2);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_pcre2_version() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.special);
let args = parse_low_raw(["--pcre2-version"]).unwrap();
assert_eq!(Some(SpecialMode::VersionPCRE2), args.special);
}
/// --pre
#[derive(Debug)]
struct Pre;
impl Flag for Pre {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"pre"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-pre")
}
fn doc_variable(&self) -> Option<&'static str> {
Some("COMMAND")
}
fn doc_category(&self) -> Category {
Category::Input
}
fn doc_short(&self) -> &'static str {
r"Search output of COMMAND for each PATH."
}
fn doc_long(&self) -> &'static str {
r#"
For each input \fIPATH\fP, this flag causes ripgrep to search the standard
output of \fICOMMAND\fP \fIPATH\fP instead of the contents of \fIPATH\fP.
This option expects the \fICOMMAND\fP program to either be a path or to be
available in your \fBPATH\fP. Either an empty string \fICOMMAND\fP or the
\fB\-\-no\-pre\fP flag will disable this behavior.
.sp
.TP 12
\fBWARNING\fP
When this flag is set, ripgrep will unconditionally spawn a process for every
file that is searched. Therefore, this can incur an unnecessarily large
performance penalty if you don't otherwise need the flexibility offered by this
flag. One possible mitigation to this is to use the \flag{pre-glob} flag to
limit which files a preprocessor is run with.
.PP
A preprocessor is not run when ripgrep is searching stdin.
.sp
When searching over sets of files that may require one of several
preprocessors, \fICOMMAND\fP should be a wrapper program which first classifies
\fIPATH\fP based on magic numbers/content or based on the \fIPATH\fP name and
then dispatches to an appropriate preprocessor. Each \fICOMMAND\fP also has its
standard input connected to \fIPATH\fP for convenience.
.sp
For example, a shell script for \fICOMMAND\fP might look like:
.sp
.EX
case "$1" in
*.pdf)
exec pdftotext "$1" -
;;
*)
case $(file "$1") in
*Zstandard*)
exec pzstd -cdq
;;
*)
exec cat
;;
esac
;;
esac
.EE
.sp
The above script uses \fBpdftotext\fP to convert a PDF file to plain text. For
all other files, the script uses the \fBfile\fP utility to sniff the type of
the file based on its contents. If it is a compressed file in the Zstandard
format, then \fBpzstd\fP is used to decompress the contents to stdout.
.sp
This overrides the \flag{search-zip} flag.
"#
}
fn completion_type(&self) -> CompletionType {
CompletionType::Executable
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let path = match v {
FlagValue::Value(v) => PathBuf::from(v),
FlagValue::Switch(yes) => {
assert!(!yes, "there is no affirmative switch for --pre");
args.pre = None;
return Ok(());
}
};
args.pre = if path.as_os_str().is_empty() { None } else { Some(path) };
if args.pre.is_some() {
args.search_zip = false;
}
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_pre() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.pre);
let args = parse_low_raw(["--pre", "foo/bar"]).unwrap();
assert_eq!(Some(PathBuf::from("foo/bar")), args.pre);
let args = parse_low_raw(["--pre", ""]).unwrap();
assert_eq!(None, args.pre);
let args = parse_low_raw(["--pre", "foo/bar", "--pre", ""]).unwrap();
assert_eq!(None, args.pre);
let args = parse_low_raw(["--pre", "foo/bar", "--pre="]).unwrap();
assert_eq!(None, args.pre);
let args = parse_low_raw(["--pre", "foo/bar", "--no-pre"]).unwrap();
assert_eq!(None, args.pre);
}
/// --pre-glob
#[derive(Debug)]
struct PreGlob;
impl Flag for PreGlob {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"pre-glob"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("GLOB")
}
fn doc_category(&self) -> Category {
Category::Input
}
fn doc_short(&self) -> &'static str {
r"Include or exclude files from a preprocessor."
}
fn doc_long(&self) -> &'static str {
r#"
This flag works in conjunction with the \flag{pre} flag. Namely, when one or
more \flag{pre-glob} flags are given, then only files that match the given set
of globs will be handed to the command specified by the \flag{pre} flag. Any
non-matching files will be searched without using the preprocessor command.
.sp
This flag is useful when searching many files with the \flag{pre} flag.
Namely, it provides the ability to avoid process overhead for files that
don't need preprocessing. For example, given the following shell script,
\fIpre-pdftotext\fP:
.sp
.EX
#!/bin/sh
pdftotext "$1" -
.EE
.sp
then it is possible to use \fB\-\-pre\fP \fIpre-pdftotext\fP \fB--pre-glob
'\fP\fI*.pdf\fP\fB'\fP to make it so ripgrep only executes the
\fIpre-pdftotext\fP command on files with a \fI.pdf\fP extension.
.sp
Multiple \flag{pre-glob} flags may be used. Globbing rules match
\fBgitignore\fP globs. Precede a glob with a \fB!\fP to exclude it.
.sp
This flag has no effect if the \flag{pre} flag is not used.
"#
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let glob = convert::string(v.unwrap_value())?;
args.pre_glob.push(glob);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_pre_glob() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Vec::<String>::new(), args.pre_glob);
let args = parse_low_raw(["--pre-glob", "*.pdf"]).unwrap();
assert_eq!(vec!["*.pdf".to_string()], args.pre_glob);
let args =
parse_low_raw(["--pre-glob", "*.pdf", "--pre-glob=foo"]).unwrap();
assert_eq!(vec!["*.pdf".to_string(), "foo".to_string()], args.pre_glob);
}
/// -p/--pretty
#[derive(Debug)]
struct Pretty;
impl Flag for Pretty {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'p')
}
fn name_long(&self) -> &'static str {
"pretty"
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Alias for colors, headings and line numbers."
}
fn doc_long(&self) -> &'static str {
r"
This is a convenience alias for \fB\-\-color=always \-\-heading
\-\-line\-number\fP. This flag is useful when you still want pretty output even
if you're piping ripgrep to another program or file. For example: \fBrg -p
\fP\fIfoo\fP \fB| less -R\fP.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--pretty has no negation");
args.color = ColorChoice::Always;
args.heading = Some(true);
args.line_number = Some(true);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_pretty() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(ColorChoice::Auto, args.color);
assert_eq!(None, args.heading);
assert_eq!(None, args.line_number);
let args = parse_low_raw(["--pretty"]).unwrap();
assert_eq!(ColorChoice::Always, args.color);
assert_eq!(Some(true), args.heading);
assert_eq!(Some(true), args.line_number);
let args = parse_low_raw(["-p"]).unwrap();
assert_eq!(ColorChoice::Always, args.color);
assert_eq!(Some(true), args.heading);
assert_eq!(Some(true), args.line_number);
}
/// -q/--quiet
#[derive(Debug)]
struct Quiet;
impl Flag for Quiet {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'q')
}
fn name_long(&self) -> &'static str {
"quiet"
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Do not print anything to stdout."
}
fn doc_long(&self) -> &'static str {
r"
Do not print anything to stdout. If a match is found in a file, then ripgrep
will stop searching. This is useful when ripgrep is used only for its exit code
(which will be an error code if no matches are found).
.sp
When \flag{files} is used, ripgrep will stop finding files after finding the
first file that does not match any ignore rules.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--quiet has no negation");
args.quiet = true;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_quiet() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.quiet);
let args = parse_low_raw(["--quiet"]).unwrap();
assert_eq!(true, args.quiet);
let args = parse_low_raw(["-q"]).unwrap();
assert_eq!(true, args.quiet);
// flags like -l and --json cannot override -q, regardless of order
let args = parse_low_raw(["-q", "--json"]).unwrap();
assert_eq!(true, args.quiet);
let args = parse_low_raw(["-q", "--files-with-matches"]).unwrap();
assert_eq!(true, args.quiet);
let args = parse_low_raw(["-q", "--files-without-match"]).unwrap();
assert_eq!(true, args.quiet);
let args = parse_low_raw(["-q", "--count"]).unwrap();
assert_eq!(true, args.quiet);
let args = parse_low_raw(["-q", "--count-matches"]).unwrap();
assert_eq!(true, args.quiet);
}
/// --regex-size-limit
#[derive(Debug)]
struct RegexSizeLimit;
impl Flag for RegexSizeLimit {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"regex-size-limit"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("NUM+SUFFIX?")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"The size limit of the compiled regex."
}
fn doc_long(&self) -> &'static str {
r"
The size limit of the compiled regex, where the compiled regex generally
corresponds to a single object in memory that can match all of the patterns
provided to ripgrep. The default limit is generous enough that most reasonable
patterns (or even a small number of them) should fit.
.sp
This useful to change when you explicitly want to let ripgrep spend potentially
much more time and/or memory building a regex matcher.
.sp
The input format accepts suffixes of \fBK\fP, \fBM\fP or \fBG\fP which
correspond to kilobytes, megabytes and gigabytes, respectively. If no suffix is
provided the input is treated as bytes.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let v = v.unwrap_value();
args.regex_size_limit = Some(convert::human_readable_usize(&v)?);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_regex_size_limit() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.regex_size_limit);
#[cfg(target_pointer_width = "64")]
{
let args = parse_low_raw(["--regex-size-limit", "9G"]).unwrap();
assert_eq!(Some(9 * (1 << 30)), args.regex_size_limit);
let args = parse_low_raw(["--regex-size-limit=9G"]).unwrap();
assert_eq!(Some(9 * (1 << 30)), args.regex_size_limit);
let args =
parse_low_raw(["--regex-size-limit=9G", "--regex-size-limit=0"])
.unwrap();
assert_eq!(Some(0), args.regex_size_limit);
}
let args = parse_low_raw(["--regex-size-limit=0K"]).unwrap();
assert_eq!(Some(0), args.regex_size_limit);
let args = parse_low_raw(["--regex-size-limit=0M"]).unwrap();
assert_eq!(Some(0), args.regex_size_limit);
let args = parse_low_raw(["--regex-size-limit=0G"]).unwrap();
assert_eq!(Some(0), args.regex_size_limit);
let result =
parse_low_raw(["--regex-size-limit", "9999999999999999999999"]);
assert!(result.is_err(), "{result:?}");
let result = parse_low_raw(["--regex-size-limit", "9999999999999999G"]);
assert!(result.is_err(), "{result:?}");
}
/// -e/--regexp
#[derive(Debug)]
struct Regexp;
impl Flag for Regexp {
fn is_switch(&self) -> bool {
false
}
fn name_short(&self) -> Option<u8> {
Some(b'e')
}
fn name_long(&self) -> &'static str {
"regexp"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("PATTERN")
}
fn doc_category(&self) -> Category {
Category::Input
}
fn doc_short(&self) -> &'static str {
r"A pattern to search for."
}
fn doc_long(&self) -> &'static str {
r"
A pattern to search for. This option can be provided multiple times, where
all patterns given are searched, in addition to any patterns provided by
\flag{file}. Lines matching at least one of the provided patterns are printed.
This flag can also be used when searching for patterns that start with a dash.
.sp
For example, to search for the literal \fB\-foo\fP:
.sp
.EX
rg \-e \-foo
.EE
.sp
You can also use the special \fB\-\-\fP delimiter to indicate that no more
flags will be provided. Namely, the following is equivalent to the above:
.sp
.EX
rg \-\- \-foo
.EE
.sp
When \flag{file} or \flag{regexp} is used, then ripgrep treats all positional
arguments as files or directories to search.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let regexp = convert::string(v.unwrap_value())?;
args.patterns.push(PatternSource::Regexp(regexp));
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_regexp() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Vec::<PatternSource>::new(), args.patterns);
let args = parse_low_raw(["--regexp", "foo"]).unwrap();
assert_eq!(vec![PatternSource::Regexp("foo".to_string())], args.patterns);
let args = parse_low_raw(["--regexp=foo"]).unwrap();
assert_eq!(vec![PatternSource::Regexp("foo".to_string())], args.patterns);
let args = parse_low_raw(["-e", "foo"]).unwrap();
assert_eq!(vec![PatternSource::Regexp("foo".to_string())], args.patterns);
let args = parse_low_raw(["-efoo"]).unwrap();
assert_eq!(vec![PatternSource::Regexp("foo".to_string())], args.patterns);
let args = parse_low_raw(["--regexp", "-foo"]).unwrap();
assert_eq!(vec![PatternSource::Regexp("-foo".to_string())], args.patterns);
let args = parse_low_raw(["--regexp=-foo"]).unwrap();
assert_eq!(vec![PatternSource::Regexp("-foo".to_string())], args.patterns);
let args = parse_low_raw(["-e", "-foo"]).unwrap();
assert_eq!(vec![PatternSource::Regexp("-foo".to_string())], args.patterns);
let args = parse_low_raw(["-e-foo"]).unwrap();
assert_eq!(vec![PatternSource::Regexp("-foo".to_string())], args.patterns);
let args = parse_low_raw(["--regexp=foo", "--regexp", "bar"]).unwrap();
assert_eq!(
vec![
PatternSource::Regexp("foo".to_string()),
PatternSource::Regexp("bar".to_string())
],
args.patterns
);
// While we support invalid UTF-8 arguments in general, patterns must be
// valid UTF-8.
#[cfg(unix)]
{
use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
let bytes = &[b'A', 0xFF, b'Z'][..];
let result = parse_low_raw([
OsStr::from_bytes(b"-e"),
OsStr::from_bytes(bytes),
]);
assert!(result.is_err(), "{result:?}");
}
// Check that combining -e/--regexp and -f/--file works as expected.
let args = parse_low_raw(["-efoo", "-fbar"]).unwrap();
assert_eq!(
vec![
PatternSource::Regexp("foo".to_string()),
PatternSource::File(PathBuf::from("bar"))
],
args.patterns
);
let args = parse_low_raw(["-efoo", "-fbar", "-equux"]).unwrap();
assert_eq!(
vec![
PatternSource::Regexp("foo".to_string()),
PatternSource::File(PathBuf::from("bar")),
PatternSource::Regexp("quux".to_string()),
],
args.patterns
);
}
/// -r/--replace
#[derive(Debug)]
struct Replace;
impl Flag for Replace {
fn is_switch(&self) -> bool {
false
}
fn name_short(&self) -> Option<u8> {
Some(b'r')
}
fn name_long(&self) -> &'static str {
"replace"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("REPLACEMENT")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Replace matches with the given text."
}
fn doc_long(&self) -> &'static str {
r#"
Replaces every match with the text given when printing results. Neither this
flag nor any other ripgrep flag will modify your files.
.sp
Capture group indices (e.g., \fB$\fP\fI5\fP) and names (e.g., \fB$\fP\fIfoo\fP)
are supported in the replacement string. Capture group indices are numbered
based on the position of the opening parenthesis of the group, where the
leftmost such group is \fB$\fP\fI1\fP. The special \fB$\fP\fI0\fP group
corresponds to the entire match.
.sp
The name of a group is formed by taking the longest string of letters, numbers
and underscores (i.e. \fB[_0-9A-Za-z]\fP) after the \fB$\fP. For example,
\fB$\fP\fI1a\fP will be replaced with the group named \fI1a\fP, not the
group at index \fI1\fP. If the group's name contains characters that aren't
letters, numbers or underscores, or you want to immediately follow the group
with another string, the name should be put inside braces. For example,
\fB${\fP\fI1\fP\fB}\fP\fIa\fP will take the content of the group at index
\fI1\fP and append \fIa\fP to the end of it.
.sp
If an index or name does not refer to a valid capture group, it will be
replaced with an empty string.
.sp
In shells such as Bash and zsh, you should wrap the pattern in single quotes
instead of double quotes. Otherwise, capture group indices will be replaced by
expanded shell variables which will most likely be empty.
.sp
To write a literal \fB$\fP, use \fB$$\fP.
.sp
Note that the replacement by default replaces each match, and not the entire
line. To replace the entire line, you should match the entire line.
.sp
This flag can be used with the \flag{only-matching} flag.
"#
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.replace = Some(convert::string(v.unwrap_value())?.into());
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_replace() {
use bstr::BString;
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.replace);
let args = parse_low_raw(["--replace", "foo"]).unwrap();
assert_eq!(Some(BString::from("foo")), args.replace);
let args = parse_low_raw(["--replace", "-foo"]).unwrap();
assert_eq!(Some(BString::from("-foo")), args.replace);
let args = parse_low_raw(["-r", "foo"]).unwrap();
assert_eq!(Some(BString::from("foo")), args.replace);
let args = parse_low_raw(["-r", "foo", "-rbar"]).unwrap();
assert_eq!(Some(BString::from("bar")), args.replace);
let args = parse_low_raw(["-r", "foo", "-r", ""]).unwrap();
assert_eq!(Some(BString::from("")), args.replace);
}
/// -z/--search-zip
#[derive(Debug)]
struct SearchZip;
impl Flag for SearchZip {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'z')
}
fn name_long(&self) -> &'static str {
"search-zip"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-search-zip")
}
fn doc_category(&self) -> Category {
Category::Input
}
fn doc_short(&self) -> &'static str {
r"Search in compressed files."
}
fn doc_long(&self) -> &'static str {
r"
This flag instructs ripgrep to search in compressed files. Currently gzip,
bzip2, xz, LZ4, LZMA, Brotli and Zstd files are supported. This option expects
the decompression binaries (such as \fBgzip\fP) to be available in your
\fBPATH\fP. If the required binaries are not found, then ripgrep will not
emit an error messages by default. Use the \flag{debug} flag to see more
information.
.sp
Note that this flag does not make ripgrep search archive formats as directory
trees. It only makes ripgrep detect compressed files and then decompress them
before searching their contents as it would any other file.
.sp
This overrides the \flag{pre} flag.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.search_zip = if v.unwrap_switch() {
args.pre = None;
true
} else {
false
};
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_search_zip() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.search_zip);
let args = parse_low_raw(["--search-zip"]).unwrap();
assert_eq!(true, args.search_zip);
let args = parse_low_raw(["-z"]).unwrap();
assert_eq!(true, args.search_zip);
let args = parse_low_raw(["-z", "--no-search-zip"]).unwrap();
assert_eq!(false, args.search_zip);
let args = parse_low_raw(["--pre=foo", "--no-search-zip"]).unwrap();
assert_eq!(Some(PathBuf::from("foo")), args.pre);
assert_eq!(false, args.search_zip);
let args = parse_low_raw(["--pre=foo", "--search-zip"]).unwrap();
assert_eq!(None, args.pre);
assert_eq!(true, args.search_zip);
let args = parse_low_raw(["--pre=foo", "-z", "--no-search-zip"]).unwrap();
assert_eq!(None, args.pre);
assert_eq!(false, args.search_zip);
}
/// -S/--smart-case
#[derive(Debug)]
struct SmartCase;
impl Flag for SmartCase {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'S')
}
fn name_long(&self) -> &'static str {
"smart-case"
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Smart case search."
}
fn doc_long(&self) -> &'static str {
r"
This flag instructs ripgrep to searches case insensitively if the pattern is
all lowercase. Otherwise, ripgrep will search case sensitively.
.sp
A pattern is considered all lowercase if both of the following rules hold:
.sp
.IP \(bu 3n
First, the pattern contains at least one literal character. For example,
\fBa\\w\fP contains a literal (\fBa\fP) but just \fB\\w\fP does not.
.sp
.IP \(bu 3n
Second, of the literals in the pattern, none of them are considered to be
uppercase according to Unicode. For example, \fBfoo\\pL\fP has no uppercase
literals but \fBFoo\\pL\fP does.
.PP
This overrides the \flag{case-sensitive} and \flag{ignore-case} flags.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--smart-case flag has no negation");
args.case = CaseMode::Smart;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_smart_case() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(CaseMode::Sensitive, args.case);
let args = parse_low_raw(["--smart-case"]).unwrap();
assert_eq!(CaseMode::Smart, args.case);
let args = parse_low_raw(["-S"]).unwrap();
assert_eq!(CaseMode::Smart, args.case);
let args = parse_low_raw(["-S", "-s"]).unwrap();
assert_eq!(CaseMode::Sensitive, args.case);
let args = parse_low_raw(["-S", "-i"]).unwrap();
assert_eq!(CaseMode::Insensitive, args.case);
let args = parse_low_raw(["-s", "-S"]).unwrap();
assert_eq!(CaseMode::Smart, args.case);
let args = parse_low_raw(["-i", "-S"]).unwrap();
assert_eq!(CaseMode::Smart, args.case);
}
/// --sort-files
#[derive(Debug)]
struct SortFiles;
impl Flag for SortFiles {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"sort-files"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-sort-files")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"(DEPRECATED) Sort results by file path."
}
fn doc_long(&self) -> &'static str {
r"
DEPRECATED. Use \fB\-\-sort=path\fP instead.
.sp
This flag instructs ripgrep to sort search results by file path
lexicographically in ascending order. Note that this currently disables all
parallelism and runs search in a single thread.
.sp
This flag overrides \flag{sort} and \flag{sortr}.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.sort = if v.unwrap_switch() {
Some(SortMode { reverse: false, kind: SortModeKind::Path })
} else {
None
};
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_sort_files() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.sort);
let args = parse_low_raw(["--sort-files"]).unwrap();
assert_eq!(
Some(SortMode { reverse: false, kind: SortModeKind::Path }),
args.sort
);
let args = parse_low_raw(["--sort-files", "--no-sort-files"]).unwrap();
assert_eq!(None, args.sort);
let args = parse_low_raw(["--sort", "created", "--sort-files"]).unwrap();
assert_eq!(
Some(SortMode { reverse: false, kind: SortModeKind::Path }),
args.sort
);
let args = parse_low_raw(["--sort-files", "--sort", "created"]).unwrap();
assert_eq!(
Some(SortMode { reverse: false, kind: SortModeKind::Created }),
args.sort
);
let args = parse_low_raw(["--sortr", "created", "--sort-files"]).unwrap();
assert_eq!(
Some(SortMode { reverse: false, kind: SortModeKind::Path }),
args.sort
);
let args = parse_low_raw(["--sort-files", "--sortr", "created"]).unwrap();
assert_eq!(
Some(SortMode { reverse: true, kind: SortModeKind::Created }),
args.sort
);
let args = parse_low_raw(["--sort=path", "--no-sort-files"]).unwrap();
assert_eq!(None, args.sort);
let args = parse_low_raw(["--sortr=path", "--no-sort-files"]).unwrap();
assert_eq!(None, args.sort);
}
/// --sort
#[derive(Debug)]
struct Sort;
impl Flag for Sort {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"sort"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("SORTBY")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Sort results in ascending order."
}
fn doc_long(&self) -> &'static str {
r"
This flag enables sorting of results in ascending order. The possible values
for this flag are:
.sp
.TP 12
\fBnone\fP
(Default) Do not sort results. Fastest. Can be multi-threaded.
.TP 12
\fBpath\fP
Sort by file path. Always single-threaded. The order is determined by sorting
files in each directory entry during traversal. This means that given the files
\fBa/b\fP and \fBa+\fP, the latter will sort after the former even though
\fB+\fP would normally sort before \fB/\fP.
.TP 12
\fBmodified\fP
Sort by the last modified time on a file. Always single-threaded.
.TP 12
\fBaccessed\fP
Sort by the last accessed time on a file. Always single-threaded.
.TP 12
\fBcreated\fP
Sort by the creation time on a file. Always single-threaded.
.PP
If the chosen (manually or by-default) sorting criteria isn't available on your
system (for example, creation time is not available on ext4 file systems), then
ripgrep will attempt to detect this, print an error and exit without searching.
.sp
To sort results in reverse or descending order, use the \flag{sortr} flag.
Also, this flag overrides \flag{sortr}.
.sp
Note that sorting results currently always forces ripgrep to abandon
parallelism and run in a single thread.
"
}
fn doc_choices(&self) -> &'static [&'static str] {
&["none", "path", "modified", "accessed", "created"]
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let kind = match convert::str(&v.unwrap_value())? {
"none" => {
args.sort = None;
return Ok(());
}
"path" => SortModeKind::Path,
"modified" => SortModeKind::LastModified,
"accessed" => SortModeKind::LastAccessed,
"created" => SortModeKind::Created,
unk => anyhow::bail!("choice '{unk}' is unrecognized"),
};
args.sort = Some(SortMode { reverse: false, kind });
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_sort() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.sort);
let args = parse_low_raw(["--sort", "path"]).unwrap();
assert_eq!(
Some(SortMode { reverse: false, kind: SortModeKind::Path }),
args.sort
);
let args = parse_low_raw(["--sort", "path", "--sort=created"]).unwrap();
assert_eq!(
Some(SortMode { reverse: false, kind: SortModeKind::Created }),
args.sort
);
let args = parse_low_raw(["--sort=none"]).unwrap();
assert_eq!(None, args.sort);
let args = parse_low_raw(["--sort", "path", "--sort=none"]).unwrap();
assert_eq!(None, args.sort);
}
/// --sortr
#[derive(Debug)]
struct Sortr;
impl Flag for Sortr {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"sortr"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("SORTBY")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Sort results in descending order."
}
fn doc_long(&self) -> &'static str {
r"
This flag enables sorting of results in descending order. The possible values
for this flag are:
.sp
.TP 12
\fBnone\fP
(Default) Do not sort results. Fastest. Can be multi-threaded.
.TP 12
\fBpath\fP
Sort by file path. Always single-threaded. The order is determined by sorting
files in each directory entry during traversal. This means that given the files
\fBa/b\fP and \fBa+\fP, the latter will sort before the former even though
\fB+\fP would normally sort after \fB/\fP when doing a reverse lexicographic
sort.
.TP 12
\fBmodified\fP
Sort by the last modified time on a file. Always single-threaded.
.TP 12
\fBaccessed\fP
Sort by the last accessed time on a file. Always single-threaded.
.TP 12
\fBcreated\fP
Sort by the creation time on a file. Always single-threaded.
.PP
If the chosen (manually or by-default) sorting criteria isn't available on your
system (for example, creation time is not available on ext4 file systems), then
ripgrep will attempt to detect this, print an error and exit without searching.
.sp
To sort results in ascending order, use the \flag{sort} flag. Also, this flag
overrides \flag{sort}.
.sp
Note that sorting results currently always forces ripgrep to abandon
parallelism and run in a single thread.
"
}
fn doc_choices(&self) -> &'static [&'static str] {
&["none", "path", "modified", "accessed", "created"]
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let kind = match convert::str(&v.unwrap_value())? {
"none" => {
args.sort = None;
return Ok(());
}
"path" => SortModeKind::Path,
"modified" => SortModeKind::LastModified,
"accessed" => SortModeKind::LastAccessed,
"created" => SortModeKind::Created,
unk => anyhow::bail!("choice '{unk}' is unrecognized"),
};
args.sort = Some(SortMode { reverse: true, kind });
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_sortr() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.sort);
let args = parse_low_raw(["--sortr", "path"]).unwrap();
assert_eq!(
Some(SortMode { reverse: true, kind: SortModeKind::Path }),
args.sort
);
let args = parse_low_raw(["--sortr", "path", "--sortr=created"]).unwrap();
assert_eq!(
Some(SortMode { reverse: true, kind: SortModeKind::Created }),
args.sort
);
let args = parse_low_raw(["--sortr=none"]).unwrap();
assert_eq!(None, args.sort);
let args = parse_low_raw(["--sortr", "path", "--sortr=none"]).unwrap();
assert_eq!(None, args.sort);
let args = parse_low_raw(["--sort=path", "--sortr=path"]).unwrap();
assert_eq!(
Some(SortMode { reverse: true, kind: SortModeKind::Path }),
args.sort
);
let args = parse_low_raw(["--sortr=path", "--sort=path"]).unwrap();
assert_eq!(
Some(SortMode { reverse: false, kind: SortModeKind::Path }),
args.sort
);
}
/// --stats
#[derive(Debug)]
struct Stats;
impl Flag for Stats {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"stats"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-stats")
}
fn doc_category(&self) -> Category {
Category::Logging
}
fn doc_short(&self) -> &'static str {
r"Print statistics about the search."
}
fn doc_long(&self) -> &'static str {
r"
When enabled, ripgrep will print aggregate statistics about the search. When
this flag is present, ripgrep will print at least the following stats to
stdout at the end of the search: number of matched lines, number of files with
matches, number of files searched, and the time taken for the entire search to
complete.
.sp
This set of aggregate statistics may expand over time.
.sp
This flag is always and implicitly enabled when \flag{json} is used.
.sp
Note that this flag has no effect if \flag{files}, \flag{files-with-matches} or
\flag{files-without-match} is passed.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.stats = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_stats() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.stats);
let args = parse_low_raw(["--stats"]).unwrap();
assert_eq!(true, args.stats);
let args = parse_low_raw(["--stats", "--no-stats"]).unwrap();
assert_eq!(false, args.stats);
}
/// --stop-on-nonmatch
#[derive(Debug)]
struct StopOnNonmatch;
impl Flag for StopOnNonmatch {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"stop-on-nonmatch"
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Stop searching after a non-match."
}
fn doc_long(&self) -> &'static str {
r"
Enabling this option will cause ripgrep to stop reading a file once it
encounters a non-matching line after it has encountered a matching line.
This is useful if it is expected that all matches in a given file will be on
sequential lines, for example due to the lines being sorted.
.sp
This overrides the \flag{multiline} flag.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--stop-on-nonmatch has no negation");
args.stop_on_nonmatch = true;
args.multiline = false;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_stop_on_nonmatch() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.stop_on_nonmatch);
let args = parse_low_raw(["--stop-on-nonmatch"]).unwrap();
assert_eq!(true, args.stop_on_nonmatch);
let args = parse_low_raw(["--stop-on-nonmatch", "-U"]).unwrap();
assert_eq!(true, args.multiline);
assert_eq!(false, args.stop_on_nonmatch);
let args = parse_low_raw(["-U", "--stop-on-nonmatch"]).unwrap();
assert_eq!(false, args.multiline);
assert_eq!(true, args.stop_on_nonmatch);
let args =
parse_low_raw(["--stop-on-nonmatch", "--no-multiline"]).unwrap();
assert_eq!(false, args.multiline);
assert_eq!(true, args.stop_on_nonmatch);
}
/// -a/--text
#[derive(Debug)]
struct Text;
impl Flag for Text {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'a')
}
fn name_long(&self) -> &'static str {
"text"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-text")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Search binary files as if they were text."
}
fn doc_long(&self) -> &'static str {
r"
This flag instructs ripgrep to search binary files as if they were text. When
this flag is present, ripgrep's binary file detection is disabled. This means
that when a binary file is searched, its contents may be printed if there is
a match. This may cause escape codes to be printed that alter the behavior of
your terminal.
.sp
When binary file detection is enabled, it is imperfect. In general, it uses
a simple heuristic. If a \fBNUL\fP byte is seen during search, then the file
is considered binary and searching stops (unless this flag is present).
Alternatively, if the \flag{binary} flag is used, then ripgrep will only quit
when it sees a \fBNUL\fP byte after it sees a match (or searches the entire
file).
.sp
This flag overrides the \flag{binary} flag.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.binary = if v.unwrap_switch() {
BinaryMode::AsText
} else {
BinaryMode::Auto
};
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_text() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(BinaryMode::Auto, args.binary);
let args = parse_low_raw(["--text"]).unwrap();
assert_eq!(BinaryMode::AsText, args.binary);
let args = parse_low_raw(["-a"]).unwrap();
assert_eq!(BinaryMode::AsText, args.binary);
let args = parse_low_raw(["-a", "--no-text"]).unwrap();
assert_eq!(BinaryMode::Auto, args.binary);
let args = parse_low_raw(["-a", "--binary"]).unwrap();
assert_eq!(BinaryMode::SearchAndSuppress, args.binary);
let args = parse_low_raw(["--binary", "-a"]).unwrap();
assert_eq!(BinaryMode::AsText, args.binary);
let args = parse_low_raw(["-a", "--no-binary"]).unwrap();
assert_eq!(BinaryMode::Auto, args.binary);
let args = parse_low_raw(["--binary", "--no-text"]).unwrap();
assert_eq!(BinaryMode::Auto, args.binary);
}
/// -j/--threads
#[derive(Debug)]
struct Threads;
impl Flag for Threads {
fn is_switch(&self) -> bool {
false
}
fn name_short(&self) -> Option<u8> {
Some(b'j')
}
fn name_long(&self) -> &'static str {
"threads"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("NUM")
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Set the approximate number of threads to use."
}
fn doc_long(&self) -> &'static str {
r"
This flag sets the approximate number of threads to use. A value of \fB0\fP
(which is the default) causes ripgrep to choose the thread count using
heuristics.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
let threads = convert::usize(&v.unwrap_value())?;
args.threads = if threads == 0 { None } else { Some(threads) };
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_threads() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.threads);
let args = parse_low_raw(["--threads", "5"]).unwrap();
assert_eq!(Some(5), args.threads);
let args = parse_low_raw(["-j", "5"]).unwrap();
assert_eq!(Some(5), args.threads);
let args = parse_low_raw(["-j5"]).unwrap();
assert_eq!(Some(5), args.threads);
let args = parse_low_raw(["-j5", "-j10"]).unwrap();
assert_eq!(Some(10), args.threads);
let args = parse_low_raw(["-j5", "-j0"]).unwrap();
assert_eq!(None, args.threads);
}
/// --trace
#[derive(Debug)]
struct Trace;
impl Flag for Trace {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"trace"
}
fn doc_category(&self) -> Category {
Category::Logging
}
fn doc_short(&self) -> &'static str {
r"Show trace messages."
}
fn doc_long(&self) -> &'static str {
r"
Show trace messages. This shows even more detail than the \flag{debug}
flag. Generally, one should only use this if \flag{debug} doesn't emit the
information you're looking for.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--trace can only be enabled");
args.logging = Some(LoggingMode::Trace);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_trace() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.logging);
let args = parse_low_raw(["--trace"]).unwrap();
assert_eq!(Some(LoggingMode::Trace), args.logging);
let args = parse_low_raw(["--debug", "--trace"]).unwrap();
assert_eq!(Some(LoggingMode::Trace), args.logging);
}
/// --trim
#[derive(Debug)]
struct Trim;
impl Flag for Trim {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"trim"
}
fn name_negated(&self) -> Option<&'static str> {
Some("no-trim")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Trim prefix whitespace from matches."
}
fn doc_long(&self) -> &'static str {
r"
When set, all ASCII whitespace at the beginning of each line printed will be
removed.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.trim = v.unwrap_switch();
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_trim() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.trim);
let args = parse_low_raw(["--trim"]).unwrap();
assert_eq!(true, args.trim);
let args = parse_low_raw(["--trim", "--no-trim"]).unwrap();
assert_eq!(false, args.trim);
}
/// -t/--type
#[derive(Debug)]
struct Type;
impl Flag for Type {
fn is_switch(&self) -> bool {
false
}
fn name_short(&self) -> Option<u8> {
Some(b't')
}
fn name_long(&self) -> &'static str {
"type"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("TYPE")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Only search files matching TYPE."
}
fn doc_long(&self) -> &'static str {
r#"
This flag limits ripgrep to searching files matching \fITYPE\fP. Multiple
\flag{type} flags may be provided.
.sp
This flag supports the special value \fBall\fP, which will behave as if
\flag{type} was provided for every file type supported by ripgrep (including
any custom file types). The end result is that \fB\-\-type=all\fP causes
ripgrep to search in "whitelist" mode, where it will only search files it
recognizes via its type definitions.
.sp
Note that this flag has lower precedence than both the \flag{glob} flag and
any rules found in ignore files.
.sp
To see the list of available file types, use the \flag{type-list} flag.
"#
}
fn completion_type(&self) -> CompletionType {
CompletionType::Filetype
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.type_changes.push(TypeChange::Select {
name: convert::string(v.unwrap_value())?,
});
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_type() {
let select = |name: &str| TypeChange::Select { name: name.to_string() };
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Vec::<TypeChange>::new(), args.type_changes);
let args = parse_low_raw(["--type", "rust"]).unwrap();
assert_eq!(vec![select("rust")], args.type_changes);
let args = parse_low_raw(["-t", "rust"]).unwrap();
assert_eq!(vec![select("rust")], args.type_changes);
let args = parse_low_raw(["-trust"]).unwrap();
assert_eq!(vec![select("rust")], args.type_changes);
let args = parse_low_raw(["-trust", "-tpython"]).unwrap();
assert_eq!(vec![select("rust"), select("python")], args.type_changes);
let args = parse_low_raw(["-tabcdefxyz"]).unwrap();
assert_eq!(vec![select("abcdefxyz")], args.type_changes);
}
/// --type-add
#[derive(Debug)]
struct TypeAdd;
impl Flag for TypeAdd {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"type-add"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("TYPESPEC")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Add a new glob for a file type."
}
fn doc_long(&self) -> &'static str {
r"
This flag adds a new glob for a particular file type. Only one glob can be
added at a time. Multiple \flag{type-add} flags can be provided. Unless
\flag{type-clear} is used, globs are added to any existing globs defined inside
of ripgrep.
.sp
Note that this must be passed to every invocation of ripgrep. Type settings are
not persisted. See \fBCONFIGURATION FILES\fP for a workaround.
.sp
Example:
.sp
.EX
rg \-\-type\-add 'foo:*.foo' -tfoo \fIPATTERN\fP
.EE
.sp
This flag can also be used to include rules from other types with the special
include directive. The include directive permits specifying one or more other
type names (separated by a comma) that have been defined and its rules will
automatically be imported into the type specified. For example, to create a
type called src that matches C++, Python and Markdown files, one can use:
.sp
.EX
\-\-type\-add 'src:include:cpp,py,md'
.EE
.sp
Additional glob rules can still be added to the src type by using this flag
again:
.sp
.EX
\-\-type\-add 'src:include:cpp,py,md' \-\-type\-add 'src:*.foo'
.EE
.sp
Note that type names must consist only of Unicode letters or numbers.
Punctuation characters are not allowed.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.type_changes
.push(TypeChange::Add { def: convert::string(v.unwrap_value())? });
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_type_add() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Vec::<TypeChange>::new(), args.type_changes);
let args = parse_low_raw(["--type-add", "foo"]).unwrap();
assert_eq!(
vec![TypeChange::Add { def: "foo".to_string() }],
args.type_changes
);
let args = parse_low_raw(["--type-add", "foo", "--type-add=bar"]).unwrap();
assert_eq!(
vec![
TypeChange::Add { def: "foo".to_string() },
TypeChange::Add { def: "bar".to_string() }
],
args.type_changes
);
}
/// --type-clear
#[derive(Debug)]
struct TypeClear;
impl Flag for TypeClear {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"type-clear"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("TYPE")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Clear globs for a file type."
}
fn doc_long(&self) -> &'static str {
r"
Clear the file type globs previously defined for \fITYPE\fP. This clears any
previously defined globs for the \fITYPE\fP, but globs can be added after this
flag.
.sp
Note that this must be passed to every invocation of ripgrep. Type settings are
not persisted. See \fBCONFIGURATION FILES\fP for a workaround.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.type_changes.push(TypeChange::Clear {
name: convert::string(v.unwrap_value())?,
});
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_type_clear() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Vec::<TypeChange>::new(), args.type_changes);
let args = parse_low_raw(["--type-clear", "foo"]).unwrap();
assert_eq!(
vec![TypeChange::Clear { name: "foo".to_string() }],
args.type_changes
);
let args =
parse_low_raw(["--type-clear", "foo", "--type-clear=bar"]).unwrap();
assert_eq!(
vec![
TypeChange::Clear { name: "foo".to_string() },
TypeChange::Clear { name: "bar".to_string() }
],
args.type_changes
);
}
/// --type-not
#[derive(Debug)]
struct TypeNot;
impl Flag for TypeNot {
fn is_switch(&self) -> bool {
false
}
fn name_short(&self) -> Option<u8> {
Some(b'T')
}
fn name_long(&self) -> &'static str {
"type-not"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("TYPE")
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r"Do not search files matching TYPE."
}
fn doc_long(&self) -> &'static str {
r#"
Do not search files matching \fITYPE\fP. Multiple \flag{type-not} flags may be
provided. Use the \flag{type-list} flag to list all available types.
.sp
This flag supports the special value \fBall\fP, which will behave
as if \flag{type-not} was provided for every file type supported by
ripgrep (including any custom file types). The end result is that
\fB\-\-type\-not=all\fP causes ripgrep to search in "blacklist" mode, where it
will only search files that are unrecognized by its type definitions.
.sp
To see the list of available file types, use the \flag{type-list} flag.
"#
}
fn completion_type(&self) -> CompletionType {
CompletionType::Filetype
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.type_changes.push(TypeChange::Negate {
name: convert::string(v.unwrap_value())?,
});
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_type_not() {
let select = |name: &str| TypeChange::Select { name: name.to_string() };
let negate = |name: &str| TypeChange::Negate { name: name.to_string() };
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Vec::<TypeChange>::new(), args.type_changes);
let args = parse_low_raw(["--type-not", "rust"]).unwrap();
assert_eq!(vec![negate("rust")], args.type_changes);
let args = parse_low_raw(["-T", "rust"]).unwrap();
assert_eq!(vec![negate("rust")], args.type_changes);
let args = parse_low_raw(["-Trust"]).unwrap();
assert_eq!(vec![negate("rust")], args.type_changes);
let args = parse_low_raw(["-Trust", "-Tpython"]).unwrap();
assert_eq!(vec![negate("rust"), negate("python")], args.type_changes);
let args = parse_low_raw(["-Tabcdefxyz"]).unwrap();
assert_eq!(vec![negate("abcdefxyz")], args.type_changes);
let args = parse_low_raw(["-Trust", "-ttoml", "-Tjson"]).unwrap();
assert_eq!(
vec![negate("rust"), select("toml"), negate("json")],
args.type_changes
);
}
/// --type-list
#[derive(Debug)]
struct TypeList;
impl Flag for TypeList {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"type-list"
}
fn doc_category(&self) -> Category {
Category::OtherBehaviors
}
fn doc_short(&self) -> &'static str {
r"Show all supported file types."
}
fn doc_long(&self) -> &'static str {
r"
Show all supported file types and their corresponding globs. This takes any
\flag{type-add} and \flag{type-clear} flags given into account. Each type is
printed on its own line, followed by a \fB:\fP and then a comma-delimited list
of globs for that type on the same line.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--type-list has no negation");
args.mode.update(Mode::Types);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_type_list() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(Mode::Search(SearchMode::Standard), args.mode);
let args = parse_low_raw(["--type-list"]).unwrap();
assert_eq!(Mode::Types, args.mode);
}
/// -u/--unrestricted
#[derive(Debug)]
struct Unrestricted;
impl Flag for Unrestricted {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'u')
}
fn name_long(&self) -> &'static str {
"unrestricted"
}
fn doc_category(&self) -> Category {
Category::Filter
}
fn doc_short(&self) -> &'static str {
r#"Reduce the level of "smart" filtering."#
}
fn doc_long(&self) -> &'static str {
r#"
This flag reduces the level of "smart" filtering. Repeated uses (up to 3) reduces
the filtering even more. When repeated three times, ripgrep will search every
file in a directory tree.
.sp
A single \flag{unrestricted} flag is equivalent to \flag{no-ignore}. Two
\flag{unrestricted} flags is equivalent to \flag{no-ignore} \flag{hidden}.
Three \flag{unrestricted} flags is equivalent to \flag{no-ignore} \flag{hidden}
\flag{binary}.
.sp
The only filtering ripgrep still does when \fB-uuu\fP is given is to skip
symbolic links and to avoid printing matches from binary files. Symbolic links
can be followed via the \flag{follow} flag, and binary files can be treated as
text files via the \flag{text} flag.
"#
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--unrestricted has no negation");
args.unrestricted = args.unrestricted.saturating_add(1);
anyhow::ensure!(
args.unrestricted <= 3,
"flag can only be repeated up to 3 times"
);
if args.unrestricted == 1 {
NoIgnore.update(FlagValue::Switch(true), args)?;
} else if args.unrestricted == 2 {
Hidden.update(FlagValue::Switch(true), args)?;
} else {
assert_eq!(args.unrestricted, 3);
Binary.update(FlagValue::Switch(true), args)?;
}
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_unrestricted() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.no_ignore_vcs);
assert_eq!(false, args.hidden);
assert_eq!(BinaryMode::Auto, args.binary);
let args = parse_low_raw(["--unrestricted"]).unwrap();
assert_eq!(true, args.no_ignore_vcs);
assert_eq!(false, args.hidden);
assert_eq!(BinaryMode::Auto, args.binary);
let args = parse_low_raw(["--unrestricted", "-u"]).unwrap();
assert_eq!(true, args.no_ignore_vcs);
assert_eq!(true, args.hidden);
assert_eq!(BinaryMode::Auto, args.binary);
let args = parse_low_raw(["-uuu"]).unwrap();
assert_eq!(true, args.no_ignore_vcs);
assert_eq!(true, args.hidden);
assert_eq!(BinaryMode::SearchAndSuppress, args.binary);
let result = parse_low_raw(["-uuuu"]);
assert!(result.is_err(), "{result:?}");
}
/// --version
#[derive(Debug)]
struct Version;
impl Flag for Version {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'V')
}
fn name_long(&self) -> &'static str {
"version"
}
fn doc_category(&self) -> Category {
Category::OtherBehaviors
}
fn doc_short(&self) -> &'static str {
r"Print ripgrep's version."
}
fn doc_long(&self) -> &'static str {
r"
This flag prints ripgrep's version. This also may print other relevant
information, such as the presence of target specific optimizations and the
\fBgit\fP revision that this build of ripgrep was compiled from.
"
}
fn update(&self, v: FlagValue, _: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--version has no negation");
// Since this flag has different semantics for -V and --version and the
// Flag trait doesn't support encoding this sort of thing, we handle it
// as a special case in the parser.
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_version() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.special);
let args = parse_low_raw(["-V"]).unwrap();
assert_eq!(Some(SpecialMode::VersionShort), args.special);
let args = parse_low_raw(["--version"]).unwrap();
assert_eq!(Some(SpecialMode::VersionLong), args.special);
let args = parse_low_raw(["-V", "--version"]).unwrap();
assert_eq!(Some(SpecialMode::VersionLong), args.special);
let args = parse_low_raw(["--version", "-V"]).unwrap();
assert_eq!(Some(SpecialMode::VersionShort), args.special);
}
/// --vimgrep
#[derive(Debug)]
struct Vimgrep;
impl Flag for Vimgrep {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"vimgrep"
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Print results in a vim compatible format."
}
fn doc_long(&self) -> &'static str {
r"
This flag instructs ripgrep to print results with every match on its own line,
including line numbers and column numbers.
.sp
With this option, a line with more than one match will be printed in its
entirety more than once. For that reason, the total amount of output as a
result of this flag can be quadratic in the size of the input. For example,
if the pattern matches every byte in an input file, then each line will be
repeated for every byte matched. For this reason, users should only use this
flag when there is no other choice. Editor integrations should prefer some
other way of reading results from ripgrep, such as via the \flag{json} flag.
One alternative to avoiding exorbitant memory usage is to force ripgrep into
single threaded mode with the \flag{threads} flag. Note though that this will
not impact the total size of the output, just the heap memory that ripgrep will
use.
"
}
fn doc_choices(&self) -> &'static [&'static str] {
&[]
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--vimgrep has no negation");
args.vimgrep = true;
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_vimgrep() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(false, args.vimgrep);
let args = parse_low_raw(["--vimgrep"]).unwrap();
assert_eq!(true, args.vimgrep);
}
/// --with-filename
#[derive(Debug)]
struct WithFilename;
impl Flag for WithFilename {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'H')
}
fn name_long(&self) -> &'static str {
"with-filename"
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Print the file path with each matching line."
}
fn doc_long(&self) -> &'static str {
r"
This flag instructs ripgrep to print the file path for each matching line.
This is the default when more than one file is searched. If \flag{heading} is
enabled (the default when printing to a tty), the file path will be shown above
clusters of matches from each file; otherwise, the file name will be shown as a
prefix for each matched line.
.sp
This flag overrides \flag{no-filename}.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--with-filename has no defined negation");
args.with_filename = Some(true);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_with_filename() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.with_filename);
let args = parse_low_raw(["--with-filename"]).unwrap();
assert_eq!(Some(true), args.with_filename);
let args = parse_low_raw(["-H"]).unwrap();
assert_eq!(Some(true), args.with_filename);
}
/// --no-filename
#[derive(Debug)]
struct WithFilenameNo;
impl Flag for WithFilenameNo {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'I')
}
fn name_long(&self) -> &'static str {
"no-filename"
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Never print the path with each matching line."
}
fn doc_long(&self) -> &'static str {
r"
This flag instructs ripgrep to never print the file path with each matching
line. This is the default when ripgrep is explicitly instructed to search one
file or stdin.
.sp
This flag overrides \flag{with-filename}.
"
}
fn doc_choices(&self) -> &'static [&'static str] {
&[]
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--no-filename has no defined negation");
args.with_filename = Some(false);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_with_filename_no() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.with_filename);
let args = parse_low_raw(["--no-filename"]).unwrap();
assert_eq!(Some(false), args.with_filename);
let args = parse_low_raw(["-I"]).unwrap();
assert_eq!(Some(false), args.with_filename);
let args = parse_low_raw(["-I", "-H"]).unwrap();
assert_eq!(Some(true), args.with_filename);
let args = parse_low_raw(["-H", "-I"]).unwrap();
assert_eq!(Some(false), args.with_filename);
}
/// -w/--word-regexp
#[derive(Debug)]
struct WordRegexp;
impl Flag for WordRegexp {
fn is_switch(&self) -> bool {
true
}
fn name_short(&self) -> Option<u8> {
Some(b'w')
}
fn name_long(&self) -> &'static str {
"word-regexp"
}
fn doc_category(&self) -> Category {
Category::Search
}
fn doc_short(&self) -> &'static str {
r"Show matches surrounded by word boundaries."
}
fn doc_long(&self) -> &'static str {
r"
When enabled, ripgrep will only show matches surrounded by word boundaries.
This is equivalent to surrounding every pattern with \fB\\b{start-half}\fP
and \fB\\b{end-half}\fP.
.sp
This overrides the \flag{line-regexp} flag.
"
}
fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--word-regexp has no negation");
args.boundary = Some(BoundaryMode::Word);
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_word_regexp() {
let args = parse_low_raw(None::<&str>).unwrap();
assert_eq!(None, args.boundary);
let args = parse_low_raw(["--word-regexp"]).unwrap();
assert_eq!(Some(BoundaryMode::Word), args.boundary);
let args = parse_low_raw(["-w"]).unwrap();
assert_eq!(Some(BoundaryMode::Word), args.boundary);
let args = parse_low_raw(["-x", "-w"]).unwrap();
assert_eq!(Some(BoundaryMode::Word), args.boundary);
let args = parse_low_raw(["-w", "-x"]).unwrap();
assert_eq!(Some(BoundaryMode::Line), args.boundary);
}
mod convert {
use std::ffi::{OsStr, OsString};
use anyhow::Context;
pub(super) fn str(v: &OsStr) -> anyhow::Result<&str> {
let Some(s) = v.to_str() else {
anyhow::bail!("value is not valid UTF-8")
};
Ok(s)
}
pub(super) fn string(v: OsString) -> anyhow::Result<String> {
let Ok(s) = v.into_string() else {
anyhow::bail!("value is not valid UTF-8")
};
Ok(s)
}
pub(super) fn usize(v: &OsStr) -> anyhow::Result<usize> {
str(v)?.parse().context("value is not a valid number")
}
pub(super) fn u64(v: &OsStr) -> anyhow::Result<u64> {
str(v)?.parse().context("value is not a valid number")
}
pub(super) fn human_readable_u64(v: &OsStr) -> anyhow::Result<u64> {
grep::cli::parse_human_readable_size(str(v)?).context("invalid size")
}
pub(super) fn human_readable_usize(v: &OsStr) -> anyhow::Result<usize> {
let size = human_readable_u64(v)?;
let Ok(size) = usize::try_from(size) else {
anyhow::bail!("size is too big")
};
Ok(size)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn available_shorts() {
let mut total = vec![false; 128];
for byte in 0..=0x7F {
match byte {
b'.' | b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z' => {
total[usize::from(byte)] = true
}
_ => continue,
}
}
let mut taken = vec![false; 128];
for flag in FLAGS.iter() {
let Some(short) = flag.name_short() else { continue };
taken[usize::from(short)] = true;
}
for byte in 0..=0x7F {
if total[usize::from(byte)] && !taken[usize::from(byte)] {
eprintln!("{}", char::from(byte));
}
}
}
#[test]
fn shorts_all_ascii_alphanumeric() {
for flag in FLAGS.iter() {
let Some(byte) = flag.name_short() else { continue };
let long = flag.name_long();
assert!(
byte.is_ascii_alphanumeric() || byte == b'.',
"\\x{byte:0X} is not a valid short flag for {long}",
)
}
}
#[test]
fn longs_all_ascii_alphanumeric() {
for flag in FLAGS.iter() {
let long = flag.name_long();
let count = long.chars().count();
assert!(count >= 2, "flag '{long}' is less than 2 characters");
assert!(
long.chars().all(|c| c.is_ascii_alphanumeric() || c == '-'),
"flag '{long}' does not match ^[-0-9A-Za-z]+$",
);
for alias in flag.aliases() {
let count = alias.chars().count();
assert!(
count >= 2,
"flag '{long}' has alias '{alias}' that is \
less than 2 characters",
);
assert!(
alias
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-'),
"flag '{long}' has alias '{alias}' that does not \
match ^[-0-9A-Za-z]+$",
);
}
let Some(negated) = flag.name_negated() else { continue };
let count = negated.chars().count();
assert!(
count >= 2,
"flag '{long}' has negation '{negated}' that is \
less than 2 characters",
);
assert!(
negated.chars().all(|c| c.is_ascii_alphanumeric() || c == '-'),
"flag '{long}' has negation '{negated}' that \
does not match ^[-0-9A-Za-z]+$",
);
}
}
#[test]
fn shorts_no_duplicates() {
let mut taken = vec![false; 128];
for flag in FLAGS.iter() {
let Some(short) = flag.name_short() else { continue };
let long = flag.name_long();
assert!(
!taken[usize::from(short)],
"flag {long} has duplicate short flag {}",
char::from(short)
);
taken[usize::from(short)] = true;
}
}
#[test]
fn longs_no_duplicates() {
use std::collections::BTreeSet;
let mut taken = BTreeSet::new();
for flag in FLAGS.iter() {
let long = flag.name_long();
assert!(taken.insert(long), "flag {long} has a duplicate name");
for alias in flag.aliases() {
assert!(
taken.insert(alias),
"flag {long} has an alias {alias} that is duplicative"
);
}
let Some(negated) = flag.name_negated() else { continue };
assert!(
taken.insert(negated),
"negated flag {negated} has a duplicate name"
);
}
}
#[test]
fn non_switches_have_variable_names() {
for flag in FLAGS.iter() {
if flag.is_switch() {
continue;
}
let long = flag.name_long();
assert!(
flag.doc_variable().is_some(),
"flag '{long}' should have a variable name"
);
}
}
#[test]
fn switches_have_no_choices() {
for flag in FLAGS.iter() {
if !flag.is_switch() {
continue;
}
let long = flag.name_long();
let choices = flag.doc_choices();
assert!(
choices.is_empty(),
"switch flag '{long}' \
should not have any choices but has some: {choices:?}",
);
}
}
#[test]
fn choices_ascii_alphanumeric() {
for flag in FLAGS.iter() {
let long = flag.name_long();
for choice in flag.doc_choices() {
assert!(
choice.chars().all(|c| c.is_ascii_alphanumeric()
|| c == '-'
|| c == ':'),
"choice '{choice}' for flag '{long}' does not match \
^[-:0-9A-Za-z]+$",
)
}
}
}
}
|