簡體   English   中英

int或long in十六進制python optparse?

[英]int or long in hexadecimal python optparse?

你好,我有optparse python的問題。

這是關於optparse的默認選項值,我將其表示為十六進制,但是在轉換為int或optparse python定義的long時,它不起作用。

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

為什么我必須為工作程序刪除該代碼的任何答案?

您有長整數參數--offset和字符串參數--variable都到達相同的目的地( offset_pattern )。 顯然,這會使optparse感到困惑,后者試圖將字符串參數的默認值應用於long參數並導致異常。

無論如何,具有不同類型和默認值的兩個選項無論如何都去同一個目的地。 更改選項之一的目的地將解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM