diff --git a/src/ahriman/application/ahriman.py b/src/ahriman/application/ahriman.py index 65a27892..ece12a03 100644 --- a/src/ahriman/application/ahriman.py +++ b/src/ahriman/application/ahriman.py @@ -762,6 +762,7 @@ def _set_shell_parser(root: SubParserAction) -> argparse.ArgumentParser: parser = root.add_parser("shell", help="envoke python shell", description="drop into python shell while having created application", formatter_class=_formatter) + parser.add_argument("code", help="instead of dropping into shell, just execute the specified code", nargs="?") parser.add_argument("-v", "--verbose", help=argparse.SUPPRESS, action="store_true") parser.set_defaults(handler=handlers.Shell, lock=None, report=False) return parser diff --git a/src/ahriman/application/handlers/shell.py b/src/ahriman/application/handlers/shell.py index ad27346c..05f069d9 100644 --- a/src/ahriman/application/handlers/shell.py +++ b/src/ahriman/application/handlers/shell.py @@ -56,4 +56,7 @@ class Shell(Handler): # licensed by https://creativecommons.org/licenses/by-sa/3.0 path = Path(sys.prefix) / "share" / "ahriman" / "templates" / "shell" StringPrinter(path.read_text(encoding="utf8")).print(verbose=False) - code.interact(local=locals()) + if args.code is None: + code.interact(local=locals()) + else: + code.InteractiveConsole(locals=locals()).runcode(args.code) diff --git a/tests/ahriman/application/handlers/test_handler_shell.py b/tests/ahriman/application/handlers/test_handler_shell.py index a0a80ab8..d7426a23 100644 --- a/tests/ahriman/application/handlers/test_handler_shell.py +++ b/tests/ahriman/application/handlers/test_handler_shell.py @@ -17,6 +17,7 @@ def _default_args(args: argparse.Namespace) -> argparse.Namespace: Returns: argparse.Namespace: generated arguments for these test cases """ + args.code = None args.verbose = False return args @@ -33,6 +34,19 @@ def test_run(args: argparse.Namespace, configuration: Configuration, mocker: Moc application_mock.assert_called_once_with(local=pytest.helpers.anyvar(int)) +def test_run_eval(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None: + """ + must run command + """ + args = _default_args(args) + args.code = """print("hello world")""" + mocker.patch("ahriman.models.repository_paths.RepositoryPaths.tree_create") + application_mock = mocker.patch("code.InteractiveConsole.runcode") + + Shell.run(args, "x86_64", configuration, report=False, unsafe=False) + application_mock.assert_called_once_with(args.code) + + def test_run_verbose(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None: """ must run command with verbose option