click

click简介

Click是一个Python包,用于以可组合的方式创建漂亮的命令行界面,只需要很少的代码。这是“命令行界面创建工具包”。它具有高度可配置性,但具有开箱即用的合理默认值。

点击三点:

  • 任意嵌套命令

  • 自动帮助页面生成

  • 支持在运行时延迟加载子命令

您可以直接从PyPI获取库:

pip install click

它是什么样子的?以下是一个简单的Click程序示例:

import click

@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
              help='The person to greet.')
def hello(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__':
    hello()

运行时的样子:

$ python hello.py --count=3
Your name: John
Hello John!
Hello John!
Hello John!

它会自动生成格式良好的帮助页面:

$ python hello.py --help
Usage: hello.py [OPTIONS]

  Simple program that greets NAME for a total of COUNT times.

Options:
  --count INTEGER  Number of greetings.
  --name TEXT      The person to greet.
  --help           Show this message and exit.
原文地址:https://www.cnblogs.com/clbao/p/11079841.html