gpt4 book ai didi

ruby - 雷神 CLI : Setting a custom order of commands in help output

转载 作者:太空宇宙 更新时间:2023-11-03 17:29:46 25 4
gpt4 key购买 nike

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 以不同的方式排列条目?

最佳答案

Thor 似乎没有为此提供配置选项。所以我现在先解决一些猴子修补问题。 aristotll's answer将我指向 Thor 的正确位置 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)

关于ruby - 雷神 CLI : Setting a custom order of commands in help output,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45716113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com