Thor CLI:在帮助输出中设置命令的自定义顺序

Thor CLI: Setting a custom order of commands in help output

thor gem 似乎总是在打印其帮助输出时按字母顺序对定义的命令进行排序。示例:

#!/usr/bin/env ruby

require "thor"

class MyCLI < Thor
  desc "z", "this should go first"
  def z; end

  desc "a", "this should go second"
  def a; end
end

MyCLI.start(ARGV)

将此脚本另存为 thor-test 并在不带参数的情况下调用它会得到以下输出:

Commands:
  thor-test a               # this should go second
  thor-test help [COMMAND]  # Describe available commands or one specific command
  thor-test z               # this should go first

问题:如何告诉 Thor 以不同的方式排列条目?

来自helpsource code

list.sort! { |a, b| a[0] <=> b[0] }

它按预期的字母顺序排序。


当然邪恶的猴子补丁总是允许的,在MyCLI之前添加以下代码。

SCRIPT = File.basename $PROGRAM_NAME
class String
  alias old_compare <=>
  # @param [String]  other_string
  # @return [Fixnum]
  def <=>(other_string)
    # currently the command name is like `script_name+space+usage`
    # a monkey patch to make z goes first
    if other_string.start_with?(SCRIPT)
      index = SCRIPT.size + 1
      if other_string[index] == 'z'
        return 1
      elsif self[index] =='z'
        return -1
      end
    end
    old_compare other_string
  end
end

输出:

Commands:
  thor-test z               # this should go first
  thor-test a               # this should go second
  thor-test help [COMMAND]  # Describe available commands or one specific command

似乎 Thor 没有为此提供配置选项。所以我现在先解决一些猴子修补问题。 pointed me to the right place in Thor's source code.

但我决定更改 help 方法的实现,而不是修改 <=> 方法。这对我来说似乎更清晰,并且具有可以进一步影响帮助输出行为的优势:

#!/usr/bin/env ruby

require "thor"

class MyCLI < Thor
  class << self
    def help(shell, subcommand = false)
      list = printable_commands(true, subcommand)
      Thor::Util.thor_classes_in(self).each do |klass|
        list += klass.printable_commands(false)
      end

      # Remove this line to disable alphabetical sorting
      # list.sort! { |a, b| a[0] <=> b[0] }

      # Add this line to remove the help-command itself from the output
      list.reject! {|l| l[0].split[1] == 'help'}

      if defined?(@package_name) && @package_name
        shell.say "#{@package_name} commands:"
      else
        shell.say "Commands:"
      end

      shell.print_table(list, :indent => 2, :truncate => true)
      shell.say
      class_options_help(shell)

      # Add this line if you want to print custom text at the end of your help output.
      # (similar to how Rails does it)
      shell.say 'All commands can be run with -h (or --help) for more information.'
    end
  end

  desc "z", "this should go first"
  def z; end

  desc "a", "this should go second"
  def a; end
end

MyCLI.start(ARGV)