Skip to content

Instantly share code, notes, and snippets.

@edunham
Last active August 29, 2015 14:07
Show Gist options
  • Save edunham/3b00f2cced7782a58838 to your computer and use it in GitHub Desktop.
Save edunham/3b00f2cced7782a58838 to your computer and use it in GitHub Desktop.
A hello world that uses getopt
#!/usr/bin/env python
import getopt
import sys
try:
opts, args = getopt.getopt(sys.argv[1:], "g:s:", ["given=", "surname="])
except getopt.GetoptError as err:
print str(err) # will print something like "option -a not recognized"
sys.exit(2)
given = surname = None
for opt, arg in opts:
if opt in ('-g', '--given'):
given = arg
elif opt in ('-s', '--surname'):
surname = arg
if given is None or surname is None:
print "please supply both given name and surname"
sys.exit(2)
print "hello, " + given + ' ' + surname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment