Skip to content

Commit a18aa26

Browse files
Merge pull request rails#50798 from jonathanhefner/command-handle-unrecognized-bare-option
Print `bin/rails` help on unrecognized bare options
2 parents 78fd6b7 + dc85b95 commit a18aa26

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

railties/CHANGELOG.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,53 @@
1+
* `bin/rails` now prints its help message when given an unrecognized bare
2+
option.
3+
4+
__Before__
5+
6+
```console
7+
$ bin/rails -v
8+
Rails 7.2.0.alpha
9+
10+
$ bin/rails -V
11+
rake, version 13.0.6
12+
13+
$ bin/rails -s
14+
Running 0 tests in a single process (parallelization threshold is 50)
15+
...
16+
17+
$ bin/rails -S
18+
invalid option: -S
19+
```
20+
21+
__After__
22+
23+
```console
24+
$ bin/rails -v
25+
Rails 7.2.0.alpha
26+
27+
$ bin/rails -V
28+
Usage:
29+
bin/rails COMMAND [options]
30+
31+
You must specify a command. The most common commands are:
32+
...
33+
34+
$ bin/rails -s
35+
Usage:
36+
bin/rails COMMAND [options]
37+
38+
You must specify a command. The most common commands are:
39+
...
40+
41+
$ bin/rails -S
42+
Usage:
43+
bin/rails COMMAND [options]
44+
45+
You must specify a command. The most common commands are:
46+
...
47+
```
48+
49+
*Jonathan Hefner*
50+
151
* Ensure `autoload_paths`, `autoload_once_paths`, `eager_load_paths`, and
252
`load_paths` only have directories when initialized from engine defaults.
353
Previously, files under the `app` directory could end up there too.

railties/lib/rails/command.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,16 @@ def rails_new_with_no_path?(args)
130130

131131
def split_namespace(namespace)
132132
case namespace
133-
when /^(.+):(\w+)$/
134-
[$1, $2]
135-
when ""
136-
["help", "help"]
137133
when HELP_MAPPINGS, "help"
138134
["help", "help_extended"]
139135
when VERSION_MAPPINGS
140136
["version", "version"]
137+
when "--tasks", "-T"
138+
["", ""]
139+
when /^-/, ""
140+
["help", "help"]
141+
when /^(.+):(\w+)$/
142+
[$1, $2]
141143
else
142144
[namespace, namespace]
143145
end

railties/test/command/help_integration_test.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ class Rails::Command::HelpIntegrationTest < ActiveSupport::TestCase
77
setup :build_app
88
teardown :teardown_app
99

10+
test "prints help on unrecognized bare option" do
11+
assert_match "You must specify a command.", rails("--zzz")
12+
assert_match "You must specify a command.", rails("-z")
13+
end
14+
1015
test "prints helpful error on unrecognized command" do
1116
output = rails "vershen", allow_failure: true
1217

@@ -33,7 +38,17 @@ class Rails::Command::HelpIntegrationTest < ActiveSupport::TestCase
3338
assert_equal help, output
3439
end
3540

36-
test "excludes application Rake tasks from command listing" do
41+
test "prints Rake tasks on --tasks / -T option" do
42+
app_file "lib/tasks/my_task.rake", <<~RUBY
43+
desc "my_task"
44+
task :my_task
45+
RUBY
46+
47+
assert_match "my_task", rails("--tasks")
48+
assert_match "my_task", rails("-T")
49+
end
50+
51+
test "excludes application Rake tasks from command list via --help" do
3752
app_file "Rakefile", <<~RUBY, "a"
3853
desc "my_task"
3954
task :my_task_1

0 commit comments

Comments
 (0)