Class: Peruby::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/peruby/cli.rb

Overview

Command-line entry point for the interpreter.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, input:, out:, err:) ⇒ CLI

Returns a new instance of CLI.



13
14
15
16
17
18
19
# File 'lib/peruby/cli.rb', line 13

def initialize(argv, input:, out:, err:)
  @argv = argv
  @input = input
  @out = out
  @err = err
  @debug = ENV.fetch('PERUBY_DEBUG', '').split(',')
end

Class Method Details

.run(argv, input: $stdin, out: $stdout, err: $stderr) ⇒ Object



9
10
11
# File 'lib/peruby/cli.rb', line 9

def self.run(argv, input: $stdin, out: $stdout, err: $stderr)
  new(argv, input:, out:, err:).run
end

Instance Method Details

#runObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/peruby/cli.rb', line 21

def run
  parser.order!(@argv)
  return 0 if @shown_version
  return dump_tokens if @dump_tokens
  return dump_ast if @dump_ast
  return dump_ops if @dump_ops

  execute
rescue OptionParser::ParseError => e
  @err.puts e.message
  1
rescue CompileError => e
  @err.puts e.message
  @err.puts "Execution of #{@file} aborted due to compilation errors." if @file
  1
rescue PerlExit => e
  status = @runtime && @runtime.stash.glob('?').scalar.get
  status.nil? ? e.status : Conv.to_num(status).to_i
rescue PerlError => e
  @err.print(e.formatted? ? e.message : @runtime.error_message(e.message, file: @file))
  1
end