函数以正确退出cli应用程序
cli-exit-tools的Python项目详细描述
cli出口工具
版本v1.1.8自2020-10-09起参见Changelog
用于正确退出cli应用程序的小工具集:
- 打印回溯信息(可使用命令行选项设置)
- 从异常中获取正确的退出代码
- 刷新流,以确保输出按正确的顺序写入
- 演示如何集成到cli模块中(请参阅用法)
自动化测试、Travis矩阵、文档、徽章等由PizzaCutter(类固醇cookiecutter)管理
需要Python版本:3.6.0或更高版本
使用python3.6、3.7、3.8、3.9-dev、pypy3在linux“仿生”上进行测试-体系结构:amd64、ppc64le、s390x、arm64
100% code coverage,flake8样式检查,mypy静态类型检查,在Linux, macOS, Windows下测试,自动每日构建和监视
在线试用
您可以使用“启动活页夹”徽章在Jupyter笔记本中立即尝试,或单击here
用法
- 主cli示例
# STDLIBimportsysfromtypingimportOptional# EXTimportclick# CONSTANTSCLICK_CONTEXT_SETTINGS=dict(help_option_names=['-h','--help'])try:from.import__init__conf__from.importcli_exit_toolsexcept(ImportError,ModuleNotFoundError):# pragma: no cover# imports for doctestimport__init__conf__# type: ignore # pragma: no coverimportcli_exit_tools# type: ignore # pragma: no coverdefinfo()->None:""" >>> info() Info for ... """__init__conf__.print_info()@click.group(help=__init__conf__.title,context_settings=CLICK_CONTEXT_SETTINGS)@click.version_option(version=__init__conf__.version,prog_name=__init__conf__.shell_command,message='{} version %(version)s'.format(__init__conf__.shell_command))@click.option('--traceback/--no-traceback',is_flag=True,type=bool,default=None,help='return traceback information on cli')defcli_main(traceback:Optional[bool]=None)->None:iftracebackisnotNone:cli_exit_tools.config.traceback=traceback@cli_main.command('info',context_settings=CLICK_CONTEXT_SETTINGS)defcli_info()->None:""" get program informations """info()# entry point if mainif__name__=='__main__':try:cli_main()exceptExceptionasexc:cli_exit_tools.print_exception_message()sys.exit(cli_exit_tools.get_system_exit_code(exc))finally:cli_exit_tools.flush_streams()
- 获取系统出口代码
- 打印异常消息
defprint_exception_message(trace_back:bool=config.traceback,stream:Optional[TextIO]=None)->None:""" Prints the Exception Message to stderr if trace_back is True, it also prints the traceback information if the exception has stdout, stderr attributes (like the subprocess.CalledProcessError) those will be also printed to stderr Parameter --------- trace_back if traceback information should be printed. This is usually set early in the CLI application to the config object via a commandline option. stream optional, to which stream to print, default = stderr Examples -------- >>> # test with exc_info = None >>> print_exception_message() >>> # test with exc_info >>> try: ... raise FileNotFoundError('test') ... except Exception: # noqa ... print_exception_message(False) ... print_exception_message(True) >>> # test with subprocess to get stdout, stderr >>> import subprocess >>> try: ... discard=subprocess.run('unknown_command', shell=True, check=True) ... except subprocess.CalledProcessError: ... print_exception_message(False) ... print_exception_message(True) ... print_exception_message(True, stream=sys.stderr) """
- 冲洗溪流
defflush_streams()->None:""" flush the streams - make sure the output is written early, otherwise the output might be printed even after another CLI command is launched Examples -------- >>> flush_streams() """
命令行的用法
Usage: cli_exit_tools [OPTIONS] COMMAND [ARGS]... functions to exit an cli application properly Options: --version Show the version and exit. --traceback / --no-traceback return traceback information on cli -h, --help Show this message and exit. Commands: info get program informations
安装和升级
- 在开始之前,强烈建议您更新pip和设置工具:
python -m pip --upgrade pip python -m pip --upgrade setuptools
- 要通过pip从PyPi安装最新版本(推荐):
python -m pip install --upgrade cli_exit_tools
- 要通过pip从github安装最新版本:
python -m pip install --upgrade git+https://github.com/bitranox/cli_exit_tools.git
- 包括在你的要求.txt公司名称:
# Insert following line in Your requirements.txt: # for the latest Release on pypi: cli_exit_tools # for the latest development version : cli_exit_tools @ git+https://github.com/bitranox/cli_exit_tools.git # to install and upgrade all modules mentioned in requirements.txt: python -m pip install --upgrade -r /<path>/requirements.txt
- 要从源代码安装最新的开发版本:
# cd ~ $ git clone https://github.com/bitranox/cli_exit_tools.git $ cd cli_exit_tools python setup.py install
- 通过生成文件: makefile是一种非常方便的安装方式。在这里我们可以做得更多, 比如安装虚拟环境、清理缓存等等。在
# from Your shell's homedirectory: $ git clone https://github.com/bitranox/cli_exit_tools.git $ cd cli_exit_tools # to run the tests: $ make test# to install the package $ make install # to clean the package $ make clean # uninstall the package $ make uninstall
要求
将自动安装以下模块:
## Project Requirements
click
致谢
- 特别感谢“鲍勃叔叔”罗伯特·C·马丁,特别是他关于“干净代码”和“干净架构”的书
贡献
我很乐意为你叉和发送我的请求,为这个项目。 -please Contribute
变更日志
- 新API的主要版本更改不兼容
- 新的次要版本,以向后兼容的方式添加了功能
- 新的补丁版本,用于向后兼容的错误修复
v1.1.8
- 2020-10-09:服务发布
- 更新travis构建矩阵
v1.1.7
- 2020-08-08:服务发布
- 修复文档
- 修理特拉维斯
- 不推荐pycodestyle
- 机具薄片8
v1.1.6
2020-08-07:固定车轮
v1.1.5
2020-07-31:固定车轮
v1.1.3
2020-07-31:首次发布
- 项目
标签: