# HG changeset patch # User Jeff Hammel # Date 1477706096 25200 # Node ID 0bc7c1a5eb83f87907329b7a9fbabb63d5ac57d4 # Parent 0fffb356064187fa455ceb2f32a8497dc818fd11 make into an almost real CLI diff -r 0fffb3560641 -r 0bc7c1a5eb83 python/tld.py --- a/python/tld.py Fri Oct 28 18:46:46 2016 -0700 +++ b/python/tld.py Fri Oct 28 18:54:56 2016 -0700 @@ -1,8 +1,15 @@ #!/usr/bin/env python +""" +Top-Level Domain names (TLDs), country codes only +""" + +import argparse +import sys +import urllib2 + URL = 'http://www.palosverdes.com/jesse/irc/country.txt' -import urllib2 def codes(): f = urllib2.urlopen(URL) @@ -16,11 +23,23 @@ return codes codes = codes() + def get(code): code = code.lstrip('.').lower() return codes.get(code) +def main(args=sys.argv[1:]): + """CLI""" + + # parse command line + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('tld', nargs='+', + help="country-code TLD to look up") + options = parser.parse_args(args) + + # lookup + output + for arg in options.tld: + print ('{}: {}'.format(arg, get(arg))) + if __name__ == '__main__': - import sys - for arg in sys.argv[1:]: - print '%s: %s' % (arg, get(arg)) + main()