Class: Byebug::Command

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/byebug/command.rb

Overview

Parent class of all byebug commands.

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

Defined Under Namespace

Classes: Subcmd

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state) ⇒ Command

Returns a new instance of Command.



14
15
16
# File 'lib/byebug/command.rb', line 14

def initialize(state)
  @match, @state = nil, state
end

Class Attribute Details

.allow_in_controlObject

Returns the value of attribute allow_in_control.



52
53
54
# File 'lib/byebug/command.rb', line 52

def allow_in_control
  @allow_in_control
end

.allow_in_post_mortemObject



55
56
57
# File 'lib/byebug/command.rb', line 55

def allow_in_post_mortem
  !defined?(@allow_in_post_mortem) ? true : false
end

.always_runObject



59
60
61
# File 'lib/byebug/command.rb', line 59

def always_run
  @always_run ||= 0
end

Class Method Details

.commandsObject



102
103
104
# File 'lib/byebug/command.rb', line 102

def commands
  @commands ||= []
end

.find(subcmds, str) ⇒ Object



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

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) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/byebug/command.rb', line 84

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_subcmdsObject



92
93
94
95
96
97
98
99
100
# File 'lib/byebug/command.rb', line 92

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(args = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/byebug/command.rb', line 63

def help(args = nil)
  if args && args[1]
    output = format_subcmd(args[1])
  else
    output = description.gsub(/^ +/, '') + "\n"
    output += format_subcmds if defined? self::Subcommands
  end
  output
end

.inherited(klass) ⇒ Object



106
107
108
# File 'lib/byebug/command.rb', line 106

def inherited(klass)
  commands << klass
end

.load_commandsObject



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

def load_commands
  Dir.glob(File.expand_path('../commands/*.rb', __FILE__)).each do |file|
    require file
  end

  Byebug.constants.grep(/Functions$/).map do |name|
    include Byebug.const_get(name)
  end
end

Instance Method Details

#match(input) ⇒ Object



18
19
20
# File 'lib/byebug/command.rb', line 18

def match(input)
  @match = regexp.match(input)
end