Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
executable file 36 lines (28 sloc) 694 Bytes
#!/usr/bin/env ruby
# frozen_string_literal: true
# urlgrep: Read input files and print lines that contain valid URIs/URLs.
require "optparse"
require "uri"
options = { urlonly: false }
OptionParser.new do |opts|
opts.banner = "Usage: urlgrep [options] [FILE ...]"
opts.on "-p", "--protocols PROTOS", Array do |protos|
options[:protos] = protos
end
opts.on "-u", "--[no-]url-only" do |urlonly|
options[:urlonly] = urlonly
end
end.parse!
pattern = URI.regexp options[:protos]
no_match = true
ARGF.each_line do |line|
line.match pattern do |m|
if options[:urlonly]
puts m[0]
else
puts line
end
no_match = false
end
end
exit 1 if no_match