python module getopt usage

时间:2022-03-16 11:41:31
 import getopt
import sys
def usage():
print 'this is a usage.'
def main():
try:
print sys.argv
#sys.argv[0],表示文件路径,sys.argv[1:],表示用户输入所有参数
#hp:i, 带有:表示需要跟参数,后面的长参数需要带‘=’
options,args = getopt.getopt(sys.argv[1:],"hp:i:",["help","ip=","port="])
print 'options:',options
print 'args:',args
except getopt.GetoptError:
sys.exit() print '--------------------' for name,value in options:
if name in ("-h","--help"):
usage()
if name in ("-i","--ip"):
print 'ip is----',value
if name in ("-p","--port"):
print 'port is----',value if __name__ == "__main__":
main()