From 656b1fc9aa5e82b46c88e5b1f67910cede815272 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Wed, 22 Feb 2012 23:05:10 -0800 Subject: [PATCH] Put "help" command in its own file --- lib/pry/command_set.rb | 82 ------------------------------ lib/pry/commands.rb | 2 + lib/pry/default_commands/help.rb | 85 ++++++++++++++++++++++++++++++++ test/test_command.rb | 1 + test/test_command_integration.rb | 15 +----- test/test_command_set.rb | 3 +- test/test_default_commands.rb | 24 ++++----- 7 files changed, 103 insertions(+), 109 deletions(-) create mode 100644 lib/pry/default_commands/help.rb diff --git a/lib/pry/command_set.rb b/lib/pry/command_set.rb index 0e6be62c..9d5d2fe9 100644 --- a/lib/pry/command_set.rb +++ b/lib/pry/command_set.rb @@ -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 diff --git a/lib/pry/commands.rb b/lib/pry/commands.rb index 34e7578a..e9d0ae33 100644 --- a/lib/pry/commands.rb +++ b/lib/pry/commands.rb @@ -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 diff --git a/lib/pry/default_commands/help.rb b/lib/pry/default_commands/help.rb new file mode 100644 index 00000000..9b4310f9 --- /dev/null +++ b/lib/pry/default_commands/help.rb @@ -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 diff --git a/test/test_command.rb b/test/test_command.rb index 5a528b3c..31efd760 100644 --- a/test/test_command.rb +++ b/test/test_command.rb @@ -4,6 +4,7 @@ describe "Pry::Command" do before do @set = Pry::CommandSet.new + @set.import Pry::DefaultCommands::Help end describe 'call_safely' do diff --git a/test/test_command_integration.rb b/test/test_command_integration.rb index 54144eab..60d714f6 100644 --- a/test/test_command_integration.rb +++ b/test/test_command_integration.rb @@ -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" diff --git a/test/test_command_set.rb b/test/test_command_set.rb index 8e043ca8..88636c25 100644 --- a/test/test_command_set.rb +++ b/test/test_command_set.rb @@ -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 diff --git a/test/test_default_commands.rb b/test/test_default_commands.rb index 64af4ed7..671876f1 100644 --- a/test/test_default_commands.rb +++ b/test/test_default_commands.rb @@ -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/