1010from pathlib import Path
1111from typing import (
1212 TYPE_CHECKING ,
13- Any ,
1413 Callable ,
1514 List ,
1615 NoReturn ,
@@ -59,7 +58,9 @@ async def wait() -> None:
5958
6059@_logger .call
6160async def _debug_adapter_server_ (
62- host : str , port : int , on_config_done_callback : Optional [Callable [["DebugAdapterServer" ], None ]]
61+ host : str ,
62+ port : int ,
63+ on_config_done_callback : Optional [Callable [["DebugAdapterServer" ], None ]],
6364) -> None :
6465 from robotcode .jsonrpc2 .server import TcpParams
6566
@@ -124,11 +125,17 @@ async def run_robot(
124125 output_timestamps : bool = False ,
125126 group_output : bool = False ,
126127 stop_on_entry : bool = False ,
127- ) -> Any :
128+ ) -> int :
128129 import robot
129130
130- from robotcode .core .async_tools import run_coroutine_from_thread_async , run_coroutine_in_thread
131- from robotcode .core .utils .debugpy import is_debugpy_installed , wait_for_debugpy_connected
131+ from robotcode .core .async_tools import (
132+ run_coroutine_from_thread_async ,
133+ run_coroutine_in_thread ,
134+ )
135+ from robotcode .core .utils .debugpy import (
136+ is_debugpy_installed ,
137+ wait_for_debugpy_connected ,
138+ )
132139
133140 from .dap_types import Event
134141 from .debugger import Debugger
@@ -142,12 +149,15 @@ async def run_robot(
142149 server_future = run_coroutine_in_thread (_debug_adapter_server_ , addresses , port , config_done_callback )
143150
144151 server = await wait_for_server ()
152+ exit_code = 255
145153
146154 try :
147155 if wait_for_client :
148156 try :
149157 await run_coroutine_from_thread_async (
150- server .protocol .wait_for_client , wait_for_client_timeout , loop = server .loop
158+ server .protocol .wait_for_client ,
159+ wait_for_client_timeout ,
160+ loop = server .loop ,
151161 )
152162 except asyncio .CancelledError :
153163 pass
@@ -159,7 +169,9 @@ async def run_robot(
159169 if wait_for_client :
160170 try :
161171 await run_coroutine_from_thread_async (
162- server .protocol .wait_for_configuration_done , configuration_done_timeout , loop = server .loop
172+ server .protocol .wait_for_configuration_done ,
173+ configuration_done_timeout ,
174+ loop = server .loop ,
163175 )
164176 except asyncio .CancelledError :
165177 pass
@@ -186,7 +198,6 @@ async def run_robot(
186198 Debugger .instance ().set_main_thread (threading .current_thread ())
187199 Debugger .instance ().start ()
188200
189- exit_code = - 1
190201 try :
191202 exit_code = robot .run_cli (args , False )
192203 finally :
@@ -206,8 +217,6 @@ async def run_robot(
206217 )
207218
208219 await run_coroutine_from_thread_async (server .protocol .exit , exit_code , loop = server .loop )
209-
210- return exit_code
211220 except asyncio .CancelledError :
212221 pass
213222 except ConnectionError as e :
@@ -229,14 +238,17 @@ async def run_robot(
229238 except asyncio .CancelledError :
230239 pass
231240
241+ return exit_code
242+
232243
233244def get_log_handler (logfile : str ) -> logging .FileHandler :
234245 log_fn = Path (logfile )
235246 roll_over = log_fn .exists ()
236247
237248 handler = RotatingFileHandler (log_fn , backupCount = 5 )
238249 formatter = logging .Formatter (
239- fmt = "[%(levelname)-7s] %(asctime)s (%(name)s) %(message)s" , datefmt = "%Y-%m-%d %H:%M:%S"
250+ fmt = "[%(levelname)-7s] %(asctime)s (%(name)s) %(message)s" ,
251+ datefmt = "%Y-%m-%d %H:%M:%S" ,
240252 )
241253 handler .setFormatter (formatter )
242254
@@ -255,7 +267,7 @@ def error(self, message: str) -> NoReturn:
255267 self .exit (252 , _ ("%(prog)s: error: %(message)s\n " ) % args )
256268
257269
258- def main () -> None :
270+ def main () -> int :
259271 parser = ArgumentParser (
260272 description = "RobotCode Debugger" ,
261273 prog = __package__ ,
@@ -271,7 +283,12 @@ def main() -> None:
271283 help = "Specify alternate bind address. If not specified '127.0.0.1' is used" ,
272284 metavar = "ADDRESS" ,
273285 )
274- parser .add_argument ("-w" , "--wait-for-client" , action = "store_true" , help = "waits for an debug client to connect" )
286+ parser .add_argument (
287+ "-w" ,
288+ "--wait-for-client" ,
289+ action = "store_true" ,
290+ help = "waits for an debug client to connect" ,
291+ )
275292 parser .add_argument (
276293 "-t" ,
277294 "--wait-for-client-timeout" ,
@@ -291,37 +308,83 @@ def main() -> None:
291308 parser .add_argument ("--log" , action = "store_true" , help = "enable logging" )
292309 parser .add_argument ("--log-debugger" , action = "store_true" , help = "show debugger log messages" )
293310 parser .add_argument ("-n" , "--no-debug" , action = "store_true" , help = "disable debugging" )
294- parser .add_argument ("--debug-asyncio" , action = "store_true" , help = "enable async io debugging messages" )
311+ parser .add_argument (
312+ "--debug-asyncio" ,
313+ action = "store_true" ,
314+ help = "enable async io debugging messages" ,
315+ )
295316 parser .add_argument ("--log-asyncio" , action = "store_true" , help = "show asyncio log messages" )
296- parser .add_argument ("--log-config" , default = None , help = "reads logging configuration from file" , metavar = "FILE" )
317+ parser .add_argument (
318+ "--log-config" ,
319+ default = None ,
320+ help = "reads logging configuration from file" ,
321+ metavar = "FILE" ,
322+ )
297323 parser .add_argument ("--log-file" , default = None , help = "enables logging to file" , metavar = "FILE" )
298- parser .add_argument ("--log-level" , default = "WARNING" , help = "sets the overall log level" , metavar = "LEVEL" )
299- parser .add_argument ("--call-tracing" , action = "store_true" , help = "enables log tracing of method calls" )
300324 parser .add_argument (
301- "--call-tracing-default-level" , default = "TRACE" , help = "sets the default level for call tracing" , metavar = "LEVEL"
325+ "--log-level" ,
326+ default = "WARNING" ,
327+ help = "sets the overall log level" ,
328+ metavar = "LEVEL" ,
329+ )
330+ parser .add_argument (
331+ "--call-tracing" ,
332+ action = "store_true" ,
333+ help = "enables log tracing of method calls" ,
334+ )
335+ parser .add_argument (
336+ "--call-tracing-default-level" ,
337+ default = "TRACE" ,
338+ help = "sets the default level for call tracing" ,
339+ metavar = "LEVEL" ,
302340 )
303341 parser .add_argument ("-d" , "--debugpy" , action = "store_true" , help = "starts a debugpy session" )
304342 parser .add_argument (
305- "-dp" , "--debugpy-port" , default = 5678 , help = "sets the port for debugpy session" , type = int , metavar = "PORT"
343+ "-dp" ,
344+ "--debugpy-port" ,
345+ default = 5678 ,
346+ help = "sets the port for debugpy session" ,
347+ type = int ,
348+ metavar = "PORT" ,
306349 )
307350 parser .add_argument (
308- "-dw" , "--debugpy-wait-for-client" , action = "store_true" , help = "waits for debugpy client to connect"
351+ "-dw" ,
352+ "--debugpy-wait-for-client" ,
353+ action = "store_true" ,
354+ help = "waits for debugpy client to connect" ,
309355 )
310356 parser .add_argument (
311- "-om" , "--output-messages" , action = "store_true" , help = "Send output messages from robotframework to client."
357+ "-om" ,
358+ "--output-messages" ,
359+ action = "store_true" ,
360+ help = "Send output messages from robotframework to client." ,
312361 )
313362 parser .add_argument (
314- "-ol" , "--output-log" , action = "store_true" , help = "Send log messages from robotframework to client."
363+ "-ol" ,
364+ "--output-log" ,
365+ action = "store_true" ,
366+ help = "Send log messages from robotframework to client." ,
315367 )
316368 parser .add_argument (
317- "-ot" , "--output-timestamps" , action = "store_true" , help = "Include timestamps in log and output messages."
369+ "-ot" ,
370+ "--output-timestamps" ,
371+ action = "store_true" ,
372+ help = "Include timestamps in log and output messages." ,
318373 )
319374 parser .add_argument (
320- "-og" , "--group-output" , action = "store_true" , help = "Fold messages/log from robotframework to client."
375+ "-og" ,
376+ "--group-output" ,
377+ action = "store_true" ,
378+ help = "Fold messages/log from robotframework to client." ,
321379 )
322380 parser .add_argument ("-soe" , "--stop-on-entry" , action = "store_true" , help = "Stops on entry." )
323381
324- parser .add_argument ("--" , help = "RobotFramework arguments. (see robot --help)" , dest = "robot args" , nargs = "*" )
382+ parser .add_argument (
383+ "--" ,
384+ help = "RobotFramework arguments. (see robot --help)" ,
385+ dest = "robot args" ,
386+ nargs = "*" ,
387+ )
325388
326389 sys_args = sys .argv [1 :]
327390
@@ -334,11 +397,11 @@ def main() -> None:
334397
335398 if args .version :
336399 print (__version__ )
337- return
400+ return 251 # exit code for --version
338401
339402 if split_index == - 1 :
340403 parser .print_help ()
341- return
404+ return 251 # exit code for --help
342405
343406 if args .log :
344407 if args .call_tracing :
@@ -379,7 +442,7 @@ def main() -> None:
379442 _logger .info (lambda : f"starting { __package__ } version={ __version__ } " )
380443 _logger .debug (lambda : f"args={ args } " )
381444
382- asyncio .run (
445+ return asyncio .run (
383446 run_robot (
384447 args .port ,
385448 robot_args ,
0 commit comments