11import inspect
22import sys
3+ import tempfile
34from pathlib import Path
45from typing import Any , Callable , Dict , List , Optional , Type , Union
56
@@ -76,6 +77,7 @@ def command( # noqa
7677 # Rich settings
7778 rich_help_panel : Union [str , None ] = Default (None ),
7879 registry : Any = None ,
80+ config : Optional [Union [Config , List [Config ]]] = None ,
7981 ) -> Callable [[CommandFunctionType ], CommandFunctionType ]:
8082 typer_command = super ().command (
8183 name = name ,
@@ -96,14 +98,35 @@ def command( # noqa
9698 },
9799 )
98100
101+ initial_config = config
102+
99103 def wrapper (fn ):
100104 validated = validate_arguments (fn )
101105
102106 @typer_command
103107 def command (ctx : Context , config : Optional [List [Path ]] = None ):
104- config_path = config
108+ config_path = config or []
105109
106110 has_meta = _fn_has_meta (fn )
111+
112+ if initial_config is not None :
113+ initial_configs = (
114+ [initial_config ]
115+ if isinstance (initial_config , Config )
116+ else initial_config
117+ )
118+
119+ initial_config_path = []
120+
121+ for c in initial_configs :
122+ temp_file = tempfile .NamedTemporaryFile (delete = False )
123+ temp_file .write (
124+ c .to_str ().encode ()
125+ ) # Write the string to the file
126+ temp_file .close ()
127+ initial_config_path .append (Path (temp_file .name ))
128+ config_path = initial_config_path + config_path
129+
107130 if config_path :
108131 config , name_from_file = merge_from_disk (config_path )
109132 else :
0 commit comments