@click.command() @click.option('--count', default=1, help='Number of greetings.') @click.option('--name', prompt='Your name', help='The person to greet.') defgreet(count, name): """Simple program that greets NAME for a total of COUNT times.""" for x in range(count): click.echo('Hello %s!' % name)
if __name__ == '__main__': greet()
在 toml 中添加程序入口
1 2
[tool.poetry.scripts] greet = "greet.greet:greet"
终端输入 poetry install 将代码安装到虚拟环境,之后输入 poetry run greet 试运行脚本,可以看到终端给出提示
1 2 3
mypc ~/tmp/clickgreet > poetry run greet Your name: jack Hello jack!
[[tool.poetry.source]] name = "douban" url = "https://pypi.doubanio.com/simple/"
Debug Click Command
测试代码,接收 count, name 参数并在终端输出
1 2 3 4 5 6 7 8 9 10 11 12 13
import click
@click.command() @click.option('--count', default=1, help='Number of greetings.') @click.option('--name', prompt='Your name', help='The person to greet.') defhello(count, name): """Simple program that greets NAME for a total of COUNT times.""" for x in range(count): click.echo('Hello %s!' % name)