Class: Byebug::Command

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

Defined Under Namespace

Classes: SubcmdStruct

Constant Summary collapse

DEF_OPTIONS =
{ allow_in_control:     false,
allow_in_post_mortem: true ,
event:                true ,
always_run:           0    ,
unknown:              false,
need_context:         false }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state) ⇒ Command

Returns a new instance of Command.



176
177
178
# File 'lib/byebug/command.rb', line 176

def initialize(state)
  @state = state
end

Class Method Details

.commandsObject



44
45
46
# File 'lib/byebug/command.rb', line 44

def commands
  @commands ||= []
end

.format_subcmd(subcmd_name) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/byebug/command.rb', line 69

def format_subcmd(subcmd_name)
  subcmd = find(self::Subcommands, subcmd_name)
  return "Invalid \"#{names.join("|")}\" " \
         "subcommand \"#{args[1]}\"." unless subcmd

  return "#{subcmd.short_help}.\n" \
         "#{subcmd.long_help || '' }"
end

.format_subcmdsObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/byebug/command.rb', line 78

def format_subcmds
  cmd_name = names.join("|")
  s = "\n"                                     \
      "--\n"                                   \
      "List of \"#{cmd_name}\" subcommands:\n" \
      "--\n"
  width = self::Subcommands.map(&:name).max_by(&:size).size
  for subcmd in self::Subcommands do
    s += sprintf \
      "%s %-#{width}s -- %s\n", cmd_name, subcmd.name, subcmd.short_help
  end
  return s
end

.help(args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/byebug/command.rb', line 55

def help(args)
  output = description.split("\n").map{|l| l.gsub(/^ +/, '')}
  output.shift if output.first && output.first.empty?
  output.pop if output.last && output.last.empty?
  output = output.join("\n") + "\n"

  if defined? self::Subcommands
    return output += format_subcmds unless args and args[1]
    output += format_subcmd(args[1])
  end

  return output
end

.inherited(klass) ⇒ Object



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

def inherited(klass)
  DEF_OPTIONS.each do |o, v|
    klass.options[o] = v if klass.options[o].nil?
  end
  commands << klass
end

.load_commandsObject



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

def load_commands
  Dir[File.join(Byebug.const_get(:BYEBUG_DIR), 'commands', '*')].each {
    |file| require file if file =~ /\.rb$/ }
  Byebug.constants.grep(/Functions$/).map {
    |name| Byebug.const_get(name) }.each { |mod| include mod }
end

.method_missing(meth, *args, &block) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/byebug/command.rb', line 106

def method_missing(meth, *args, &block)
  if meth.to_s =~ /^(.+?)=$/
    @options[$1.intern] = args.first
  else
    if @options.has_key?(meth)
      @options[meth]
    else
      super
    end
  end
end

.optionsObject



118
119
120
# File 'lib/byebug/command.rb', line 118

def options
  @options ||= {}
end

.register_setting_get(name, &block) ⇒ Object



151
152
153
154
# File 'lib/byebug/command.rb', line 151

def register_setting_get(name, &block)
  settings_map[name] ||= {}
  settings_map[name][:getter] = block
end

.register_setting_set(name, &block) ⇒ Object



156
157
158
159
# File 'lib/byebug/command.rb', line 156

def register_setting_set(name, &block)
  settings_map[name] ||= {}
  settings_map[name][:setter] = block
end

.register_setting_var(name, default) ⇒ Object



144
145
146
147
148
149
# File 'lib/byebug/command.rb', line 144

def register_setting_var(name, default)
  var_name = "@@#{name}"
  class_variable_set(var_name, default)
  register_setting_get(name) { class_variable_get(var_name) }
  register_setting_set(name) { |value| class_variable_set(var_name, value) }
end

.settingsObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/byebug/command.rb', line 127

def settings
  unless defined? @settings and @settings
    @settings = Object.new
    map = settings_map
    c = class << @settings; self end
    c.send(:define_method, :[]) do |name|
      raise "No such setting #{name}" unless map.has_key?(name)
      map[name][:getter].call
    end
    c.send(:define_method, :[]=) do |name, value|
      raise "No such setting #{name}" unless map.has_key?(name)
      map[name][:setter].call(value)
    end
  end
  @settings
end

.settings_mapObject



122
123
124
# File 'lib/byebug/command.rb', line 122

def settings_map
  @@settings_map ||= {}
end

Instance Method Details

#match(input) ⇒ Object



180
181
182
# File 'lib/byebug/command.rb', line 180

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