88from  importlib .metadata  import  version 
99from  textual .constants  import  DEVTOOLS_HOST , DEVTOOLS_PORT 
1010
11- from  .tools .run  import  exec_command , run_app 
11+ from  .tools .run  import  _is_python_path ,  exec_command , run_app 
1212
1313WINDOWS  =  platform .system () ==  "Windows" 
1414"""True if we're running on Windows.""" 
@@ -228,7 +228,7 @@ def _run_app(
228228
229229
230230@run .command ("serve" ) 
231- @click .argument ("command " ) 
231+ @click .argument ("import_name"  ,  metavar = "FILE or FILE:APP " ) 
232232@click .option ("-h" , "--host" , type = str , default = "localhost" , help = "Host to serve on" ) 
233233@click .option ("-p" , "--port" , type = int , default = 8000 , help = "Port of server" ) 
234234@click .option ( 
@@ -240,8 +240,25 @@ def _run_app(
240240) 
241241@click .option ("-u" , "--url" , type = str , default = None , help = "Public URL" ) 
242242@click .option ("--dev" , type = bool , default = False , is_flag = True , help = "Enable debug mode" ) 
243+ @click .option ( 
244+     "-c" , 
245+     "--command" , 
246+     "command" , 
247+     type = bool , 
248+     default = False , 
249+     help = "Run as command rather that a file / module." , 
250+     is_flag = True , 
251+ ) 
252+ @click .argument ("extra_args" , nargs = - 1 , type = click .UNPROCESSED ) 
243253def  serve (
244-     command : str , host : str , port : int , title : str , url : str  |  None , dev : bool 
254+     import_name : str ,
255+     host : str ,
256+     port : int ,
257+     title : str ,
258+     url : str  |  None ,
259+     dev : bool ,
260+     extra_args : tuple [str ],
261+     command : bool  =  False ,
245262) ->  None :
246263    """Run a local web server to serve the application. 
247264
@@ -257,7 +274,20 @@ def serve(
257274    """ 
258275    from  textual_serve .server  import  Server 
259276
260-     server  =  Server (command , host , port , title = title , public_url = url )
277+     import_name , * args  =  [* shlex .split (import_name , posix = not  WINDOWS ), * extra_args ]
278+ 
279+     if  command :
280+         run_args  =  shlex .join (args )
281+         run_command  =  f"{ import_name }   { run_args }  " 
282+         server  =  Server (run_command , host , port , title = title , public_url = url )
283+     else :
284+         if  _is_python_path (import_name ):
285+             run_command  =  f"python { import_name }  " 
286+         else :
287+             run_args  =  shlex .join (args )
288+             run_command  =  f"{ import_name }   { run_args }  " 
289+         server  =  Server (run_command , host , port , title = title , public_url = url )
290+ 
261291    server .serve (debug = dev )
262292
263293
0 commit comments