1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

add Pry::CommandSet#[]=.

Pry::CommandSet#[]= can be used to re-assign commands or add new ones to
a command set. The key matches the pattern of a command you'd like to
replace and the value is the command you'd the pattern to be matched
with.

The key is really important. You could say this:

Pry.commands.command "hello" do
  output.puts "Hello! :)"
end
Pry.commands["help"] = Pry.commands["hello"]

I would expect "help" to print "Hello! :)", but the command is bound to
the pattern "hello". A new copy of the "hello" command is created
and the key is used as the match pattern, so "help" will print "Hello! :)".

You can use #[]= to replace commands, maybe your own 'help' implementation:
Pry.commands["help"] = MyHelpCommand

Commands can also be deleted by assigning a value of nil:
Pry.commands["help"] = nil

I decided to implement this based on SO post found here:
http://stackoverflow.com/questions/17475829/configure-help-output-in-pry
This commit is contained in:
Robert Gleeson 2013-09-21 21:16:28 +02:00
parent f5ba4de3ca
commit c05c8f31c1

View file

@ -152,12 +152,6 @@ class Pry
@commands.each(&block)
end
# Add a given command object to this set.
# @param [Command] command The subclass of Pry::Command you wish to add.
def add_command(command)
commands[command.match] = command
end
# Removes some commands from the set
# @param [Array<String>] searches the matches or listings of the commands to remove
def delete(*searches)
@ -317,10 +311,56 @@ class Pry
# Find a command that matches the given line
# @param [String] val The line that might be a command invocation
# @return [Pry::Command, nil]
def find_command(val)
commands.values.select{ |c| c.matches?(val) }.sort_by{ |c| c.match_score(val) }.last
def [](pattern)
commands.values.select do |command|
command.matches?(pattern)
end.sort_by do |command|
command.match_score(pattern)
end.last
end
alias_method :find_command, :[]
#
# Re-assign the command found at _pattern_ with _command_.
#
# @param [Regexp, String] pattern
# The command to add or replace(found at _pattern_).
#
# @param [Pry::Command] command
# The command to add.
#
# @return [Pry::Command]
# Returns the new command (matched with "pattern".)
#
# @example
# Pry.commands["help"] = MyHelpCommand
#
def []=(pattern, command)
if command.equal?(nil)
return commands.delete(pattern)
end
unless command < Pry::Command
raise TypeError, "command is not a subclass of Pry::Command"
end
bind_command_to_pattern = pattern != command.match
if bind_command_to_pattern
command_copy = command.dup
command_copy.match = pattern
@commands[pattern] = command_copy
else
@commands[pattern] = command
end
end
#
# Add a command to set.
#
# @param [Command] command
# a subclass of Pry::Command.
#
def add_command(command)
self[command.match] = command
end
alias_method :[], :find_command
# Find the command that the user might be trying to refer to.
# @param [String] search The user's search.