class Byebug::Command - byebug-4.0.5 Documentation

# class Byebug::Command

Parent class of all byebug commands.

Subclasses need to implement a `regexp` and an `execute` command.

### Constants

Subcmd

### Attributes

allow_in_control[RW]
allow_in_post_mortem[W]
always_run[W]

### Public Class Methods

new(state) click to toggle source

    # File lib/byebug/command.rb, line 19
    def initialize(state)
      @match, @state = nil, state
    end

### Protected Class Methods

allow_in_post_mortem() click to toggle source

    # File lib/byebug/command.rb, line 73
    def allow_in_post_mortem
      !defined?(@allow_in_post_mortem) ? true : false
    end

always_run() click to toggle source

    # File lib/byebug/command.rb, line 77
    def always_run
      @always_run ||= 0
    end

commands() click to toggle source

    # File lib/byebug/command.rb, line 118
    def commands
      @commands ||= []
    end

find(subcmds, str) click to toggle source

    # File lib/byebug/command.rb, line 89
    def find(subcmds, str)
      str.downcase!
      subcmds.each do |subcmd|
        if (str.size >= subcmd.min) && (subcmd.name[0..str.size - 1] == str)
          return subcmd
        end
      end

      nil
    end

format_subcmd(subcmd_name) click to toggle source

    # File lib/byebug/command.rb, line 100
    def format_subcmd(subcmd_name)
      subcmd = find(self::Subcommands, subcmd_name)
      return "Invalid \"#{names.join('|')}\" "                 "subcommand \"#{args[1]}\"." unless subcmd

      "\n  #{subcmd.help}.\n\n"
    end

format_subcmds() click to toggle source

    # File lib/byebug/command.rb, line 108
    def format_subcmds
      header = names.join('|')
      s = "  List of \"#{header}\" subcommands:\n  --\n"
      w = self::Subcommands.map(&:name).max_by(&:size).size
      self::Subcommands.each do |subcmd|
        s += format("  %s %-#{w}s -- %s\n", header, subcmd.name, subcmd.help)
      end
      s + "\n"
    end

help(subcmd = nil) click to toggle source

    # File lib/byebug/command.rb, line 81
    def help(subcmd = nil)
      return format_subcmd(subcmd) if subcmd

      output = description
      output += format_subcmds if defined? self::Subcommands
      output
    end

inherited(klass) click to toggle source

    # File lib/byebug/command.rb, line 122
    def inherited(klass)
      commands << klass
    end

### Public Instance Methods

match(input) click to toggle source

    # File lib/byebug/command.rb, line 23
    def match(input)
      @match = regexp.match(input)
    end

### Protected Instance Methods

bb_eval(str, b = get_binding) click to toggle source

Evaluates a string containing Ruby code, using binding `b`. In case of error full stack trace and error are printed.

    # File lib/byebug/command.rb, line 39
    def bb_eval(str, b = get_binding)
      b.eval(str)
    rescue StandardError, ScriptError => e
      at = e.backtrace
      locations = []
      locations << "#{at.shift}: #{e.class} Exception(#{e.message})"
      locations += at.map { |path| "\tfrom #{path}" }

      errmsg(pr('eval.exception', text_message: locations.join("\n")))
      nil
    end

bb_warning_eval(str, b = get_binding) click to toggle source

Evaluates a string containing Ruby code, using binding `b`. In case of error, an error message with the exception is printed.

    # File lib/byebug/command.rb, line 55
    def bb_warning_eval(str, b = get_binding)
      b.eval(str)
    rescue StandardError, ScriptError => e
      text_message = "#{e.class} Exception: #{e.message}"
      errmsg(pr('eval.exception', text_message: text_message))
      nil
    end

get_binding(pos = @state.frame) click to toggle source

    # File lib/byebug/command.rb, line 63
    def get_binding(pos = @state.frame)
      @state.context ? @state.context.frame_binding(pos) : TOPLEVEL_BINDING
    end