diff python/tld.py @ 812:0bc7c1a5eb83

make into an almost real CLI
author Jeff Hammel <k0scist@gmail.com>
date Fri, 28 Oct 2016 18:54:56 -0700
parents c7edd15d7f8b
children
line wrap: on
line diff
--- 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()