Class: Peeek::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/peeek/cli.rb,
lib/peeek/cli/options.rb

Defined Under Namespace

Modules: EncodingOptions Classes: Help, Options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output, argv) ⇒ CLI

Initialize the CLI object.



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

def initialize(input, output, argv)
  @input   = input
  @output  = output
  @options = Options.new(argv)
rescue Help => e
  output.puts(e.message)
  exit
end

Instance Attribute Details

#optionsPeeek::CLI::Options (readonly)



23
24
25
# File 'lib/peeek/cli.rb', line 23

def options
  @options
end

Instance Method Details

#run(binding) ⇒ Peeek::Calls

Run the command or the program file with a binding. And capture calls that raised when running it.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/peeek/cli.rb', line 30

def run(binding)
  if Options.encoding_options_enabled?
    Encoding.default_external = @options.external_encoding
    Encoding.default_internal = @options.internal_encoding
  end

  $DEBUG   = @options.debug
  $VERBOSE = @options.verbose

  Dir.chdir(@options.working_directory) if @options.working_directory
  $LOAD_PATH.unshift(*@options.directories_to_load)
  @options.required_libraries.each(&method(:require))

  hook_targets = materialize_hook_targets(binding)
  process = setup_to_execute(binding)

  @output.puts("peeek-#{VERSION}") if @options.version_requested?

  original_stdout = $stdout
  $stdout = @output

  begin
    Peeek.capture(hook_targets, &process)
  rescue => e
    e.set_backtrace(e.backtrace.reject { |line| line =~ %r{lib/peeek} })
    raise e
  ensure
    $stdout = original_stdout
  end
end