【问题标题】:How to write entry_points argument for multiple Click CLI functions?如何为多个 Click CLI 函数编写 entry_points 参数?
【发布时间】:2018-04-15 05:51:11
【问题描述】:

我正在尝试为多个功能配置 setuptools 和 Click 模块。

单击Nesting Commands 部分中的文档说明以使用click.group()

如何为多个 CLick CLI 函数编写 entry_points?

我在玩弄他们的语法,我设法让一些东西工作,但我无法重新创建它。我是这样的,

entry_points='''
    [console_scripts]
    somefunc=yourscript:somefunc
    morefunc=yourscript:morefunc
'''

按照下面给出的示例,我将语法转换为字典:

entry_points= {'console_scripts': 
        ['somefunc = yourscript:somefunc',
        'morefunc = yourscript:morefunc'
    ]},

我重新安装后,调用脚本引发了这个错误:

(clickenv) > somefunc          
Traceback (most recent call last):
[...]
    raise TypeError('Attempted to convert a callback into a '
TypeError: Attempted to convert a callback into a command twice.

我第一次做这项工作的方式是安装脚本,然后通过各种示例逐渐更改代码。有一次,正如文档中所述,我使用$ yourscript somefunc 调用了脚本。然而,当我在我的项目中重新创建模式时,我得到了那个错误。

在这里我已经卸载并重新安装(尽管它被宣传为不必要的,pip install -e .)并删除了第二个入口点。这是我的测试示例。函数morefunc 需要一个.txt 输入文件。

# yourscript.py
import click

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

@cli.command()
def somefunc():
    click.echo('Hello World!')

@cli.command()
@click.argument('input', type=click.File('rb'))
@click.option('--saveas', default='HelloWorld.txt', type=click.File('wb'))
def morefunc(input, saveas):
    while True:
        chunk = input.read(1024)
        if not chunk:
            break
        saveas.write(chunk)

# setup.py
from setuptools import setup
setup(
    name='ClickCLITest',
    version='0.1',
    py_modules=['yourscript'],
    install_requires=[
        'Click',
    ],
    entry_points= {'console_scripts': 
            ['somefunc = yourscript:somefunc']},
 )

【问题讨论】:

    标签: python command-line-interface setuptools python-click


    【解决方案1】:

    https://setuptools.readthedocs.io/en/latest/setuptools.html#automatic-script-creation

    setup(
        …
        entry_points={
            'console_scripts': [
                'somefunc=yourscript:somefunc',
                'morefunc=yourscript:morefunc',
            ],
        },
    …
    )
    

    【讨论】:

    • 奇怪的是,该链接显示了两个不同的模块,而我的示例尝试从同一个模块中提取函数...
    • 没关系,我敢肯定。我相信您可以从一个模块中使用两个入口点。
    猜你喜欢
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-08
    相关资源
    最近更新 更多