2013-04-08 72 views
1

我有一个Python程序,它使用docopt并似乎解析命令行参数就好了。但是,当我尝试在调用中使用memory_profiler时(使用-m memory_profiler),docopt无法解析命令并打印使用语句。下面是一个简单的程序:冲突使用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 

我缺少什么?

回答

1

看来memory_profiler不从sys.argv中剥离本身,所以黑客会(我猜)是自己做:

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

非常有意义。谢谢! – zenzic 2013-04-08 17:04:26

+2

在memory_profiler的最后一个版本(0.26)中,此错误已得到纠正 – 2013-04-26 12:00:56