/bin/dietrb
Ruby | 63 lines | 56 code | 5 blank | 2 comment | 0 complexity | e9b6586c4dde6fca500c7ad551ddc63a MD5 | raw file
1#!/usr/bin/env ruby 2 3require 'irb' 4 5IRB_CONTEXT_TOPLEVEL_ARGS = [self, TOPLEVEL_BINDING.dup] 6 7module IRB 8 # Just a namespace so not to pollute the toplevel namespace with lvars. 9 module Bin 10 driver = nil 11 ignore_irbrc = false 12 13 unless ARGV.empty? 14 require 'optparse' 15 16 OptionParser.new do |opt| 17 bin = File.basename($0) 18 opt.banner = "Usage: #{bin} [options] [programfile] [arguments]" 19 opt.on("-f", "Ignore ~/.irbrc") { |i| ignore_irbrc = i } 20 opt.on("-r load-lib", "Loads the given library (same as `ruby -r')") { |l| require l } 21 opt.on("-d", "Set $DEBUG to true (same as `ruby -d')") { $DEBUG = true } 22 opt.on("-I path", "Add path to $LOAD_PATH") { |p| $LOAD_PATH.unshift(p) } 23 opt.on("--driver name", "As driver, use one of: tty, readline, or socket") { |d| driver = d } 24 opt.on("--noinspect", "Don't use inspect for output") { IRB.formatter.inspect = false } 25 opt.on("--simple-prompt", "Simple prompt mode") { IRB.formatter.prompt = :simple } 26 opt.on("--noprompt", "No prompt mode") { IRB.formatter.prompt = nil } 27 opt.on("-v", "--version", "Print the version of #{bin}") do 28 $stdout.puts IRB::VERSION::DESCRIPTION 29 exit 30 end 31 end.parse!(ARGV) 32 end 33 34 unless ignore_irbrc 35 irbrc = File.expand_path("~/.irbrc") 36 load(irbrc) if File.exist?(irbrc) 37 end 38 39 IRB.formatter.filter_from_backtrace << /^#{__FILE__}/ 40 41 if ARGV.empty? 42 if driver == 'socket' 43 require "irb/driver/socket" 44 IRB::Driver.redirect_output! do 45 irb(*IRB_CONTEXT_TOPLEVEL_ARGS) 46 end 47 else 48 driver ||= begin 49 require 'readline' 50 'readline' 51 rescue LoadError 52 'tty' 53 end 54 require "irb/driver/#{driver}" 55 irb(*IRB_CONTEXT_TOPLEVEL_ARGS) 56 end 57 else 58 path = ARGV.shift 59 context = IRB::Context.new(*IRB_CONTEXT_TOPLEVEL_ARGS) 60 File.open(path, 'r') { |f| f.each_line { |line| context.input_line(line) } } 61 end 62 end 63end