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

#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



47
48
49
50
# File 'lib/byebug/runner.rb', line 47

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.



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

def help
  @help
end

#init_scriptObject



70
71
72
# File 'lib/byebug/runner.rb', line 70

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

#interfaceObject



109
110
111
# File 'lib/byebug/runner.rb', line 109

def interface
  @interface ||= LocalInterface.new
end

#quitObject

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



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

def quit
  @quit
end

#remoteObject

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



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

def remote
  @remote
end

#stopObject

Signals that we should stop before program starts



33
34
35
# File 'lib/byebug/runner.rb', line 33

def stop
  @stop
end

#versionObject

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



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

def version
  @version
end

Instance Method Details

Usage banner.



77
78
79
80
81
82
83
# File 'lib/byebug/runner.rb', line 77

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

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

#debug_programObject

Debugs a script only if syntax checks okay.



178
179
180
181
# File 'lib/byebug/runner.rb', line 178

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)


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

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

#invalid_script?Boolean

Checks the debugged script has correct syntax

Returns:

  • (Boolean)


168
169
170
171
172
173
# File 'lib/byebug/runner.rb', line 168

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)


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

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)


158
159
160
161
162
163
# File 'lib/byebug/runner.rb', line 158

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)


134
135
136
# File 'lib/byebug/runner.rb', line 134

def non_script_option?
  version || help || remote
end

#option_parserObject

Processes options passed from the command line.



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

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



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

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

#programObject



124
125
126
127
128
129
# File 'lib/byebug/runner.rb', line 124

def program
  @program ||= begin
                 candidate = which($ARGV.shift)
                 candidate == which('ruby') ? which($ARGV.shift) : candidate
               end
end

#runObject

Starts byebug to debug a program.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/byebug/runner.rb', line 88

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.process_commands
  end
end