class Haml::Exec::Generic
:nodoc:
for all three executables.
A class that encapsulates the executable code
:nodoc:
It shouldn’t need to be invoked by client code.
such as command-line parsing stuff.
haml, sass, and haml2html executables,
This module contains code for working with the
def get_line(exception)
def get_line(exception) # SyntaxErrors have weird line reporting # when there's trailing whitespace, # which there is for Haml documents. return exception.message.scan(/:(\d+)/)[0] if exception.is_a?(::SyntaxError) exception.backtrace[0].scan(/:(\d+)/)[0] end
def initialize(args)
for all three executables.
A class that encapsulates the executable code
:nodoc:
It shouldn't need to be invoked by client code.
such as command-line parsing stuff.
haml, sass, and haml2html executables,
This module contains code for working with the
def initialize(args) @args = args @options = {} end
def open_file(filename, flag = 'r')
def open_file(filename, flag = 'r') return if filename.nil? File.open(filename, flag) end
def parse!
def parse! begin @opts = OptionParser.new(&method(:set_opts)) @opts.parse!(@args) process_result @options rescue Exception => e raise e if e.is_a? SystemExit $stderr.print "#{e.class} on line #{get_line e}: " if @options[:trace] $stderr.puts e.message e.backtrace[1..-1].each { |t| $stderr.puts " #{t}" } if @options[:trace] exit 1 end exit 0 end
def process_result
def process_result input, output = @options[:input], @options[:output] input_file, output_file = if input [nil, open_file(ARGV[0], 'w')] else [open_file(ARGV[0]), open_file(ARGV[1], 'w')] end input ||= input_file output ||= output_file input ||= $stdin output ||= $stdout @options[:input], @options[:output] = input, output end
def set_opts(opts)
def set_opts(opts) opts.on('-s', '--stdin', :NONE, 'Read input from standard input instead of an input file') do @options[:input] = $stdin end opts.on('--trace', :NONE, 'Show a full traceback on error') do @options[:trace] = true end opts.on_tail("-?", "-h", "--help", "Show this message") do puts opts exit end opts.on_tail("-v", "--version", "Print version") do puts("Haml #{::Haml.version[:string]}") exit end end
def to_s
def to_s @opts.to_s end