Class: Ripl::Runner

Inherits:
Object
  • Object
show all
Extended by:
API
Defined in:
lib/ripl/runner.rb

Defined Under Namespace

Modules: API

Constant Summary collapse

OPTIONS_ARR =
%w{-f -F -d -I -r -v -h}
OPTIONS =
{
  '-f' => ['-f', 'Suppress loading ~/.irbrc'],
  '-F' => ['-F', 'Suppress loading ~/.riplrc'],
  '-d' => ['-d, --debug', "Set $DEBUG to true (same as `ruby -d')"],
  '-I' => ['-I PATH', "Add to front of $LOAD_PATH. Delimit multiple paths with ':'"],
  '-r' => ['-r, --require FILE', "Require file (same as `ruby -r')"],
  '-v' => ['-v, --version', 'Print version'],
  '-h' => ['-h, --help', 'Print help']
}
MESSAGES =
{
  'usage' => 'Usage', 'options' => 'Options', 'args' => 'ARGS',
  'command' => 'COMMAND', 'run_command' => "`%s' is not a %s command.",
  'start' => "Unused arguments", 'load_rc' => 'Error while loading %s',
  'parse_option' => 'invalid option'
}

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from API

format_error, help, parse_option, parse_options

Class Attribute Details

.appObject

Returns the value of attribute app.



19
20
21
# File 'lib/ripl/runner.rb', line 19

def app
  @app
end

.argvObject

Returns the value of attribute argv.



19
20
21
# File 'lib/ripl/runner.rb', line 19

def argv
  @argv
end

Class Method Details

.add_options(*options) ⇒ Object

Adds commandline options for –help



23
24
25
26
27
28
# File 'lib/ripl/runner.rb', line 23

def self.add_options(*options)
  options.each {|e|
    OPTIONS[e[0][/-\w+/]] = e
    OPTIONS_ARR << e[0][/-\w+/]
  }
end

.load_rc(file) ⇒ Object



52
53
54
55
56
# File 'lib/ripl/runner.rb', line 52

def self.load_rc(file)
  load file if File.exists?(File.expand_path(file))
rescue StandardError, SyntaxError, LoadError
  $stderr.puts "#{app}: #{MESSAGES['load_rc'] % file}:", format_error($!)
end

.run(argv = ARGV) ⇒ Object



30
31
32
# File 'lib/ripl/runner.rb', line 30

def self.run(argv=ARGV)
  argv[0].to_s[/^[^-]/] ? run_command(argv) : start(:argv => argv)
end

.run_command(argv) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/ripl/runner.rb', line 34

def self.run_command(argv)
  exec "#{app}-#{cmd = argv.shift}", *argv
rescue SystemCallError
  raise unless $!.message =~ /No such file or directory.*#{app}-(\w+)/ ||
    $!.message.include?("Invalid argument - execvp(2) failed")
  abort MESSAGES['run_command'] % [cmd, app]
end

.start(options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/ripl/runner.rb', line 42

def self.start(options={})
  @argv = options.delete(:argv) || ARGV
  argv = @argv.dup
  load_rc(Ripl.config[:riplrc]) unless argv.delete('-F') || options[:riplrc] == false
  argv.each {|e| e[/^-/] ? break : argv.shift } if $0[/#{app}-\w+$/]
  parse_options(argv) if $0[/#{app}$|#{app}-\w+$/]
  warn "#{app}: #{MESSAGES['start']}: #{argv.inspect}" if !argv.empty?
  Ripl.shell(options).loop
end