2011-01-11 20:16:04 -05:00
|
|
|
class Pry
|
|
|
|
|
2011-01-12 08:03:45 -05:00
|
|
|
# Basic command functionality. All user-defined commands must
|
|
|
|
# inherit from this class. It provides the `command` method.
|
2011-01-11 20:16:04 -05:00
|
|
|
class CommandBase
|
|
|
|
class << self
|
|
|
|
attr_accessor :commands
|
|
|
|
attr_accessor :command_info
|
2011-01-17 09:38:09 -05:00
|
|
|
attr_accessor :opts
|
2011-01-11 20:16:04 -05:00
|
|
|
|
2011-01-17 09:38:09 -05:00
|
|
|
# private because we want to force function style invocation. We require
|
|
|
|
# that the location where the block is defined has the `opts`
|
|
|
|
# method in scope.
|
|
|
|
private
|
2011-01-11 20:16:04 -05:00
|
|
|
|
2011-01-17 09:38:09 -05:00
|
|
|
# Defines a new Pry command.
|
|
|
|
# @param [String, Array] name The name of the command (or array of
|
|
|
|
# command name aliases).
|
2011-01-17 22:40:28 -05:00
|
|
|
# @param [String] description A description of the command.
|
|
|
|
# @yield The action to perform. The parameters in the block
|
|
|
|
# determines the parameters the command will receive. All
|
|
|
|
# parameters passed into the block will be strings. Successive
|
|
|
|
# command parameters are separated by whitespace at the Pry prompt.
|
|
|
|
# @example
|
|
|
|
# class MyCommands < Pry::CommandBase
|
|
|
|
# command "greet", "Greet somebody" do |name|
|
|
|
|
# puts "Good afternoon #{name.capitalize}!"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # From pry:
|
|
|
|
# # pry(main)> _pry_.commands = MyCommands
|
|
|
|
# # pry(main)> greet john
|
|
|
|
# # Good afternoon John!
|
|
|
|
# # pry(main)> help greet
|
|
|
|
# # Greet somebody
|
2011-01-17 09:38:09 -05:00
|
|
|
def command(name, description="No description.", &block)
|
|
|
|
@commands ||= {}
|
|
|
|
@command_info ||= {}
|
2011-01-11 20:16:04 -05:00
|
|
|
|
2011-01-17 09:38:09 -05:00
|
|
|
arg_match = '(?:\s+(\S+))?' * 20
|
|
|
|
if name.is_a?(Array)
|
|
|
|
matcher = []
|
|
|
|
name.each do |n|
|
2011-01-17 22:40:28 -05:00
|
|
|
matcher << /^#{n}(?!\S)#{arg_match}?/
|
2011-01-17 09:38:09 -05:00
|
|
|
end
|
|
|
|
else
|
2011-01-17 22:40:28 -05:00
|
|
|
matcher = /^#{name}(?!\S)#{arg_match}?/
|
2011-01-17 09:38:09 -05:00
|
|
|
end
|
2011-01-11 20:16:04 -05:00
|
|
|
|
2011-01-17 09:38:09 -05:00
|
|
|
commands[matcher] = block
|
|
|
|
command_info[name] = description
|
2011-01-11 20:16:04 -05:00
|
|
|
end
|
|
|
|
end
|
2011-01-17 09:38:09 -05:00
|
|
|
command "help", "This menu." do |cmd|
|
|
|
|
out = opts[:output]
|
|
|
|
command_info = opts[:command_info]
|
|
|
|
param = cmd
|
2011-01-11 20:16:04 -05:00
|
|
|
|
2011-01-17 09:38:09 -05:00
|
|
|
if !param
|
|
|
|
out.puts "Command list:"
|
|
|
|
out.puts "--"
|
|
|
|
command_info.each do |k, v|
|
|
|
|
out.puts "#{Array(k).first}".ljust(18) + v
|
|
|
|
end
|
2011-01-12 08:03:45 -05:00
|
|
|
else
|
2011-01-17 09:38:09 -05:00
|
|
|
key = command_info.keys.find { |v| Array(v).any? { |k| k === param } }
|
|
|
|
if key
|
|
|
|
out.puts command_info[key]
|
2011-01-11 20:16:04 -05:00
|
|
|
else
|
2011-01-17 09:38:09 -05:00
|
|
|
out.puts "No info for command: #{param}"
|
2011-01-11 20:16:04 -05:00
|
|
|
end
|
|
|
|
end
|
2011-01-17 09:38:09 -05:00
|
|
|
end
|
2011-01-11 20:16:04 -05:00
|
|
|
|
2011-01-17 22:40:28 -05:00
|
|
|
# Ensures that commands can be inherited
|
2011-01-11 20:16:04 -05:00
|
|
|
def self.inherited(klass)
|
2011-01-14 06:12:43 -05:00
|
|
|
klass.commands = commands.dup
|
|
|
|
klass.command_info = command_info.dup
|
2011-01-11 20:16:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|