看到一个 Python 的命令行工具 – click,很方便,因此记录下,官方主页:http://click.pocoo.org/3/

支持:

  • 命令的任意嵌套
  • 自动生成帮助信息
  • 支持在运行时子命令的延迟加载

安装方法是使用 pip:

pip install 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.

扩展阅读

Build Modular Command-Line Tools with Click