控制台脚本样板
csboilerplate的Python项目详细描述
控制台脚本样板文件
一些控制台脚本的样板文件。灵感来自现在未维护的pyCLI
。
修饰您的主函数,并在您的setup.py
中将其注册为console_scripts
。
入口点[1]。函数将接收一个app对象作为参数。
你得到:
- Python
ArgumentParser
KeyboardInterrupt
被捕获- ^ {CD6> }被处理,以便上下文管理器将正确退出
- 日志助手
exit_handler
和sigterm_handler
可以自定义。
[1]https://setuptools.readthedocs.io/en/latest/setuptools.html#automatic-script-creation
示例
fromcsboilerplateimportcli_app@cli_app(name=__name__)defmain(app):app.logging_config(log_level=app.args.debug)# do your stuffmain.argparser.add_argument('-d','--debug',action='count',default=0,help='lower logging threshold, may be used twice')
开发环境
签出git并:
virtualenv .env -p python3 . .env/bin/activate pip install -e .[dev]