我有一个用Python编写的实用程序,它可以单独使用,也可以与其他shell实用程序一起使用。因此,我的实用程序会以状态代码退出(例如,如果一切正常,则为0
,如果输入文件或输出目录不存在,则为1
,等等)
我的问题是:我正在使用argparse模块,它工作得很好,不仅用于解析选项,而且用于生成帮助。不过,我也希望能够在帮助中添加一些关于我退出状态的信息。使用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