You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tutorial/splitting-apps.md
+28-26Lines changed: 28 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,45 +15,54 @@ mycli/
15
15
├── users/
16
16
│ ├── __init__.py
17
17
│ ├── add.py
18
-
│ ├── delete.py
18
+
│ └── delete.py
19
+
└── version.py
19
20
```
20
21
22
+
This application will have the following commands:
23
+
24
+
-`users add`
25
+
-`users delete`
26
+
-`version`
27
+
21
28
## Implementation
22
29
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.
24
33
25
34
```python
26
35
import typer
27
-
from .users import app as users_app
28
36
29
37
app = typer.Typer()
30
38
31
-
app.add_typer(users_app, name="users")
39
+
@app.command()
40
+
defversion():
41
+
typer.echo("MyCLI version 1.0.0")
42
+
```
32
43
33
-
@app.callback()
34
-
defcallback():
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`.
39
45
40
-
if__name__=="__main__":
41
-
app()
42
-
```
46
+
### Main Module (`main.py`)
43
47
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.
45
49
46
50
```python
47
51
import typer
48
52
49
-
from.addimport app asadd_app
50
-
from.deleteimport app asdelete_app
53
+
fromversionimport app asversion_app
54
+
fromusersimport app asusers_app
51
55
52
56
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")
55
60
```
56
61
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
+
57
66
### Users Add Command (`users/add.py`)
58
67
59
68
```python
@@ -78,11 +87,4 @@ def delete(name: str):
78
87
typer.echo(f"Deleting user: {name
79
88
```
80
89
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