Python命令行參數(shù)解析模塊optparse使用實(shí)例
示例
from optparse import OptionParser
[...]
def main():
usage = "usage: %prog [options] arg"
parser = OptionParser(usage)
parser.add_option("-f", "--file", dest="filename",
help="read data from FILENAME")
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose")
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose")
[...]
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("incorrect number of arguments")
if options.verbose:
print "reading %s..." % options.filename
[...]
if __name__ == "__main__":
main()
增加選項(xiàng)(add_option())
OptionParser.add_option(option)
OptionParser.add_option(*opt_str, attr=value, ...)
定義短選項(xiàng)
parser.add_option(“-f”, attr=value, …)
定義長選項(xiàng)
parser.add_option(“–foo”, attr=value, …)
如果定義
parser.add_option("-f", "--file", action="store", type="string", dest="filename")
命令行格式可以有以下形式
-ffoo
-f foo
--file=foo
--file foo
解析后結(jié)果
options.filename = “foo”
解析(parse_args())
(options, args) = parser.parse_args()
options 解析后的參數(shù),以字典形式保存
args 不能解析的參數(shù),以列表形式保存
行為(action)
●store 默認(rèn)行為,保存值到dest
●“store_const” 保存常量
●“append” append this option's argument to a list
●“count” increment a counter by one
●“callback” call a specified function
設(shè)置默認(rèn)值(default)
parser.add_option("-v", action="store_true", dest="verbose", default=True)
parser.set_defaults(verbose=True)
生成幫助提示(help)
提供help選項(xiàng)即可,可以用parser.print_help()打印出來
parser.add_option(“-f”, “–file”, dest=”filename”,help=”write report to FILE”, metavar=”FILE”)
設(shè)置boolean值
支持store_true和store_false兩個(gè)行為
parser.add_option("-v", action="store_true", dest="verbose")
parser.add_option("-q", action="store_false", dest="verbose")
如果遇到-v,verbose=True;如果遇到-q,verbose=False
錯(cuò)誤處理
(options, args) = parser.parse_args()
[...]
if options.a and options.b:
parser.error("options -a and -b are mutually exclusive")
選項(xiàng)組(Grouping Options)
格式如下
class optparse.OptionGroup(parser, title, description=None)
group = OptionGroup(parser, "Dangerous Options",
"Caution: use these options at your own risk. "
"It is believed that some of them bite.")
group.add_option("-g", action="store_true", help="Group option.")
parser.add_option_group(group)
提示結(jié)果如下
Usage: <yourscript> [options] arg1 arg2
Options:
-h, --help show this help message and exit
-v, --verbose make lots of noise [default]
-q, --quiet be vewwy quiet (I'm hunting wabbits)
-f FILE, --filename=FILE
write output to FILE
-m MODE, --mode=MODE interaction mode: novice, intermediate, or
expert [default: intermediate]
Dangerous Options:
Caution: use these options at your own risk. It is believed that some
of them bite.
-g Group option.
相關(guān)文章
python2.7使用plotly繪制本地散點(diǎn)圖和折線圖
這篇文章主要為大家詳細(xì)介紹了python2.7使用plotly繪制本地散點(diǎn)圖和折線圖實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Pycharm沒有報(bào)錯(cuò)提示(誤觸ignore)的完美解決方案
這篇文章主要介紹了Pycharm沒有報(bào)錯(cuò)提示(誤觸ignore)的解決方案,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12Python數(shù)據(jù)分析Numpy中常用相關(guān)性函數(shù)
這篇文章主要為大家介紹了Python數(shù)據(jù)分析Numpy中常用相關(guān)性函數(shù)講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06python代碼打印100-999之間的回文數(shù)示例
今天小編就為大家分享一篇python代碼打印100-999之間的回文數(shù)示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11