Skip to content

Commit 9280c79

Browse files
authored
Merge pull request #2061 from anpol/cli-tools-guide-clarify
Clarifications to example code in the command-line tools guide
2 parents d380ef6 + 8358a44 commit 9280c79

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ named after the main module:
3535

3636
.. code-block:: python
3737
38-
import typer
38+
from typer import Argument, Option
3939
from typing_extensions import Annotated
4040
4141
4242
def greet(
43-
name: Annotated[str, typer.Argument(help="The (last, if --title is given) name of the person to greet")] = "",
44-
title: Annotated[str, typer.Option(help="The preferred title of the person to greet")] = "",
45-
doctor: Annotated[bool, typer.Option(help="Whether the person is a doctor (MD or PhD)")] = False,
46-
count: Annotated[int, typer.Option(help="Number of times to greet the person")] = 1
43+
name: Annotated[str, Argument(help="The (last, if --title is given) name of the person to greet")] = "",
44+
title: Annotated[str, Option(help="The preferred title of the person to greet")] = "",
45+
doctor: Annotated[bool, Option(help="Whether the person is a doctor (MD or PhD)")] = False,
46+
count: Annotated[int, Option(help="Number of times to greet the person")] = 1
4747
):
4848
greeting = "Greetings, "
4949
if doctor and not title:
@@ -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`,
@@ -91,9 +90,12 @@ so initialize the command-line interface here:
9190

9291
.. code-block:: python
9392
94-
if __name__ == "__main__":
95-
from greetings.cli import app
96-
app()
93+
import sys
94+
95+
if __name__ == "__main__":
96+
from greetings.cli import main
97+
98+
sys.exit(main())
9799
98100
.. note::
99101

@@ -113,7 +115,7 @@ For the project to be recognised as a command-line tool, additionally a ``consol
113115
.. code-block:: toml
114116
115117
[project.scripts]
116-
greet = "greetings.cli:app"
118+
greet = "greetings.cli:main"
117119
118120
Now, the project's source tree is ready to be transformed into a :term:`distribution package <Distribution Package>`,
119121
which makes it installable.
@@ -162,7 +164,7 @@ The same can be defined as follows in :file:`pyproject.toml`:
162164
.. code-block:: toml
163165
164166
[project.entry-points."pipx.run"]
165-
greetings = "greetings.cli:app"
167+
greetings = "greetings.cli:main"
166168
167169
168170
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)