mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Put "help" command in its own file
This commit is contained in:
parent
abf7e1fe32
commit
656b1fc9aa
7 changed files with 103 additions and 109 deletions
|
@ -341,88 +341,6 @@ class Pry
|
|||
end
|
||||
|
||||
def define_default_commands
|
||||
|
||||
create_command "help" do |cmd|
|
||||
description "Show a list of commands, or help for one command"
|
||||
|
||||
banner <<-BANNER
|
||||
Usage: help [ COMMAND ]
|
||||
|
||||
With no arguments, help lists all the available commands in the current
|
||||
command-set along with their description.
|
||||
|
||||
When given a command name as an argument, shows the help for that command.
|
||||
BANNER
|
||||
|
||||
# Get a hash of available commands grouped by the "group" name.
|
||||
def grouped_commands
|
||||
commands.values.group_by(&:group)
|
||||
end
|
||||
|
||||
def process
|
||||
if args.empty?
|
||||
display_index
|
||||
else
|
||||
display_topic(args.first)
|
||||
end
|
||||
end
|
||||
|
||||
# Display the index view, with headings and short descriptions per command.
|
||||
#
|
||||
# @param Hash[String => Array[Commands]]
|
||||
def display_index(groups=grouped_commands)
|
||||
help_text = []
|
||||
|
||||
groups.keys.sort.each do |key|
|
||||
|
||||
commands = groups[key].select do |command|
|
||||
command.description && !command.description.empty?
|
||||
end.sort_by do |command|
|
||||
command.options[:listing].to_s
|
||||
end
|
||||
|
||||
unless commands.empty?
|
||||
help_text << "\n#{text.bold(key)}"
|
||||
help_text += commands.map do |command|
|
||||
" #{command.options[:listing].to_s.ljust(18)} #{command.description}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
stagger_output(help_text.join("\n"))
|
||||
end
|
||||
|
||||
# Display help for an individual command or group.
|
||||
#
|
||||
# @param String The string to search for.
|
||||
def display_topic(search)
|
||||
if command = command_set.find_command_for_help(search)
|
||||
stagger_output command.new.help
|
||||
else
|
||||
filtered = grouped_commands.select{ |key, value| normalize(key).start_with?(normalize(search)) }
|
||||
|
||||
if filtered.empty?
|
||||
raise CommandError, "No help found for '#{args.first}'"
|
||||
elsif filtered.size == 1
|
||||
display_index(filtered.first.first => filtered.first.last)
|
||||
else
|
||||
names = filtered.map(&:first)
|
||||
last = names.pop
|
||||
output.puts "Did you mean: #{names.join(", ")} or #{last}?"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Clean search terms to make it easier to search group names
|
||||
#
|
||||
# @param String
|
||||
# @return String
|
||||
def normalize(key)
|
||||
key.downcase.gsub(/pry\W+/, '')
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
create_command "install-command", "Install a disabled command." do |name|
|
||||
|
||||
banner <<-BANNER
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require "pry/default_commands/basic"
|
||||
require "pry/default_commands/help"
|
||||
require "pry/default_commands/gems"
|
||||
require "pry/default_commands/context"
|
||||
require "pry/default_commands/input_and_output"
|
||||
|
@ -14,6 +15,7 @@ class Pry
|
|||
# Default commands used by Pry.
|
||||
Commands = Pry::CommandSet.new do
|
||||
import DefaultCommands::Basic
|
||||
import DefaultCommands::Help
|
||||
import DefaultCommands::Gems
|
||||
import DefaultCommands::Context
|
||||
import DefaultCommands::NavigatingPry
|
||||
|
|
85
lib/pry/default_commands/help.rb
Normal file
85
lib/pry/default_commands/help.rb
Normal file
|
@ -0,0 +1,85 @@
|
|||
class Pry
|
||||
module DefaultCommands
|
||||
Help = Pry::CommandSet.new do
|
||||
create_command "help" do |cmd|
|
||||
description "Show a list of commands, or help for one command"
|
||||
|
||||
banner <<-BANNER
|
||||
Usage: help [ COMMAND ]
|
||||
|
||||
With no arguments, help lists all the available commands in the current
|
||||
command-set along with their description.
|
||||
|
||||
When given a command name as an argument, shows the help for that command.
|
||||
BANNER
|
||||
|
||||
# Get a hash of available commands grouped by the "group" name.
|
||||
def grouped_commands
|
||||
commands.values.group_by(&:group)
|
||||
end
|
||||
|
||||
def process
|
||||
if args.empty?
|
||||
display_index
|
||||
else
|
||||
display_topic(args.first)
|
||||
end
|
||||
end
|
||||
|
||||
# Display the index view, with headings and short descriptions per command.
|
||||
#
|
||||
# @param Hash[String => Array[Commands]]
|
||||
def display_index(groups=grouped_commands)
|
||||
help_text = []
|
||||
|
||||
groups.keys.sort.each do |key|
|
||||
|
||||
commands = groups[key].select do |command|
|
||||
command.description && !command.description.empty?
|
||||
end.sort_by do |command|
|
||||
command.options[:listing].to_s
|
||||
end
|
||||
|
||||
unless commands.empty?
|
||||
help_text << "\n#{text.bold(key)}"
|
||||
help_text += commands.map do |command|
|
||||
" #{command.options[:listing].to_s.ljust(18)} #{command.description}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
stagger_output(help_text.join("\n"))
|
||||
end
|
||||
|
||||
# Display help for an individual command or group.
|
||||
#
|
||||
# @param String The string to search for.
|
||||
def display_topic(search)
|
||||
if command = command_set.find_command_for_help(search)
|
||||
stagger_output command.new.help
|
||||
else
|
||||
filtered = grouped_commands.select{ |key, value| normalize(key).start_with?(normalize(search)) }
|
||||
|
||||
if filtered.empty?
|
||||
raise CommandError, "No help found for '#{args.first}'"
|
||||
elsif filtered.size == 1
|
||||
display_index(filtered.first.first => filtered.first.last)
|
||||
else
|
||||
names = filtered.map(&:first)
|
||||
last = names.pop
|
||||
output.puts "Did you mean: #{names.join(", ")} or #{last}?"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Clean search terms to make it easier to search group names
|
||||
#
|
||||
# @param String
|
||||
# @return String
|
||||
def normalize(key)
|
||||
key.downcase.gsub(/pry\W+/, '')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -4,6 +4,7 @@ describe "Pry::Command" do
|
|||
|
||||
before do
|
||||
@set = Pry::CommandSet.new
|
||||
@set.import Pry::DefaultCommands::Help
|
||||
end
|
||||
|
||||
describe 'call_safely' do
|
||||
|
|
|
@ -287,18 +287,6 @@ describe "commands" do
|
|||
str_output.string.should =~ /goodbye world/
|
||||
end
|
||||
|
||||
it 'should inherit "help" command from Pry::CommandBase' do
|
||||
klass = Pry::CommandSet.new do
|
||||
command "h", "h command" do
|
||||
end
|
||||
end
|
||||
|
||||
klass.commands.keys.size.should == 3
|
||||
klass.commands.keys.include?("help").should == true
|
||||
klass.commands.keys.include?("install-command").should == true
|
||||
klass.commands.keys.include?("h").should == true
|
||||
end
|
||||
|
||||
it 'should inherit commands from Pry::Commands' do
|
||||
klass = Pry::CommandSet.new Pry::Commands do
|
||||
command "v" do
|
||||
|
@ -313,13 +301,14 @@ describe "commands" do
|
|||
|
||||
it 'should alias a command with another command' do
|
||||
klass = Pry::CommandSet.new do
|
||||
import Pry::DefaultCommands::Help
|
||||
alias_command "help2", "help"
|
||||
end
|
||||
klass.commands["help2"].block.should == klass.commands["help"].block
|
||||
end
|
||||
|
||||
it 'should change description of a command using desc' do
|
||||
klass = Pry::CommandSet.new do; end
|
||||
klass = Pry::CommandSet.new do; import Pry::DefaultCommands::Help; end
|
||||
orig = klass.commands["help"].description
|
||||
klass.instance_eval do
|
||||
desc "help", "blah"
|
||||
|
|
|
@ -2,7 +2,7 @@ require 'helper'
|
|||
|
||||
describe Pry::CommandSet do
|
||||
before do
|
||||
@set = Pry::CommandSet.new
|
||||
@set = Pry::CommandSet.new{ import Pry::DefaultCommands::Help }
|
||||
@ctx = {
|
||||
:target => binding,
|
||||
:command_set => @set
|
||||
|
@ -268,7 +268,6 @@ describe Pry::CommandSet do
|
|||
order = [doc.index("boo"),
|
||||
doc.index("foo"),
|
||||
doc.index("goo"),
|
||||
doc.index("help"),
|
||||
doc.index("moo")]
|
||||
|
||||
order.should == order.sort
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
require 'helper'
|
||||
|
||||
describe "Pry::Commands" do
|
||||
before do
|
||||
@set = Pry::CommandSet.new do
|
||||
import Pry::DefaultCommands::Help
|
||||
end
|
||||
end
|
||||
|
||||
describe "help" do
|
||||
it 'should display help for a specific command' do
|
||||
str_output = StringIO.new
|
||||
|
@ -11,35 +17,29 @@ describe "Pry::Commands" do
|
|||
end
|
||||
|
||||
it 'should display help for a regex command with a "listing"' do
|
||||
set = Pry::CommandSet.new do
|
||||
command /bar(.*)/, "Test listing", :listing => "foo" do
|
||||
end
|
||||
end
|
||||
@set.command /bar(.*)/, "Test listing", :listing => "foo" do; end
|
||||
|
||||
str_output = StringIO.new
|
||||
redirect_pry_io(InputTester.new("help foo"), str_output) do
|
||||
Pry.new(:commands => set).rep
|
||||
Pry.new(:commands => @set).rep
|
||||
end
|
||||
str_output.string.each_line.count.should == 1
|
||||
str_output.string.should =~ /Test listing/
|
||||
end
|
||||
|
||||
it 'should display help for a command with a spaces in its name' do
|
||||
set = Pry::CommandSet.new do
|
||||
command "command with spaces", "description of a command with spaces" do
|
||||
end
|
||||
end
|
||||
@set.command "command with spaces", "description of a command with spaces" do; end
|
||||
|
||||
str_output = StringIO.new
|
||||
redirect_pry_io(InputTester.new("help \"command with spaces\""), str_output) do
|
||||
Pry.new(:commands => set).rep
|
||||
Pry.new(:commands => @set).rep
|
||||
end
|
||||
str_output.string.each_line.count.should == 1
|
||||
str_output.string.should =~ /description of a command with spaces/
|
||||
end
|
||||
|
||||
it 'should display help for all commands with a description' do
|
||||
set = Pry::CommandSet.new do
|
||||
@set.instance_eval do
|
||||
command /bar(.*)/, "Test listing", :listing => "foo" do; end
|
||||
command "b", "description for b", :listing => "foo" do; end
|
||||
command "c" do;end
|
||||
|
@ -48,7 +48,7 @@ describe "Pry::Commands" do
|
|||
|
||||
str_output = StringIO.new
|
||||
redirect_pry_io(InputTester.new("help"), str_output) do
|
||||
Pry.new(:commands => set).rep
|
||||
Pry.new(:commands => @set).rep
|
||||
end
|
||||
str_output.string.should =~ /Test listing/
|
||||
str_output.string.should =~ /description for b/
|
||||
|
|
Loading…
Add table
Reference in a new issue