Skip to content

Instantly share code, notes, and snippets.

@Aeternam
Created November 29, 2016 08:43
Show Gist options
  • Save Aeternam/cf16daf794437fdaa148ea74babf5a0c to your computer and use it in GitHub Desktop.
Save Aeternam/cf16daf794437fdaa148ea74babf5a0c to your computer and use it in GitHub Desktop.
best getopt exmaple
#!/bin/env python
#coding:utf-8
#
import getopt,sys
def usage():
"""
The output configuration file contents.
Usage: config.py [-d|--domain,[number|'m']] [-c|--cache,[allow|deny]] [-h|--help] [-v|--version] [output, =[argument]]
Description
-d,--domain Generate domain configuration,take 13 or 19 number,"m" is the second - tier cities.
-c,--cache Configure cache policy. "allow" or "deny".
-h,--help Display help information.
-v,--version Display version number.
for example:
python config.py -d 13
python config.py -c allow
python config.py -h
python config.py -d 13 -c allow
"""
def getopttest():
try:
options,args = getopt.getopt(sys.argv[1:],"d:c:hv",["domain=","cache=","help","version"])
except getopt.GetoptError as err:
print str(err)
print usage.__doc__
sys.exit(1)
for o,a in options:
#for b in args: # Value other than args agruments for getopt format.
if o in ("-d","--domain") and a in ("13"):
ttt.domain(int(a))
elif o in ("-d","--domain") and a in ("19"):
ttt.domain(int(a))
elif o in ("-c","--cache") and a in ("allow"):
ttt.cache("allow")
elif o in ("-h","--help"):
print usage.__doc__
sys.exit()
elif o in ("-v","--version"):
version = xxx
else:
print "Using the wrong way,please view the help information."
if __name__ == "__main__":
getopttest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment