Class | Haml::Exec::Generic |
In: |
lib/haml/exec.rb
|
Parent: | Object |
An abstract class that encapsulates the executable code for all three executables.
@param args [Array<String>] The command-line arguments
# File lib/haml/exec.rb, line 11 11: def initialize(args) 12: @args = args 13: @options = {} 14: end
Parses the command-line arguments and runs the executable. Calls `Kernel#exit` at the end, so it never returns.
# File lib/haml/exec.rb, line 18 18: def parse! 19: begin 20: @opts = OptionParser.new(&method(:set_opts)) 21: @opts.parse!(@args) 22: 23: process_result 24: 25: @options 26: rescue Exception => e 27: raise e if @options[:trace] || e.is_a?(SystemExit) 28: 29: $stderr.puts e.message 30: exit 1 31: end 32: exit 0 33: end
@return [String] A description of the executable
# File lib/haml/exec.rb, line 36 36: def to_s 37: @opts.to_s 38: end
Finds the line of the source template on which an exception was raised.
@param exception [Exception] The exception @return [String] The line number
# File lib/haml/exec.rb, line 47 47: def get_line(exception) 48: # SyntaxErrors have weird line reporting 49: # when there's trailing whitespace, 50: # which there is for Haml documents. 51: return exception.message.scan(/:(\d+)/).first.first if exception.is_a?(::SyntaxError) 52: exception.backtrace[0].scan(/:(\d+)/).first.first 53: end
Processes the options set by the command-line arguments. In particular, sets `@options[:input]` and `@options[:output]` to appropriate IO streams.
This is meant to be overridden by subclasses so they can run their respective programs.
# File lib/haml/exec.rb, line 94 94: def process_result 95: input, output = @options[:input], @options[:output] 96: input_file, output_file = if input 97: [nil, open_file(@args[0], 'w')] 98: else 99: @options[:filename] = @args[0] 100: [open_file(@args[0]), open_file(@args[1], 'w')] 101: end 102: 103: input ||= input_file 104: output ||= output_file 105: input ||= $stdin 106: output ||= $stdout 107: 108: @options[:input], @options[:output] = input, output 109: end
Tells optparse how to parse the arguments available for all executables.
This is meant to be overridden by subclasses so they can add their own options.
@param opts [OptionParser]
# File lib/haml/exec.rb, line 62 62: def set_opts(opts) 63: opts.on('-s', '--stdin', :NONE, 'Read input from standard input instead of an input file') do 64: @options[:input] = $stdin 65: end 66: 67: opts.on('--trace', :NONE, 'Show a full traceback on error') do 68: @options[:trace] = true 69: end 70: 71: if RbConfig::CONFIG['host_os'] =~ /mswin|windows/i 72: opts.on('--unix-newlines', 'Use Unix-style newlines in written files.') do 73: @options[:unix_newlines] = true 74: end 75: end 76: 77: opts.on_tail("-?", "-h", "--help", "Show this message") do 78: puts opts 79: exit 80: end 81: 82: opts.on_tail("-v", "--version", "Print version") do 83: puts("Haml/Sass #{::Haml.version[:string]}") 84: exit 85: end 86: end