Sha256: 4f498305746b78d853ceb19a32b8f6961dc9fac8bdd41647182075eca9dbdd0c

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 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

    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 = Ace::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

4 entries across 4 versions & 1 rubygems

Version Path
antelope-0.2.0 lib/antelope/cli.rb
antelope-0.1.11 lib/antelope/cli.rb
antelope-0.1.10 lib/antelope/cli.rb
antelope-0.1.9 lib/antelope/cli.rb