Commits

Anonymous committed b2cc79b

Added optparse to handle optionnal key when using the command line tool.

  • Participants
  • Parent commits 2805b3b

Comments (0)

Files changed (1)

File pygeocoder.py

 
 if __name__ == "__main__":
     import sys
+    from optparse import OptionParser
 
-    def main(argv):
+    def main():
         """
         Geocodes a location given on the command line.
 
         they are separated by a comma and no space.
 
         """
+        usage = "usage: %prog [options] address"
+        parser = OptionParser(usage, version=VERSION)
+        parser.add_option("-k", "--key",
+                  dest="key", help="Your Google Maps API key")
+        (options, args) = parser.parse_args()
 
-        if len(argv) < 2 or len(argv) > 4:
-            print main.__doc__
+        if len(args) != 1:
+            parser.print_usage()
             sys.exit(1)
 
-        query = argv[1]
-        gcoder = Geocoder(argv[2])
+        query = args[0]
+        gcoder = Geocoder(options.key)
 
         try:
             result = gcoder.geocode(query)
         json.dump(result, sys.stdout, indent=4)
         sys.stdout.write('\n')
 
-    main(sys.argv)
+    main()