Class: Byebug::Runner

Inherits:
Object
  • Object
show all
Includes:
Helpers::BinHelper, Helpers::ParseHelper, Helpers::StringHelper
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::StringHelper

#camelize, #deindent, #prettify

Methods included from Helpers::ParseHelper

#get_int, #parse_steps, #syntax_valid?

Methods included from Helpers::BinHelper

#executable_file_extensions, #find_executable, #real_executable?, #search_paths, #which

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



49
50
51
52
# File 'lib/byebug/runner.rb', line 49

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.



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

def help
  @help
end

#init_scriptObject



74
75
76
# File 'lib/byebug/runner.rb', line 74

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

#quitObject

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



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

def quit
  @quit
end

#remoteObject

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



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

def remote
  @remote
end

#stopObject

Signals that we should stop before program starts



35
36
37
# File 'lib/byebug/runner.rb', line 35

def stop
  @stop
end

#versionObject

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



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

def version
  @version
end

Instance Method Details

Usage banner.



81
82
83
84
85
86
87
# File 'lib/byebug/runner.rb', line 81

def banner
  prettify <<-BANNER
    byebug #{Byebug::VERSION}

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

#debug_programObject

Debugs a script only if syntax checks okay.



185
186
187
188
# File 'lib/byebug/runner.rb', line 185

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

#error_in_script?Boolean

There is an error with the specified script

Returns:

  • (Boolean)


148
149
150
# File 'lib/byebug/runner.rb', line 148

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

#interfaceObject



111
112
113
# File 'lib/byebug/runner.rb', line 111

def interface
  @interface ||= Context.interface
end

#invalid_script?Boolean

Checks the debugged script has correct syntax

Returns:

  • (Boolean)


175
176
177
178
179
180
# File 'lib/byebug/runner.rb', line 175

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

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

#no_script?Boolean

No script to debug specified

Returns:

  • (Boolean)


155
156
157
158
159
160
# File 'lib/byebug/runner.rb', line 155

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)


165
166
167
168
169
170
# File 'lib/byebug/runner.rb', line 165

def non_existing_script?
  return false if program

  print_error("The script doesn't exist")
  true
end

#non_script_option?Boolean

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

Returns:

  • (Boolean)


141
142
143
# File 'lib/byebug/runner.rb', line 141

def non_script_option?
  version || help || remote
end

#option_parserObject

Processes options passed from the command line.



118
119
120
121
122
123
124
# File 'lib/byebug/runner.rb', line 118

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



193
194
195
196
# File 'lib/byebug/runner.rb', line 193

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

#programObject



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/byebug/runner.rb', line 126

def program
  @program ||= begin
                 candidate = which($ARGV.shift)

                 if [which("ruby"), RbConfig.ruby].include?(candidate)
                   which($ARGV.shift)
                 else
                   candidate
                 end
               end
end

#runObject

Starts byebug to debug a program.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/byebug/runner.rb', line 92

def run
  Byebug.mode = :standalone

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

  $PROGRAM_NAME = program

  Byebug.run_init_script if init_script

  loop do
    debug_program

    break if quit

    ControlProcessor.new(nil, interface).process_commands
  end
end