bin/shorturl in shorturl-0.7.0 vs bin/shorturl in shorturl-0.8.0

- old
+ new

@@ -1,36 +1,38 @@ #!/usr/bin/env ruby require "rubygems" require "shorturl" +require "optparse" -def usage - puts "Usage: #$0 <url> [<service>]" - puts "Available services:" - ShortURL.valid_services.each { |s| puts "\t#{s}" } - exit(-1) +def service_list + ShortURL.valid_services.map { |s| s.to_s }.sort.join("\n\t") end def main - if ARGV.size < 1 - usage + service = :rubyurl + op = OptionParser.new do |opts| + opts.banner = "Usage: #{File.basename($0)} [<options>] <url>" + opts.on("-h", "--help", "Displays help") { puts opts; exit } + opts.on("-s", "--service <service>", String, + "Uses <service> instead of RubyURL +Available services:\n\t" + service_list) { |s| service = s.to_sym } + opts.parse! end - url = ARGV[0] - service = ARGV[1] - - shorturl = if service.nil? - ShortURL.shorten(url) + if ARGV.empty? + puts op + exit(-1) + end + + url = ARGV.shift + shorturl = if ShortURL.valid_services.include?(service) + ShortURL.shorten(url, service) else - if ShortURL.valid_services.include?(service.to_sym) - ShortURL.shorten(url, service.to_sym) - else - puts "Invalid service" - exit(-1) - end + puts "Invalid service" + exit(-1) end - puts shorturl - + puts shorturl || "Unable to get shortened URL. Maybe the URL is already too short?" end main