Class: Byebug::Runner

Inherits:
Object
  • Object
show all
Includes:
Helpers::ParseHelper
Defined in:
lib/byebug/runner.rb

Overview

Responsible for starting the debugger when started from the command line.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::ParseHelper

#get_int, #parse_steps, #syntax_valid?

Constructor Details

#initialize(stop = true, quit = true) ⇒ Runner

starting the program.

finishing the program.

Parameters:

  • stop (Boolean) (defaults to: true)

    Whether the runner should stop right before

  • quit (Boolean) (defaults to: true)

    Whether the runner should quit right after



43
44
45
46
# File 'lib/byebug/runner.rb', line 43

def initialize(stop = true, quit = true)
  @stop = stop
  @quit = quit
end

Instance Attribute Details

#helpObject

Special working modes that don’t actually start the debugger.



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

def help
  @help
end

#init_scriptObject



66
67
68
# File 'lib/byebug/runner.rb', line 66

def init_script
  defined?(@init_script) ? @init_script : true
end

#interfaceObject



103
104
105
# File 'lib/byebug/runner.rb', line 103

def interface
  @interface ||= LocalInterface.new
end

#quitObject

Signals that we should exit after the debugged program is finished.



24
25
26
# File 'lib/byebug/runner.rb', line 24

def quit
  @quit
end

#remoteObject

Special working modes that don’t actually start the debugger.



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

def remote
  @remote
end

#stopObject

Signals that we should stop before program starts



29
30
31
# File 'lib/byebug/runner.rb', line 29

def stop
  @stop
end

#versionObject

Special working modes that don’t actually start the debugger.



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

def version
  @version
end

Instance Method Details

Usage banner.



73
74
75
76
77
78
79
80
81
# File 'lib/byebug/runner.rb', line 73

def banner
  <<-EOB.gsub(/^ {6}/, '')

    byebug #{Byebug::VERSION}

    Usage: byebug [options] <script.rb> -- <script.rb parameters>

  EOB
end

#debug_programObject

Debugs a script only if syntax checks okay.



173
174
175
176
# File 'lib/byebug/runner.rb', line 173

def debug_program
  error = Byebug.debug_load($PROGRAM_NAME, stop)
  puts "#{error}\n#{error.backtrace}" if error
end

#error_in_script?Boolean

There is an error with the specified script

Returns:

  • (Boolean)


128
129
130
# File 'lib/byebug/runner.rb', line 128

def error_in_script?
  no_script? || non_existing_script? || invalid_script?
end

#invalid_script?Boolean

Checks the debugged script has correct syntax

Returns:

  • (Boolean)


163
164
165
166
167
168
# File 'lib/byebug/runner.rb', line 163

def invalid_script?
  return false if syntax_valid?(File.read($PROGRAM_NAME))

  print_error('The script has incorrect syntax')
  true
end

#no_script?Boolean

No script to debug specified

Returns:

  • (Boolean)


135
136
137
138
139
140
# File 'lib/byebug/runner.rb', line 135

def no_script?
  return false unless $ARGV.empty?

  print_error('You must specify a program to debug')
  true
end

#non_existing_script?Boolean

Extracts debugged program from command line args.

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/byebug/runner.rb', line 145

def non_existing_script?
  Byebug.mode = :standalone

  program = which($ARGV.shift)
  program = which($ARGV.shift) if program == which('ruby')

  if program
    $PROGRAM_NAME = program
    false
  else
    print_error("The script doesn't exist")
    true
  end
end

#non_script_option?Boolean

An option that doesn’t need a script specified was given

Returns:

  • (Boolean)


121
122
123
# File 'lib/byebug/runner.rb', line 121

def non_script_option?
  version || help || remote
end

#option_parserObject

Processes options passed from the command line.



110
111
112
113
114
115
116
# File 'lib/byebug/runner.rb', line 110

def option_parser
  @option_parser ||= OptionParser.new(banner, 25) do |opts|
    opts.banner = banner

    OptionSetter.new(self, opts).setup
  end
end

Prints an error message and a help string



199
200
201
202
# File 'lib/byebug/runner.rb', line 199

def print_error(msg)
  interface.errmsg(msg)
  interface.puts(option_parser.help)
end

#runObject

Starts byebug to debug a program.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/byebug/runner.rb', line 86

def run
  option_parser.order!($ARGV)
  return if non_script_option? || error_in_script?

  Byebug.run_init_script if init_script

  loop do
    debug_program

    break if quit

    ControlProcessor.new.process_commands
  end
end

#which(cmd) ⇒ Object

Cross-platform way of finding an executable in the $PATH. Borrowed from: stackoverflow.com/questions/2108727



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/byebug/runner.rb', line 182

def which(cmd)
  return File.expand_path(cmd) if File.exist?(cmd)

  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end

  nil
end