如何为 Python Click 的具有多个命令组的控制台脚本设置入口点? [英] How to set entry point for console script with multiple command groups for Python Click?

查看:48
本文介绍了如何为 Python Click 的具有多个命令组的控制台脚本设置入口点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我的带有 foobar.py 的库是这样设置的:

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

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

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

Hierarchy of CLI in the console script:

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 中设置我的条目?

How should I set my entry in setup.py?

有很多示例,但它们只显示单个入口点的单个命令,例如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 点击脚本的结构来设置控制台脚本?

But is it even possible to setup the console script with how the my foobar.py click script is structured?

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

If not, how should I restructure the commands in foobar.py?

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

For context, I have this script for the sacremoses library: https://github.com/alvations/sacremoses/blob/cli/sacremoses.py

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

But I couldn't figure how to configure the setup.py to install the sacremoses.py script properly: https://github.com/alvations/sacremoses/blob/cli/setup.py

推荐答案

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

To make the entry points work in your example you need:

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

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

What you are missing is an understanding of the meaning of:

command_line_name=foobar:cli

[console_scripts]

command_line_name=foobar:cli 中有三件事:

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

setup.py

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

setup.py

For your github example, I would suggest:

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 的建议更改,一切都应该正常工作.

Command Handler

In the referenced branch of your github repo, there is NO cli.py file. The [code] from your question needs to be saved in sacremoses/cli.py, and then combined with the suggested changes to your setup.py, everything should work fine.

这篇关于如何为 Python Click 的具有多个命令组的控制台脚本设置入口点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆