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 5e24aec commit 79bf9fb
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions docs/tutorial/splitting-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,54 @@ mycli/
├── users/
│ ├── __init__.py
│ ├── add.py
│ ├── delete.py
│ └── delete.py
└── version.py
```

This application will have the following commands:

- `users add`
- `users delete`
- `version`

## Implementation

### Main Application (`main.py`)
### Version Module (`version.py`)

Let's start by creating a simple module that prints the version of the application.

```python
import typer
from .users import app as users_app

app = typer.Typer()

app.add_typer(users_app, name="users")
@app.command()
def version():
typer.echo("MyCLI version 1.0.0")
```

@app.callback()
def callback():
"""
Awesome CLI application that does everything
"""
pass
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`.

if __name__ == "__main__":
app()
```
### Main Module (`main.py`)

### Users Module (`users/__init__.py`)
The main module will be the entry point of the application. It will import the version module and the users module.

```python
import typer

from .add import app as add_app
from .delete import app as delete_app
from version import app as version_app
from users import app as users_app

app = typer.Typer()
app.add_typer(add_app)
app.add_typer(delete_app)

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

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.

Let's now create the `users` module with the `add` and `delete` commands.

### Users Add Command (`users/add.py`)

```python
Expand All @@ -78,11 +87,4 @@ def delete(name: str):
typer.echo(f"Deleting user: {name
```

## Running the Application

To run the application, use the following command:

```bash
$ python mycli/main.py users add John
Adding user: John
```
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.

0 comments on commit 79bf9fb

Please sign in to comment.