另一种方法是使用register:
class CLI < Thor
register(SubTask, 'sub', 'sub <command>', 'Description.')
end
class SubTask < Thor
desc "bar", "..."
def bar()
end
end
CLI.start
现在假设你的可执行文件名为foo,你可以调用:
$ foo sub bar
在当前的Thor版本(0.15.0.rc2)中存在一个错误,导致帮助文本跳过子命令的名称空间:
$ foo sub
Tasks:
foo help [COMMAND] # Describe subcommands or one specific subcommand
foo bar #
您可以通过重写self.banner并明确设置命名空间来解决此问题。
class SubTask < Thor
namespace :sub
def bar ...
def self.banner(task, namespace = true, subcommand = false)
"#{basename} #{task.formatted_usage(self, true, subcommand)}"
end
end
formatted_usage的第二个参数是banner原始实现的唯一差异。你也可以这样做,只需执行一次并使其他子命令Thor类从SubTask继承。现在你得到:
$ foo sub
Tasks:
foo sub help [COMMAND] # Describe subcommands or one specific subcommand
foo sub bar #
希望这能有所帮助。