Python CLI 开发 entry_point click
命令行打包
目录结构:
hellopython.py 文件内容:
py_str = """Hello Python !!!!"""
def main():
print(py_str)
if __name__ == '__main__':
main()
setup.py 文件内容:
from setuptools import setup
setup(
name='hellopython',
entry_points={
'console_scripts': [
'hellopython = hellopython:main',
],
}
)
打包命令:
python setup.py develop
running develop
running egg_info
writing hellopython.egg-info\PKG-INFO
writing dependency_links to hellopython.egg-info\dependency_links.txt
writing entry points to hellopython.egg-info\entry_points.txt
writing top-level names to hellopython.egg-info\top_level.txt
reading manifest file 'hellopython.egg-info\SOURCES.txt'
writing manifest file 'hellopython.egg-info\SOURCES.txt'
running build_ext
Creating d:\python\python39\lib\site-packages\hellopython.egg-link (link to .)
hellopython 0.0.0 is already the active version in easy-install.pth
Installing hellopython-script.py script to D:\Python\Python39\Scripts
Installing hellopython.exe script to D:\Python\Python39\Scripts
在终端输入 hellopython 就会出现结果
配合click
修改hellopython.py文件:
import click
@click.command()
@click.option("--name", prompt="姓名", help="输入参数名字,参数类型是字符串")
@click.option("--version", prompt="版本号", help="输入参数版本号,参数类型是字符串")
def main(name, version):
print(f"Hello {name}-{version}")
if __name__ == '__main__':
main()
重新用 python setup.py develop 命令打包 使用方式:
hellopython --name python --version 1
hellopython --help
Options:
--name TEXT 输入参数名字,参数类型是字符串
--version TEXT 输入参数版本号,参数类型是字符串
--help Show this message and exit.
- @click.command() 装饰一个函数,使之成为命令行接口
- @click.option() 等装饰函数,为其添加命令行选项等
参考链接: Python setup.py entry_points 详解 python打包 entry_point+click:快速生成python命令行接口 Python:命令行库Click
|