class Haml::Exec::Generic
An abstract class that encapsulates the executable code for all three executables.
def get_line(exception)
-
(String)
- The line number
Parameters:
-
exception
(Exception
) -- The 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+)/).first.first if exception.is_a?(::SyntaxError) exception.backtrace[0].scan(/:(\d+)/).first.first end
def initialize(args)
-
args
(Array
) -- The command-line arguments
def initialize(args) @args = args @options = {} end
def open_file(filename, flag = 'r')
def open_file(filename, flag = 'r') return if filename.nil? flag = 'wb' if @options[:unix_newlines] && flag == 'w' File.open(filename, flag) end
def parse!
Parses the command-line arguments and runs the executable.
def parse! begin @opts = OptionParser.new(&method(:set_opts)) @opts.parse!(@args) process_result @options rescue Exception => e raise e if @options[:trace] || e.is_a?(SystemExit) $stderr.puts e.message exit 1 end exit 0 end
def process_result
This is meant to be overridden by subclasses
to appropriate IO streams.
In particular, sets `@options[:input]` and `@options[:output]`
Processes the options set by the command-line arguments.
def process_result input, output = @options[:input], @options[:output] input_file, output_file = if input [nil, open_file(@args[0], 'w')] else @options[:filename] = @args[0] [open_file(@args[0]), open_file(@args[1], 'w')] end input ||= input_file output ||= output_file input ||= $stdin output ||= $stdout @options[:input], @options[:output] = input, output end
def set_opts(opts)
-
opts
(OptionParser
) --
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 if RbConfig::CONFIG['host_os'] =~ /mswin|windows/i opts.on('--unix-newlines', 'Use Unix-style newlines in written files.') do @options[:unix_newlines] = true end end opts.on_tail("-?", "-h", "--help", "Show this message") do puts opts exit end opts.on_tail("-v", "--version", "Print version") do puts("Haml/Sass #{::Haml.version[:string]}") exit end end
def to_s
-
(String)
- A description of the executable
def to_s @opts.to_s end