diff --git a/lib/geocoder/cli.rb b/lib/geocoder/cli.rb index e31f10a9d113a34acb1395fac28be3e1482cc9f8..88e12e58c743184395df326d79499778a92281a1 100644 --- a/lib/geocoder/cli.rb +++ b/lib/geocoder/cli.rb @@ -5,7 +5,8 @@ module Geocoder class Cli def self.run(args, out = STDOUT) - url_only = false + show_url = false + show_json = false OptionParser.new{ |opts| opts.banner = "Usage:\n geocode [options] location" @@ -32,8 +33,12 @@ module Geocoder Geocoder::Configuration.timeout = timeout.to_i end + opts.on("-j", "--json", "Print API's raw JSON response") do + show_json = true + end + opts.on("-u", "--url", "Print URL for API instead of result") do - url_only = true + show_url = true end opts.on_tail("-v", "--version", "Print version number") do @@ -54,18 +59,28 @@ module Geocoder exit 1 end - if url_only + if show_url and show_json + out << "You can only specify one of -j and -u.\n" + exit 2 + end + + if show_url out << Geocoder.send(:lookup).send(:query_url, query) + "\n" exit 0 end + if show_json + out << Geocoder.send(:lookup).send(:fetch_raw_data, query) + "\n" + exit 0 + end + if (result = Geocoder.search(query).first) out << result.coordinates.join(',') + "\n" out << result.address + "\n" exit 0 else out << "Location '#{query}' not found.\n" - exit 2 + exit 1 end end end