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/one-file-per-command.md
+24-9Lines changed: 24 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# One File Per Command
2
2
3
-
When your CLI application grows, you can split it into multiple files and modules. This pattern helps maintain a clean and organized code structure.
3
+
When your CLI application grows, you can split it into multiple files and modules. This pattern helps maintain a clean and organized code structure. ✨
4
4
5
5
This tutorial will show you how to use `add_typer` to create sub commands and organize your commands in multiple files.
6
6
@@ -25,23 +25,27 @@ mycli/
25
25
└── version.py
26
26
```
27
27
28
-
`mycli` will be our <abbrtitle="a directory with an __init__.py file">package</abbr>, and it will contain the following modules:
28
+
`mycli` will be our <abbrtitle="a directory with an __init__.py file, it can be imported">package</abbr>, and it will contain the following modules:
29
29
30
30
-`main.py`: The main <abbrtitle="a Python file that can be imported">module</abbr> that will import the `version` and `users` modules.
31
31
-`version.py`: A <abbrtitle="a Python file that can be imported">module</abbr> that will contain the `version` command.
32
-
-`users/`: A <abbrtitle="another directory with an __init__.py file">package</abbr> (inside of our `mycli` package) that will contain the `add` and `delete` commands.
32
+
-`users/`: A <abbrtitle="another directory with an __init__.py file, it can also be imported">package</abbr> (inside of our `mycli` package) that will contain the `add` and `delete` commands.
33
33
34
34
## Implementation
35
35
36
-
Let's start implementing our CLI! We'll create the `version` module, the `main` module, and the `users` package.
36
+
Let's start implementing our CLI! 🚀
37
+
38
+
We'll create the `version` module, the `main` module, and the `users` package.
37
39
38
40
### Version Module (`version.py`)
39
41
40
42
Let's start by creating the `version` module. This module will contain the `version` command.
41
43
42
44
{* docs_src/one_file_per_command/version.py *}
43
45
44
-
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`.
46
+
In this file we are creating a new Typer app instance for the `version` command.
47
+
48
+
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 `app.add_typer()`.
45
49
46
50
Let's see that next!
47
51
@@ -51,15 +55,15 @@ The main module will be the entry point of the application. It will import the v
51
55
52
56
/// tip
53
57
54
-
We'll see how to implement the user module in the next section.
58
+
We'll see how to implement the users module in the next section.
In this module, we import the `version` and `users` modules and add them to the main app using `add_typer`.
64
+
In this module, we import the `version` and `users` modules and add them to the main app using `app.add_typer()`.
61
65
62
-
For the `users` module, we specify the name as `users` to group the commands under the `users` sub-command.
66
+
For the `users` module, we specify the name as `"users"` to group the commands under the `users` sub-command.
63
67
64
68
Notice that we didn't add a name for the `version_app` Typer app. Because of this, Typer will add the commands (just one in this case) declared in the `version_app` directly at the top level. So, there will be a top-level `version` sub-command.
65
69
@@ -97,7 +101,7 @@ Similarly to the `version` module, we create a new `Typer` app instance for the
97
101
98
102
## Running the Application
99
103
100
-
Now we are ready to run the application!
104
+
Now we are ready to run the application! 😎
101
105
102
106
To run the application, you can execute it as a Python module:
103
107
@@ -127,3 +131,14 @@ My CLI Version 1.0
127
131
$ mycli users add Camila
128
132
129
133
Adding user: Camila
134
+
```
135
+
136
+
</div>
137
+
138
+
## Callbacks
139
+
140
+
Have in mind that if you include a sub-app with `app.add_typer()`**without a name**, the commands will be added to the top level, so **only the top level callback** (if there's any) will be used, the one declared in the main app.
141
+
142
+
If you **want to use a callback** for a sub-app, you need to include the sub-app **with a name**, which creates a sub-command grouping the commands in that sub-app. 🤓
143
+
144
+
In the example above, if the `users` sub-app had a callback, it would be used. But if the `version` sub-app had a callback, it would not be used, because the `version` sub-app was included without a name.
0 commit comments