diff --git a/docs/tutorial/splitting-apps.md b/docs/tutorial/splitting-apps.md index 58d3bf77ca..af2f7498eb 100644 --- a/docs/tutorial/splitting-apps.md +++ b/docs/tutorial/splitting-apps.md @@ -31,33 +31,15 @@ This application will have the following commands: Let's start by creating a simple module that prints the version of the application. -```python -import typer +{* docs_src/splitting_apps/version.py hl[3,6,7,8] *} -app = typer.Typer() - -@app.command() -def version(): - typer.echo("MyCLI version 1.0.0") -``` - -In this example, we are creating a new `Typer` app instance for the `version` module. This is not required for a single-file application but is necessary for a multi-file application, as it will allow us to include this command in the main app using `add_typer`. +In this file we are creating a new Typer app instance for the version command. This is not required in single-file applications, but in the case of multi-file applications it will allow us to include this command in the main application using `add_typer`. ### Main Module (`main.py`) -The main module will be the entry point of the application. It will import the version module and the users module. +The main module will be the entry point of the application. It will import the version module and the users module. We'll see how to implement the user module in the next section. -```python -import typer - -from version import app as version_app -from users import app as users_app - -app = typer.Typer() - -app.add_typer(version_app) -app.add_typer(users_app, name="users") -``` +{* docs_src/splitting_apps/main.py hl[8,9] *} In this module, we import the `version` and `users` modules and add them to the main app using `add_typer`. For the `users` module, we specify the name as `users` to group the commands under the `users` namespace. @@ -65,26 +47,14 @@ Let's now create the `users` module with the `add` and `delete` commands. ### Users Add Command (`users/add.py`) -```python -import typer - -app = typer.Typer() - -@app.command() -def add(name: str): - typer.echo(f"Adding user: {name}") -``` +{* docs_src/splitting_apps/users/add.py *} ### Users Delete Command (`users/delete.py`) -```python -import typer +{* docs_src/splitting_apps/users/delete.py *} -app = typer.Typer() +### Users' app -@app.command() -def delete(name: str): - typer.echo(f"Deleting user: {name -``` +{* docs_src/splitting_apps/users/__init__.py *} Similar to the `version` module, we create a new `Typer` app instance for the `users` module. This allows us to include the `add` and `delete` commands in the users app. diff --git a/docs_src/splitting_apps/__init__.py b/docs_src/splitting_apps/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docs_src/splitting_apps/main.py b/docs_src/splitting_apps/main.py new file mode 100644 index 0000000000..445afdfd74 --- /dev/null +++ b/docs_src/splitting_apps/main.py @@ -0,0 +1,9 @@ +import typer + +from .users import app as users_app +from .version import app as version_app + +app = typer.Typer() + +app.add_typer(version_app) +app.add_typer(users_app, name="users") diff --git a/docs_src/splitting_apps/users/__init__.py b/docs_src/splitting_apps/users/__init__.py new file mode 100644 index 0000000000..ccfbb0775d --- /dev/null +++ b/docs_src/splitting_apps/users/__init__.py @@ -0,0 +1,9 @@ +import typer + +from .add import app as add_app +from .delete import app as delete_app + +app = typer.Typer() + +app.add_typer(add_app) +app.add_typer(delete_app) diff --git a/docs_src/splitting_apps/users/add.py b/docs_src/splitting_apps/users/add.py new file mode 100644 index 0000000000..3d393c06f3 --- /dev/null +++ b/docs_src/splitting_apps/users/add.py @@ -0,0 +1,8 @@ +import typer + +app = typer.Typer() + + +@app.command() +def add(name: str): + typer.echo(f"Adding user: {name}") diff --git a/docs_src/splitting_apps/users/delete.py b/docs_src/splitting_apps/users/delete.py new file mode 100644 index 0000000000..7cd73db507 --- /dev/null +++ b/docs_src/splitting_apps/users/delete.py @@ -0,0 +1,8 @@ +import typer + +app = typer.Typer() + + +@app.command() +def delete(name: str): + typer.echo(f"Deleting user: {name}") diff --git a/docs_src/splitting_apps/version.py b/docs_src/splitting_apps/version.py new file mode 100644 index 0000000000..638b8ee219 --- /dev/null +++ b/docs_src/splitting_apps/version.py @@ -0,0 +1,8 @@ +import typer + +app = typer.Typer() + + +@app.command() +def version(): + typer.echo("My CLI Version 1.0") diff --git a/tests/test_tutorial/test_splitting_apps/__init__.py b/tests/test_tutorial/test_splitting_apps/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/test_tutorial/test_splitting_apps/test_tutorial.py b/tests/test_tutorial/test_splitting_apps/test_tutorial.py new file mode 100644 index 0000000000..44df21983a --- /dev/null +++ b/tests/test_tutorial/test_splitting_apps/test_tutorial.py @@ -0,0 +1,48 @@ +import os +import subprocess +import sys + +from typer.testing import CliRunner + +from docs_src.splitting_apps import main as mod + +runner = CliRunner() + + +def test_help(): + result = runner.invoke(mod.app, ["--help"]) + + assert result.exit_code == 0 + + assert "version" in result.output + assert "users" in result.output + + +def test_version(): + result = runner.invoke(mod.app, ["version"]) + + assert result.exit_code == 0 + assert "My CLI Version 1.0" in result.output + + +def test_users_help(): + result = runner.invoke(mod.app, ["users", "--help"]) + + assert result.exit_code == 0 + + assert "add" in result.output + assert "delete" in result.output + + +def test_add_user(): + result = runner.invoke(mod.app, ["users", "add", "Camila"]) + + assert result.exit_code == 0 + assert "Adding user: Camila" in result.output + + +def test_delete_user(): + result = runner.invoke(mod.app, ["users", "delete", "Camila"]) + + assert result.exit_code == 0 + assert "Deleting user: Camila" in result.output