Sha256: 8dadf4d1a68d09f93b4d22f748fd6d76fda7c6906049535681b5b3d227d8e368

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

require 'slim'
require 'optparse'

module Slim
  # Slim commandline interface
  # @api private
  class Command
    def initialize(args)
      @args = args
      @options = {}
    end

    # Run command
    def run
      @opts = OptionParser.new(&method(:set_opts))
      @opts.parse!(@args)
      process
      exit 0
    rescue Exception => ex
      raise ex if @options[:trace] || SystemExit === ex
      $stderr.print "#{ex.class}: " if ex.class != RuntimeError
      $stderr.puts ex.message
      $stderr.puts '  Use --trace for backtrace.'
      exit 1
    end

    private

    # Configure 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

      opts.on('-c', '--compile', :NONE, 'Compile only but do not run') do
        @options[:compile] = true
      end

      opts.on_tail('-h', '--help', 'Show this message') do
        puts opts
        exit
      end

      opts.on_tail('-v', '--version', 'Print version') do
        puts "Slim #{Slim.version}"
        exit
      end
    end

    # Process command
    def process
      args = @args.dup
      unless @options[:input]
        file = args.shift
        if file
          @options[:file] = file
          @options[:input] = File.open(file, 'r')
        else
          @options[:file] = 'STDIN'
          @options[:input] = $stdin
        end
      end

      unless @options[:output]
        file = args.shift
        @options[:output] = file ? File.open(file, 'w') : $stdout
      end

      if @options[:compile]
        @options[:output].puts(Slim::Engine.new(:file => @options[:file]).compile(@options[:input].read))
      else
        @options[:output].puts(Slim::Template.new(@options[:file]) { @options[:input].read }.render)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slim-0.7.1 lib/slim/command.rb
slim-0.7.0 lib/slim/command.rb