Skip to content

Instantly share code, notes, and snippets.

@844196
Created February 6, 2016 21:49
Show Gist options
  • Save 844196/801d7bbe62015051b207 to your computer and use it in GitHub Desktop.
Save 844196/801d7bbe62015051b207 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'optparse'
require 'logger'
Version = 'v0.1.0'
Release = '2016-02-07'
Signal.trap(:INT) { exit(1) }
logger = Logger.new(STDERR)
option = {:log_level => :warn} # defalut
status = true
OptionParser.new do |opt|
opt.banner = "使用法: #{opt.program_name} [オプション] ファイル"
opt.on('-L', '--log-level=LEVEL', '出力するログレベルを指定') {|l| option[:log_level] = l.downcase.to_sym }
opt.on('-h', '--help', 'この使い方を表示して終了') { puts opt.help; exit }
opt.on('-v', '--version', 'バージョン情報を表示して終了') { puts opt.ver; exit }
opt.parse! rescue (warn($!.message); exit(1))
end
logger.level = option[:log_level]
define_method(:parse) do |line|
line.chomp
.split("\t")
.map {|pair| pair.split(':', 2) }
.map {|(key,val)| [key&.to_sym, val] }
.to_h
.tap {|hash| logger.debug "Parsed line -- #{hash}" }
end
define_method(:check_header) do |hash|
unless hash[:filetype] == 'polyaness_dict'
logger.warn "No exist metainfo line on header"
status = false
end
end
define_method(:check_line) do |hash, lineno|
unless hash[:speaker] && hash[:quote]
logger.warn "No exist `speaker' or `quote' column (line: #{lineno})"
status = false
end
end
ARGF.each.with_index(1) do |line, lineno|
case lineno
when 1
check_header parse(line)
else
check_line parse(line), lineno
end
end
exit(status ? 0 : 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment