Skip to content

Allow to Override Order of Commands in Help#642

Merged
rafaelfranca merged 4 commits into
rails:mainfrom
alessio-signorini:allow-to-override-commands-sorting
May 11, 2023
Merged

Allow to Override Order of Commands in Help#642
rafaelfranca merged 4 commits into
rails:mainfrom
alessio-signorini:allow-to-override-commands-sorting

Conversation

@alessio-signorini

Copy link
Copy Markdown

The current implementation of the help function sorts the command lexicographically. A request for enhancement (#361) has been open for a while and it seems that people are resorting to hacks to change it.

This PR adds a class method called sort_commands!. The default implementation maintains the lexicographic ordering but the method can be easily overridden by the subclass to obtain any sort of ordering.

Here is a quick example that would place the help command first

class MyScript < Thor
  ...
  def self.sort_commands!(list)
    list.sort_by! do |a,b|
      a[0] == :help ? -1 : a[0] <=> b[0]
    end
  end
end

@Roriz

Roriz commented Jan 2, 2020

Copy link
Copy Markdown

up

Comment thread .travis.yml Outdated
Comment thread spec/sort_spec.rb Outdated
Comment thread spec/sort_spec.rb Outdated
@alessio-signorini alessio-signorini force-pushed the allow-to-override-commands-sorting branch from 5032f3f to 3447a54 Compare May 25, 2020 22:18
@alessio-signorini

Copy link
Copy Markdown
Author

@rafaelfranca, @Roriz - is it time to merge this? 😃

@alexevanczuk

Copy link
Copy Markdown

Hi everyone! Are you still hoping to merge this PR? It would be a great help!

@rafaelfranca rafaelfranca force-pushed the allow-to-override-commands-sorting branch from 3447a54 to a562cdc Compare May 11, 2023 20:46
Thor will not inherit from any other class, and the goal is to allow
subclasses to override the sort_commands! method, so we don't need to
check for super.
@rafaelfranca rafaelfranca force-pushed the allow-to-override-commands-sorting branch from a562cdc to b2590c1 Compare May 11, 2023 20:47
@rafaelfranca rafaelfranca merged commit 91aa66f into rails:main May 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants