hello i've problem with optparse python.
it's about default option value of optparse i represent that as hexadecimal but it's doesn't work when converting to int or long as defined by optparse python.
http://docs.python.org/library/optparse.html#standard-option-types
and this is my little piece of code :
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]")
but it's still give me an error like this even i use int or long as data type
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'
and this
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'
any help ? thanks, guns.
[edit] i've remove this code and the program works
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]")
any answer why i must removing that code for working program ?
You have both the long integer parameter --offset
and the string parameter --variable
going to the same destination ( offset_pattern
). Apparently this confuses optparse, which tries to apply the string parameter's default value to the long parameter and causes the exception.
It doesn't make sense for two options with different types and default values to go to the same destination anyway. Changing the destination for one of the options will fix the issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.