Debian Codegraph @

docs/source/cli.rst

raw
1 Command Line Interface
2 ======================
3
4 Dask provides a ``dask`` executable for a command line interface.
5 Dask's CLI is :ref:`designed to be extensible <extending-cli>` allowing
6 other projects in the Dask ecosystem (such as ``distributed``) to
7 add subcommands.
8
9 Built-in commands
10 -----------------
11
12 ``dask`` comes with the following commands.
13
14 .. click:: dask.cli:info
15 :prog: dask info
16 :show-nested:
17
18 .. click:: dask.cli:docs
19 :prog: dask docs
20 :show-nested:
21
22
23 .. _extending-cli:
24
25 Extending the Dask CLI
26 ----------------------
27
28 .. note::
29
30 This section is intended for library authors who want to
31 integrate their library with the ``dask`` CLI.
32
33 Third party packages can extend the ``dask`` command line tool via
34 entry points and Click_. Dask will discover :obj:`click.Command` and
35 :obj:`click.Group` objects registered as entry points under the
36 ``dask_cli`` namespace. Below you'll find two examples which augment
37 the ``dask`` CLI by adding a ``dask_cli`` entry point to a project.
38
39 Click provides great documentation for writing commands; more
40 documentation on entry points can be found at:
41
42 - `The python packaging documentation
43 <https://setuptools.pypa.io/en/latest/userguide/entry_point.html>`_.
44 - `The setuptools user guide
45 <https://setuptools.pypa.io/en/latest/userguide/entry_point.html>`_.
46 - `The poetry plugins documentation
47 <https://python-poetry.org/docs/pyproject/#plugins>`_.
48
49 Example: PEP-621
50 ~~~~~~~~~~~~~~~~
51
52 Since `PEP-621 <https://peps.python.org/pep-0621/>`_, if starting a
53 new project, the canonical way to add an entry point to your Python
54 project is to use the ``[project.entry-points]`` table in the
55 ``pyproject.toml`` file. This method should be picked up by any Python
56 build system that is compatible with ``PEP-621``'s ``project``
57 configuration. Hatch_, Flit_, and setuptools_ (version 61.0.0 or
58 later) are three example build systems which are PEP-621 compatible
59 and use ``[project.entry-points]``.
60
61 For example, if your project is called ``mypackage``, and it contains
62 a ``cli.py`` module under the ``mypackage`` namespace with the
63 following contents:
64
65 .. code-block::
66
67 # in the file mypackage/cli.py
68 import click
69
70 @click.command(name="mycommand")
71 @click.argument("name", type=str)
72 @click.option("-c", "--count", default=1)
73 def main(name, count):
74 for _ in range(count):
75 click.echo(f"hello {name} from mycommand!")
76
77 You can create an entry point that will be discovered by Dask by
78 adding to ``pyproject.toml``:
79
80 .. code-block:: toml
81
82 [project.entry-points."dask_cli"]
83 mycommand = "mypackage.cli:main"
84
85 After installing ``mypackage``, the ``mycommand`` subcommand should be
86 available to the ``dask`` CLI:
87
88 .. code-block:: shell
89
90 $ dask mycommand world
91 hello world from mycommand!
92
93 $ dask mycommand user -c 3
94 hello user from mycommand!
95 hello user from mycommand!
96 hello user from mycommand!
97
98 Example: setup.cfg and setup.py
99 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100
101 .. note::
102
103 If you are starting a new project the recommendation from the
104 Python Packaging Authority (PyPA_) is to use PEP-621, these
105 setuptools instructions are provided for existing projects.
106
107 If your project already uses ``setuptools`` with a ``setup.cfg`` file
108 and/or a ``setup.py`` file, we can create an entry point for the same
109 ``mycommand.cli:main`` function introduced in the previous section. If
110 using ``setup.cfg``, the entry point can be registered by adding the
111 following block to the file:
112
113 .. code-block:: ini
114
115 [options.entry_points]
116 dask_cli =
117 mycommand = mypackage.cli:main
118
119 Or the entry point can be registered directly in ``setup.py`` with:
120
121 .. code-block:: python
122
123 from setuptools import setup
124
125 setup(
126 ...
127 entry_points="""
128 [dask_cli]
129 mycommand=mypackage.cli:main
130 """,
131 )
132
133 .. _Click: https://click.palletsprojects.com/
134 .. _Hatch: https://github.com/pypa/hatch
135 .. _setuptools: https://setuptools.pypa.io/en/latest/index.html
136 .. _PyPA: https://pypa.io/
137 .. _Flit: https://flit.pypa.io/