class CssCompare::Comparison

Public Class Methods

new(args) click to toggle source
# File lib/css_compare/exec.rb, line 6
def initialize(args)
  @args = args
  @options = {}
end

Public Instance Methods

parse() click to toggle source

Parses the command-line arguments and runs the executable.

# File lib/css_compare/exec.rb, line 27
def parse
  @opts = OptionParser.new { |opts| process_opts(opts) }
  @opts.parse!(@args)

  process_args

  @options
end
parse!() click to toggle source

Parses the command-line arguments and runs the executable. Calls ‘Kernel#exit` at the end, so it never returns.

@see parse

# File lib/css_compare/exec.rb, line 15
def parse!
  begin
    parse
  rescue StandardError => e
    raise e if @options[:trace] || e.is_a?(SystemExit)
    $stderr.puts "#{e.class}: " + e.message.to_s
    exit 1
  end
  exit 0
end

Protected Instance Methods

common_options(opts) click to toggle source
# File lib/css_compare/exec.rb, line 52
def common_options(opts)
  opts.on('-?', '-h', '--help', 'Show this help message.') do
    puts opts
    exit
  end

  opts.on('-v', '--version', 'Print the css_compare version.') do
    puts("v#{CssCompare::VERSION}")
    exit
  end
end
input_and_output(opts) click to toggle source

@todo: specify an option for outputting the diff, when feature is ready

# File lib/css_compare/exec.rb, line 65
def input_and_output(opts)
  # opts.separator ''
  # opts.separator 'Input and Output:'
end
open_file(filename, flag = 'r') click to toggle source
# File lib/css_compare/exec.rb, line 70
def open_file(filename, flag = 'r')
  return if filename.nil?
  File.open(filename, flag)
end
process_args() click to toggle source

Processes the options set by the command-line arguments - ‘@options` and `@options` are being set to appropriate IO streams.

This method is being overridden by subclasses to run their respective programs.

# File lib/css_compare/exec.rb, line 81
def process_args
  args = @args.dup
  @options[:operands] = nil
  unless args.length >= 2
    puts @opts
    exit 1
  end
  @options[:operands] = args.shift(2)
  @options[:output_filename] = args.shift unless args.empty?
  @options[:output] ||= @options[:output_filename] || $stdout

  run
end
process_opts(opts) click to toggle source

Tells optparse how to parse the arguments.

@param opts [OptionParser]

# File lib/css_compare/exec.rb, line 41
    def process_opts(opts)
      opts.banner = <<END
Usage: css_compare <CSS file> <CSS file>
Description:
Checks the equality of
END

      common_options(opts)
      input_and_output(opts)
    end
run() click to toggle source

Runs the comparison.

# File lib/css_compare/exec.rb, line 104
def run
  result = CssCompare::Engine.new(@options)
                             .parse!
                             .equal?
  write_output(result.to_s + "\n", @options[:output])
end
write_output(text, destination) click to toggle source
# File lib/css_compare/exec.rb, line 95
def write_output(text, destination)
  if destination.is_a?(String)
    open_file(destination, 'w') { |file| file.write(text) }
  else
    destination.write(text)
  end
end