Skip to content

Commit 01a7007

Browse files
committed
Separate reusable CLI configuration from executable entry points
The intent of `greetings.cli` is likely to preconfigure the `app` object, exportable for reuse by others. Allowing `greetings.cli` to be runnable as a module didn't make much sense, because the `greetings` module itself is already runnable. Exporting the `main()` function and referencing it from the `pyproject.toml` is likely more common and concise than relying on the fact that the `app` object itself is callable.
1 parent a9d99f0 commit 01a7007

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

source/guides/creating-command-line-tools.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,12 @@ in :file:`cli.py`:
6969
7070
from .greet import greet
7171
72-
7372
app = typer.Typer()
7473
app.command()(greet)
7574
7675
77-
if __name__ == "__main__":
78-
app()
76+
def main():
77+
return app()
7978
8079
The command-line interface is built with typer_, an easy-to-use CLI parser based on Python type hints. It provides
8180
auto-completion and nicely styled command-line help out of the box. Another option would be :py:mod:`argparse`,
@@ -113,7 +112,7 @@ For the project to be recognised as a command-line tool, additionally a ``consol
113112
.. code-block:: toml
114113
115114
[project.scripts]
116-
greet = "greetings.cli:app"
115+
greet = "greetings.cli:main"
117116
118117
Now, the project's source tree is ready to be transformed into a :term:`distribution package <Distribution Package>`,
119118
which makes it installable.
@@ -162,7 +161,7 @@ The same can be defined as follows in :file:`pyproject.toml`:
162161
.. code-block:: toml
163162
164163
[project.entry-points."pipx.run"]
165-
greetings = "greetings.cli:app"
164+
greetings = "greetings.cli:main"
166165
167166
168167
Thanks to this entry point (which *must* match the package name), ``pipx`` will pick up the executable script as the

0 commit comments

Comments
 (0)