@@ -1653,6 +1653,34 @@ def cli(one, two):
16531653 runner .invoke (cli , [])
16541654
16551655
1656+ def test_custom_type_click_class_flag_value (runner ):
1657+ """A reproduction of
1658+ https://github.com/pallets/click/issues/3024#issuecomment-3146511356
1659+ """
1660+
1661+ NO_CONFIG = object ()
1662+
1663+ class ConfigParamType (click .ParamType ):
1664+ name = "config"
1665+
1666+ def convert (self , value , param , ctx ):
1667+ if value is NO_CONFIG :
1668+ return value
1669+ else :
1670+ return click .Path (exists = True , dir_okay = False ).convert (
1671+ value , param , ctx
1672+ )
1673+
1674+ @click .command ()
1675+ @click .option ("-c" , "--config" , type = ConfigParamType ())
1676+ @click .option ("--no-config" , "config" , flag_value = NO_CONFIG , type = ConfigParamType ())
1677+ def main (config ):
1678+ click .echo (repr (config ), nl = False )
1679+
1680+ result = runner .invoke (main )
1681+ assert result .output == repr (None )
1682+
1683+
16561684class EngineType (enum .Enum ):
16571685 OSS = enum .auto ()
16581686 PRO = enum .auto ()
@@ -1694,23 +1722,29 @@ def scan(pro):
16941722 assert result .output == expected
16951723
16961724
1697- def test_custom_type_click_class_flag_value (runner ):
1725+ @pytest .mark .parametrize (
1726+ ("args" , "expected" ),
1727+ [
1728+ ([], None ),
1729+ (["--oss" ], EngineType .OSS ),
1730+ (["--pro" ], EngineType .PRO ),
1731+ ],
1732+ )
1733+ def test_unprocessed_type_enum_flag_value_shared_valiable (runner , args , expected ):
16981734 """A reproduction of
1699- https://github.com/pallets/click/issues/3024#issuecomment-3146511356
1735+ https://github.com/pallets/click/issues/3024#issuecomment-3146508536
17001736 """
17011737
1702- NO_CONFIG = object ()
1738+ @click .command ()
1739+ @click .option ("--oss" , "mode" , flag_value = EngineType .OSS , type = click .UNPROCESSED )
1740+ @click .option ("--pro" , "mode" , flag_value = EngineType .PRO , type = click .UNPROCESSED )
1741+ def main (mode ):
1742+ click .echo (repr (mode ), nl = False )
17031743
1704- class ConfigParamType (click .ParamType ):
1705- name = "config"
1744+ result = runner .invoke (main , args )
1745+ assert result .output == repr (expected )
1746+ assert result .exit_code == 0
17061747
1707- def convert (self , value , param , ctx ):
1708- if value is NO_CONFIG :
1709- return value
1710- else :
1711- return click .Path (exists = True , dir_okay = False ).convert (
1712- value , param , ctx
1713- )
17141748
17151749 @click .command ()
17161750 @click .option ("-c" , "--config" , type = ConfigParamType ())
0 commit comments