Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve help message for no meta commands #948

Merged
merged 2 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/irb/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ class Base
class << self
def category(category = nil)
@category = category if category
@category
@category || "No category"
end

def description(description = nil)
@description = description if description
@description
@description || "No description provided."
end

def help_message(help_message = nil)
Expand Down
26 changes: 17 additions & 9 deletions lib/irb/command/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def execute(command_name)
help_message
else
if command_class = Command.load_command(command_name)
command_class.help_message || command_class.description || ""
command_class.help_message || command_class.description
else
"Can't find command `#{command_name}`. Please check the command name and try again.\n\n"
end
Expand All @@ -28,33 +28,41 @@ def help_message
commands_grouped_by_categories = commands_info.group_by { |cmd| cmd[:category] }
commands_grouped_by_categories["Helper methods"] = helper_methods_info

user_aliases = irb_context.instance_variable_get(:@user_aliases)

commands_grouped_by_categories["Aliases"] = user_aliases.map do |alias_name, target|
{ display_name: alias_name, description: "Alias for `#{target}`" }
end

if irb_context.with_debugger
# Remove the original "Debugging" category
commands_grouped_by_categories.delete("Debugging")
# Add an empty "Debugging (from debug.gem)" category at the end
commands_grouped_by_categories["Debugging (from debug.gem)"] = []
end

longest_cmd_name_length = commands_info.map { |c| c[:display_name].length }.max

output = StringIO.new

help_cmds = commands_grouped_by_categories.delete("Help")
no_category_cmds = commands_grouped_by_categories.delete("No category")
aliases = irb_context.instance_variable_get(:@user_aliases).map do |alias_name, target|
{ display_name: alias_name, description: "Alias for `#{target}`" }
end

# Display help commands first
add_category_to_output("Help", help_cmds, output, longest_cmd_name_length)

# Display the rest of the commands grouped by categories
commands_grouped_by_categories.each do |category, cmds|
add_category_to_output(category, cmds, output, longest_cmd_name_length)
end

# Display commands without a category
if no_category_cmds
add_category_to_output("No category", no_category_cmds, output, longest_cmd_name_length)
end

# Display aliases
add_category_to_output("Aliases", aliases, output, longest_cmd_name_length)

# Append the debugger help at the end
if irb_context.with_debugger
# Add "Debugging (from debug.gem)" category as title
add_category_to_output("Debugging (from debug.gem)", [], output, longest_cmd_name_length)
output.puts DEBUGGER__.help
end

Expand Down
18 changes: 7 additions & 11 deletions test/irb/command/test_custom_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class PrintCommand < IRB::Command::Base
description 'print_command'
def execute(*)
puts "Hello from PrintCommand"
nil
end
end

Expand All @@ -25,7 +24,7 @@ def execute(*)
RUBY

output = run_ruby_file do
type "print!\n"
type "print!"
type "exit"
end

Expand All @@ -41,7 +40,6 @@ class PrintCommand < IRB::Command::Base
description 'print_command'
def execute(*)
puts "Hello from PrintCommand"
nil
end
end

Expand All @@ -51,7 +49,7 @@ def execute(*)
RUBY

output = run_ruby_file do
type "print!\n"
type "print!"
type "exit"
end

Expand All @@ -69,7 +67,6 @@ def execute(arg)
$nth_execution ||= 0
puts "\#{$nth_execution} arg=\#{arg.inspect}"
$nth_execution += 1
nil
end
end

Expand All @@ -79,9 +76,9 @@ def execute(arg)
RUBY

output = run_ruby_file do
type "print_arg\n"
type "print_arg"
type "print_arg \n"
type "print_arg a r g\n"
type "print_arg a r g"
type "print_arg a r g \n"
type "exit"
end
Expand All @@ -103,7 +100,6 @@ def execute(*)
$nth_execution ||= 1
puts "\#{$nth_execution} FooBar executed"
$nth_execution += 1
nil
end
end

Expand Down Expand Up @@ -131,7 +127,6 @@ def test_no_meta_command_also_works
class NoMetaCommand < IRB::Command::Base
def execute(*)
puts "This command does not override meta attributes"
nil
end
end

Expand All @@ -141,12 +136,13 @@ def execute(*)
RUBY

output = run_ruby_file do
type "no_meta\n"
type "help no_meta\n"
type "no_meta"
type "help no_meta"
type "exit"
end

assert_include(output, "This command does not override meta attributes")
assert_include(output, "No description provided.")
assert_not_include(output, "Maybe IRB bug")
end
end
Expand Down
Loading