你好,我对 optparse python 有问题。
这是关于 optparse 的默认选项值,我将其表示为十六进制,但在转换为 int 或 long 时不起作用,由 optparse python 定义。
http://docs.python.org/library/optparse.html#standard-option-types
这是我的一小段代码:
parser.add_option("-o", "--offset",
dest="offset_pattern",
default=0x41306141,
type="long",
action="store",
metavar="HEX",
help="define the offset will be found [default : %default]")
但即使我使用 int 或 long 作为数据类型,它仍然给我这样的错误
Traceback (most recent call last):
File "./pattern.py", line 155, in <module>
main()
File "./pattern.py", line 151, in main
proxyengine.parseoption()
File "./pattern.py", line 132, in parseoption
(options, args) = parser.parse_args()
File "/usr/lib/python2.6/optparse.py", line 1365, in parse_args
values = self.get_default_values()
File "/usr/lib/python2.6/optparse.py", line 1310, in get_default_values
defaults[option.dest] = option.check_value(opt_str, default)
File "/usr/lib/python2.6/optparse.py", line 756, in check_value
return checker(self, opt, value)
File "/usr/lib/python2.6/optparse.py", line 416, in check_builtin
_("option %s: invalid %s value: %r") % (opt, what, value))
optparse.OptionValueError: option --offset: invalid long integer value: 'buff'
和这个
Traceback (most recent call last):
File "./pattern.py", line 155, in <module>
main()
File "./pattern.py", line 151, in main
proxyengine.parseoption()
File "./pattern.py", line 132, in parseoption
(options, args) = parser.parse_args()
File "/usr/lib/python2.6/optparse.py", line 1365, in parse_args
values = self.get_default_values()
File "/usr/lib/python2.6/optparse.py", line 1310, in get_default_values
defaults[option.dest] = option.check_value(opt_str, default)
File "/usr/lib/python2.6/optparse.py", line 756, in check_value
return checker(self, opt, value)
File "/usr/lib/python2.6/optparse.py", line 416, in check_builtin
_("option %s: invalid %s value: %r") % (opt, what, value))
optparse.OptionValueError: option --offset: invalid integer value: 'buff'
有什么帮助吗?谢谢,枪。
[编辑] 我已经删除了这段代码并且程序可以运行
parser.add_option("-n", "--variable",
dest="offset_pattern",
default="buff",
type="string",
action="store",
metavar="STR",
help="define variable buffer name will be create [default : %default]")
任何答案为什么我必须删除工作程序的代码?