简体   繁体   English

int或long in十六进制python optparse?

[英]int or long in hexadecimal python optparse?

hello i've problem with optparse python. 你好,我有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. 这是关于optparse的默认选项值,我将其表示为十六进制,但是在转换为int或optparse python定义的long时,它不起作用。

http://docs.python.org/library/optparse.html#standard-option-types 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 但是即使我使用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'

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 ). 您有长整数参数--offset和字符串参数--variable都到达相同的目的地( offset_pattern )。 Apparently this confuses optparse, which tries to apply the string parameter's default value to the long parameter and causes the exception. 显然,这会使optparse感到困惑,后者试图将字符串参数的默认值应用于long参数并导致异常。

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. 更改选项之一的目的地将解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM