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 10 10: def initialize(args) 11: @args = args 12: @options = {} 13: 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 17 17: def parse! 18: begin 19: @opts = OptionParser.new(&method(:set_opts)) 20: @opts.parse!(@args) 21: 22: process_result 23: 24: @options 25: rescue Exception => e 26: raise e if @options[:trace] || e.is_a?(SystemExit) 27: 28: $stderr.puts e.message 29: exit 1 30: end 31: exit 0 32: end
@return [String] A description of the executable
# File lib/haml/exec.rb, line 35 35: def to_s 36: @opts.to_s 37: 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 46 46: def get_line(exception) 47: # SyntaxErrors have weird line reporting 48: # when there's trailing whitespace, 49: # which there is for Haml documents. 50: return exception.message.scan(/:(\d+)/).first.first if exception.is_a?(::SyntaxError) 51: exception.backtrace[0].scan(/:(\d+)/).first.first 52: 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 87 87: def process_result 88: input, output = @options[:input], @options[:output] 89: input_file, output_file = if input 90: [nil, open_file(@args[0], 'w')] 91: else 92: @options[:filename] = @args[0] 93: [open_file(@args[0]), open_file(@args[1], 'w')] 94: end 95: 96: input ||= input_file 97: output ||= output_file 98: input ||= $stdin 99: output ||= $stdout 100: 101: @options[:input], @options[:output] = input, output 102: 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 61 61: def set_opts(opts) 62: opts.on('-s', '--stdin', :NONE, 'Read input from standard input instead of an input file') do 63: @options[:input] = $stdin 64: end 65: 66: opts.on('--trace', :NONE, 'Show a full traceback on error') do 67: @options[:trace] = true 68: end 69: 70: opts.on_tail("-?", "-h", "--help", "Show this message") do 71: puts opts 72: exit 73: end 74: 75: opts.on_tail("-v", "--version", "Print version") do 76: puts("Haml #{::Haml.version[:string]}") 77: exit 78: end 79: end