Sha256: 5243998b0ae49f7538610a8b4e7ea615916f01a30502b5d9a29d96522c84e250

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

require 'nymphia'
require 'optparse'
require 'pathname'

class Nymphia::CLI
  def self.start(argv)
    new(argv).run
  end

  def initialize(argv)
    @argv = argv.dup
    parser.parse!(@argv)
  end

  def run
    validate_args!

    dsl_code_file = File.open(@file_path)
    absolute_dsl_file_path = File.absolute_path(dsl_code_file.path)
    dsl_code = dsl_code_file.read

    dsl = Nymphia::DSL.new(dsl_code, absolute_dsl_file_path)
    dsl.compile

    if @output_file_path
      File.open(@output_file_path, 'w') do |file|
        dsl.render(file)
      end
    else
      dsl.render(STDOUT)
    end
  end

  private

  def parser
    @parser ||= OptionParser.new do |opts|
      opts.banner = 'nymphia'
      opts.version = Nymphia::VERSION
      opts.on('-f', '--file=FILE', 'Your DSL code file') { |f| @file_path = f }
      opts.on('-o', '--output=FILE', 'Output file (default: stdout)') { |o| @output_file_path = o }
    end
  end

  def validate_args!
    unless @file_path
      raise ArgumentError.new('-f (--file) options is required.')
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nymphia-0.2.1 lib/nymphia/cli.rb
nymphia-0.2.0 lib/nymphia/cli.rb
nymphia-0.1.2 lib/nymphia/cli.rb