Method: Ccfg::Handler#handler

Defined in:
lib/ccfg/handler.rb

#handler(global_options, args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ccfg/handler.rb', line 45

def handler ( global_options, args )
  if args.size == 0 then
    puts "Error: to few arguments for command."
    exit
  end
  input = args[0]
  args.delete_at(0)
  cconfigure_definition_list = {}
  # Get options from STDIN.
  args.each do |a|
    keyval = []
    if a.include? '=' then
      keyval = a.split('=')
    else
      keyval << a
      keyval << ""
    end
    cconfigure_definition_list[keyval[0]] = keyval[1]
  end
  # Set output file from option or STDOUT.
  if global_options[:output].value != nil then
    fout = File.new( global_options[:output].value, 'w' )
  end
  # Start to parse lines.
  File.open( input, 'r' ).readlines.each do |line|
    line = parse_line line, cconfigure_definition_list
    if global_options[:output].value != nil
      fout.puts line
    else
      puts line
    end
  end
  # Close file if option will be set.
  if global_options[:output].value != nil then
    fout.close
  end
end