#!/usr/bin/env ruby require 'rubygems' require_relative '../lib/livetext' def usage STDERR.puts <<~EOF Usage: livetext [options] [filename] Options: -v, --version Print version information -t, --test Run all tests -p, --path Print the path of the installed gem -b, --backtrace Enable backtrace information -m name, --mixin name Load the specified mixin before processing -s, --stdin Read from standard input -h, --help Print this help information -i name, --install name Install the specified plugin EOF exit end def old_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 Load a mixin: livetext -m name livetext --mixin name 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/all.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