Skip to content

Commit 8668aba

Browse files
committed
📝 Update docs for one-file-per-command
1 parent 909c036 commit 8668aba

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

docs/tutorial/one-file-per-command.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# One File Per Command
22

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.
44

55
This tutorial will show you how to use `add_typer` to create sub commands and organize your commands in multiple files.
66

@@ -25,23 +25,27 @@ mycli/
2525
└── version.py
2626
```
2727

28-
`mycli` will be our <abbr title="a directory with an __init__.py file">package</abbr>, and it will contain the following modules:
28+
`mycli` will be our <abbr title="a directory with an __init__.py file, it can be imported">package</abbr>, and it will contain the following modules:
2929

3030
- `main.py`: The main <abbr title="a Python file that can be imported">module</abbr> that will import the `version` and `users` modules.
3131
- `version.py`: A <abbr title="a Python file that can be imported">module</abbr> that will contain the `version` command.
32-
- `users/`: A <abbr title="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 <abbr title="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.
3333

3434
## Implementation
3535

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.
3739

3840
### Version Module (`version.py`)
3941

4042
Let's start by creating the `version` module. This module will contain the `version` command.
4143

4244
{* docs_src/one_file_per_command/version.py *}
4345

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()`.
4549

4650
Let's see that next!
4751

@@ -51,15 +55,15 @@ The main module will be the entry point of the application. It will import the v
5155

5256
/// tip
5357

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.
5559

5660
///
5761

5862
{* docs_src/one_file_per_command/main.py hl[8,9] *}
5963

60-
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()`.
6165

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.
6367

6468
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.
6569

@@ -97,7 +101,7 @@ Similarly to the `version` module, we create a new `Typer` app instance for the
97101

98102
## Running the Application
99103

100-
Now we are ready to run the application!
104+
Now we are ready to run the application! 😎
101105

102106
To run the application, you can execute it as a Python module:
103107

@@ -127,3 +131,14 @@ My CLI Version 1.0
127131
$ mycli users add Camila
128132

129133
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

Comments
 (0)