Class: MemoryProfiler::CLI

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

Constant Summary collapse

BIN_NAME =
"ruby-memory-profiler"
VERSION_INFO =
"#{BIN_NAME}  #{MemoryProfiler::VERSION}"
STATUS_SUCCESS =
0
STATUS_ERROR =
1
DEFAULTS =
{
  ignore_files: "memory_profiler/lib"
}.freeze

Instance Method Summary collapse

Instance Method Details

#run(argv) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/memory_profiler/cli.rb', line 17

def run(argv)
  options = {}
  parser = option_parser(options)
  parser.parse!(argv)

  options = DEFAULTS.merge(options)

  # Make sure the user specified at least one file
  unless (script = argv.shift)
    puts parser
    puts ""
    puts "#{VERSION_INFO}  |  ERROR: Must specify a script to run"
    return STATUS_ERROR
  end

  if script == "run"
    # We are profiling a command.
    profile_command(options, argv)
  else
    # We are profiling a ruby file.
    begin
      MemoryProfiler.start(options)
      load(script)
    ensure
      report = MemoryProfiler.stop
      report.pretty_print(**options)
    end
    STATUS_SUCCESS
  end
rescue OptionParser::InvalidOption, OptionParser::InvalidArgument, OptionParser::MissingArgument => e
  puts parser
  puts e.message
  STATUS_ERROR
end