Skip to content

Commit

Permalink
Print bin/rails help on unrecognized bare options
Browse files Browse the repository at this point in the history
Prior to this commit, `bin/rails` would pass unrecognized bare options
on to Rake:

  ```console
  $ bin/rails -v
  Rails 7.2.0.alpha

  $ bin/rails -V
  rake, version 13.0.6

  $ bin/rails -s
  Running 0 tests in a single process (parallelization threshold is 50)
  ...

  $ bin/rails -S
  invalid option: -S
  ```

This commit changes `bin/rails` to print its help message when given an
unrecognized bare option:

  ```console
  $ bin/rails -v
  Rails 7.2.0.alpha

  $ bin/rails -V
  Usage:
    bin/rails COMMAND [options]

  You must specify a command. The most common commands are:
  ...

  $ bin/rails -s
  Usage:
    bin/rails COMMAND [options]

  You must specify a command. The most common commands are:
  ...

  $ bin/rails -S
  Usage:
    bin/rails COMMAND [options]

  You must specify a command. The most common commands are:
  ...
  ```

However, for backward compatibility, an exception has been made for the
`-T` / `--tasks` option:

  ```console
  $ bin/rails -T
  # Prints list of Rake tasks...
  ```

Addresses rails#50712.
  • Loading branch information
jonathanhefner committed Jan 18, 2024
1 parent 78fd6b7 commit dc85b95
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
50 changes: 50 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
* `bin/rails` now prints its help message when given an unrecognized bare
option.

__Before__

```console
$ bin/rails -v
Rails 7.2.0.alpha

$ bin/rails -V
rake, version 13.0.6

$ bin/rails -s
Running 0 tests in a single process (parallelization threshold is 50)
...

$ bin/rails -S
invalid option: -S
```

__After__

```console
$ bin/rails -v
Rails 7.2.0.alpha

$ bin/rails -V
Usage:
bin/rails COMMAND [options]

You must specify a command. The most common commands are:
...

$ bin/rails -s
Usage:
bin/rails COMMAND [options]

You must specify a command. The most common commands are:
...

$ bin/rails -S
Usage:
bin/rails COMMAND [options]

You must specify a command. The most common commands are:
...
```

*Jonathan Hefner*

* Ensure `autoload_paths`, `autoload_once_paths`, `eager_load_paths`, and
`load_paths` only have directories when initialized from engine defaults.
Previously, files under the `app` directory could end up there too.
Expand Down
10 changes: 6 additions & 4 deletions railties/lib/rails/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,16 @@ def rails_new_with_no_path?(args)

def split_namespace(namespace)
case namespace
when /^(.+):(\w+)$/
[$1, $2]
when ""
["help", "help"]
when HELP_MAPPINGS, "help"
["help", "help_extended"]
when VERSION_MAPPINGS
["version", "version"]
when "--tasks", "-T"
["", ""]
when /^-/, ""
["help", "help"]
when /^(.+):(\w+)$/
[$1, $2]
else
[namespace, namespace]
end
Expand Down
17 changes: 16 additions & 1 deletion railties/test/command/help_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class Rails::Command::HelpIntegrationTest < ActiveSupport::TestCase
setup :build_app
teardown :teardown_app

test "prints help on unrecognized bare option" do
assert_match "You must specify a command.", rails("--zzz")
assert_match "You must specify a command.", rails("-z")
end

test "prints helpful error on unrecognized command" do
output = rails "vershen", allow_failure: true

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

test "excludes application Rake tasks from command listing" do
test "prints Rake tasks on --tasks / -T option" do
app_file "lib/tasks/my_task.rake", <<~RUBY
desc "my_task"
task :my_task
RUBY

assert_match "my_task", rails("--tasks")
assert_match "my_task", rails("-T")
end

test "excludes application Rake tasks from command list via --help" do
app_file "Rakefile", <<~RUBY, "a"
desc "my_task"
task :my_task_1
Expand Down

0 comments on commit dc85b95

Please sign in to comment.