1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#!/usr/bin/env python # coding=utf-8 import getopt,sys try: opts,args = getopt.getopt(sys.argv[1:],"hvo:",["help","out="]) #上一行中,hvo是三个不同的参数,“”中间的表示是短参数,例如我们常用的-a -l -c ,这些重有些是需要参数的,有些是不需要的,不需要参数的例如 -h, 我们不需要后跟参数,这种是开关类的,但是有一些是需要参数的,例如 -o abc.txt,其中三个参数是h v 是不需要参数的,o 因为后便跟了:,所以需要参数,[]中间的是长参数,例如 --help,也分为 开关类和需要值的,带=的就需要值 print args for o,a in opts: #print '%s : %s' % (o,a) if o in ("-h","--help"): print 'you have input the "-h" or "--help" ,so the help info is Usage: sys.argv[0] --help' if o in ("-v"): print 'you have input the "-v",so will print debug info' if o in ("--out"): print 'you have input the "--out" ,so we have something to out',a except getopt.GetoptError: print 'something wrong' |
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021