#!/usr/bin/env ruby require 'rubygems' require_relative '../lib/livetext' def usage STDERR.puts <<~EOF Usage: livetext filename Read STDIN livetext -s livetext --stdin Show help: livetext livetext -h livetext --help Show path: livetext -p livetext --path Run tests: livetext -t livetext --test Show version: livetext -v livetext --version Turn on backtrace: livetext -b livetext --backtrace Install plugin: livetext -i filename.rb livetext -install filename.rb EOF exit end def version puts "#{Livetext::VERSION}\n " end def testing file = "#{Livetext::Path}/../test/test.rb" flag = @backtrace ? "-back" : "" cmd = "ruby #{file} cmdline#{flag}" puts cmd system(cmd) end def mixin_flag mod = ARGV.shift @live.mixin(ARGV.shift) end def read_stdin src = STDIN.read @live.process(src) end def install_plugin lib = ARGV.shift system("cp #{lib} #{Livetext::Path}/../../plugin/") end def parse_command_line usage if ARGV.empty? success = true loop do arg = ARGV.shift break if arg.nil? case arg when "-v", "--version"; version when "-t", "--test"; testing when "-p", "--path"; puts Livetext::Path when "-b", "--backtrace"; @backtrace = true when "-m", "--mixin" mixin_flag next when "-s", "--stdin"; read_stdin when "-h", "--help"; usage when "-i", "--install"; install_plugin when Object; @live.process_file(arg, true) # , @backtrace) else success = false STDERR.puts "Command line - #{arg.inspect} is unknown" end end @live.dump if success rescue => err STDERR.puts "Unexpected error! #{err.inspect}\n#{err.backtrace.join("\n")}" end # Main @live = Livetext.new @backtrace = false parse_command_line