示例
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, …)
定義長(zhǎng)選項(xiàng)
parser.add_option(“?Cfoo”, 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”, “?Cfile”, 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:
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.
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
