2012-02-01 61 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