Skip to content

Commit b1ef58a

Browse files
authored
Improve help message for no meta commands (#948)
* Remove unnecessary code from command tests * Improve help message for no meta commands 1. Add placeholder values for both command category and description 2. Update help command's output to give different types of categories more explicit ordering
1 parent 4a4d7a4 commit b1ef58a

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

lib/irb/command/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ class Base
1818
class << self
1919
def category(category = nil)
2020
@category = category if category
21-
@category
21+
@category || "No category"
2222
end
2323

2424
def description(description = nil)
2525
@description = description if description
26-
@description
26+
@description || "No description provided."
2727
end
2828

2929
def help_message(help_message = nil)

lib/irb/command/help.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def execute(command_name)
1212
help_message
1313
else
1414
if command_class = Command.load_command(command_name)
15-
command_class.help_message || command_class.description || ""
15+
command_class.help_message || command_class.description
1616
else
1717
"Can't find command `#{command_name}`. Please check the command name and try again.\n\n"
1818
end
@@ -28,33 +28,41 @@ def help_message
2828
commands_grouped_by_categories = commands_info.group_by { |cmd| cmd[:category] }
2929
commands_grouped_by_categories["Helper methods"] = helper_methods_info
3030

31-
user_aliases = irb_context.instance_variable_get(:@user_aliases)
32-
33-
commands_grouped_by_categories["Aliases"] = user_aliases.map do |alias_name, target|
34-
{ display_name: alias_name, description: "Alias for `#{target}`" }
35-
end
36-
3731
if irb_context.with_debugger
3832
# Remove the original "Debugging" category
3933
commands_grouped_by_categories.delete("Debugging")
40-
# Add an empty "Debugging (from debug.gem)" category at the end
41-
commands_grouped_by_categories["Debugging (from debug.gem)"] = []
4234
end
4335

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

4638
output = StringIO.new
4739

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

46+
# Display help commands first
5047
add_category_to_output("Help", help_cmds, output, longest_cmd_name_length)
5148

49+
# Display the rest of the commands grouped by categories
5250
commands_grouped_by_categories.each do |category, cmds|
5351
add_category_to_output(category, cmds, output, longest_cmd_name_length)
5452
end
5553

54+
# Display commands without a category
55+
if no_category_cmds
56+
add_category_to_output("No category", no_category_cmds, output, longest_cmd_name_length)
57+
end
58+
59+
# Display aliases
60+
add_category_to_output("Aliases", aliases, output, longest_cmd_name_length)
61+
5662
# Append the debugger help at the end
5763
if irb_context.with_debugger
64+
# Add "Debugging (from debug.gem)" category as title
65+
add_category_to_output("Debugging (from debug.gem)", [], output, longest_cmd_name_length)
5866
output.puts DEBUGGER__.help
5967
end
6068

test/irb/command/test_custom_command.rb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class PrintCommand < IRB::Command::Base
1515
description 'print_command'
1616
def execute(*)
1717
puts "Hello from PrintCommand"
18-
nil
1918
end
2019
end
2120
@@ -25,7 +24,7 @@ def execute(*)
2524
RUBY
2625

2726
output = run_ruby_file do
28-
type "print!\n"
27+
type "print!"
2928
type "exit"
3029
end
3130

@@ -41,7 +40,6 @@ class PrintCommand < IRB::Command::Base
4140
description 'print_command'
4241
def execute(*)
4342
puts "Hello from PrintCommand"
44-
nil
4543
end
4644
end
4745
@@ -51,7 +49,7 @@ def execute(*)
5149
RUBY
5250

5351
output = run_ruby_file do
54-
type "print!\n"
52+
type "print!"
5553
type "exit"
5654
end
5755

@@ -69,7 +67,6 @@ def execute(arg)
6967
$nth_execution ||= 0
7068
puts "\#{$nth_execution} arg=\#{arg.inspect}"
7169
$nth_execution += 1
72-
nil
7370
end
7471
end
7572
@@ -79,9 +76,9 @@ def execute(arg)
7976
RUBY
8077

8178
output = run_ruby_file do
82-
type "print_arg\n"
79+
type "print_arg"
8380
type "print_arg \n"
84-
type "print_arg a r g\n"
81+
type "print_arg a r g"
8582
type "print_arg a r g \n"
8683
type "exit"
8784
end
@@ -103,7 +100,6 @@ def execute(*)
103100
$nth_execution ||= 1
104101
puts "\#{$nth_execution} FooBar executed"
105102
$nth_execution += 1
106-
nil
107103
end
108104
end
109105
@@ -131,7 +127,6 @@ def test_no_meta_command_also_works
131127
class NoMetaCommand < IRB::Command::Base
132128
def execute(*)
133129
puts "This command does not override meta attributes"
134-
nil
135130
end
136131
end
137132
@@ -141,12 +136,13 @@ def execute(*)
141136
RUBY
142137

143138
output = run_ruby_file do
144-
type "no_meta\n"
145-
type "help no_meta\n"
139+
type "no_meta"
140+
type "help no_meta"
146141
type "exit"
147142
end
148143

149144
assert_include(output, "This command does not override meta attributes")
145+
assert_include(output, "No description provided.")
150146
assert_not_include(output, "Maybe IRB bug")
151147
end
152148
end

0 commit comments

Comments
 (0)