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

alv*_*vas 3 python console command-line setuptools python-click

Given that my library with foobar.py is setup as such:

\foobar.py
\foobar
    \__init__.py
\setup.py
Run Code Online (Sandbox Code Playgroud)

Hierarchy of CLI in the console script:

foobar.py
    \cli
         \foo
             \kungfu
             \kungpow
         \bar
             \blacksheep
             \haveyouanywool
Run Code Online (Sandbox Code Playgroud)

[code]:

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?')
Run Code Online (Sandbox Code Playgroud)

How should I set my entry in setup.py?

There are many examples but they only show a single command for a single entry point, e.g. 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

Ste*_*uch 6

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

entry_points='''
    [console_scripts]
    command_line_name=foobar:cli
''',
Run Code Online (Sandbox Code Playgroud)

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

command_line_name=foobar:cli
Run Code Online (Sandbox Code Playgroud)

[控制台脚本]

里面有三件事command_line_name=foobar:cli

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

设置文件

对于您的 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,
)
Run Code Online (Sandbox Code Playgroud)

命令处理程序

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