From 316392180a354add887902db66df2e1c6d51b99b Mon Sep 17 00:00:00 2001 From: Brian Pugh Date: Mon, 30 Dec 2024 21:14:12 -0500 Subject: [PATCH 1/2] Add App.update method. --- cyclopts/core.py | 10 ++++++++++ docs/source/api.rst | 2 +- tests/test_app_utils.py | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/cyclopts/core.py b/cyclopts/core.py index 93f9e441..185fbf5c 100644 --- a/cyclopts/core.py +++ b/cyclopts/core.py @@ -1349,6 +1349,16 @@ def default_dispatcher(command, bound, _): except Exception: print(traceback.format_exc()) + def update(self, app: "App"): + """Copy over all commands from another :class:`App`. + + Parameters + ---------- + app: cyclopts.App + All commands from this application will be copied over. + """ + self._commands.update(app._commands) + def __repr__(self): """Only shows non-default values.""" non_defaults = {} diff --git a/docs/source/api.rst b/docs/source/api.rst index 1712b38d..fa059d13 100644 --- a/docs/source/api.rst +++ b/docs/source/api.rst @@ -5,7 +5,7 @@ API === .. autoclass:: cyclopts.App - :members: default, command, version_print, help_print, interactive_shell, parse_commands, parse_known_args, parse_args, assemble_argument_collection + :members: default, command, version_print, help_print, interactive_shell, parse_commands, parse_known_args, parse_args, assemble_argument_collection, update :special-members: __call__, __getitem__, __iter__ Cyclopts Application. diff --git a/tests/test_app_utils.py b/tests/test_app_utils.py index a975d550..85250baf 100644 --- a/tests/test_app_utils.py +++ b/tests/test_app_utils.py @@ -1,3 +1,6 @@ +from cyclopts import App + + def test_app_iter(app): """Like a dictionary, __iter__ of an App should yield keys (command names).""" @@ -31,3 +34,20 @@ def fizz(): actual = list(app.meta) assert actual == ["--help", "-h", "--version", "fizz", "foo", "bar"] + + +def test_app_update(): + app1 = App() + app2 = App() + + @app1.command + def foo(): + pass + + @app2.command + def bar(): + pass + + app1.update(app2) + + assert list(app1) == ["--help", "-h", "--version", "foo", "bar"] From e24be403fc39a7898fbaa0e9d82bacae6a81bdc6 Mon Sep 17 00:00:00 2001 From: Brian Pugh Date: Mon, 30 Dec 2024 21:16:36 -0500 Subject: [PATCH 2/2] clarify that meta-app commands will not be copied over. --- cyclopts/core.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cyclopts/core.py b/cyclopts/core.py index 185fbf5c..5db645a3 100644 --- a/cyclopts/core.py +++ b/cyclopts/core.py @@ -1352,6 +1352,8 @@ def default_dispatcher(command, bound, _): def update(self, app: "App"): """Copy over all commands from another :class:`App`. + Commands from the meta app will **not** be copied over. + Parameters ---------- app: cyclopts.App