[FEATURE] Help для субкомманд

This commit is contained in:
Gravit 2019-07-06 19:17:23 +07:00
parent 97bf5816c0
commit aac9aef821
No known key found for this signature in database
GPG key ID: 061981E1E85D3216

View file

@ -1,5 +1,6 @@
package pro.gravit.utils.command.basic; package pro.gravit.utils.command.basic;
import java.util.Arrays;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.fusesource.jansi.Ansi; import org.fusesource.jansi.Ansi;
@ -13,7 +14,7 @@
public final class HelpCommand extends Command { public final class HelpCommand extends Command {
private CommandHandler handler; private CommandHandler handler;
private static void printCommand(String name, Command command) { public static void printCommand(String name, Command command) {
String args = command.getArgsDescription(); String args = command.getArgsDescription();
//LogHelper.subInfo("%s %s - %s", name, args == null ? "[nothing]" : args, command.getUsageDescription()); //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()), () -> { 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("<font color=\"green\">%s</font> <font color=\"cyan\">%s</font> - <font color=\"yellow\">%s</font>", name, args == null ? "[nothing]" : args, command.getUsageDescription()), true)); }, () -> LogHelper.htmlFormatLog(LogHelper.Level.INFO, LogHelper.getDataTime(), String.format("<font color=\"green\">%s</font> <font color=\"cyan\">%s</font> - <font color=\"yellow\">%s</font>", 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) { private static void printCategory(String name, String description) {
if (description != null) LogHelper.info("Category: %s - %s", name, description); if (description != null) LogHelper.info("Category: %s - %s", name, description);
else LogHelper.info("Category: %s", name); else LogHelper.info("Category: %s", name);
@ -58,7 +80,9 @@ public void invoke(String... args) throws CommandException {
} }
// Print command help // Print command help
if(args.length == 1)
printCommand(args[0]); printCommand(args[0]);
printSubCommandsHelp(args[0], Arrays.copyOfRange(args, 1 , args.length), handler.lookup(args[0]));
} }
private void printCommand(String name) throws CommandException { private void printCommand(String name) throws CommandException {