implement command_prefix feature

This commit is contained in:
Ryan Fitzgerald 2011-07-26 00:34:54 -07:00
parent d74ddeea7a
commit 7ece261723
7 changed files with 65 additions and 4 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ doc/
pkg/
coverage/
.yardoc/
/tags

View File

@ -55,7 +55,10 @@ class Pry
def command_matched(val, target)
_, cmd_data = commands.commands.find do |name, data|
command_regex = /^#{convert_to_regex(name)}(?!\S)/
prefix = Regexp.escape(Pry.config.command_prefix)
prefix = "(?:#{prefix})?" unless data.options[:require_prefix]
command_regex = /^#{prefix}#{convert_to_regex(name)}(?!\S)/
if data.options[:interpolate]
# If interpolation fails then the command cannot be matched,

View File

@ -105,7 +105,8 @@ class Pry
:keep_retval => false,
:argument_required => false,
:interpolate => true,
:listing => name
:listing => name,
:require_prefix => true
}.merge!(options)
unless command_dependencies_met? options

View File

@ -57,6 +57,11 @@ class Pry
# @return [String, #call]
attr_accessor :editor
# A string that must precede all Pry commands (e.g., if command_prefix is
# set to "%", the "cd" command must be invoked as "%cd").
# @return [String]
attr_accessor :command_prefix
# @return [Boolean] Toggle Pry color on and off.
attr_accessor :color

View File

@ -3,7 +3,7 @@ class Pry
Shell = Pry::CommandSet.new do
command(/\.(.*)/, "All text following a '.' is forwarded to the shell.", :listing => ".<shell command>") do |cmd|
command(/\.(.*)/, "All text following a '.' is forwarded to the shell.", :listing => ".<shell command>", :require_prefix => false) do |cmd|
if cmd =~ /^cd\s+(.+)/i
dest = $1
begin

View File

@ -199,6 +199,7 @@ class Pry
config.editor = default_editor_for_platform
config.should_load_rc = true
config.disable_auto_reload = false
config.command_prefix = ""
config.plugins ||= OpenStruct.new
config.plugins.enabled = true

View File

@ -93,6 +93,57 @@ describe "Pry::CommandProcessor" do
pos.should == command.name.length
end
it 'should correctly match a command preceded by the command_prefix if one is defined' do
Pry.config.command_prefix = "%"
@pry.commands.command("test-command") {}
command, captures, pos = @command_processor.command_matched "%test-command hello", binding
command.name.should == "test-command"
captures.should == []
pos.should == "test-command".length + "%".length
Pry.config.command_prefix = ''
end
it 'should not match a command not preceded by the command_prefix if one is defined' do
Pry.config.command_prefix = "%"
@pry.commands.command("test-command") {}
command, captures, pos = @command_processor.command_matched "test-command hello", binding
command.should == nil
captures.should == nil
Pry.config.command_prefix = ''
end
it 'should match a command preceded by the command_prefix when :require_prefix => false' do
Pry.config.command_prefix = "%"
@pry.commands.command("test-command", "", :require_prefix => false) {}
command, captures, pos = @command_processor.command_matched "%test-command hello", binding
command.name.should == "test-command"
captures.should == []
pos.should == "test-command".length + "%".length
Pry.config.command_prefix = ''
end
it 'should match a command not preceded by the command_prefix when :require_prefix => false' do
Pry.config.command_prefix = "%"
@pry.commands.command("test-command", "", :require_prefix => false) {}
command, captures, pos = @command_processor.command_matched "test-command hello", binding
command.name.should == "test-command"
captures.should == []
pos.should == "test-command".length
Pry.config.command_prefix = ''
end
it 'should correctly match a regex command with spaces in its name' do
regex_command_name = /test\s+(.+)\s+command/
@pry.commands.command(regex_command_name) {}
@ -201,5 +252,4 @@ describe "Pry::CommandProcessor" do
# you'll cause yourself incredible confusion
lambda { @command_processor.command_matched('boast #{c}', binding) }.should.not.raise NameError
end
end