2011-04-22 81 views
0

你好,我的問題與optparse python。int或long在十六進制python optparse?

它是關於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或者只要數據類型的錯誤

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]") 

任何回答,爲什麼我必須消除對工作程序的代碼?

+0

我不認爲這個錯誤出現在您發佈的代碼中。 – Omnifarious 2011-04-22 16:10:41

+0

該代碼適用於我。 – interjay 2011-04-22 16:12:44

+0

什麼是「buff」?你怎麼調用你的腳本? – zxt 2011-04-22 16:13:51

回答

3

您將長整型參數--offset和字符串參數--variable都轉到同一目的地(offset_pattern)。很明顯,這會混淆optparse,它試圖將字符串參數的默認值應用於long參數並導致異常。

無論如何,對於具有不同類型和默認值的兩個選項來說,無論如何都無法使用相同的目標。更改其中一個選項的目標將解決問題。

+0

謝謝你是對的。我看到... – 2011-04-22 16:25:40