Skip to content

Instantly share code, notes, and snippets.

@itorres
Created October 1, 2014 02:46
Show Gist options
  • Save itorres/82b9c43574f91636bb42 to your computer and use it in GitHub Desktop.
Save itorres/82b9c43574f91636bb42 to your computer and use it in GitHub Desktop.
YAPOP (Yet Another Python Option Parser)
def getopt(wat,default=None):
"""
getopt("flag") returns a bool
getopt("flag","default value") returns the next item in the argument list
Examples:
when calling "script -f filename":
getopt("-f","defaultname") would return "filename"
getopt("-v") would return False
when calling "script -v":
getopt("-f","defaultname") would return "defaultname"
getopt("-v") would return True
"""
if wat in sys.argv:
index = sys.argv.index(wat)
del sys.argv[index]
if default != None:
field = sys.argv[index]
del sys.argv[index]
return field
else:
return True
else:
if default != None:
return default
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment