簡體   English   中英

同時使用memory_profiler和docopt發生沖突

[英]Conflict using both memory_profiler and docopt

我有一個使用docopt的Python程序,它看起來可以很好地解析命令行參數。 但是,當我嘗試在調用中包括memory_profiler時(使用-m memory_profiler)時,docopt無法解析命令並打印用法語句。 這是一個示例程序:

"""
Usage:
  ids_transform [options]
  ids_transform --block BLOCK_MSG
  ids_transform --unblock
  ids_transform --version

Examples:
    ids_transform.py --block '2013-03-15 IM#123456 database down'
    ids_transform.py -c ../shared/etc/ids_transform.yaml

Options:
  -h --help                 show this help message and exit
  -c CONFIG --config=CONFIG config file
  -d --debug                begin debugging
  --force                   override block file; force run
  --profile                 use cProfile to gather statistics on process
"""

from docopt import docopt


if __name__ == '__main__':
    arguments = docopt(__doc__, version='1.0.0rc2')
    print(arguments)

這是一個成功的調用:

$ python ids.py -d --force -c foo.yml
{'--block': False,
 '--config': 'foo.yml',
 '--debug': True,
 '--force': True,
 '--help': False,
 '--profile': False,
 '--unblock': False,
 '--version': False,
 'BLOCK_MSG': None}

這是使用memory_profiler時的錯誤:

$ python -m memory_profiler ids.py -d --force -c foo.yml
Usage:
  ids_transform [options]
  ids_transform --block BLOCK_MSG
  ids_transform --unblock
  ids_transform --version

我想念什么?

似乎memory_profiler不會從sys.argv中剝離自身,所以(我想)可以自己做一個破解:

if sys.argv[0].endswith('memory_profiler.py'):
    del sys.argv[0]

暫無
暫無

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

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