Class: Grizzled::Grinc::GrincRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/grizzled/grinc.rb

Instance Method Summary collapse

Constructor Details

#initializeGrincRunner

Returns a new instance of GrincRunner.



78
79
# File 'lib/grizzled/grinc.rb', line 78

def initialize
end

Instance Method Details

#parse_paramsObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/grizzled/grinc.rb', line 110

def parse_params
  options_hash = {}
  error = nil
  option_parser = OptionParser.new do |opts|
    opts.program_name = PROGRAM_NAME
    opts.banner = "Usage: #{opts.program_name} [OPTIONS] inputfile ..."
    opts.separator ''
    opts.separator 'OPTIONS:'

    opts.on('-o FILE', 'Output file. Default: standard output.') do |f|
      options_hash[:output] = f
    end

    opts.on('-n', '--nesting n',
            "Max nesting. Default: #{DEFAULT_MAX_NEST}") do |n|
      if n !~ /^[0-9]+$/
        error = "Non-numeric parameter \"#{n}\" to -n option."
      end
      options_hash[:max_nesting] = n.to_i
    end
  end

  begin
    option_parser.parse!(ARGV)
  rescue OptionParser::InvalidOption => ex
    error = ex.to_s
  end

  if error
    $stderr.puts(error) unless error.nil?
    option_parser.display
    raise UsageError.new
  end

  if ARGV.length == 0
    options_hash[:input_files] = nil
  else
    options_hash[:input_files] = ARGV
  end

  Parameters.new(options_hash, ARGV)
end

#runObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/grizzled/grinc.rb', line 81

def run
  begin
    params = parse_params
    out = params.output.nil? ? $stderr : File.open(params.output, 'w')

    if params.input_paths.nil?
      process_include($stdin, out, params.max_nesting)
    else
      params.input_paths.each do |f|
        process_include(File.open(f), out, params.max_nesting)
      end
    end

  rescue UsageError
    return 1

  rescue Interrupt
    $stderr.puts("\nAborted")
    return 1

  rescue
    $stderr.puts("#{PROGRAM_NAME}: #{$!}")
    return 1

  else
    return 0
  end
end