python - argparse --help 是否可以显示我的退出状态?

标签 python argparse

我有一个用 Python 编写的实用程序,旨在单独使用或与其他 shell 实用程序结合使用。因此,我的实用程序以状态代码退出(例如,如果一切正常,则为 0;如果输入文件或输出目录不存在,则为 1,等等)。

我的问题:我正在使用 argparse module它工作得很好,不仅用于解析选项,而且用于生成帮助。但是,我也希望能够在帮助中添加一些关于我的退出状态的信息。 argparse 这可能吗?我错过了什么吗?

最佳答案

这是我放入 epilog 中的信息.

调整官方文档中的例子:

>>> import argparse
>>> parser = argparse.ArgumentParser(
...     description='This is my utility description.',
...     epilog='The exit status will be 0 if everything is fine, '
...            'and 1 if an input-file or an output-directory does not exist')
>>> 
>>> parser.print_help()
usage: [-h]

This is my utility description.

optional arguments:
  -h, --help  show this help message and exit

The exit status will be 0 if everything is fine, and 1 when an input-file or
an output-directory does not exist

关于python - argparse --help 是否可以显示我的退出状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9098485/

相关文章:

python - 无法为 Google Cloud Storage 的网址签名

python - 在数据帧中的好坏堆叠条上显示前 3 个 'bad' 系统(按一个值排序,但在图表上显示两个值)

python - 通过 Anaconda 安装 `libm.so.6`

python - 使用 openpyxl 获取单元格值的显示字符串

python - Argparse:在 '--help' 中包含默认值的方法?

python - 使用 argparse.ArgumentParser() 获取分组结构

python - 在 Docker 容器中安装 python 时执行格式错误

python - 4 位数字作为参数的命令行解析器,具有多种组合 (Python)

python argparse 不显示正确的帮助信息

python argparse : allow unregistered arguments