使用自定义cmd选项运行web.py应用程序

问题描述:

我想使用web.py为一些较大的库构建http界面,该界面还提供了一个可选参数的命令行脚本。使用自定义cmd选项运行web.py应用程序

当我尝试将简单的web.py教程示例与optparse结合使用时,我遇到了web.py始终将第一个cmd参数作为端口的问题,这不是我想要的。有没有办法告诉web-py不要检查命令行参数。这里有一个例子:

#!/usr/bin/env python 
# encoding: utf-8 
""" 
web_interface.py: A simple Web interface 

""" 

import optparse 
import web 

urls = ("/.*", "hello") 
app = web.application(urls, globals()) 

class hello: 
    def GET(self): 
     return 'Hello, world!\n' 

if __name__ == "__main__": 
    p = optparse.OptionParser() 
    p.add_option('--test', '-t', help="the number of seed resources") 
    options, arguments = p.parse_args() 
    print options.test 
    app.run() 

...我想作为运行如下:

​​

这是一个黑客攻击的一位,但我想你可以这样做:

import sys 
... 

if __name__ == "__main__": 
    p = optparse.OptionParser() 
    p.add_option('--test', '-t', help="the number of seed resources") 
    options, arguments = p.parse_args() 
    print options.test 

    # set sys.argv to the remaining arguments after 
    # everything consumed by optparse 
    sys.argv = arguments 

    app.run()