我有一个用 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/