test.py
#!/usr/bin/env python# -*- coding: utf-8 -*-import getoptimport sys#-h-f-v为了下面的识别opts,args = getopt.getopt(sys.argv[1:],'-h-f-v',['help','filename=','version'])#print(opts)for opt_name,opt_value in opts: if opt_name in ('-h','--help'): print("[*] Help info") sys.exit() if opt_name in ('-v','--version'): print("[*] Version is 0.01 ") sys.exit() if opt_name in ('-f','--filename'): fileName = opt_value print("[*] Filename is ",fileName) # do something sys.exit()
运行test.py -v
打印 "[*] Version is 0.01 "