1 import getopt 2 import sys 3 def usage(): 4 print ‘this is a usage.‘ 5 def main(): 6 try: 7 print sys.argv 8 #sys.argv[0],表示文件路径,sys.argv[1:],表示用户输入所有参数 9 #hp:i, 带有:表示需要跟参数,后面的长参数需要带‘=’ 10 options,args = getopt.getopt(sys.argv[1:],"hp:i:",["help","ip=","port="]) 11 print ‘options:‘,options 12 print ‘args:‘,args 13 except getopt.GetoptError: 14 sys.exit() 15 16 print ‘--------------------‘ 17 18 for name,value in options: 19 if name in ("-h","--help"): 20 usage() 21 if name in ("-i","--ip"): 22 print ‘ip is----‘,value 23 if name in ("-p","--port"): 24 print ‘port is----‘,value 25 26 27 if __name__ == "__main__": 28 main()
时间: 2024-10-25 08:05:17