Sha256: efd8d7cb8189c8c62be6d3587758dc5b7412ae2afb1157e78ca8a317b9fe91bb

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

# encoding: utf-8

require "thor"

module Antelope

  # Handles the command line interface.
  class CLI < Thor

    class_option :verbose, default: false, type: :boolean

    option :type, default: nil, type: :string,
      desc: "The type of generator to use"
    desc "compile FILE [FILE]*", "compile the given files"

    # Compile.
    def compile(*files)
      files.each do |file|
        compile_file(file)
      end
    end

    desc "check FILE [FILE]*", "check the syntax of the given files"

    # Check.
    def check(*files)
      files.each do |file|
        compile_file(file, [Generator::Null])
      end
    end

    desc "version", "Displays the running version of antelope"
    def version
      puts "Antelope version #{Antelope::VERSION}"
    end

    private

    # Compiles the given file, and then generates.  If an error
    # occurs, it prints it out to stderr, along with a backtrace if
    # the verbose flag was set.
    #
    # @param file [String] the file to compile.
    # @param gen [Array, Symbol] the generator to use.
    # @return [void]
    def compile_file(file, gen = :guess)
      puts "Compiling #{file}... "

      grammar = Grammar.from_file(file)
      grammar.generate(options, gen)

    rescue => e
      $stderr.puts "Error while compiling: #{e.class}: #{e.message}"

      if options[:verbose]
        $stderr.puts e.backtrace[0..10].map { |_| "\t#{_}" }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
antelope-0.3.2 lib/antelope/cli.rb
antelope-0.3.0 lib/antelope/cli.rb