Skip to content

Commit 79bf9fb

Browse files
committed
Update docs
1 parent 5e24aec commit 79bf9fb

1 file changed

Lines changed: 28 additions & 26 deletions

File tree

docs/tutorial/splitting-apps.md

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,54 @@ mycli/
1515
├── users/
1616
│ ├── __init__.py
1717
│ ├── add.py
18-
│ ├── delete.py
18+
│ └── delete.py
19+
└── version.py
1920
```
2021

22+
This application will have the following commands:
23+
24+
- `users add`
25+
- `users delete`
26+
- `version`
27+
2128
## Implementation
2229

23-
### Main Application (`main.py`)
30+
### Version Module (`version.py`)
31+
32+
Let's start by creating a simple module that prints the version of the application.
2433

2534
```python
2635
import typer
27-
from .users import app as users_app
2836

2937
app = typer.Typer()
3038

31-
app.add_typer(users_app, name="users")
39+
@app.command()
40+
def version():
41+
typer.echo("MyCLI version 1.0.0")
42+
```
3243

33-
@app.callback()
34-
def callback():
35-
"""
36-
Awesome CLI application that does everything
37-
"""
38-
pass
44+
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`.
3945

40-
if __name__ == "__main__":
41-
app()
42-
```
46+
### Main Module (`main.py`)
4347

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

4650
```python
4751
import typer
4852

49-
from .add import app as add_app
50-
from .delete import app as delete_app
53+
from version import app as version_app
54+
from users import app as users_app
5155

5256
app = typer.Typer()
53-
app.add_typer(add_app)
54-
app.add_typer(delete_app)
57+
58+
app.add_typer(version_app)
59+
app.add_typer(users_app, name="users")
5560
```
5661

62+
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.
63+
64+
Let's now create the `users` module with the `add` and `delete` commands.
65+
5766
### Users Add Command (`users/add.py`)
5867

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

81-
## Running the Application
82-
83-
To run the application, use the following command:
84-
85-
```bash
86-
$ python mycli/main.py users add John
87-
Adding user: John
88-
```
90+
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 commit comments

Comments
 (0)