require'geocoder'require'optparse'moduleGeocoderclassClidefself.run(args,out=STDOUT)show_url=falseshow_json=false# remove arguments that are probably coordinates so they are not# processed as arguments (eg: -31.96047031,115.84274631)coords=args.select{|i|i.match(/^-\d/)}args-=coordsOptionParser.new{|opts|opts.banner="Usage:\n geocode [options] <location>"opts.separator"\nOptions: "opts.on("-k <key>","--key <key>","Key for geocoding API (usually optional). Enclose multi-part keys in quotes and separate parts by spaces")do|key|if(key_parts=key.split(/\s+/)).size>1Geocoder.configure(:api_key=>key_parts)elseGeocoder.configure(:api_key=>key)endendopts.on("-l <language>","--language <language>","Language of output (see API docs for valid choices)")do|language|Geocoder.configure(:language=>language)endopts.on("-p <proxy>","--proxy <proxy>","HTTP proxy server to use (user:pass@host:port)")do|proxy|Geocoder.configure(:http_proxy=>proxy)endopts.on("-s <service>",Geocoder::Lookup.all_services_except_test,"--service <service>","Geocoding service: #{Geocoder::Lookup.all_services_except_test*', '}")do|service|Geocoder.configure(:lookup=>service.to_sym)Geocoder.configure(:ip_lookup=>service.to_sym)endopts.on("-t <seconds>","--timeout <seconds>","Maximum number of seconds to wait for API response")do|timeout|Geocoder.configure(:timeout=>timeout.to_i)endopts.on("-j","--json","Print API's raw JSON response")doshow_json=trueendopts.on("-u","--url","Print URL for API query instead of result")doshow_url=trueendopts.on_tail("-v","--version","Print version number")dorequire"geocoder/version"out<<"Geocoder #{Geocoder::VERSION}\n"exitendopts.on_tail("-h","--help","Print this help")doout<<"Look up geographic information about a location.\n\n"out<<optsout<<"\nCreated and maintained by Alex Reisner, available under the MIT License.\n"out<<"Report bugs and contribute at http://github.com/alexreisner/geocoder\n"exitend}.parse!(args)# concatenate args with coords that might have been removed# before option processingquery=(args+coords).join(" ")ifquery==""out<<"Please specify a location (run `geocode -h` for more info).\n"exit1endifshow_urlandshow_jsonout<<"You can only specify one of -j and -u.\n"exit2endifshow_urlq=Geocoder::Query.new(query)out<<q.url+"\n"exit0endifshow_jsonq=Geocoder::Query.new(query)out<<q.lookup.send(:fetch_raw_data,q)+"\n"exit0endif(result=Geocoder.search(query).first)nominatim=Geocoder::Lookup.get(:nominatim)lines=[["Latitude",result.latitude],["Longitude",result.longitude],["Full address",result.address],["City",result.city],["State/province",result.state],["Postal code",result.postal_code],["Country",result.country],["Map",nominatim.map_link_url(result.coordinates)],]lines.eachdo|line|out<<(line[0]+": ").ljust(18)+line[1].to_s+"\n"endexit0elseout<<"Location '#{query}' not found.\n"exit1endendendend