class Pry::CommandSet

different sets, aliased, removed, etc.
This class is used to create sets of commands. Commands can be imported from

def add_command(command)

Parameters:
  • command (Command) -- The subclass of Pry::Command you wish to add.
def add_command(command)
  commands[command.match] = command
end

def after_command(search, &block)

Other tags:
    Example: Display text 'command complete' after invoking command -

Other tags:
    Yield: - The block to be run after the command.

Parameters:
  • search (String, Regexp) -- The match or listing of the command.
def after_command(search, &block)
  cmd = find_command_by_match_or_listing(search)
  cmd.hooks[:after] << block
end

def alias_command(match, action, options={})

Other tags:
    Example: Pass explicit description (overriding default). -
    Example: Creating an alias for `ls -M` -

Parameters:
  • options (Hash) -- The optional configuration parameters,
  • action (String) -- The action to be performed (typically
  • match (String, Regex) -- The match of the alias (can be a regex).
def alias_command(match, action,  options={})
  cmd = find_command(action) or fail "Command: `#{action}` not found"
  original_options = cmd.options.dup
  options = original_options.merge!({
                                      :desc => "Alias for `#{action}`",
                                      :listing => match
                                    }).merge!(options)
  # ensure default description is used if desc is nil
  desc = options.delete(:desc).to_s
  c = block_command match, desc, options do |*args|
    run action, *args
  end
  c.class_eval do
    define_method(:complete) do |input|
      cmd.new(context).complete(input)
    end
  end
  c.group "Aliases"
  c
end

def before_command(search, &block)

Other tags:
    Example: Display parameter before invoking command -

Other tags:
    Yield: - The block to be run before the command.

Parameters:
  • search (String, Regexp) -- The match or listing of the command.
def before_command(search, &block)
  cmd = find_command_by_match_or_listing(search)
  cmd.hooks[:before].unshift block
end

def block_command(match, description="No description.", options={}, &block)

Other tags:
    Example: Regexp command -

Other tags:
    Yield: - The action to perform. The parameters in the block

Options Hash: (**options)
  • :shellwords (Boolean) -- Whether the command's arguments
  • :use_prefix (Boolean) -- Whether the command uses
  • :listing (String) -- The listing name of the
  • :interpolate (Boolean) -- Whether string #{} based
  • :requires_gem (Array) -- Whether the command has
  • :keep_retval (Boolean) -- Whether or not to use return value

Parameters:
  • options (Hash) -- The optional configuration parameters.
  • description (String) -- A description of the command.
  • match (String, Regexp) -- The start of invocations of this command.
def block_command(match, description="No description.", options={}, &block)
  description, options = ["No description.", description] if description.is_a?(Hash)
  options = Pry::Command.default_options(match).merge!(options)
  commands[match] = Pry::BlockCommand.subclass(match, description, options, helper_module, &block)
end

def complete(search, context={})

Returns:
  • (Array) -

Parameters:
  • context (Hash) -- The context to create the command with
  • search (String) -- The line to search for
def complete(search, context={})
  if command = find_command(search)
    command.new(context).complete(search)
  else
    commands.keys.select do |x|
      String === x && x.start_with?(search)
    end.map{ |command| command + " " } + Bond::DefaultMission.completions
  end
end

def create_command(match, description="No description.", options={}, &block)

Other tags:
    Yield: - The class body's definition.

Parameters:
  • options (Hash) -- The optional configuration parameters, see {#command}
  • description (String) -- A description of the command.
  • match (String, Regexp) -- The start of invocations of this command.
def create_command(match, description="No description.", options={}, &block)
  description, options = ["No description.", description] if description.is_a?(Hash)
  options = Pry::Command.default_options(match).merge!(options)
  commands[match] = Pry::ClassCommand.subclass(match, description, options, helper_module, &block)
  commands[match].class_eval(&block)
  commands[match]
end

def delete(*searches)

Parameters:
  • searches (Array) -- the matches or listings of the commands to remove
def delete(*searches)
  searches.each do |search|
    cmd = find_command_by_match_or_listing(search)
    commands.delete cmd.match
  end
end

def deprecated_command(name_of_deprecated_command, deprecation_message, matcher=name_of_deprecated_command)

def deprecated_command(name_of_deprecated_command, deprecation_message, matcher=name_of_deprecated_command)
  create_command name_of_deprecated_command do
    match matcher
    description ""
    define_method(:process) do
      output.puts "DEPRECATED: #{deprecation_message}"
    end
  end
end

def desc(search, description=nil)

Other tags:
    Example: Getting -
    Example: Setting -

Parameters:
  • description (String?) -- (nil) The command description.
  • search (String, Regexp) -- The command match.
def desc(search, description=nil)
  cmd = find_command_by_match_or_listing(search)
  return cmd.description if !description
  cmd.description = description
end

def each(&block)

def each(&block)
  @commands.each(&block)
end

def find_command(val)

Returns:
  • (Pry::Command, nil) -

Parameters:
  • val (String) -- The line that might be a command invocation
def find_command(val)
  commands.values.select{ |c| c.matches?(val) }.sort_by{ |c| c.match_score(val) }.last
end

def find_command_by_match_or_listing(match_or_listing)

Returns:
  • (Command) - The command object matched.

Parameters:
  • match_or_listing (String, Regexp) -- The match or listing of a command.
def find_command_by_match_or_listing(match_or_listing)
  cmd = (commands[match_or_listing] ||
    Pry::Helpers::BaseHelpers.find_command(match_or_listing, commands))
  cmd or raise ArgumentError, "Cannot find a command: '#{match_or_listing}'!"
end

def find_command_for_help(search)

Returns:
  • (Pry::Command?) -

Parameters:
  • search (String) -- The user's search.
def find_command_for_help(search)
  find_command(search) || (begin
    find_command_by_match_or_listing(search)
  rescue ArgumentError
    nil
  end)
end

def helpers(&block)

Other tags:
    Yield: - A block defining helper methods
def helpers(&block)
  helper_module.class_eval(&block)
end

def import(*sets)

Returns:
  • (Pry::CommandSet) - Returns the reciever (a command set).

Parameters:
  • sets (Array) -- Command sets, all of the commands of which
def import(*sets)
  sets.each do |set|
    commands.merge! set.commands
    helper_module.send :include, set.helper_module
  end
  self
end

def import_from(set, *matches)

Returns:
  • (Pry::CommandSet) - Returns the reciever (a command set).

Parameters:
  • matches (Array) -- Commands to import
  • set (CommandSet) -- Set to import commands from
def import_from(set, *matches)
  helper_module.send :include, set.helper_module
  matches.each do |match|
    cmd = set.find_command_by_match_or_listing(match)
    commands[cmd.match] = cmd
  end
  self
end

def initialize(*imported_sets, &block)

Other tags:
    Yield: - Optional block run to define commands

Parameters:
  • imported_sets (Array) -- Sets which will be imported
def initialize(*imported_sets, &block)
  @commands      = {}
  @helper_module = Module.new
  import(*imported_sets)
  instance_eval(&block) if block
end

def list_commands

Returns:
  • (Array) - The list of commands provided by the command set.
def list_commands
  commands.keys
end

def process_line(val, context={})

Returns:
  • (CommandSet::Result) -

Parameters:
  • context (Hash) -- The context to execute the commands with
  • val (String) -- The line to execute
def process_line(val, context={})
  if command = find_command(val)
    context = context.merge(:command_set => self)
    retval = command.new(context).process_line(val)
    Result.new(true, retval)
  else
    Result.new(false)
  end
end

def rename_command(new_match, search, options={})

Other tags:
    Example: Renaming the `ls` command and changing its description. -

Parameters:
  • options (Hash) -- The optional configuration parameters,
  • search (String, Regexp) -- The command's current match or listing.
  • new_match (String, Regexp) -- The new match for the command.
def rename_command(new_match, search, options={})
  cmd = find_command_by_match_or_listing(search)
  options = {
    :listing     => new_match,
    :description => cmd.description
  }.merge!(options)
  commands[new_match] = cmd.dup
  commands[new_match].match = new_match
  commands[new_match].description = options.delete(:description)
  commands[new_match].options.merge!(options)
  commands.delete(cmd.match)
end

def run_command(context, match, *args)

Other tags:
    Private: - (used for testing)
def run_command(context, match, *args)
  command = commands[match] or raise NoCommandError.new(match, self)
  command.new(context).call_safely(*args)
end

def valid_command?(val)

Returns:
  • (Boolean) -

Parameters:
  • val (String) --
def valid_command?(val)
  !!find_command(val)
end