Skip to content

Commit

Permalink
Merge pull request #281 from BrianPugh/app-update
Browse files Browse the repository at this point in the history
Add App.update method.
  • Loading branch information
BrianPugh authored Dec 31, 2024
2 parents 34717e3 + e24be40 commit b90abad
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
12 changes: 12 additions & 0 deletions cyclopts/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,18 @@ def default_dispatcher(command, bound, _):
except Exception:
print(traceback.format_exc())

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
All commands from this application will be copied over.
"""
self._commands.update(app._commands)

def __repr__(self):
"""Only shows non-default values."""
non_defaults = {}
Expand Down
2 changes: 1 addition & 1 deletion docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions tests/test_app_utils.py
Original file line number Diff line number Diff line change
@@ -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)."""

Expand Down Expand Up @@ -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"]

0 comments on commit b90abad

Please sign in to comment.