如何为 Python Click 的具有多个命令组的控制台脚本设置入口点?

How to set entry point for console script with multiple command groups for Python Click?

假设我的 foobar.py 库是这样设置的:

\foobar.py
\foobar
    \__init__.py
\setup.py

控制台脚本中的 CLI 层次结构:

foobar.py
    \cli
         \foo
             \kungfu
             \kungpow
         \bar
             \blacksheep
             \haveyouanywool

[代码]:

import click

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])


@click.group()
@click.version_option()
def cli():
    pass

@cli.group(context_settings=CONTEXT_SETTINGS)
def foo():
    pass

@cli.group(context_settings=CONTEXT_SETTINGS)
def bar():
    pass

@foo.command('kungfu')
def kungfu():
    print('bruise lee')

@foo.command('kungpow')
def kungpow():
    print('chosen one')

@bar.command('blacksheep')
def blacksheep():
    print('bah bah blacksheep')

@bar.command('haveyouanywool')
def haveyouanywool():
    print('have you any wool?')

我应该如何在 setup.py 中设置我的条目?

有很多示例,但它们只显示了针对单个入口点的单个命令,例如Entry Points in setup.py

但是 是否可以根据我的 foobar.py 点击脚本的结构来设置控制台脚本?

如果没有,我应该如何重组 foobar.py 中的命令?


对于上下文,我有这个 sacremoses 库的脚本:https://github.com/alvations/sacremoses/blob/cli/sacremoses.py

但我不知道如何配置 setup.py 以正确安装 sacremoses.py 脚本:https://github.com/alvations/sacremoses/blob/cli/setup.py

要使入口点在您的示例中起作用,您需要:

entry_points='''
    [console_scripts]
    command_line_name=foobar:cli
''',

您缺少的是对以下含义的理解:

command_line_name=foobar:cli

[console_scripts]

command_line_name=foobar:cli中有三件事:

  1. 命令行脚本的名称(command_line_name)
  2. 点击命令处理程序所在的模块(foobar)
  3. 该模块中点击的名称 command/group (cli)

setup.py

对于您的 github 示例,我建议:

from distutils.core import setup
import setuptools

console_scripts = """
[console_scripts]
sacremoses=sacremoses.cli:cli
"""

setup(
    name='sacremoses',
    packages=['sacremoses'],
    version='0.0.7',
    description='SacreMoses',
    long_description='LGPL MosesTokenizer in Python',
    author='',
    license='',
    package_data={'sacremoses': [
        'data/perluniprops/*.txt', 
        'data/nonbreaking_prefixes/nonbreaking_prefix.*'
    ]},
    url='https://github.com/alvations/sacremoses',
    keywords=[],
    classifiers=[],
    install_requires=['six', 'click', 'joblib', 'tqdm'],
    entry_points=console_scripts,
)

命令处理程序

在您的 github 存储库的引用分支中,没有 cli.py 文件。您问题中的 [code] 需要保存在 sacremoses/cli.py 中,然后结合对 setup.py 的建议更改,一切都应该正常。