Refactored to use argparse, added the if __name__ idiom, and added doctests

Replace argv parsing with argparse, added if __name__ == "__main__" block and main() function, added doctests