class Grizzled::Grinc::GrincRunner

Public Class Methods

new() click to toggle source
# File lib/grizzled/grinc.rb, line 83
def initialize
end

Public Instance Methods

run() click to toggle source
# File lib/grizzled/grinc.rb, line 86
def run
  begin
    params = parse_params
    if params.show_version
      show_version_only
    else
      run_includer params
    end

  rescue UsageError
    return 1

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

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

  else
    return 0
  end
end

Private Instance Methods

parse_params() click to toggle source
# File lib/grizzled/grinc.rb, line 135
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

    opts.on('-V', '--version', 'Display version number and exit.') do
      options_hash[:show_version] = true
    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
process_include(input_file, output_file, max_nesting = 100) click to toggle source
# File lib/grizzled/grinc.rb, line 184
def process_include(input_file, output_file, max_nesting = 100)
  Includer.new(input_file, :max_nesting => max_nesting).each do |line|
    output_file.write(line)
  end
end
run_includer(params) click to toggle source
# File lib/grizzled/grinc.rb, line 123
def run_includer(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
end
show_version_only() click to toggle source
# File lib/grizzled/grinc.rb, line 113
def show_version_only
  gem_spec = Gem.searcher.find(THIS_GEM_PATH)
  if not gem_spec
    raise StandardError.new("Can't find Gem specification for path \"" +
                            "#{THIS_GEM_PATH}\".")
  end
  puts("#{PROGRAM_NAME}, version #{gem_spec.version}. " +
       "#{gem_spec.homepage}")
end