【发布时间】:2009-11-19 23:07:13
【问题描述】:
感谢江湖君指点一二!
以下代码是我第一次尝试使用 Optparse 编写代码。
在获得 Optparse 的帮助时如何解决以下错误?
#!/usr/bin/env python
import sys
import os
from optparse import OptionParser
e = sys.argv[1]
b = sys.argv[2]
no = sys.argv[3]
def set_figu(figu):
sum = 777
return sum
def main():
usage = "Usage: negative_bin_base.py <eksponentti> <siirre> <figu>"
parser = OptionParser(usage)
parser.add_option("-h", "--help", dest="help",
help="get synopsis of parameters")
# print the output of the work-horse
print set_figu(no)
(options, args) = parser.parse_args()
if len(args) < 4:
parser.error("incorrect number of arguments")
if options.informative:
print "reading %s..." % options.help
if __name__ == "__main__":
main()
参数数量正确的错误输出示例
python negative_bin_base.py 13 13 332
Traceback (most recent call last):
File "negative_bin_base.py", line 37, in <module>
main()
File "negative_bin_base.py", line 26, in main
help="get synopsis of parameters")
File "/usr/lib/python2.6/optparse.py", line 1020, in add_option
self._check_conflict(option)
File "/usr/lib/python2.6/optparse.py", line 995, in _check_conflict
option)
optparse.OptionConflictError: option -h/--help: conflicting option string(s): -h, --help
【问题讨论】:
-
查看 optparse 模块,这在常见情况下会变得相当容易:docs.python.org/library/optparse.html
-
一次性处理 args 没问题,但从长远来看,花时间学习 optparse 是值得的。
标签: python debugging arguments