Ruby 初心者スレッド Part 19
■ このスレッドは過去ログ倉庫に格納されています
0503498
2008/05/12(月) 00:17:51#!/usr/bin/env ruby
require 'optparse'
if $0 == __FILE__
printpattern = "%1$s:%2$s:%3$s" # filename:linnum:line
print = proc { |filename,linenum,line| STDOUT << (printpattern %[filename, linenum, line]) << "\n" }
filepatterns = wordpatterns = []
ARGV.options { |opt|
dest = wordpatterns
opt.on('-f' , '--filepattern') { dest = filepatterns }
opt.on('-w' , '--wordpattern') { dest = wordpatterns }
opt.on('-p S' , '--printpattern S') {|s| printpattern = s }
opt.order! { |v|
if wordpatterns.__id__ == dest.__id__ v = eval(v) end; dest << v
}
}
wordpatterns << /\n/
re = Regexp.union(*wordpatterns)
text = ''
filepatterns.each { |filepattern|
Dir.glob(filepattern) {|filepath|
next if !File.file?(filepath)
linenum = 1
text = IO.read(filepath)
text.scan(re) {|m|
linenum += $~[0].count("\n")
print.call(filepath, linenum, m) if "\n" != $~[-1]
}
}
}
end
■ このスレッドは過去ログ倉庫に格納されています