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]</pre><span style="color:#444444;font-family:georgia, times, 'times new roman', serif;font-size:13px;line-height:22px;background-color:#ffffff;"></span>(注意:当脚本打印帮助信息后会退出,不会解析其他选项参数)自定义程序使用方法:<pre class="brush:python;toolbar:false;">usage = "usage: %prog [options] arg1 arg2"</pre>这行信息会优先打印在程序的选项信息前。当中的 %prog,optparse 会以当前程序名的字符串来替代:如 os.path.basename.(sys.argv[0])。如果用户没有提供自定义的使用方法信息,optparse 会默认使用: “usage: %prog [options]”。用户在定义命令行参数的帮助信息时,不用担心换行带来的问题,optparse 会处理好这一切。设置 add_option 方法中的 metavar 参数,有助于提醒用户,该命令行参数所期待的参数,如 metavar=“mode”:<pre class="brush:python;toolbar:false;">-m MODE, --mode=MODE</pre>注意: metavar 参数中的字符串会自动变为大写。在 help 参数的帮助信息里使用 %default 可以插入该命令行参数的默认值。如果程序有很多的命令行参数,你可能想为他们进行分组,这时可以使用 <tt class="xref py py-class docutils literal" style="margin:0px;padding:0px 1px;border:0px;outline:0px;font-size:0.95em;vertical-align:baseline;background-color:transparent;font-weight:bold;font-family:'andale mono', 'lucida console', monospace;line-height:1.5;"><span class="pre" style="margin:0px;padding:0px;border:0px;outline:0px;vertical-align:baseline;background-color:transparent;">OptionGroup</span></tt>:<pre class="brush:python;toolbar:false;">group = OptionGroup(parser, "Dangerous Options",
"Caution: use these options at your own risk. "
"It is believed that some of them bite.")
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.完整的列子:
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)
group = OptionGroup(parser, "Debug Options")
group.add_option("-d", "--debug", action="store_true",