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
|
import enum
import os
import re
import sys
import tempfile
from contextlib import nullcontext
from typing import Literal
if sys.version_info < (3, 11):
enum.StrEnum = enum.Enum # type: ignore[assignment]
import pytest
import asyncclick as click
from asyncclick import Option
from asyncclick import UNPROCESSED
from asyncclick._utils import UNSET
def test_prefixes(runner):
@click.command()
@click.option("++foo", is_flag=True, help="das foo")
@click.option("--bar", is_flag=True, help="das bar")
def cli(foo, bar):
click.echo(f"foo={foo} bar={bar}")
result = runner.invoke(cli, ["++foo", "--bar"])
assert not result.exception
assert result.output == "foo=True bar=True\n"
result = runner.invoke(cli, ["--help"])
assert re.search(r"\+\+foo\s+das foo", result.output) is not None
assert re.search(r"--bar\s+das bar", result.output) is not None
def test_invalid_option(runner):
with pytest.raises(TypeError, match="name was passed") as exc_info:
click.Option(["foo"])
message = str(exc_info.value)
assert "name was passed (foo)" in message
assert "declare an argument" in message
assert "'--foo'" in message
@pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])
def test_deprecated_usage(runner, deprecated):
@click.command()
@click.option("--foo", default="bar", deprecated=deprecated)
def cmd(foo):
click.echo(foo)
result = runner.invoke(cmd, ["--help"])
assert "(DEPRECATED" in result.output
if isinstance(deprecated, str):
assert deprecated in result.output
@pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])
def test_deprecated_warning(runner, deprecated):
@click.command()
@click.option(
"--my-option", required=False, deprecated=deprecated, default="default option"
)
def cli(my_option: str):
click.echo(f"{my_option}")
# defaults should not give a deprecated warning
result = runner.invoke(cli, [])
assert result.exit_code == 0, result.output
assert "is deprecated" not in result.output
result = runner.invoke(cli, ["--my-option", "hello"])
assert result.exit_code == 0, result.output
assert "option 'my_option' is deprecated" in result.output
if isinstance(deprecated, str):
assert deprecated in result.output
def test_deprecated_required(runner):
with pytest.raises(ValueError, match="is deprecated and still required"):
click.Option(["--a"], required=True, deprecated=True)
def test_deprecated_prompt(runner):
with pytest.raises(ValueError, match="`deprecated` options cannot use `prompt`"):
click.Option(["--a"], prompt=True, deprecated=True)
def test_invalid_nargs(runner):
with pytest.raises(TypeError, match="nargs=-1 is not supported for options."):
@click.command()
@click.option("--foo", nargs=-1)
def cli(foo):
pass
def test_nargs_tup_composite_mult(runner):
@click.command()
@click.option("--item", type=(str, int), multiple=True)
def copy(item):
for name, id in item:
click.echo(f"name={name} id={id:d}")
result = runner.invoke(copy, ["--item", "peter", "1", "--item", "max", "2"])
assert not result.exception
assert result.output.splitlines() == ["name=peter id=1", "name=max id=2"]
def test_counting(runner):
@click.command()
@click.option("-v", count=True, help="Verbosity", type=click.IntRange(0, 3))
def cli(v):
click.echo(f"verbosity={v:d}")
result = runner.invoke(cli, ["-vvv"])
assert not result.exception
assert result.output == "verbosity=3\n"
result = runner.invoke(cli, ["-vvvv"])
assert result.exception
assert "Invalid value for '-v': 4 is not in the range 0<=x<=3." in result.output
result = runner.invoke(cli, [])
assert not result.exception
assert result.output == "verbosity=0\n"
result = runner.invoke(cli, ["--help"])
assert re.search(r"-v\s+Verbosity", result.output) is not None
@pytest.mark.parametrize("unknown_flag", ["--foo", "-f"])
def test_unknown_options(runner, unknown_flag):
@click.command()
def cli():
pass
result = runner.invoke(cli, [unknown_flag])
assert result.exception
assert f"No such option: {unknown_flag}" in result.output
@pytest.mark.parametrize(
("value", "expect"),
[
("--cat", "Did you mean --count?"),
("--bounds", "(Possible options: --bound, --count)"),
("--bount", "(Possible options: --bound, --count)"),
],
)
def test_suggest_possible_options(runner, value, expect):
cli = click.Command(
"cli", params=[click.Option(["--bound"]), click.Option(["--count"])]
)
result = runner.invoke(cli, [value])
assert expect in result.output
def test_multiple_required(runner):
@click.command()
@click.option("-m", "--message", multiple=True, required=True)
def cli(message):
click.echo("\n".join(message))
result = runner.invoke(cli, ["-m", "foo", "-mbar"])
assert not result.exception
assert result.output == "foo\nbar\n"
result = runner.invoke(cli, [])
assert result.exception
assert "Error: Missing option '-m' / '--message'." in result.output
@pytest.mark.parametrize(
("multiple", "nargs", "default", "expected"),
[
# If multiple values are allowed, defaults should be iterable.
(True, 1, [], ()),
(True, 1, (), ()),
(True, 1, tuple(), ()),
(True, 1, set(), ()),
(True, 1, frozenset(), ()),
(True, 1, {}, ()),
# Special values.
(True, 1, None, ()),
(True, 1, UNSET, ()),
# Number of values are kept as-is in the default.
(True, 1, [1], (1,)),
(True, 1, [1, 2], (1, 2)),
(True, 1, [1, 2, 3], (1, 2, 3)),
(True, 1, [1.1, 2.2], (1.1, 2.2)),
(True, 1, ["1", "2"], ("1", "2")),
(True, 1, [None, None], (None, None)),
# Contrary to list or tuples, native Python types not supported by Click are
# not recognized and are converted to the default format: tuple of strings.
# Refs: https://github.com/pallets/click/issues/3036
(True, 1, {1, 2}, ("1", "2")),
(True, 1, frozenset([1, 2]), ("1", "2")),
(True, 1, {1: "a", 2: "b"}, ("1", "2")),
# Multiple values with nargs > 1.
(True, 2, [], ()),
(True, 2, (), ()),
(True, 2, tuple(), ()),
(True, 2, set(), ()),
(True, 2, frozenset(), ()),
(True, 2, {}, ()),
(True, 2, None, ()),
(True, 2, UNSET, ()),
(True, 2, [[1, 2]], ((1, 2),)),
(True, 2, [[1, 2], [3, 4]], ((1, 2), (3, 4))),
(True, 2, [[1, 2], [3, 4], [5, 6]], ((1, 2), (3, 4), (5, 6))),
(True, 2, [[1.1, 2.2], [3.3, 4.4]], ((1.1, 2.2), (3.3, 4.4))),
(True, 2, [["1", "2"], ["3", "4"]], (("1", "2"), ("3", "4"))),
(True, 2, [[None, None], [None, None]], ((None, None), (None, None))),
(True, 2, [[1, 2.2], ["3", None]], ((1, 2.2), (3, None))),
(True, 2, [[1, 2.2], None], ((1, 2.2), None)),
# Default of the right length works for non-multiples.
(False, 2, [1, 2], (1, 2)),
],
)
def test_good_defaults_for_multiple(runner, multiple, nargs, default, expected):
@click.command()
@click.option("-a", multiple=multiple, nargs=nargs, default=default)
def cmd(a):
click.echo(repr(a), nl=False)
result = runner.invoke(cmd)
assert result.output == repr(expected)
@pytest.mark.parametrize(
("multiple", "nargs", "default", "exception", "message"),
[
# Non-iterables defaults.
(
True,
1,
"Yo",
None,
"Error: Invalid value for '-a': Value must be an iterable.",
),
(
True,
1,
"",
None,
"Error: Invalid value for '-a': Value must be an iterable.",
),
(
True,
1,
True,
None,
"Error: Invalid value for '-a': Value must be an iterable.",
),
(
True,
1,
False,
None,
"Error: Invalid value for '-a': Value must be an iterable.",
),
(
True,
1,
12,
None,
"Error: Invalid value for '-a': Value must be an iterable.",
),
(
True,
1,
7.9,
None,
"Error: Invalid value for '-a': Value must be an iterable.",
),
#
(
False,
2,
42,
None,
"Error: Invalid value for '-a': Value must be an iterable.",
),
(
True,
2,
["test string which is not a list in the list"],
None,
"Error: Invalid value for '-a': Value must be an iterable.",
),
# Multiple options, each with 2 args, but with wrong length.
(
True,
2,
(1,),
None,
"Error: Invalid value for '-a': Value must be an iterable.",
),
(
True,
2,
(1, 2, 3),
None,
"Error: Invalid value for '-a': Value must be an iterable.",
),
(
True,
2,
[tuple()],
ValueError,
r"'nargs' must be 0 \(or None\) for type <asyncclick\.types\.Tuple "
r"object at 0x[0-9A-Fa-f]+>, but it was 2\.",
),
(
True,
2,
[(1,)],
ValueError,
r"'nargs' must be 1 \(or None\) for type <asyncclick\.types\.Tuple "
r"object at 0x[0-9A-Fa-f]+>, but it was 2\.",
),
(
True,
2,
[(1, 2, 3)],
ValueError,
r"'nargs' must be 3 \(or None\) for type <asyncclick\.types\.Tuple "
r"object at 0x[0-9A-Fa-f]+>, but it was 2\.",
),
# A mix of valid and invalid defaults.
(
True,
2,
[[1, 2.2], []],
None,
"Error: Invalid value for '-a': 2 values are required, but 0 were given.",
),
# Default values that are iterable but not of the right length.
(
False,
2,
[1],
None,
"Error: Invalid value for '-a': Takes 2 values but 1 was given.",
),
(
True,
2,
[[1]],
ValueError,
r"'nargs' must be 1 \(or None\) for type <asyncclick\.types\.Tuple "
r"object at 0x[0-9A-Fa-f]+>, but it was 2\.",
),
],
)
def test_bad_defaults_for_multiple(
runner, multiple, nargs, default, exception, message
):
if exception:
assert issubclass(exception, Exception)
else:
assert exception is None
with (
pytest.raises(exception, match=re.compile(message))
if exception
else nullcontext()
):
@click.command()
@click.option("-a", multiple=multiple, nargs=nargs, default=default)
def cmd(a):
click.echo(repr(a))
result = runner.invoke(cmd)
assert message in result.stderr
@pytest.mark.parametrize("env_key", ["MYPATH", "AUTO_MYPATH"])
def test_empty_envvar(runner, env_key):
@click.command()
@click.option("--mypath", type=click.Path(exists=True), envvar="MYPATH")
def cli(mypath):
click.echo(f"mypath: {mypath}")
result = runner.invoke(cli, env={env_key: ""}, auto_envvar_prefix="AUTO")
assert result.exception is None
assert result.output == "mypath: None\n"
def test_multiple_envvar(runner):
@click.command()
@click.option("--arg", multiple=True)
def cmd(arg):
click.echo("|".join(arg))
result = runner.invoke(
cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar baz"}
)
assert not result.exception
assert result.output == "foo|bar|baz\n"
@click.command()
@click.option("--arg", multiple=True, envvar="X")
def cmd(arg):
click.echo("|".join(arg))
result = runner.invoke(cmd, [], env={"X": "foo bar baz"})
assert not result.exception
assert result.output == "foo|bar|baz\n"
@click.command()
@click.option("--arg", multiple=True, type=click.Path())
def cmd(arg):
click.echo("|".join(arg))
result = runner.invoke(
cmd,
[],
auto_envvar_prefix="TEST",
env={"TEST_ARG": f"foo{os.path.pathsep}bar"},
)
assert not result.exception
assert result.output == "foo|bar\n"
@pytest.mark.parametrize(
("envvar_name", "envvar_value", "expected"),
(
# Lower-cased variations of value.
("SHOUT", "true", True),
("SHOUT", "false", False),
# Title-cased variations of value.
("SHOUT", "True", True),
("SHOUT", "False", False),
# Upper-cased variations of value.
("SHOUT", "TRUE", True),
("SHOUT", "FALSE", False),
# Random-cased variations of value.
("SHOUT", "TruE", True),
("SHOUT", "truE", True),
("SHOUT", "FaLsE", False),
("SHOUT", "falsE", False),
# Extra spaces around the value.
("SHOUT", "true ", True),
("SHOUT", " true ", True),
("SHOUT", " true", True),
("SHOUT", "false ", False),
("SHOUT", " false ", False),
("SHOUT", " false", False),
# Integer variations.
("SHOUT", "1", True),
("SHOUT", "0", False),
# Alternative states.
("SHOUT", "t", True),
("SHOUT", "T", True),
("SHOUT", " T ", True),
("SHOUT", "f", False),
("SHOUT", "y", True),
("SHOUT", "n", False),
("SHOUT", "yes", True),
("SHOUT", "no", False),
("SHOUT", "on", True),
("SHOUT", "off", False),
# Blank value variations.
("SHOUT", None, False),
("SHOUT", "", False),
("SHOUT", " ", False),
("SHOUT", " ", False),
# Variable names are not stripped of spaces and so don't match the
# flag, which then naturraly takes its default value.
("SHOUT ", "True", False),
("SHOUT ", "False", False),
(" SHOUT ", "True", False),
(" SHOUT ", "False", False),
(" SHOUT", "True", False),
(" SHOUT", "False", False),
("SH OUT", "True", False),
("SH OUT", "False", False),
# Same for random and reverse environment variable names: they are not
# recognized by the option.
("RANDOM", "True", False),
("NO_SHOUT", "True", False),
("NO_SHOUT", "False", False),
("NOSHOUT", "True", False),
("NOSHOUT", "False", False),
),
)
def test_boolean_flag_envvar(runner, envvar_name, envvar_value, expected):
assert isinstance(envvar_name, str)
assert isinstance(envvar_value, str) or envvar_value is None
@click.command()
@click.option("--shout/--no-shout", envvar="SHOUT")
def cli(shout):
click.echo(repr(shout), nl=False)
result = runner.invoke(cli, [], env={envvar_name: envvar_value})
assert result.exit_code == 0
assert result.output == repr(expected)
@pytest.mark.parametrize(
"value",
(
# Extra spaces inside the value.
"tr ue",
"fa lse",
# Numbers.
"10",
"01",
"00",
"11",
"0.0",
"1.1",
# Random strings.
"randomstring",
"None",
"'None'",
"A B",
" 1 2 ",
"9.3",
"a;n",
"x:y",
"i/o",
),
)
def test_boolean_envvar_bad_values(runner, value):
@click.command()
@click.option("--shout/--no-shout", envvar="SHOUT")
def cli(shout):
click.echo(shout)
result = runner.invoke(cli, [], env={"SHOUT": value})
assert result.exit_code == 2
assert (
f"Invalid value for '--shout': {value!r} is not a valid boolean."
in result.output
)
def test_multiple_default_help(runner):
@click.command()
@click.option("--arg1", multiple=True, default=("foo", "bar"), show_default=True)
@click.option("--arg2", multiple=True, default=(1, 2), type=int, show_default=True)
def cmd(arg, arg2):
pass
result = runner.invoke(cmd, ["--help"])
assert not result.exception
assert "foo, bar" in result.output
assert "1, 2" in result.output
def test_show_default_default_map(runner):
@click.command()
@click.option("--arg", default="a", show_default=True)
def cmd(arg):
click.echo(arg)
result = runner.invoke(cmd, ["--help"], default_map={"arg": "b"})
assert not result.exception
assert "[default: b]" in result.output
def test_multiple_default_type():
opt = click.Option(["-a"], multiple=True, default=(1, 2))
assert opt.nargs == 1
assert opt.multiple
assert opt.type is click.INT
ctx = click.Context(click.Command("test"))
assert opt.get_default(ctx) == (1, 2)
def test_multiple_default_composite_type():
opt = click.Option(["-a"], multiple=True, default=[(1, "a")])
assert opt.nargs == 2
assert opt.multiple
assert isinstance(opt.type, click.Tuple)
assert opt.type.types == [click.INT, click.STRING]
ctx = click.Context(click.Command("test"))
assert opt.type_cast_value(ctx, opt.get_default(ctx)) == ((1, "a"),)
def test_parse_multiple_default_composite_type(runner):
@click.command()
@click.option("-a", multiple=True, default=("a", "b"))
@click.option("-b", multiple=True, default=[(1, "a")])
def cmd(a, b):
click.echo(a)
click.echo(b)
# result = runner.invoke(cmd, "-a c -a 1 -a d -b 2 two -b 4 four".split())
# assert result.output == "('c', '1', 'd')\n((2, 'two'), (4, 'four'))\n"
result = runner.invoke(cmd)
assert result.output == "('a', 'b')\n((1, 'a'),)\n"
def test_dynamic_default_help_unset(runner):
@click.command()
@click.option(
"--username",
prompt=True,
default=lambda: os.environ.get("USER", ""),
show_default=True,
)
def cmd(username):
print("Hello,", username)
result = runner.invoke(cmd, ["--help"])
assert result.exit_code == 0
assert "--username" in result.output
assert "lambda" not in result.output
assert "(dynamic)" in result.output
def test_dynamic_default_help_text(runner):
@click.command()
@click.option(
"--username",
prompt=True,
default=lambda: os.environ.get("USER", ""),
show_default="current user",
)
def cmd(username):
print("Hello,", username)
result = runner.invoke(cmd, ["--help"])
assert result.exit_code == 0
assert "--username" in result.output
assert "lambda" not in result.output
assert "(current user)" in result.output
def test_dynamic_default_help_special_method(runner):
class Value:
def __call__(self):
return 42
def __str__(self):
return "special value"
opt = click.Option(["-a"], default=Value(), show_default=True)
ctx = click.Context(click.Command("cli"))
assert opt.get_help_extra(ctx) == {"default": "special value"}
assert "special value" in opt.get_help_record(ctx)[1]
@pytest.mark.parametrize(
("type", "expect"),
[
(click.IntRange(1, 32), "1<=x<=32"),
(click.IntRange(1, 32, min_open=True, max_open=True), "1<x<32"),
(click.IntRange(1), "x>=1"),
(click.IntRange(max=32), "x<=32"),
],
)
def test_intrange_default_help_text(type, expect):
option = click.Option(["--num"], type=type, show_default=True, default=2)
context = click.Context(click.Command("test"))
assert option.get_help_extra(context) == {"default": "2", "range": expect}
result = option.get_help_record(context)[1]
assert expect in result
def test_count_default_type_help():
"""A count option with the default type should not show >=0 in help."""
option = click.Option(["--count"], count=True, help="some words")
context = click.Context(click.Command("test"))
assert option.get_help_extra(context) == {}
result = option.get_help_record(context)[1]
assert result == "some words"
def test_file_type_help_default():
"""The default for a File type is a filename string. The string
should be displayed in help, not an open file object.
Type casting is only applied to defaults in processing, not when
getting the default value.
"""
option = click.Option(
["--in"], type=click.File(), default=__file__, show_default=True
)
context = click.Context(click.Command("test"))
assert option.get_help_extra(context) == {"default": __file__}
result = option.get_help_record(context)[1]
assert __file__ in result
def test_toupper_envvar_prefix(runner):
@click.command()
@click.option("--arg")
def cmd(arg):
click.echo(arg)
result = runner.invoke(cmd, [], auto_envvar_prefix="test", env={"TEST_ARG": "foo"})
assert not result.exception
assert result.output == "foo\n"
def test_nargs_envvar(runner):
@click.command()
@click.option("--arg", nargs=2)
def cmd(arg):
click.echo("|".join(arg))
result = runner.invoke(
cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar"}
)
assert not result.exception
assert result.output == "foo|bar\n"
@click.command()
@click.option("--arg", nargs=2, multiple=True)
def cmd(arg):
for item in arg:
click.echo("|".join(item))
result = runner.invoke(
cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "x 1 y 2"}
)
assert not result.exception
assert result.output == "x|1\ny|2\n"
def test_show_envvar(runner):
@click.command()
@click.option("--arg1", envvar="ARG1", show_envvar=True)
def cmd(arg):
pass
result = runner.invoke(cmd, ["--help"])
assert not result.exception
assert "ARG1" in result.output
def test_show_envvar_auto_prefix(runner):
@click.command()
@click.option("--arg1", show_envvar=True)
def cmd(arg):
pass
result = runner.invoke(cmd, ["--help"], auto_envvar_prefix="TEST")
assert not result.exception
assert "TEST_ARG1" in result.output
def test_show_envvar_auto_prefix_dash_in_command(runner):
@click.group()
def cli():
pass
@cli.command()
@click.option("--baz", show_envvar=True)
def foo_bar(baz):
pass
result = runner.invoke(cli, ["foo-bar", "--help"], auto_envvar_prefix="TEST")
assert not result.exception
assert "TEST_FOO_BAR_BAZ" in result.output
def test_custom_validation(runner):
def validate_pos_int(ctx, param, value):
if value < 0:
raise click.BadParameter("Value needs to be positive")
return value
@click.command()
@click.option("--foo", callback=validate_pos_int, default=1)
def cmd(foo):
click.echo(foo)
result = runner.invoke(cmd, ["--foo", "-1"])
assert "Invalid value for '--foo': Value needs to be positive" in result.output
result = runner.invoke(cmd, ["--foo", "42"])
assert result.output == "42\n"
@pytest.mark.anyio
async def test_callback_validates_prompt(arunner):
def validate(ctx, param, value):
if value < 0:
raise click.BadParameter("should be positive")
return value
@click.command()
@click.option("-a", type=int, callback=validate, prompt=True)
def cli(a):
click.echo(a)
result = await arunner.invoke(cli, input="-12\n60\n")
assert result.output == "A: -12\nError: should be positive\nA: 60\n60\n"
def test_winstyle_options(runner):
@click.command()
@click.option("/debug;/no-debug", help="Enables or disables debug mode.")
def cmd(debug):
click.echo(debug)
result = runner.invoke(cmd, ["/debug"], help_option_names=["/?"])
assert result.output == "True\n"
result = runner.invoke(cmd, ["/no-debug"], help_option_names=["/?"])
assert result.output == "False\n"
result = runner.invoke(cmd, [], help_option_names=["/?"])
assert result.output == "False\n"
result = runner.invoke(cmd, ["/?"], help_option_names=["/?"])
assert "/debug; /no-debug Enables or disables debug mode." in result.output
assert "/? Show this message and exit." in result.output
def test_legacy_options(runner):
@click.command()
@click.option("-whatever")
def cmd(whatever):
click.echo(whatever)
result = runner.invoke(cmd, ["-whatever", "42"])
assert result.output == "42\n"
result = runner.invoke(cmd, ["-whatever=23"])
assert result.output == "23\n"
@pytest.mark.parametrize(
("value", "expect_missing", "processed_value"),
[
(UNSET, True, None),
(None, False, None),
# Default type of the argument is str, so everything is processed as strings.
("", False, ""),
(" ", False, " "),
("foo", False, "foo"),
("12", False, "12"),
(12, False, "12"),
(12.1, False, "12.1"),
(list(), False, "[]"),
(tuple(), False, "()"),
(set(), False, "set()"),
(frozenset(), False, "frozenset()"),
(dict(), False, "{}"),
],
)
@pytest.mark.anyio
async def test_required_option(value, expect_missing, processed_value):
ctx = click.Context(click.Command(""))
argument = click.Option(["-a"], required=True)
if expect_missing:
with pytest.raises(click.MissingParameter) as excinfo:
await argument.process_value(ctx, value)
assert str(excinfo.value) == "Missing parameter: a"
else:
value = await argument.process_value(ctx, value)
assert value == processed_value
def test_missing_required_flag(runner):
cli = click.Command(
"cli", params=[click.Option(["--on/--off"], is_flag=True, required=True)]
)
result = runner.invoke(cli)
assert result.exit_code == 2
assert "Error: Missing option '--on'." in result.output
def test_missing_choice(runner):
@click.command()
@click.option("--foo", type=click.Choice(["foo", "bar"]), required=True)
def cmd(foo):
click.echo(foo)
result = runner.invoke(cmd)
assert result.exit_code == 2
error, separator, choices = result.output.partition("Choose from")
assert "Error: Missing option '--foo'. " in error
assert "Choose from" in separator
assert "foo" in choices
assert "bar" in choices
def test_missing_envvar(runner):
cli = click.Command(
"cli", params=[click.Option(["--foo"], envvar="bar", required=True)]
)
result = runner.invoke(cli)
assert result.exit_code == 2
assert "Error: Missing option '--foo'." in result.output
cli = click.Command(
"cli",
params=[click.Option(["--foo"], envvar="bar", show_envvar=True, required=True)],
)
result = runner.invoke(cli)
assert result.exit_code == 2
assert "Error: Missing option '--foo' (env var: 'bar')." in result.output
cli = click.Command(
"cli", params=[click.Option(["--foo"], show_envvar=True, required=True)]
)
result = runner.invoke(cli)
assert result.exit_code == 2
assert "Error: Missing option '--foo'." in result.output
def test_case_insensitive_choice(runner):
@click.command()
@click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))
def cmd(foo):
click.echo(foo)
result = runner.invoke(cmd, ["--foo", "apple"])
assert result.exit_code == 0
assert result.output == "Apple\n"
result = runner.invoke(cmd, ["--foo", "oRANGe"])
assert result.exit_code == 0
assert result.output == "Orange\n"
result = runner.invoke(cmd, ["--foo", "Apple"])
assert result.exit_code == 0
assert result.output == "Apple\n"
@click.command()
@click.option("--foo", type=click.Choice(["Orange", "Apple"]))
def cmd2(foo):
click.echo(foo)
result = runner.invoke(cmd2, ["--foo", "apple"])
assert result.exit_code == 2
result = runner.invoke(cmd2, ["--foo", "oRANGe"])
assert result.exit_code == 2
result = runner.invoke(cmd2, ["--foo", "Apple"])
assert result.exit_code == 0
def test_case_insensitive_choice_returned_exactly(runner):
@click.command()
@click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))
def cmd(foo):
click.echo(foo)
result = runner.invoke(cmd, ["--foo", "apple"])
assert result.exit_code == 0
assert result.output == "Apple\n"
def test_option_help_preserve_paragraphs(runner):
@click.command()
@click.option(
"-C",
"--config",
type=click.Path(),
help="""Configuration file to use.
If not given, the environment variable CONFIG_FILE is consulted
and used if set. If neither are given, a default configuration
file is loaded.""",
)
def cmd(config):
pass
result = runner.invoke(cmd, ["--help"])
assert result.exit_code == 0
i = " " * 21
assert (
" -C, --config PATH Configuration file to use.\n"
f"{i}\n"
f"{i}If not given, the environment variable CONFIG_FILE is\n"
f"{i}consulted and used if set. If neither are given, a default\n"
f"{i}configuration file is loaded."
) in result.output
def test_argument_custom_class(runner):
class CustomArgument(click.Argument):
def get_default(self, ctx, call=True):
"""a dumb override of a default value for testing"""
return "I am a default"
@click.command()
@click.argument("testarg", cls=CustomArgument, default="you wont see me")
def cmd(testarg):
click.echo(testarg)
result = runner.invoke(cmd)
assert "I am a default" in result.output
assert "you wont see me" not in result.output
def test_option_custom_class(runner):
class CustomOption(click.Option):
def get_help_record(self, ctx):
"""a dumb override of a help text for testing"""
return ("--help", "I am a help text")
@click.command()
@click.option("--testoption", cls=CustomOption, help="you wont see me")
def cmd(testoption):
click.echo(testoption)
result = runner.invoke(cmd, ["--help"])
assert "I am a help text" in result.output
assert "you wont see me" not in result.output
def test_option_custom_class_reusable(runner):
"""Ensure we can reuse a custom class option. See Issue #926"""
class CustomOption(click.Option):
def get_help_record(self, ctx):
"""a dumb override of a help text for testing"""
return ("--help", "I am a help text")
# Assign to a variable to re-use the decorator.
testoption = click.option("--testoption", cls=CustomOption, help="you wont see me")
@click.command()
@testoption
def cmd1(testoption):
click.echo(testoption)
@click.command()
@testoption
def cmd2(testoption):
click.echo(testoption)
# Both of the commands should have the --help option now.
for cmd in (cmd1, cmd2):
result = runner.invoke(cmd, ["--help"])
assert "I am a help text" in result.output
assert "you wont see me" not in result.output
@pytest.mark.parametrize("custom_class", (True, False))
@pytest.mark.parametrize(
("name_specs", "expected"),
(
(
("-h", "--help"),
" -h, --help Show this message and exit.\n",
),
(
("-h",),
" -h Show this message and exit.\n"
" --help Show this message and exit.\n",
),
(
("--help",),
" --help Show this message and exit.\n",
),
),
)
def test_help_option_custom_names_and_class(runner, custom_class, name_specs, expected):
class CustomHelpOption(click.Option):
pass
option_attrs = {}
if custom_class:
option_attrs["cls"] = CustomHelpOption
@click.command()
@click.help_option(*name_specs, **option_attrs)
def cmd():
pass
for arg in name_specs:
result = runner.invoke(cmd, [arg])
assert not result.exception
assert result.exit_code == 0
assert expected in result.output
def test_bool_flag_with_type(runner):
@click.command()
@click.option("--shout/--no-shout", default=False, type=bool)
def cmd(shout):
pass
result = runner.invoke(cmd)
assert not result.exception
def test_aliases_for_flags(runner):
@click.command()
@click.option("--warnings/--no-warnings", " /-W", default=True)
def cli(warnings):
click.echo(warnings)
result = runner.invoke(cli, ["--warnings"])
assert result.output == "True\n"
result = runner.invoke(cli, ["--no-warnings"])
assert result.output == "False\n"
result = runner.invoke(cli, ["-W"])
assert result.output == "False\n"
@click.command()
@click.option("--warnings/--no-warnings", "-w", default=True)
def cli_alt(warnings):
click.echo(warnings)
result = runner.invoke(cli_alt, ["--warnings"])
assert result.output == "True\n"
result = runner.invoke(cli_alt, ["--no-warnings"])
assert result.output == "False\n"
result = runner.invoke(cli_alt, ["-w"])
assert result.output == "True\n"
@pytest.mark.parametrize(
"option_args,expected",
[
(["--aggressive", "--all", "-a"], "aggressive"),
(["--first", "--second", "--third", "-a", "-b", "-c"], "first"),
(["--apple", "--banana", "--cantaloupe", "-a", "-b", "-c"], "apple"),
(["--cantaloupe", "--banana", "--apple", "-c", "-b", "-a"], "cantaloupe"),
(["-a", "-b", "-c"], "a"),
(["-c", "-b", "-a"], "c"),
(["-a", "--apple", "-b", "--banana", "-c", "--cantaloupe"], "apple"),
(["-c", "-a", "--cantaloupe", "-b", "--banana", "--apple"], "cantaloupe"),
(["--from", "-f", "_from"], "_from"),
(["--return", "-r", "_ret"], "_ret"),
],
)
def test_option_names(runner, option_args, expected):
@click.command()
@click.option(*option_args, is_flag=True)
def cmd(**kwargs):
click.echo(str(kwargs[expected]))
assert cmd.params[0].name == expected
for form in option_args:
if form.startswith("-"):
result = runner.invoke(cmd, [form])
assert result.output == "True\n"
def test_flag_duplicate_names(runner):
with pytest.raises(ValueError, match="cannot use the same flag for true/false"):
click.Option(["--foo/--foo"], default=False)
@pytest.mark.parametrize(("default", "expect"), [(False, "no-cache"), (True, "cache")])
def test_show_default_boolean_flag_name(runner, default, expect):
"""When a boolean flag has distinct True/False opts, it should show
the default opt name instead of the default value. It should only
show one name even if multiple are declared.
"""
opt = click.Option(
("--cache/--no-cache", "--c/--nc"),
default=default,
show_default=True,
help="Enable/Disable the cache.",
)
ctx = click.Context(click.Command("test"))
assert opt.get_help_extra(ctx) == {"default": expect}
message = opt.get_help_record(ctx)[1]
assert f"[default: {expect}]" in message
def test_show_true_default_boolean_flag_value(runner):
"""When a boolean flag only has one opt and its default is True,
it will show the default value, not the opt name.
"""
opt = click.Option(
("--cache",),
is_flag=True,
show_default=True,
default=True,
help="Enable the cache.",
)
ctx = click.Context(click.Command("test"))
assert opt.get_help_extra(ctx) == {"default": "True"}
message = opt.get_help_record(ctx)[1]
assert "[default: True]" in message
@pytest.mark.parametrize("default", [False, None])
def test_hide_false_default_boolean_flag_value(runner, default):
"""When a boolean flag only has one opt and its default is False or
None, it will not show the default
"""
opt = click.Option(
("--cache",),
is_flag=True,
show_default=True,
default=default,
help="Enable the cache.",
)
ctx = click.Context(click.Command("test"))
assert opt.get_help_extra(ctx) == {}
message = opt.get_help_record(ctx)[1]
assert "[default: " not in message
def test_show_default_string(runner):
"""When show_default is a string show that value as default."""
opt = click.Option(["--limit"], show_default="unlimited")
ctx = click.Context(click.Command("cli"))
assert opt.get_help_extra(ctx) == {"default": "(unlimited)"}
message = opt.get_help_record(ctx)[1]
assert "[default: (unlimited)]" in message
def test_show_default_with_empty_string(runner):
"""When show_default is True and default is set to an empty string."""
opt = click.Option(["--limit"], default="", show_default=True)
ctx = click.Context(click.Command("cli"))
message = opt.get_help_record(ctx)[1]
assert '[default: ""]' in message
def test_do_not_show_no_default(runner):
"""When show_default is True and no default is set do not show None."""
opt = click.Option(["--limit"], show_default=True)
ctx = click.Context(click.Command("cli"))
assert opt.get_help_extra(ctx) == {}
message = opt.get_help_record(ctx)[1]
assert "[default: None]" not in message
def test_do_not_show_default_empty_multiple():
"""When show_default is True and multiple=True is set, it should not
print empty default value in --help output.
"""
opt = click.Option(["-a"], multiple=True, help="values", show_default=True)
ctx = click.Context(click.Command("cli"))
assert opt.get_help_extra(ctx) == {}
message = opt.get_help_record(ctx)[1]
assert message == "values"
@pytest.mark.parametrize(
("ctx_value", "opt_value", "extra_value", "expect"),
[
(None, None, {}, False),
(None, False, {}, False),
(None, True, {"default": "1"}, True),
(False, None, {}, False),
(False, False, {}, False),
(False, True, {"default": "1"}, True),
(True, None, {"default": "1"}, True),
(True, False, {}, False),
(True, True, {"default": "1"}, True),
(False, "one", {"default": "(one)"}, True),
],
)
def test_show_default_precedence(ctx_value, opt_value, extra_value, expect):
ctx = click.Context(click.Command("test"), show_default=ctx_value)
opt = click.Option("-a", default=1, help="value", show_default=opt_value)
assert opt.get_help_extra(ctx) == extra_value
help = opt.get_help_record(ctx)[1]
assert ("default:" in help) is expect
@pytest.mark.parametrize(
("args", "expect"),
[
(None, (None, None, ())),
(["--opt"], ("flag", None, ())),
(["--opt", "-a", 42], ("flag", "42", ())),
(["--opt", "test", "-a", 42], ("test", "42", ())),
(["--opt=test", "-a", 42], ("test", "42", ())),
(["-o"], ("flag", None, ())),
(["-o", "-a", 42], ("flag", "42", ())),
(["-o", "test", "-a", 42], ("test", "42", ())),
(["-otest", "-a", 42], ("test", "42", ())),
(["a", "b", "c"], (None, None, ("a", "b", "c"))),
(["--opt", "a", "b", "c"], ("a", None, ("b", "c"))),
(["--opt", "test"], ("test", None, ())),
(["-otest", "a", "b", "c"], ("test", None, ("a", "b", "c"))),
(["--opt=test", "a", "b", "c"], ("test", None, ("a", "b", "c"))),
],
)
def test_option_with_optional_value(runner, args, expect):
@click.command()
@click.option("-o", "--opt", is_flag=False, flag_value="flag")
@click.option("-a")
@click.argument("b", nargs=-1)
def cli(opt, a, b):
return opt, a, b
result = runner.invoke(cli, args, standalone_mode=False, catch_exceptions=False)
assert result.return_value == expect
def test_multiple_option_with_optional_value(runner):
cli = click.Command(
"cli",
params=[
click.Option(["-f"], is_flag=False, flag_value="flag", multiple=True),
click.Option(["-a"]),
click.Argument(["b"], nargs=-1),
],
callback=lambda **kwargs: kwargs,
)
result = runner.invoke(
cli,
["-f", "-f", "other", "-f", "-a", "1", "a", "b"],
standalone_mode=False,
catch_exceptions=False,
)
assert result.return_value == {
"f": ("flag", "other", "flag"),
"a": "1",
"b": ("a", "b"),
}
def test_type_from_flag_value():
param = click.Option(["-a", "x"], default=True, flag_value=4)
assert param.type is click.INT
param = click.Option(["-b", "x"], flag_value=8)
assert param.type is click.INT
@pytest.mark.parametrize(
("opt_params", "args", "expected"),
[
# The type passed to the option is responsible to converting the value, whether
# we pass the option flag or not.
({"type": bool}, [], False),
({"type": bool}, ["--foo"], True),
({"type": click.BOOL}, [], False),
({"type": click.BOOL}, ["--foo"], True),
({"type": str}, [], None),
({"type": str}, ["--foo"], "True"),
# Default value is given as-is to the --foo option when it is not passed,
# whatever the type. Now if --foo is passed, the value is always True, whatever
# the type. In both case the type of the option is responsible for the
# conversion of the value.
({"type": bool, "default": True}, [], True),
({"type": bool, "default": True}, ["--foo"], True),
({"type": bool, "default": False}, [], False),
({"type": bool, "default": False}, ["--foo"], True),
# ({"type": bool, "default": "foo"}, [], "foo"),
({"type": bool, "default": "foo"}, ["--foo"], True),
({"type": bool, "default": None}, [], None),
({"type": bool, "default": None}, ["--foo"], True),
({"type": click.BOOL, "default": True}, [], True),
({"type": click.BOOL, "default": True}, ["--foo"], True),
({"type": click.BOOL, "default": False}, [], False),
({"type": click.BOOL, "default": False}, ["--foo"], True),
# ({"type": click.BOOL, "default": "foo"}, [], "foo"),
({"type": click.BOOL, "default": "foo"}, ["--foo"], True),
({"type": click.BOOL, "default": None}, [], None),
({"type": click.BOOL, "default": None}, ["--foo"], True),
({"type": str, "default": True}, [], "True"),
({"type": str, "default": True}, ["--foo"], "True"),
({"type": str, "default": False}, [], "False"),
({"type": str, "default": False}, ["--foo"], "True"),
({"type": str, "default": "foo"}, [], "foo"),
({"type": str, "default": "foo"}, ["--foo"], "True"),
({"type": str, "default": None}, [], None),
({"type": str, "default": None}, ["--foo"], "True"),
# Flag value is given as-is to the --foo option when it is passed, then
# converted by the option type.
({"type": bool, "flag_value": True}, [], False),
({"type": bool, "flag_value": True}, ["--foo"], True),
({"type": bool, "flag_value": False}, [], False),
({"type": bool, "flag_value": False}, ["--foo"], False),
({"type": bool, "flag_value": None}, [], False),
({"type": bool, "flag_value": None}, ["--foo"], None),
({"type": click.BOOL, "flag_value": True}, [], False),
({"type": click.BOOL, "flag_value": True}, ["--foo"], True),
({"type": click.BOOL, "flag_value": False}, [], False),
({"type": click.BOOL, "flag_value": False}, ["--foo"], False),
({"type": click.BOOL, "flag_value": None}, [], False),
({"type": click.BOOL, "flag_value": None}, ["--foo"], None),
({"type": str, "flag_value": True}, [], None),
({"type": str, "flag_value": True}, ["--foo"], "True"),
({"type": str, "flag_value": False}, [], None),
({"type": str, "flag_value": False}, ["--foo"], "False"),
({"type": str, "flag_value": "foo"}, [], None),
({"type": str, "flag_value": "foo"}, ["--foo"], "foo"),
({"type": str, "flag_value": None}, [], None),
({"type": str, "flag_value": None}, ["--foo"], None),
# Not passing --foo returns the default value as-is, in its Python type, then
# converted by the option type.
({"type": bool, "default": True, "flag_value": True}, [], True),
({"type": bool, "default": True, "flag_value": False}, [], False),
({"type": bool, "default": False, "flag_value": True}, [], False),
({"type": bool, "default": False, "flag_value": False}, [], False),
({"type": bool, "default": None, "flag_value": True}, [], None),
({"type": bool, "default": None, "flag_value": False}, [], None),
({"type": str, "default": True, "flag_value": True}, [], "True"),
({"type": str, "default": True, "flag_value": False}, [], "False"),
({"type": str, "default": False, "flag_value": True}, [], "False"),
({"type": str, "default": False, "flag_value": False}, [], "False"),
({"type": str, "default": "foo", "flag_value": True}, [], "foo"),
({"type": str, "default": "foo", "flag_value": False}, [], "foo"),
({"type": str, "default": "foo", "flag_value": "bar"}, [], "foo"),
({"type": str, "default": "foo", "flag_value": None}, [], "foo"),
({"type": str, "default": None, "flag_value": True}, [], None),
({"type": str, "default": None, "flag_value": False}, [], None),
# Passing --foo returns the flag_value that was explicitly set by the user,
# with its Python type, but still converted by the option type.
({"type": bool, "default": True, "flag_value": True}, ["--foo"], True),
({"type": bool, "default": True, "flag_value": False}, ["--foo"], False),
({"type": bool, "default": False, "flag_value": True}, ["--foo"], True),
({"type": bool, "default": False, "flag_value": False}, ["--foo"], False),
({"type": bool, "default": None, "flag_value": True}, ["--foo"], True),
({"type": bool, "default": None, "flag_value": False}, ["--foo"], False),
({"type": str, "default": True, "flag_value": True}, ["--foo"], "True"),
({"type": str, "default": True, "flag_value": False}, ["--foo"], "False"),
({"type": str, "default": False, "flag_value": True}, ["--foo"], "True"),
({"type": str, "default": False, "flag_value": False}, ["--foo"], "False"),
({"type": str, "default": "foo", "flag_value": True}, ["--foo"], "True"),
({"type": str, "default": "foo", "flag_value": False}, ["--foo"], "False"),
({"type": str, "default": "foo", "flag_value": "bar"}, ["--foo"], "bar"),
({"type": str, "default": "foo", "flag_value": None}, ["--foo"], None),
({"type": str, "default": None, "flag_value": True}, ["--foo"], "True"),
({"type": str, "default": None, "flag_value": False}, ["--foo"], "False"),
],
)
def test_flag_value_and_default(runner, opt_params, args, expected):
@click.command()
@click.option("--foo", is_flag=True, **opt_params)
def cmd(foo):
click.echo(repr(foo), nl=False)
result = runner.invoke(cmd, args)
assert result.output == repr(expected)
@pytest.mark.parametrize(
("args", "opts"),
[
([], {"type": bool, "default": "foo"}),
([], {"type": click.BOOL, "default": "foo"}),
(["--foo"], {"type": bool, "flag_value": "foo"}),
(["--foo"], {"type": click.BOOL, "flag_value": "foo"}),
],
)
def test_invalid_flag_definition(runner, args, opts):
@click.command()
@click.option("--foo", is_flag=True, **opts)
def cmd(foo):
click.echo(foo)
result = runner.invoke(cmd, args)
assert (
"Error: Invalid value for '--foo': 'foo' is not a valid boolean"
in result.output
)
@pytest.mark.parametrize(
("default", "args", "expected"),
# These test cases are similar to the ones in
# tests/test_basic.py::test_flag_value_dual_options, so keep them in sync.
(
# Each option is returning its own flag_value, whatever the default is.
(True, ["--js"], "js"),
(True, ["--xml"], "xml"),
(False, ["--js"], "js"),
(False, ["--xml"], "xml"),
(None, ["--js"], "js"),
(None, ["--xml"], "xml"),
(UNSET, ["--js"], "js"),
(UNSET, ["--xml"], "xml"),
# Check that the last option wins when both are specified.
(True, ["--js", "--xml"], "xml"),
(True, ["--xml", "--js"], "js"),
# Check that the default is returned as-is when no option is specified.
("js", [], "js"),
("xml", [], "xml"),
("jS", [], "jS"),
("xMl", [], "xMl"),
(" ᕕ( ᐛ )ᕗ ", [], " ᕕ( ᐛ )ᕗ "),
(None, [], None),
# Special case: UNSET is not provided as-is to the callback, but normalized to
# None.
(UNSET, [], None),
# Special case: if default=True and flag_value is set, the value returned is the
# flag_value, not the True Python value itself.
(True, [], "js"),
# Non-string defaults are process as strings by the default Parameter's type.
(False, [], "False"),
(42, [], "42"),
(12.3, [], "12.3"),
),
)
def test_default_dual_option_callback(runner, default, args, expected):
"""Check how default is processed by the callback when options compete for the same
variable name.
Reproduction of the issue reported in
https://github.com/pallets/click/pull/3030#discussion_r2271571819
"""
def _my_func(ctx, param, value):
# Print the value received by the callback as-is, so we can check for it.
return f"Callback value: {value!r}"
@click.command()
@click.option("--js", "fmt", flag_value="js", callback=_my_func, default=default)
@click.option("--xml", "fmt", flag_value="xml", callback=_my_func)
def main(fmt):
click.secho(fmt, nl=False)
result = runner.invoke(main, args)
assert result.output == f"Callback value: {expected!r}"
assert result.exit_code == 0
@pytest.mark.parametrize(
("flag_value", "envvar_value", "expected"),
[
# The envvar match exactly the flag value and is case-sensitive.
("bar", "bar", "bar"),
("BAR", "BAR", "BAR"),
(" bar ", " bar ", " bar "),
("42", "42", "42"),
("None", "None", "None"),
("True", "True", "True"),
("False", "False", "False"),
("true", "true", "true"),
("false", "false", "false"),
("A B", "A B", "A B"),
(" 1 2 ", " 1 2 ", " 1 2 "),
("9.3", "9.3", "9.3"),
("a;n", "a;n", "a;n"),
("x:y", "x:y", "x:y"),
("i/o", "i/o", "i/o"),
# Empty or absent envvar is consider unset, so the flag default value is
# returned, which is None in this case.
("bar", "", None),
("bar", None, None),
("BAR", "", None),
("BAR", None, None),
(" bar ", "", None),
(" bar ", None, None),
(42, "", None),
(42, None, None),
("42", "", None),
("42", None, None),
(None, "", None),
(None, None, None),
("None", "", None),
("None", None, None),
(True, "", None),
(True, None, None),
("True", "", None),
("True", None, None),
("true", "", None),
("true", None, None),
(False, "", None),
(False, None, None),
("False", "", None),
("False", None, None),
("false", "", None),
("false", None, None),
# Activate the flag with a value recognized as True by the envvar, which
# returns the flag_value.
("bar", "True", "bar"),
("bar", "true", "bar"),
("bar", "trUe", "bar"),
("bar", " TRUE ", "bar"),
("bar", "1", "bar"),
("bar", "yes", "bar"),
("bar", "on", "bar"),
("bar", "t", "bar"),
("bar", "y", "bar"),
# Deactivating the flag with a value recognized as False by the envvar, which
# explicitly return the 'False' as the option is explicitly declared as a
# string.
("bar", "False", "False"),
("bar", "false", "False"),
("bar", "faLse", "False"),
("bar", " FALSE ", "False"),
("bar", "0", "False"),
("bar", "no", "False"),
("bar", "off", "False"),
("bar", "f", "False"),
("bar", "n", "False"),
# Any other value than the flag_value, or a recogned True or False value, fails
# to explicitly activate or deactivate the flag. So the flag default value is
# returned, which is None in this case.
("bar", " bar ", None),
("bar", "BAR", None),
("bar", "random", None),
("bar", "bar random", None),
("bar", "random bar", None),
("BAR", "bar", None),
(" bar ", "bar", None),
(" bar ", "BAR", None),
(42, "42", None),
(42, "foo", None),
(None, "foo", None),
("None", "foo", None),
],
)
def test_envvar_string_flag_value(runner, flag_value, envvar_value, expected):
"""Ensure that flag_value is recognized by the envvar."""
@click.command()
@click.option("--upper", type=str, flag_value=flag_value, envvar="UPPER")
def cmd(upper):
click.echo(repr(upper), nl=False)
result = runner.invoke(cmd, env={"UPPER": envvar_value})
assert result.exit_code == 0
assert result.output == repr(expected)
@pytest.mark.parametrize(
("opt_decls", "opt_params", "expect_is_flag", "expect_is_bool_flag"),
[
# Not boolean flags.
("-a", {"type": int}, False, False),
("-a", {"type": bool}, False, False),
("-a", {"default": True}, False, False),
("-a", {"default": False}, False, False),
("-a", {"flag_value": 1}, True, False),
# Boolean flags.
("-a", {"is_flag": True}, True, True),
("-a/-A", {}, True, True),
("-a", {"flag_value": True}, True, True),
# Non-flag with flag_value.
("-a", {"is_flag": False, "flag_value": 1}, False, False),
],
)
def test_flag_auto_detection(
opt_decls, opt_params, expect_is_flag, expect_is_bool_flag
):
option = Option([opt_decls], **opt_params)
assert option.is_flag is expect_is_flag
assert option.is_bool_flag is expect_is_bool_flag
@pytest.mark.parametrize(
("kwargs", "message"),
[
({"count": True, "multiple": True}, "'count' is not valid with 'multiple'."),
({"count": True, "is_flag": True}, "'count' is not valid with 'is_flag'."),
],
)
def test_invalid_flag_combinations(runner, kwargs, message):
with pytest.raises(TypeError) as e:
click.Option(["-a"], **kwargs)
assert message in str(e.value)
def test_non_flag_with_non_negatable_default(runner):
class NonNegatable:
def __bool__(self):
raise ValueError("Cannot negate this object")
@click.command()
@click.option("--foo", default=NonNegatable())
def cmd(foo):
pass
result = runner.invoke(cmd)
assert result.exit_code == 0
class HashType(enum.Enum):
MD5 = "MD5"
SHA1 = "SHA1"
SHA256 = "SHA-256"
class Number(enum.IntEnum):
ONE = enum.auto()
TWO = enum.auto()
class Letter(enum.StrEnum):
NAME_1 = "Value-1"
NAME_2 = "Value_2"
NAME_3 = "42_value"
class Color(enum.Flag):
RED = enum.auto()
GREEN = enum.auto()
BLUE = enum.auto()
class ColorInt(enum.IntFlag):
RED = enum.auto()
GREEN = enum.auto()
BLUE = enum.auto()
@pytest.mark.parametrize(
("choices", "metavar"),
[
(["foo", "bar"], "[TEXT]"),
([1, 2], "[INTEGER]"),
([1.0, 2.0], "[FLOAT]"),
([True, False], "[BOOLEAN]"),
(["foo", 1], "[TEXT|INTEGER]"),
(HashType, "[HASHTYPE]"),
(Number, "[NUMBER]"),
(Letter, "[LETTER]"),
(Color, "[COLOR]"),
(ColorInt, "[COLORINT]"),
],
)
def test_choice_usage_rendering(runner, choices, metavar):
"""BY default ``--help`` prints choice's values in the usage message.
But ``show_choices=False`` makes ``--help`` prints choice's METAVAR instead of
values.
Also check that usage error message always suggests the actual values.
"""
@click.command()
@click.option("-g", type=click.Choice(choices))
def cli_with_choices(g):
pass
@click.command()
@click.option("-g", type=click.Choice(choices), show_choices=False)
def cli_without_choices(g):
pass
display_values = tuple(
i.name if isinstance(i, enum.Enum) else str(i) for i in choices
)
# Check that the choices values are rendered as-is in the usage message.
result = runner.invoke(cli_with_choices, ["--help"])
assert f"[{'|'.join(display_values)}]" in result.stdout
assert not result.stderr
assert result.exit_code == 0
# Check that the metavar is rendered instead of the choices values themselves.
result = runner.invoke(cli_without_choices, ["--help"])
assert metavar in result.stdout
assert not result.stderr
assert result.exit_code == 0
# Check the usage error message suggests the actual accepted values.
for cli in (cli_with_choices, cli_without_choices):
result = runner.invoke(cli, ["-g", "random"])
assert (
"\n\nError: Invalid value for '-g': 'random' is not one of "
f"{', '.join(map(repr, display_values))}.\n" in result.stderr
)
assert not result.stdout
assert result.exit_code == 2
@pytest.mark.parametrize(
("choices", "default", "default_string"),
[
(["foo", "bar"], "bar", "bar"),
# The default value is not enforced to be in the choices.
(["foo", "bar"], "random", "random"),
# None cannot be a default value as-is: it left the default value as unset.
(["foo", "bar"], None, None),
([0, 1], 0, "0"),
# Values are not coerced to the type of the choice, even if equivalent.
([0, 1], 0.0, "0.0"),
([1, 2], 2, "2"),
([1.0, 2.0], 2, "2"),
([1.0, 2.0], 2.0, "2.0"),
([True, False], True, "True"),
([True, False], False, "False"),
(["foo", 1], "foo", "foo"),
(["foo", 1], 1, "1"),
# Enum choices are rendered as their names, not values.
# See: https://github.com/pallets/click/issues/2911
(HashType, HashType.SHA1, "SHA1"),
# Enum choices allow defaults strings that are their names.
(HashType, HashType.SHA256, "SHA256"),
(HashType, "SHA256", "SHA256"),
(Number, Number.TWO, "TWO"),
(Number, "TWO", "TWO"),
(Letter, Letter.NAME_1, "NAME_1"),
(Letter, Letter.NAME_2, "NAME_2"),
(Letter, Letter.NAME_3, "NAME_3"),
(Letter, "NAME_1", "NAME_1"),
(Letter, "NAME_2", "NAME_2"),
(Letter, "NAME_3", "NAME_3"),
(Color, Color.GREEN, "GREEN"),
(Color, "GREEN", "GREEN"),
(ColorInt, ColorInt.GREEN, "GREEN"),
(ColorInt, "GREEN", "GREEN"),
],
)
def test_choice_default_rendering(runner, choices, default, default_string):
@click.command()
@click.option("-g", type=click.Choice(choices), default=default, show_default=True)
def cli_with_choices(g):
pass
# Check that the default value is kept normalized to the type of the choice.
assert cli_with_choices.params[0].default == default
result = runner.invoke(cli_with_choices, ["--help"])
extra_usage = f"[default: {default_string}]"
if default_string is None:
assert extra_usage not in result.output
else:
assert extra_usage in result.output
@pytest.mark.parametrize(
"opts_one,opts_two",
[
# No duplicate shortnames
(
("-a", "--aardvark"),
("-a", "--avocado"),
),
# No duplicate long names
(
("-a", "--aardvark"),
("-b", "--aardvark"),
),
],
)
def test_duplicate_names_warning(runner, opts_one, opts_two):
@click.command()
@click.option(*opts_one)
@click.option(*opts_two)
def cli(one, two):
pass
with pytest.warns(UserWarning):
runner.invoke(cli, [])
OBJECT_SENTINEL = object()
"""An object-based sentinel value."""
class EnumSentinel(enum.Enum):
FALSY_SENTINEL = object()
def __bool__(self) -> Literal[False]:
"""Force the sentinel to be falsy to make sure it is not caught by Click
internal implementation.
Falsy sentinels have been discussed in:
https://github.com/pallets/click/pull/3030#pullrequestreview-3106604795
https://github.com/pallets/click/pull/3030#pullrequestreview-3108471552
"""
return False
# Any kind of sentinel value is recognized by Click as a valid flag value.
@pytest.mark.parametrize("sentinel", (OBJECT_SENTINEL, EnumSentinel.FALSY_SENTINEL))
@pytest.mark.parametrize(
("args", "expected"),
(
# Option default to None.
([], None),
# Config has no default value, and no flag value, so it requires an argument.
(
["--config"],
re.compile(re.escape("Error: Option '--config' requires an argument.\n")),
),
(
["--no-config", "--config"],
re.compile(re.escape("Error: Option '--config' requires an argument.\n")),
),
# Passing --no-config defaults to the sentinel value because of the flag_value,
# and then the custom type receives that sentinel and returns a message.
(["--no-config"], "No configuration file provided."),
# Passing --config with an argument returns the file path.
(["--config", "foo.conf"], "foo.conf"),
# An argument is not allowed for --no-config, so it raises an error.
(
["--no-config", "foo.conf"],
re.compile(
r"Usage: main \[OPTIONS\]\n"
r"Try 'main --help' for help.\n"
r"\n"
r"Error: Got unexpected extra argument (.+)\n"
),
),
# Passing --config with an argument that does not exist raises an error.
(
["--config", "random-file.conf"],
re.compile(
r"Usage: main \[OPTIONS\]\n"
r"Try 'main --help' for help.\n"
r"\n"
r"Error: Invalid value for '-c' / '--config': "
r"File 'random-file.conf' does not exist.\n"
),
),
(
["--config", "--no-config"],
re.compile(
r"Usage: main \[OPTIONS\]\n"
r"Try 'main --help' for help.\n"
r"\n"
r"Error: Invalid value for '-c' / '--config': "
r"File '--no-config' does not exist.\n"
),
),
(
["--config", "--no-config", "foo.conf"],
re.compile(
r"Usage: main \[OPTIONS\]\n"
r"Try 'main --help' for help.\n"
r"\n"
r"Error: Invalid value for '-c' / '--config': "
r"File '--no-config' does not exist.\n"
),
),
# --config is passed last and overrides the --no-config option.
(["--no-config", "--config", "foo.conf"], "foo.conf"),
),
)
def test_dual_options_custom_type_sentinel_flag_value(runner, sentinel, args, expected):
"""Check that an object-based sentinel, used as a flag value, is returned as-is
to a custom type that is shared by two options, competing for the same variable
name.
A reproduction of
https://github.com/pallets/click/issues/3024#issuecomment-3146511356
"""
class ConfigParamType(click.ParamType):
"""A custom type that accepts a file path or a sentinel value."""
def convert(self, value, param, ctx):
if value is sentinel:
return "No configuration file provided."
else:
return click.Path(exists=True, dir_okay=False).convert(
value, param, ctx
)
@click.command()
@click.option("-c", "--config", type=ConfigParamType())
@click.option("--no-config", "config", flag_value=sentinel, type=ConfigParamType())
def main(config):
click.echo(repr(config), nl=False)
with tempfile.NamedTemporaryFile(mode="w") as named_tempfile:
if "foo.conf" in args:
named_tempfile.write("Blah blah")
named_tempfile.flush()
args = [named_tempfile.name if a == "foo.conf" else a for a in args]
result = runner.invoke(main, args)
if isinstance(expected, re.Pattern):
assert re.match(expected, result.output)
else:
assert result.output == repr(
named_tempfile.name if expected == "foo.conf" else expected
)
class EngineType(enum.Enum):
OSS = enum.auto()
PRO = enum.auto()
MAX = enum.auto()
class Class1:
pass
class Class2:
pass
@pytest.mark.parametrize(
("opt_params", "args", "expected"),
[
# Check that the flag value is returned as-is when the option is passed, and
# not normalized to a boolean, even if it is explicitly declared as a flag.
({"type": EngineType, "flag_value": None}, ["--pro"], None),
(
{"type": EngineType, "is_flag": True, "flag_value": None},
["--pro"],
None,
),
({"type": EngineType, "flag_value": EngineType.OSS}, ["--pro"], EngineType.OSS),
(
{"type": EngineType, "is_flag": True, "flag_value": EngineType.OSS},
["--pro"],
EngineType.OSS,
),
(
{"type": EngineType, "is_flag": True, "default": EngineType.OSS},
["--pro"],
EngineType.OSS,
),
# The default value is returned as-is when the option is not passed, whatever
# the flag value.
({"type": EngineType, "flag_value": None}, [], None),
({"type": EngineType, "is_flag": True, "flag_value": None}, [], None),
({"type": EngineType, "flag_value": EngineType.OSS}, [], None),
(
{"type": EngineType, "is_flag": True, "flag_value": EngineType.OSS},
[],
None,
),
(
{"type": EngineType, "is_flag": True, "default": EngineType.OSS},
[],
EngineType.OSS,
),
# The option has not enough parameters to be detected as flag-like, so it
# requires an argument.
(
{"type": EngineType, "default": EngineType.OSS},
["--pro"],
re.compile(re.escape("Error: Option '--pro' requires an argument.\n")),
),
({"type": EngineType, "default": EngineType.OSS}, [], EngineType.OSS),
# If a flag value is set, it is returned instead of the default value.
(
{"type": EngineType, "flag_value": EngineType.OSS, "default": True},
["--pro"],
EngineType.OSS,
),
(
{"type": EngineType, "flag_value": EngineType.OSS, "default": True},
[],
EngineType.OSS,
),
# Type is not specified and default to string, so the default value is
# returned as a string, even if it is a boolean. Also, defaults to the
# flag_value instead of the default value to support legacy behavior.
({"flag_value": "1", "default": True}, [], "1"),
({"flag_value": "1", "default": 42}, [], "42"),
({"flag_value": EngineType.OSS, "default": True}, [], "EngineType.OSS"),
({"flag_value": EngineType.OSS, "default": 42}, [], "42"),
# See: the result is the same if we force the type to be str.
({"type": str, "flag_value": 1, "default": True}, [], "1"),
({"type": str, "flag_value": 1, "default": 42}, [], "42"),
({"type": str, "flag_value": "1", "default": True}, [], "1"),
({"type": str, "flag_value": "1", "default": 42}, [], "42"),
(
{"type": str, "flag_value": EngineType.OSS, "default": True},
[],
"EngineType.OSS",
),
({"type": str, "flag_value": EngineType.OSS, "default": 42}, [], "42"),
# But having the flag value set to integer is automaticcally recognized by
# Click.
({"flag_value": 1, "default": True}, [], 1),
({"flag_value": 1, "default": 42}, [], 42),
({"type": int, "flag_value": 1, "default": True}, [], 1),
({"type": int, "flag_value": 1, "default": 42}, [], 42),
({"type": int, "flag_value": "1", "default": True}, [], 1),
({"type": int, "flag_value": "1", "default": 42}, [], 42),
],
)
def test_custom_type_flag_value_standalone_option(runner, opt_params, args, expected):
"""Test how the type and flag_value influence the returned value.
Cover cases reported in:
https://github.com/pallets/click/issues/3024#issuecomment-3146480714
https://github.com/pallets/click/issues/2012#issuecomment-892437060
"""
@click.command()
@click.option("--pro", **opt_params)
def scan(pro):
click.echo(repr(pro), nl=False)
result = runner.invoke(scan, args)
if isinstance(expected, re.Pattern):
assert re.match(expected, result.output)
else:
assert result.output == repr(expected)
@pytest.mark.parametrize(
("opt1_params", "opt2_params", "args", "expected"),
[
# Dual options sharing the same variable name, are not competitive, and the
# flag value is returned as-is. Especially when the type is force to be
# unprocessed.
(
{"flag_value": EngineType.OSS, "type": UNPROCESSED},
{"flag_value": EngineType.PRO, "type": UNPROCESSED},
[],
None,
),
(
{"flag_value": EngineType.OSS, "type": UNPROCESSED},
{"flag_value": EngineType.PRO, "type": UNPROCESSED},
["--opt1"],
EngineType.OSS,
),
(
{"flag_value": EngineType.OSS, "type": UNPROCESSED},
{"flag_value": EngineType.PRO, "type": UNPROCESSED},
["--opt2"],
EngineType.PRO,
),
# Check that passing exotic flag values like classes is supported, but are
# rendered to strings when the type is not specified.
(
{"flag_value": Class1, "default": True},
{"flag_value": Class2},
[],
re.compile(r"'<test_options.Class1 object at 0x[0-9A-Fa-f]+>'"),
),
(
{"flag_value": Class1, "default": True},
{"flag_value": Class2},
["--opt1"],
"<class 'test_options.Class1'>",
),
(
{"flag_value": Class1, "default": True},
{"flag_value": Class2},
["--opt2"],
"<class 'test_options.Class2'>",
),
# Even the default is processed as a string.
({"flag_value": Class1, "default": "True"}, {"flag_value": Class2}, [], "True"),
({"flag_value": Class1, "default": None}, {"flag_value": Class2}, [], None),
# To get the classes as-is, we need to specify the type as UNPROCESSED.
(
{"flag_value": Class1, "type": UNPROCESSED, "default": True},
{"flag_value": Class2, "type": UNPROCESSED},
[],
re.compile(r"<test_options.Class1 object at 0x[0-9A-Fa-f]+>"),
),
(
{"flag_value": Class1, "type": UNPROCESSED, "default": True},
{"flag_value": Class2, "type": UNPROCESSED},
["--opt1"],
Class1,
),
(
{"flag_value": Class1, "type": UNPROCESSED, "default": True},
{"flag_value": Class2, "type": UNPROCESSED},
["--opt2"],
Class2,
),
# Setting the default to a class, an instance of the class is returned instead
# of the class itself, because the default is allowed to be callable (and
# consummd). And this happens whatever the type is.
(
{"flag_value": Class1, "default": Class1},
{"flag_value": Class2},
[],
re.compile(r"'<test_options.Class1 object at 0x[0-9A-Fa-f]+>'"),
),
(
{"flag_value": Class1, "default": Class2},
{"flag_value": Class2},
[],
re.compile(r"'<test_options.Class2 object at 0x[0-9A-Fa-f]+>'"),
),
(
{"flag_value": Class1, "type": UNPROCESSED, "default": Class1},
{"flag_value": Class2, "type": UNPROCESSED},
[],
re.compile(r"<test_options.Class1 object at 0x[0-9A-Fa-f]+>"),
),
(
{"flag_value": Class1, "type": UNPROCESSED, "default": Class2},
{"flag_value": Class2, "type": UNPROCESSED},
[],
re.compile(r"<test_options.Class2 object at 0x[0-9A-Fa-f]+>"),
),
# Having the flag value set to integer is automaticcally recognized by Click.
(
{"flag_value": 1, "default": True},
{"flag_value": "1"},
[],
1,
),
(
{"flag_value": 1, "type": int, "default": True},
{"flag_value": "1", "type": int},
[],
1,
),
],
)
def test_custom_type_flag_value_dual_options(
runner, opt1_params, opt2_params, args, expected
):
"""Test how flag values are processed with dual options competing for the same
variable name.
Reproduce issues reported in:
https://github.com/pallets/click/issues/3024#issuecomment-3146508536
https://github.com/pallets/click/issues/2012#issue-946471049
https://github.com/pallets/click/issues/2012#issuecomment-892437060
"""
@click.command()
@click.option("--opt1", "dual_option", **opt1_params)
@click.option("--opt2", "dual_option", **opt2_params)
def cli(dual_option):
click.echo(repr(dual_option), nl=False)
result = runner.invoke(cli, args)
if isinstance(expected, re.Pattern):
assert re.match(expected, result.output)
else:
assert result.output == repr(expected)
def test_custom_type_frozenset_flag_value(runner):
"""Check that frozenset is correctly handled as a type, a flag value and a default.
Reproduces https://github.com/pallets/click/issues/2610
"""
@click.command()
@click.option(
"--without-scm-ignore-files",
"scm_ignore_files",
is_flag=True,
type=frozenset,
flag_value=frozenset(),
default=frozenset(["git"]),
)
def rcli(scm_ignore_files):
click.echo(repr(scm_ignore_files), nl=False)
result = runner.invoke(rcli)
assert result.stdout == "frozenset({'git'})"
assert result.exit_code == 0
result = runner.invoke(rcli, ["--without-scm-ignore-files"])
assert result.stdout == "frozenset()"
assert result.exit_code == 0
|