发现论坛上关于setuptools的使用教程都没有从实际的项目出发,要不就是项目十分简单,要不就是随便从国外论坛随便抄的,看起来十分闹心,有的步骤与当前python3不兼容的情况也很多,这里以我的一个小型python工程作为例子,细说一下怎么使用setuptools制作CLI工具。
首先项目的文件结构如下图:
简单解释一下,我的项目名称叫glue,是一个文件写入优化算法,我的项目有一个入口程序glue,代码下面有,这里是CLI工具的实现逻辑,gluelib为我的算法实现,细节就不多说了,你可以在gluelib这一级目录下放你自己的工程文件,通常被入口程序所调用。setup.py文件与cli和gluelib同级。下面是入口程序glue.py的代码实现,关于这块click库的使用,这篇博客讲的很清楚:
Python Click 学习笔记
import os
import click
from gluelib import normal_read,normal_write,read_files,write_files
@click.group()
def losf():
pass
@click.command()
@click.option('-filenum',default=1000,help='number of the imgs will be writen')
@click.option('-file',default='myvideo.mp4',help='the path of your video for us to catch the each frame of it')
def write(filenum,file):
""" write files using a normal way which doesn't merge the files """
normal_write.normalwrite(filenum,file