命令行界面
内容
命令行界面¶
Dask 提供了一个 dask 可执行文件作为命令行接口。Dask 的 CLI 是 设计为可扩展的,允许 Dask 生态系统中的其他项目(如 distributed)添加子命令。
扩展 Dask CLI¶
备注
本节面向希望将其库与 dask CLI 集成的库作者。
Third party packages can extend the dask command line tool via
entry points and Click. Dask will discover click.Command and
click.Group objects registered as entry points under the
dask_cli namespace. Below you’ll find two examples which augment
the dask CLI by adding a dask_cli entry point to a project.
Click 为编写命令提供了出色的文档;更多关于入口点的文档可以在以下网址找到:
示例:PEP-621¶
自从 PEP-621 以来,如果开始一个新项目,向你的 Python 项目添加入口点的标准方法是使用 pyproject.toml 文件中的 [project.entry-points] 表。这种方法应该被任何兼容 PEP-621 的 project 配置的 Python 构建系统所识别。Hatch、Flit 和 setuptools_(版本 61.0.0 或更高)是三个兼容 PEP-621 并使用 [project.entry-points] 的构建系统示例。
例如,如果你的项目名为 mypackage,并且在 mypackage 命名空间下包含一个 cli.py 模块,其内容如下:
# in the file mypackage/cli.py
import click
@click.command(name="mycommand")
@click.argument("name", type=str)
@click.option("-c", "--count", default=1)
def main(name, count):
for _ in range(count):
click.echo(f"hello {name} from mycommand!")
你可以通过在 pyproject.toml 中添加以下内容来创建一个Dask可以发现的入口点:
[project.entry-points."dask_cli"]
mycommand = "mypackage.cli:main"
安装 mypackage 后,mycommand 子命令应该可以在 dask CLI 中使用:
$ dask mycommand world
hello world from mycommand!
$ dask mycommand user -c 3
hello user from mycommand!
hello user from mycommand!
hello user from mycommand!
示例:setup.cfg 和 setup.py¶
备注
如果你正在开始一个新项目,Python 打包权威机构 (PyPA) 的建议是使用 PEP-621,这些 setuptools 指令是为现有项目提供的。
如果你的项目已经使用了 setuptools 并带有 setup.cfg 文件和/或 setup.py 文件,我们可以为上一节中介绍的相同 mycommand.cli:main 函数创建一个入口点。如果使用 setup.cfg,可以通过向文件中添加以下块来注册入口点:
[options.entry_points]
dask_cli =
mycommand = mypackage.cli:main
或者可以在 setup.py 中直接注册入口点:
from setuptools import setup
setup(
...
entry_points="""
[dask_cli]
mycommand=mypackage.cli:main
""",
)