File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Typer Multi-File Applications
2+
3+ When your CLI application grows, you can split it into multiple files and modules. This pattern helps maintain clean and organized code structure.
4+
5+ In this tutorial, you will learn how to create a multi-file Typer application.
6+
7+ ## Basic Structure
8+
9+ Here is a basic structure for a multi-file Typer application:
10+
11+ ``` text
12+ mycli/
13+ ├── __init__.py
14+ ├── main.py
15+ ├── users/
16+ │ ├── __init__.py
17+ │ ├── add.py
18+ │ ├── delete.py
19+ ```
20+
21+ ## Implementation
22+
23+ ### Main Application (` main.py ` )
24+
25+ ``` python
26+ import typer
27+ from .users import app as users_app
28+
29+ app = typer.Typer()
30+
31+ app.add_typer(users_app, name = " users" )
32+
33+ @app.callback ()
34+ def callback ():
35+ """
36+ Awesome CLI application that does everything
37+ """
38+ pass
39+
40+ if __name__ == " __main__" :
41+ app()
42+ ```
43+
44+ ### Users Module (` users/__init__.py ` )
45+
46+ ``` python
47+ import typer
48+
49+ from .add import app as add_app
50+ from .delete import app as delete_app
51+
52+ app = typer.Typer()
53+ app.add_typer(add_app)
54+ app.add_typer(delete_app)
55+ ```
56+
57+ ### Users Add Command (` users/add.py ` )
58+
59+ ``` python
60+ import typer
61+
62+ app = typer.Typer()
63+
64+ @app.command ()
65+ def add (name : str ):
66+ typer.echo(f " Adding user: { name} " )
67+ ```
68+
69+ ### Users Delete Command (` users/delete.py ` )
70+
71+ ``` python
72+ import typer
73+
74+ app = typer.Typer()
75+
76+ @app.command ()
77+ def delete (name : str ):
78+ typer.echo(f " Deleting user: { name
79+ ```
80+
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+ ```
You can’t perform that action at this time.
0 commit comments