命令行神器 Click教程A篇

摘要

这里介绍一个快速创建命令行的神器click,是一个用Python开发的第三方模块。
相比较Argparse来创建命令行或者参数解析。click确实方便了很多。

官网 的介绍如下:

Click is a Python package for creating beautiful command line 
interfaces in a composable way with as little code as necessary.
It’s the “Command Line Interface Creation Kit”. It’s highly 
configurable but comes with sensible defaults out of the box.

安装

官网建议通过virtualevn来安装,关于virtualenv安装Python第三方包的详细教程和注意问题请参考 Centos 下 python 纯净虚拟环境

安装命令

pip install click

安装完成验证

root@pts/3 $ python
Python 2.7.5 (default, Nov 20 2015, 02:00:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import click
>>> click.__version__
'6.6'
>>>

使用

command

这里先参考官网给出来的一个例子:

## python case script
root@pts/3 $ cat case-1.py
#!/usr/bin/env python
#encoding:  utf-8
#author:    Colin
#date:      2016-12-29
#desc:
#

import click

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

if __name__ == '__main__':
    hello()
    
## execute
root@pts/3 $ python case-1.py
Hello World!

root@pts/3 $ python case-1.py --help
Usage: case-1.py [OPTIONS]

Options:
  --help  Show this message and exit.

这里发现 click 自动添加了一个 --help 的参数

《命令行神器 Click教程A篇》 python-click-case-1.png

option

这里介绍用 option 构造命令行参数, 再上面的例子基础上继续修改

## scripts
import click

@click.command()
@click.option('--name', prompt='enter your name here: ',
            help='greet to given name')
def hello(name):
    click.echo('Hello World! hello %s' % name)

if __name__ == '__main__':
    hello()
    

## execute script
root@pts/3 $ python case-2.py
enter your name here: : Colin
Hello World! hello Colin

root@pts/3 $ python case-2.py --name Colin
Hello World! hello Colin

root@pts/3 $ python case-2.py --name=James
Hello World! hello James

root@pts/3 $ python case-2.py --help
Usage: case-2.py [OPTIONS]

Options:
  --name TEXT  greet to given name
  --help       Show this message and exit.

脚本结果如下:

《命令行神器 Click教程A篇》 python-click-case-2.png

关于option使用过程中遇到的几个问题:

  • 参数名称只能是--xxxx,比如要以`–“开头,不然报错如下:

《命令行神器 Click教程A篇》 python-click-option-error-1.png

group

使用 group 可以把多个函数组合成子命令;

有两种方式:

A:

通过 `@click.group()` 装饰器定义一个函数 A,最后通过 
`A.add_command(hello)` 把别的函数加入到 `group` 函数中去。
注意 `hello` 是脚本中定义的一个命令行函数

B:

通过 `@click.group()` 装饰器定义一个函数 B,在定义一般命令行函数的时候,用 
`@B.command()` 来替代 `@click.command()` 装饰命令行函数`hello`

这里给出第一个方式的结果。第二个可以自己尝试看看结果是否一致。具体例子详见:

## python script
import click


@click.group()
def gpfun():
    pass

@click.command()
@click.option('--name', prompt='enter your name here: ',
            help='greet to given name')
def hello(name):
    click.echo('Hello World! hello %s' % name)

gpfun.add_command(hello)

if __name__ == '__main__':
    gpfun()


## execute
root@pts/3 $ python case-3.py --help
Usage: case-3.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  hello

root@pts/3 $ python case-3.py hello --help
Usage: case-3.py hello [OPTIONS]

Options:
  --name TEXT  greet to given name
  --help       Show this message and exit.

root@pts/3 $ python case-3.py hello --name='Colin'
Hello World! hello Colin

这里注意多了个gpfun函数,其上的装饰器是@click.group()

另外注意最下面的 if 判断里面的函数调用变成了 gpfun()

脚本结果如下:

《命令行神器 Click教程A篇》 python-click-case-3.png

其他详细的用法待续…
您要是觉得不错,可以点个赞哦~

《命令行神器 Click教程A篇》 公众号: DailyJobOps

    原文作者:全栈运维
    原文地址: https://www.jianshu.com/p/d2b61a892c32
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞