#!/usr/bin/env ruby $:.unshift File.dirname(__FILE__) $:.unshift File.join(File.dirname(__FILE__), "../lib") require "optparse" require "ostruct" require "yaml" require "yaml-cv" require "filewatcher" class Options def self.initialize @@options = OpenStruct.new @@options.file_name = "" @@options.watch = false @@options.html_file = "" @@options.pdf_file = "" end def self.parser OptionParser.new do |opts| opts.banner = "Usage: yaml-cv [options]" opts.on("-w", "--watch", "Watch the input file for changes and automatically regenerate the CV.") do |watch| @@options.watch = watch end opts.on("--html [HTML_FILE]", "The output path to the generated HTML file.") do |html| @@options.html_file = html end opts.on("--pdf [PDF_FILE]", "The output path to the generated PDF file.") do |pdf| @@options.pdf_file = pdf end opts.on_tail("-h", "--help", "Print this message and exit.") do puts opts exit 1 end end end def self.parse(args) initialize parser.parse!(args) @@options.file_name = ARGV.pop if !@@options.file_name puts self.parser exit 1 end end def self.options @@options end end def generate() cv = CV.new Options.options.file_name if !Options.options.html_file.to_s.empty? cv.write_html Options.options.html_file end if !Options.options.pdf_file.to_s.empty? cv.write_pdf Options.options.pdf_file end if Options.options.html_file.to_s.empty? and Options.options.pdf_file.to_s.empty? puts cv.render end end Options.parse(ARGV) generate if Options.options.watch watchlist = [ Options.options.file_name, File.join(File.dirname(__FILE__), "../lib/**/*") ] Filewatcher.new(watchlist, spinner: true).watch do |filename, event| puts "Input file updated." begin generate rescue StandardError => e puts e.message end end end