# nameparse2.py
# Written by Todd Wareham for CS 2500
"""
Given the name of a textfile of proper names (one per line) consisting
 of a last name and a first name composed of up to three first names and/or
 initials as a command-line argument, print reversed short form of
 each proper name in given file.
"""

import sys
import re

if len(sys.argv) != 2:
    print "usage: ", sys.argv[0], " name-file"
    sys.exit(1)

lastNameP   = "([A-Z][a-z]+)"
firstNameP  = r"([A-Z]\.|[A-Z][a-z]+)"
firstNamesP = firstNameP + "(?: " + firstNameP + ")?" + "(?: " + firstNameP + ")?"
nameP       = lastNameP + ", " + firstNamesP
r = re.compile(nameP)

for line in file(sys.argv[1]):
    m = r.match(line)
    print line.rstrip(), " --> ", m.expand(r"\2 \1")

