From d681bb55436f10c6779740b27d8f33e65f62dcf8 Mon Sep 17 00:00:00 2001 From: Alessio Signorini Date: Mon, 25 May 2020 22:17:42 +0000 Subject: [PATCH 1/4] Thor: added overridable sort_commands! method --- lib/thor.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/thor.rb b/lib/thor.rb index e9001962..148b3424 100644 --- a/lib/thor.rb +++ b/lib/thor.rb @@ -197,7 +197,7 @@ def help(shell, subcommand = false) Thor::Util.thor_classes_in(self).each do |klass| list += klass.printable_commands(false) end - list.sort! { |a, b| a[0] <=> b[0] } + sort_commands!(list) if defined?(@package_name) && @package_name shell.say "#{@package_name} commands:" @@ -495,6 +495,23 @@ def help(command = nil, subcommand = true); super; end " end alias_method :subtask_help, :subcommand_help + + # Sort the commands, lexicographically by default + # + # Can be overridden in the subclass to change the display order of the + # commands, for example defining + # + # def self.sort_commands!(list) + # list.sort_by! do |a,b| + # a[0] == :help ? -1 : a[0] <=> b[0] + # end + # end + # + # will put the +help+ command at the top + def sort_commands!(list) + defined?(super) ? super(list) : list.sort! { |a, b| a[0] <=> b[0] } + end + end include Thor::Base From e5031d2254c6be09063510197bc80a597ec6aed2 Mon Sep 17 00:00:00 2001 From: Alessio Signorini Date: Mon, 25 May 2020 22:17:57 +0000 Subject: [PATCH 2/4] Tests: added tests for Thor.sort_commands! --- spec/sort_spec.rb | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 spec/sort_spec.rb diff --git a/spec/sort_spec.rb b/spec/sort_spec.rb new file mode 100644 index 00000000..306144aa --- /dev/null +++ b/spec/sort_spec.rb @@ -0,0 +1,75 @@ +require "helper" + +describe Thor do + def shell + @shell ||= Thor::Base.shell.new + end + + describe "#sort - default" do + my_script = Class.new(Thor) do + desc "a", "First Command" + def a; end + + desc "z", "Last Command" + def z; end + end + + before do + @content = capture(:stdout) { my_script.help(shell) } + end + + it "sorts them lexicographillay" do + expect(@content).to match(/:a.+:help.+:z/m) + end + end + + + describe "#sort - simple override" do + my_script = Class.new(Thor) do + desc "a", "First Command" + def a; end + + desc "z", "Last Command" + def z; end + + def self.sort_commands!(list) + list.sort! + list.reverse! + end + + end + + before do + @content = capture(:stdout) { my_script.help(shell) } + end + + it "sorts them in reverse" do + expect(@content).to match(/:z.+:help.+:a/m) + end + end + + + describe "#sort - simple override" do + my_script = Class.new(Thor) do + desc "a", "First Command" + def a; end + + desc "z", "Last Command" + def z; end + + def self.sort_commands!(list) + list.sort_by! do |a,b| + a[0] == :help ? -1 : a[0] <=> b[0] + end + end + end + + before do + @content = capture(:stdout) { my_script.help(shell) } + end + + it "puts help first then sorts them lexicographillay" do + expect(@content).to match(/:help.+:a.+:z/m) + end + end +end \ No newline at end of file From 60abf76b54b88e26f5c5a3c6e50c2563c1e82aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 11 May 2023 20:44:09 +0000 Subject: [PATCH 3/4] Copy edit the documentation --- lib/thor.rb | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/lib/thor.rb b/lib/thor.rb index 148b3424..e6101fbf 100644 --- a/lib/thor.rb +++ b/lib/thor.rb @@ -496,22 +496,13 @@ def help(command = nil, subcommand = true); super; end end alias_method :subtask_help, :subcommand_help - # Sort the commands, lexicographically by default + # Sort the commands, lexicographically by default. # # Can be overridden in the subclass to change the display order of the - # commands, for example defining - # - # def self.sort_commands!(list) - # list.sort_by! do |a,b| - # a[0] == :help ? -1 : a[0] <=> b[0] - # end - # end - # - # will put the +help+ command at the top + # commands. def sort_commands!(list) defined?(super) ? super(list) : list.sort! { |a, b| a[0] <=> b[0] } end - end include Thor::Base From b2590c1e846fea038ca0c383c74718fac4dbd229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 11 May 2023 20:45:51 +0000 Subject: [PATCH 4/4] There is no super method to check 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. --- lib/thor.rb | 2 +- spec/sort_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/thor.rb b/lib/thor.rb index e6101fbf..7e6a11be 100644 --- a/lib/thor.rb +++ b/lib/thor.rb @@ -501,7 +501,7 @@ def help(command = nil, subcommand = true); super; end # Can be overridden in the subclass to change the display order of the # commands. def sort_commands!(list) - defined?(super) ? super(list) : list.sort! { |a, b| a[0] <=> b[0] } + list.sort! { |a, b| a[0] <=> b[0] } end end diff --git a/spec/sort_spec.rb b/spec/sort_spec.rb index 306144aa..45054920 100644 --- a/spec/sort_spec.rb +++ b/spec/sort_spec.rb @@ -72,4 +72,4 @@ def self.sort_commands!(list) expect(@content).to match(/:help.+:a.+:z/m) end end -end \ No newline at end of file +end