Python系列 - optparse

时间:2023-03-09 13:30:02
Python系列 - optparse

我们知道sys.argv[] 可以获得命令行参数

同样,optparse 对此提供了更为强大的功能。

import optparse
class ArgvHandler(object):
def __init__(self):
self.op=optparse.OptionParser() self.op.add_option("-s","--host",dest="server")
self.op.add_option("-p","--port",dest="port") options,args=self.op.parse_args() print(options,args)
print(options.server)
print(options.port)

结果:

Python系列 - optparse

如此可以轻松的获得指定的参数了。