Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick91 committed Dec 2, 2024
1 parent c699873 commit 831ab7f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
60 changes: 48 additions & 12 deletions docs/tutorial/splitting-apps.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# Typer Multi-File Applications
# Multi-File Applications

When your CLI application grows, you can split it into multiple files and modules. This pattern helps maintain clean and organized code structure.

In this tutorial, you will learn how to create a multi-file Typer application.
This tutorial will show you how to use `add_typer` to create sub commands and organize your commands in multiple files.

## Basic Structure
We will create a simple CLI with the following commands:

Here is a basic structure for a multi-file Typer application:
- `version`
- `users add NAME`
- `users delete NAME`

## CLI structure

Here is the structure we'll be working with:

```text
mycli/
Expand All @@ -19,22 +25,26 @@ mycli/
└── version.py
```

This application will have the following commands:
`mycli` will be our package, and it will contain the following modules:

- `users add`
- `users delete`
- `version`
- `main.py`: The main module that will import the `version` and `users` modules.
- `version.py`: A module that will contain the `version` command.
- `users/`: A package that will contain the `add` and `delete` commands.

## Implementation

Let's start implementing our CLI! We'll create the `version` module, the `main` module, and the `users` package.

### Version Module (`version.py`)

Let's start by creating a simple module that prints the version of the application.
Let's start by creating the `version` module. This module will contain the `version` command.

{* docs_src/splitting_apps/version.py hl[3,6,7,8] *}
{* docs_src/splitting_apps/version.py *}

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`.

Let's see that next!

### 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. We'll see how to implement the user module in the next section.
Expand All @@ -49,12 +59,38 @@ Let's now create the `users` module with the `add` and `delete` commands.

{* docs_src/splitting_apps/users/add.py *}

Like the `version` module, we create a new Typer app instance for the `users/add` command. This allows us to include the `add` command in the users app.

### Users Delete Command (`users/delete.py`)

{* docs_src/splitting_apps/users/delete.py *}

### Users' app
And once again, we create a new Typer app instance for the `users/delete` command. This allows us to include the `delete` command in the users app.

### Users' app (`users/__init__.py`)

Finally, we need to create an `__init__.py` file in the `users` directory to define the `users` app.

{* 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.
Similarly 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.

## Running the Application

Now we are ready to run the application!

To run the application, execute the `main.py` file:

<div class="termy">

```console
$ python main.py version

My CLI Version 1.0

$ python main.py users add Camila

Adding user: Camila
```

</div>
4 changes: 4 additions & 0 deletions docs_src/splitting_apps/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@

app.add_typer(version_app)
app.add_typer(users_app, name="users")


if __name__ == "__main__":
app()
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ nav:
- tutorial/using-click.md
- tutorial/package.md
- tutorial/exceptions.md
- tutorial/splitting-apps.md
- tutorial/typer-command.md
- Resources:
- resources/index.md
Expand Down

0 comments on commit 831ab7f

Please sign in to comment.