From aac9aef821d375f5561f5fd0b296d6d59b729e7f Mon Sep 17 00:00:00 2001 From: Gravit Date: Sat, 6 Jul 2019 19:17:23 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20Help=20=D0=B4=D0=BB=D1=8F=20=D1=81?= =?UTF-8?q?=D1=83=D0=B1=D0=BA=D0=BE=D0=BC=D0=BC=D0=B0=D0=BD=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/command/basic/HelpCommand.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/LauncherCore/src/main/java/pro/gravit/utils/command/basic/HelpCommand.java b/LauncherCore/src/main/java/pro/gravit/utils/command/basic/HelpCommand.java index 77c819d1..95f00723 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/command/basic/HelpCommand.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/command/basic/HelpCommand.java @@ -1,5 +1,6 @@ package pro.gravit.utils.command.basic; +import java.util.Arrays; import java.util.Map.Entry; import org.fusesource.jansi.Ansi; @@ -13,7 +14,7 @@ public final class HelpCommand extends Command { private CommandHandler handler; - private static void printCommand(String name, Command command) { + public static void printCommand(String name, Command command) { String args = command.getArgsDescription(); //LogHelper.subInfo("%s %s - %s", name, args == null ? "[nothing]" : args, command.getUsageDescription()); LogHelper.rawLog(() -> FormatHelper.rawFormat(LogHelper.Level.INFO, LogHelper.getDataTime(), true) + String.format("%s %s - %s", name, args == null ? "[nothing]" : args, command.getUsageDescription()), () -> { @@ -31,6 +32,27 @@ private static void printCommand(String name, Command command) { }, () -> LogHelper.htmlFormatLog(LogHelper.Level.INFO, LogHelper.getDataTime(), String.format("%s %s - %s", name, args == null ? "[nothing]" : args, command.getUsageDescription()), true)); } + public static void printSubCommandsHelp(String base, Command command) + { + command.childCommands.forEach((k, v) -> { + printCommand(base.concat(" ").concat(k), v); + }); + } + + public static void printSubCommandsHelp(String name, String[] args, Command command) throws CommandException + { + if(args.length == 0) + { + printSubCommandsHelp(name, command); + } + else + { + Command child = command.childCommands.get(args[0]); + if(child == null) throw new CommandException(String.format("Unknown sub command: '%s'", args[0])); + printSubCommandsHelp(name.concat(" ").concat(args[0]), Arrays.copyOfRange(args,1 , args.length), child); + } + } + private static void printCategory(String name, String description) { if (description != null) LogHelper.info("Category: %s - %s", name, description); else LogHelper.info("Category: %s", name); @@ -58,7 +80,9 @@ public void invoke(String... args) throws CommandException { } // Print command help - printCommand(args[0]); + if(args.length == 1) + printCommand(args[0]); + printSubCommandsHelp(args[0], Arrays.copyOfRange(args, 1 , args.length), handler.lookup(args[0])); } private void printCommand(String name) throws CommandException {