Skip to content

Commit f52b1cc

Browse files
committed
Show descriptions for all commands in Rails help
When calling `rails help` most commands missed their description. We now show the same descriptions as shown in `rails -T`. We can show the description after the command name and use some padding to align the descriptions. Lines are truncated to the width of the terminal. Rails::Command had logic for printing help commands not in 'usage': `print_commands`, `commands` and the `COMMANDS_IN_USAGE` constant. As these methods were only used by Rails::Command::HelpCommand, they have been moved HelpCommand. This also changes the `printing_commands` methods on several classes. Instead of returning just the commands, they return the commands with the descriptions. As these are only used for printing help it does not affect any other code. Some commands (routes, credentials:diff, etc) show no description yet, but these can be added later.
1 parent 532294b commit f52b1cc

File tree

6 files changed

+28
-8
lines changed

6 files changed

+28
-8
lines changed

railties/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Show descriptions for all commands in Rails help
2+
3+
When calling `rails help` most commands missed their description. We now
4+
show the same descriptions as shown in `rails -T`.
5+
6+
*Petrik de Heus*
7+
18
* Always generate the storage/ directory with rails new to ensure there's a stable place to
29
put permanent files, and a single mount point for containers to map. Then default sqlite3 databases
310
to live there instead of db/, which is only meant for configuration, not data.

railties/lib/rails/command.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ def root
8686
end
8787
end
8888

89-
def print_commands # :nodoc:
90-
commands.each { |command| puts(" #{command}") }
89+
def printing_commands # :nodoc:
90+
lookup!
91+
92+
(subclasses - hidden_commands).flat_map(&:printing_commands)
9193
end
9294

9395
private

railties/lib/rails/command/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def perform(command, args, config) # :nodoc:
9292
end
9393

9494
def printing_commands
95-
namespaced_commands
95+
namespaced_commands.map { |command| [command, ""] }
9696
end
9797

9898
def executable(subcommand = nil)

railties/lib/rails/commands/help/help_command.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,19 @@ class HelpCommand < Base # :nodoc:
88
def help(*)
99
say self.class.desc
1010

11-
Rails::Command.print_commands
11+
other_commands = printing_commands_not_in_usage.sort_by(&:first)
12+
print_table(other_commands, indent: 1, truncate: true)
1213
end
14+
15+
private
16+
COMMANDS_IN_USAGE = %w(generate console server test test:system dbconsole new)
17+
private_constant :COMMANDS_IN_USAGE
18+
19+
def printing_commands_not_in_usage # :nodoc:
20+
Rails::Command.printing_commands.reject do |command, _|
21+
command.in?(COMMANDS_IN_USAGE)
22+
end
23+
end
1324
end
1425
end
1526
end

railties/lib/rails/commands/rake/rake_command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class RakeCommand < Base # :nodoc:
99

1010
class << self
1111
def printing_commands
12-
formatted_rake_tasks.map(&:first)
12+
formatted_rake_tasks
1313
end
1414

1515
def perform(task, args, config)

railties/test/command/base_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
class Rails::Command::BaseTest < ActiveSupport::TestCase
1010
test "printing commands" do
11-
assert_equal %w(generate), Rails::Command::GenerateCommand.printing_commands
12-
assert_equal %w(secrets:setup secrets:edit secrets:show), Rails::Command::SecretsCommand.printing_commands
13-
assert_equal %w(db:system:change), Rails::Command::Db::System::ChangeCommand.printing_commands
11+
assert_equal [["generate", ""]], Rails::Command::GenerateCommand.printing_commands
12+
assert_equal [["secrets:setup", ""], ["secrets:edit", ""], ["secrets:show", ""]], Rails::Command::SecretsCommand.printing_commands
13+
assert_equal [["db:system:change", ""]], Rails::Command::Db::System::ChangeCommand.printing_commands
1414
end
1515

1616
test "printing commands hides hidden commands" do

0 commit comments

Comments
 (0)