Module: Smacro::CLI

Defined in:
lib/smacro/cli.rb

Class Method Summary collapse

Class Method Details

.run(argv) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/smacro/cli.rb', line 6

def self.run(argv)
  options = {}
  parser = OptionParser.new do |opts|
    opts.banner = "Usage: smacro [options] app.rb [app.smacro.rb]"
    opts.on("-o", "--output FILE", "Write expanded Ruby to FILE") { |path| options[:output] = path }
    opts.on("-v", "--version", "Print version") { puts Smacro::VERSION; return }
    opts.on("-h", "--help", "Show help") { puts opts; return }
  end
  parser.parse!(argv)
  abort parser.to_s unless (1..2).cover?(argv.length)

  app, macro_file = argv
  macro_file ||= app.sub(/\.rb\z/, ".smacro.rb")
  load File.expand_path(macro_file) if File.exist?(macro_file)
  expanded = Smacro.expand(File.read(app))
  options[:output] ? File.write(options[:output], expanded) : print(expanded)
end