class Pry::Command

use this class directly.
{Pry::CommandSet#create_command} which creates a ClassCommand. Please don’t
{Pry::CommandSet#command} which creates a BlockCommand or
The super-class of all commands, new commands should be created by calling

def _pry_

def _pry_
  Pry::Warning.warn('_pry_ is deprecated, use pry_instance instead')
  pry_instance
end

def after_hooks

def after_hooks
  find_hooks('after')
end

def banner(arg = nil)

Define or get the command's banner
def banner(arg = nil)
  @banner = arg if arg
  @banner ||= description
end

def before_hooks

def before_hooks
  find_hooks('before')
end

def block

def block
  @block || instance_method(:process)
end

def block

def block
  self.class.block
end

def call_safely(*args)

Returns:
  • (Object) - The return value of the `#call` method, or

Parameters:
  • args (Array) -- The arguments to pass to this command.
def call_safely(*args)
  if command_options[:argument_required] && args.empty?
    raise CommandError, "The command '#{command_name}' requires an argument."
  end
  ret = use_unpatched_symbol do
    call_with_hooks(*args)
  end
  command_options[:keep_retval] ? ret : void
end

def call_with_hooks(*args)

Returns:
  • (Object) - The return value from `#call`

Parameters:
  • args (Array) -- The arguments to `#call`
def call_with_hooks(*args)
  before_hooks.each { |block| instance_exec(*args, &block) }
  ret = call(*args)
  after_hooks.each do |block|
    ret = instance_exec(*args, &block)
  end
  ret
end

def check_for_command_collision(command_match, arg_string)

the current scope.
Display a warning if a command collides with a local/method in
def check_for_command_collision(command_match, arg_string)
  collision_type = target.eval("defined?(#{command_match})")
  collision_type ||= 'local-variable' if arg_string =~ %r{\A\s*[-+*/%&|^]*=}
  if collision_type
    output.puts(
      "#{Helpers::Text.bold('WARNING:')} Calling Pry command '#{command_match}', " \
      "which conflicts with a #{collision_type}.\n\n"
    )
  end
rescue Pry::RescuableException # rubocop:disable Lint/HandleExceptions
end

def command_name

def command_name
  options[:listing]
end

def command_name

def command_name
  self.class.command_name
end

def command_options(arg = nil)

Define or get the command's options
def command_options(arg = nil)
  @command_options ||= default_options(match)
  @command_options.merge!(arg) if arg
  @command_options
end

def command_options

def command_options
  self.class.options
end

def command_regex

def command_regex
  prefix = convert_to_regex(Pry.config.command_prefix)
  prefix = "(?:#{prefix})?" unless options[:use_prefix]
  /\A#{prefix}#{convert_to_regex(match)}(?!\S)/
end

def commands

def commands
  command_set.to_hash
end

def complete(_search)

Returns:
  • (Array) - Completion words

Parameters:
  • _search (String) -- The line typed so far
def complete(_search)
  []
end

def convert_to_regex(obj)

def convert_to_regex(obj)
  case obj
  when String
    Regexp.escape(obj)
  else
    obj
  end
end

def default_options(match)

def default_options(match)
  {
    keep_retval: false,
    argument_required: false,
    interpolate: true,
    shellwords: true,
    listing: (match.is_a?(String) ? match : match.inspect),
    use_prefix: true,
    takes_block: false
  }
end

def description(arg = nil)

Define or get the command's description
def description(arg = nil)
  @description = arg if arg
  @description ||= nil
end

def description

def description
  self.class.description
end

def doc

def doc
  new.help
end

def find_hooks(event)

def find_hooks(event)
  event_name = "#{event}_#{command_name}"
  (hooks || Pry.hooks || self.class.hooks).get_hooks(event_name).values
end

def group(name = nil)

Group should not be changed once it is initialized.
manually overridden if necessary.
This is usually auto-generated from directory naming, but it can be
The group in which the command should be displayed in "help" output.
def group(name = nil)
  @group ||= begin
    name || case Pry::Method(block).source_file
            when %r{/pry/.*_commands/(.*).rb}
              Regexp.last_match(1).capitalize.tr('_', " ")
            when /(pry-\w+)-([\d\.]+([\w\.]+)?)/
              name = Regexp.last_match(1)
              version = Regexp.last_match(2)
              "#{name} (v#{version})"
            when /pryrc/
              "pryrc"
            else
              "(other)"
            end
  end
end

def initialize(context = {})

Parameters:
  • context (Hash) -- The runtime context to use with this command.
def initialize(context = {})
  self.context      = context
  self.target       = context[:target]
  self.output       = context[:output]
  self.eval_string  = context[:eval_string]
  self.command_set  = context[:command_set]
  self.hooks        = context[:hooks]
  self.pry_instance = context[:pry_instance]
end

def inspect

def inspect
  name
end

def interpolate_string(str)

Returns:
  • (String) - The reevaluated string with interpolations

Parameters:
  • str (String) -- The string to reevaluate with interpolation.
def interpolate_string(str)
  dumped_str = str.dump
  if dumped_str.gsub!(/\\\#\{/, '#{')
    target.eval(dumped_str)
  else
    str
  end
end

def match(arg = nil)

def match(arg = nil)
  if arg
    @command_options ||= default_options(arg)
    @command_options[:listing] = arg.is_a?(String) ? arg : arg.inspect
    @match = arg
  end
  @match ||= nil
end

def match

def match
  self.class.match
end

def match_score(val)

Returns:
  • (Fixnum) -

Parameters:
  • val (String) -- A line input at the REPL
def match_score(val)
  if command_regex =~ val
    if Regexp.last_match.size > 1
      Regexp.last_match.begin(1)
    else
      Regexp.last_match.end(0)
    end
  else
    -1
  end
end

def matches?(val)

Returns:
  • (Boolean) -

Parameters:
  • val (String) -- A line input at the REPL
def matches?(val)
  command_regex =~ val
end

def name

def name
  super.to_s == "" ? "#<class(Pry::Command #{match.inspect})>" : super
end

def name

Make those properties accessible to instances
def name
  self.class.name
end

def normalize_method_args(method, args)

Returns:
  • (Array) - a (possibly shorter) array of the arguments to pass

Parameters:
  • args (Array) --
  • method (Integer) --
def normalize_method_args(method, args)
  case method.arity
  when -1
    args
  when 0
    []
  else
    args.values_at(*(0..(method.arity - 1)).to_a)
  end
end

def pass_block(arg_string)

Parameters:
  • arg_string (String) -- The arguments (as a string) passed to the command.
def pass_block(arg_string)
  # Workaround for weird JRuby bug where rindex in this case can return nil
  # even when there's a match.
  arg_string.scan(/\| *(?:do|\{)/)
  block_index = $LAST_MATCH_INFO && $LAST_MATCH_INFO.offset(0)[0]
  return unless block_index
  block_init_string = arg_string.slice!(block_index..-1)[1..-1]
  prime_string = "proc #{block_init_string}\n"
  block_string =
    if !Pry::Code.complete_expression?(prime_string)
      pry_instance.r(target, prime_string)
    else
      prime_string
    end
  begin
    self.command_block = target.eval(block_string)
  rescue Pry::RescuableException
    raise CommandError, "Incomplete block definition."
  end
end

def process_line(line)

Returns:
  • (Object, Command::VOID_VALUE) -

Parameters:
  • line (String) -- The line to process
def process_line(line)
  command_match, arg_string, captures, args = tokenize(line)
  if Pry.config.collision_warning
    check_for_command_collision(command_match, arg_string)
  end
  self.arg_string = arg_string
  self.captures = captures
  call_safely(*(captures + args))
end

def run(command_string, *args)

Parameters:
  • args (Array) -- Further arguments to pass to the command
  • command_string (String) -- The string that invokes the command
def run(command_string, *args)
  command_string = pry_instance.config.command_prefix.to_s + command_string
  complete_string = "#{command_string} #{args.join(' ')}".rstrip
  command_set.process_line(complete_string, context)
end

def source

def source
  file, line = block.source_location
  strip_leading_whitespace(Pry::Code.from_file(file).expression_at(line))
end

def source

def source
  self.class.source
end

def source_file

def source_file
  Array(block.source_location).first
end

def source_line

def source_line
  Array(block.source_location).last
end

def state

def state
  Pry::CommandState.default.state_for(match)
end

def state

Returns:
  • (Hash) - Pry commands can store arbitrary state
def state
  self.class.state
end

def subclass(match, description, options, helpers, &block)

Returns:
  • (Class) - (a subclass of {Pry::Command})

Other tags:
    Yield: - optional, used for BlockCommands

Parameters:
  • helpers (Module) -- A module of helper functions to be included.
  • options (Hash) -- Behavioral options (see {Pry::CommandSet#command})
  • description (String) -- The description to appear in `help`
  • match (String, Regex) -- The thing that triggers this command
def subclass(match, description, options, helpers, &block)
  klass = Class.new(self)
  klass.send(:include, helpers)
  klass.match = match
  klass.description = description
  klass.command_options = options
  klass.block = block
  klass
end

def target_self

Returns:
  • (Object) - The value of `self` inside the `target` binding.
def target_self
  target.eval('self')
end

def tokenize(val)

Returns:
  • (Array) -

Parameters:
  • val (String) -- The line of input
def tokenize(val)
  val = interpolate_string(val) if command_options[:interpolate]
  self.class.command_regex =~ val
  # please call Command.matches? before Command#call_safely
  unless Regexp.last_match
    raise CommandError, "fatal: called a command which didn't match?!"
  end
  captures = Regexp.last_match.captures
  pos = Regexp.last_match.end(0)
  arg_string = val[pos..-1]
  # remove the one leading space if it exists
  arg_string.slice!(0) if arg_string.start_with?(" ")
  # process and pass a block if one is found
  pass_block(arg_string) if command_options[:takes_block]
  args =
    if arg_string
      if command_options[:shellwords]
        Shellwords.shellwords(arg_string)
      else
        arg_string.split(" ")
      end
    else
      []
    end
  [val[0..pos].rstrip, arg_string, captures, args]
end

def use_unpatched_symbol

def use_unpatched_symbol
  call_method = Symbol.method_defined?(:call) && Symbol.instance_method(:call)
  Symbol.class_eval { undef :call } if call_method
  yield
ensure
  Symbol.instance_eval { define_method(:call, call_method) } if call_method
end

def void

def void
  VOID_VALUE
end