Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions source/guides/creating-command-line-tools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ Creating the package
First of all, create a source tree for the :term:`project <Project>`. For the sake of an example, we'll
build a simple tool outputting a greeting (a string) for a person based on arguments given on the command-line.

.. todo:: Advise on the optimal structure of a Python package in another guide or discussion and link to it here.

This project will adhere to :ref:`src-layout <src-layout-vs-flat-layout>` and in the end be alike this file tree,
with the top-level folder and package name ``greetings``:

Expand Down Expand Up @@ -95,6 +93,16 @@ so initialize the command-line interface here:
from greetings.cli import app
app()

In this layout, importing ``greetings`` runs :file:`__init__.py`, which can
stay empty. Running ``python -m greetings`` then runs :file:`__main__.py`.
Keep that entry point small and put the reusable CLI callable in an importable
module such as :file:`cli.py`. Both :file:`__main__.py` and the installed
script declared in ``[project.scripts]`` can call the same object. Code that
needs that callable can import it from ``greetings.cli``.
See the `Python documentation for __main__`_ for more details.

.. _Python documentation for __main__: https://docs.python.org/3/library/__main__.html

.. note::

In order to enable calling the command-line interface directly from the :term:`source tree <Project Source Tree>`,
Expand Down
Loading