@@ -1410,29 +1410,38 @@ def build_pipe_stdout(pipe_parts: list[str]) -> Iterator[TextIO]:
14101410 yield pipe_stdin
14111411
14121412
1413- def open_shell (targets : list [str | pathlib .Path ], python : bool , registry : bool ) -> None :
1413+ def open_shell (targets : list [str | pathlib .Path ], python : bool , registry : bool , commands : list [ str ] | None ) -> None :
14141414 """Helper method for starting a regular, Python or registry shell for one or multiple targets."""
14151415 targets = list (Target .open_all (targets ))
14161416
14171417 if python :
1418- python_shell (targets )
1418+ python_shell (targets , commands = commands )
14191419 else :
14201420 cli_cls = RegistryCli if registry else TargetCli
1421- target_shell (targets , cli_cls = cli_cls )
1421+ target_shell (targets , cli_cls = cli_cls , commands = commands )
14221422
14231423
1424- def target_shell (targets : list [Target ], cli_cls : type [TargetCmd ]) -> None :
1424+ def target_shell (targets : list [Target ], cli_cls : type [TargetCmd ], commands : list [ str ] | None ) -> None :
14251425 """Helper method for starting a :class:`TargetCli` or :class:`TargetHubCli` for one or multiple targets."""
14261426 if cli := create_cli (targets , cli_cls ):
1427+ if commands is not None :
1428+ for command in commands :
1429+ cli .onecmd (command )
1430+ return
14271431 run_cli (cli )
14281432
14291433
1430- def python_shell (targets : list [Target ]) -> None :
1434+ def python_shell (targets : list [Target ], commands : list [ str ] | None ) -> None :
14311435 """Helper method for starting a (I)Python shell with multiple targets."""
14321436 banner = "Loaded targets in 'targets' variable. First target is in 't'."
14331437 ns = {"targets" : targets , "t" : targets [0 ]}
14341438
14351439 try :
1440+ if commands is not None :
1441+ for command in commands :
1442+ eval (command , ns )
1443+ return
1444+
14361445 import IPython
14371446
14381447 IPython .embed (header = banner , user_ns = ns , colors = "linux" )
@@ -1512,7 +1521,7 @@ def main() -> None:
15121521 default = None ,
15131522 help = "select a specific loader (i.e. vmx, raw)" ,
15141523 )
1515-
1524+ parser . add_argument ( "-c" , "--commands" , action = "store" , nargs = "*" , help = "commands to execute" )
15161525 configure_generic_arguments (parser )
15171526 args , rest = parser .parse_known_args ()
15181527 args .targets = args_to_uri (args .targets , args .loader , rest ) if args .loader else args .targets
@@ -1537,7 +1546,7 @@ def main() -> None:
15371546 )
15381547
15391548 try :
1540- open_shell (args .targets , args .python , args .registry )
1549+ open_shell (args .targets , args .python , args .registry , args . commands )
15411550 except TargetError as e :
15421551 log .error (e )
15431552 log .debug ("" , exc_info = e )
0 commit comments