#!/usr/bin/env ruby # Copyright (c) 2023 Yegor Bugayenko # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the 'Software'), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. STDOUT.sync = true require 'slop' require 'loog' require 'backtrace' require_relative '../lib/damsi/version' require_relative '../lib/damsi/dfg' loog = Loog::REGULAR def config(path) f = File.expand_path(path) args = [] args += File.readlines(f).map(&:strip).reject { |a| a.empty? } if File.exist?(f) args end args = config('~/.damsi') + config('.damsi') + ARGV opts = Slop.parse(args, strict: true, help: true) do |o| o.banner = "Usage (#{Damsi::VERSION}): damsi [options] " o.bool '-h', '--help', 'Show these instructions' o.bool '--version', 'Show current version' end if opts.help? puts opts exit end if opts.version? loog.info(Damsi::VERSION) exit end tex = Class.new do def info(msg) puts msg end def debug(msg) puts '% ' + msg end end.new Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_8 begin input = opts.arguments[0] raise 'File name is required' if input.nil? Damsi::DFG.new(File.read(input)).simulate(tex) rescue StandardError => e loog.error(Backtrace.new(e)) exit -1 end