2012-02-01 57 views
3

我有一个用Python编写的实用程序,意在独立使用或与其他shell实用程序结合使用。因此,我的实用程序以状态代码退出(例如,如果一切正常,则为0,当输入文件或输出目录不存在时为1等)。argparse的--help是否可以显示我的退出状态?

我的问题:我正在使用argparse module,它的效果很好,不仅用于解析选项,还用于生成帮助。不过,我希望能够向我的帮助中添加一些关于退出状态的信息。这是否可能与argparse;我错过了什么吗?

+1

你有没有考虑过把这些信息在[结语](http://docs.python.org/library/argparse.html#epilog)? – 2012-02-01 15:31:50

+0

@RikPoggi:你的评论应该是一个答案。 – 2012-02-01 15:43:44

回答

6

这是我放入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 
+0

非常感谢,这正是我应该做的! – JStroop 2012-02-01 16:50:15