# class Byebug::Runner

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

### Attributes

help[RW]

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

remote[RW]

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

version[RW]

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

### Public Class Methods

new(stop = true, quit = true) click to toggle source

@param stop [Boolean] Whether the runner should stop right before starting the program.

@param quit [Boolean] Whether the runner should quit right after finishing the program.

    # File lib/byebug/runner.rb, line 38
    def initialize(stop = true, quit = true)
      @stop, @quit = stop, quit
    end

### Public Instance Methods

banner() click to toggle source

Usage banner.

    # File lib/byebug/runner.rb, line 45
        def banner
          "
              byebug #{Byebug::VERSION}

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

    ".gsub(/^ {8}/, '')
        end

run() click to toggle source

Starts byebug to debug a program

    # File lib/byebug/runner.rb, line 58
    def run
      prepare_options.order!($ARGV)

      if version
        Byebug.puts("\n  Running byebug #{version}\n")
        return
      end

      if help
        Byebug.puts("#{help}\n")
        return
      end

      if remote
        Byebug.start_client(*remote)
        return
      end

      setup_cmd_line_args

      loop do
        debug_program

        break if @quit

        processor = Byebug::ControlCommandProcessor.new
        processor.process_commands
      end
    end

### Private Instance Methods

debug_program() click to toggle source

Debugs a script only if syntax checks okay.

    # File lib/byebug/runner.rb, line 162
    def debug_program
      ok = syntax_valid?(File.read($PROGRAM_NAME))
      fail(InvalidScript, 'The script has incorrect syntax') unless ok

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

prepare_options() click to toggle source

Processes options passed from the command line

    # File lib/byebug/runner.rb, line 93
    def prepare_options
      OptionParser.new(banner, 25) do |opts|
        opts.banner = banner

        opts.on '-d', '--debug', 'Set $DEBUG=true' do
          $DEBUG = true
        end

        opts.on('-I', '--include list', 'Add to paths to $LOAD_PATH') do |list|
          $LOAD_PATH.push(list.split(':')).flatten!
        end

        opts.on '-m', '--[no-]post-mortem', 'Use post-mortem mode' do |v|
          Setting[:post_mortem] = v
        end

        opts.on '-q', '--[no-]quit', 'Quit when script finishes' do |v|
          @quit = v
        end

        opts.on '-x', '--[no-]rc', 'Run byebug initialization file' do |v|
          Byebug.run_init_script if v
        end

        opts.on '-s', '--[no-]stop', 'Stop when script is loaded' do |v|
          @stop = v
        end

        opts.on '-r', '--require file', 'Require library before script' do |lib|
          require lib
        end

        opts.on '-R', '--remote [host:]port', 'Remote debug [host:]port' do |p|
          self.remote = Byebug.parse_host_and_port(p)
        end

        opts.on '-t', '--[no-]trace', 'Turn on line tracing' do |v|
          Setting[:linetrace] = v
        end

        opts.on '-v', '--version', 'Print program version' do
          self.version = VERSION
        end

        opts.on('-h', '--help', 'Display this message') do
          self.help = opts.help
        end
      end
    end

setup_cmd_line_args() click to toggle source

Extracts debugged program from command line args

    # File lib/byebug/runner.rb, line 146
    def setup_cmd_line_args
      Byebug.mode = :standalone

      fail(NoScript, 'You must specify a program to debug...') if $ARGV.empty?

      program = which($ARGV.shift)
      program = which($ARGV.shift) if program == which('ruby')
      fail(NonExistentScript, "The script doesn't exist") unless program

      $PROGRAM_NAME = program
    end

which(cmd) click to toggle source

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

    # File lib/byebug/runner.rb, line 174
    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