Newer
Older
KeeperJerry_Launcher / LaunchServer / source / command / basic / HelpCommand.java
@KeeperJerry KeeperJerry on 27 Jun 2020 1 KB Рефактор кода
package launchserver.command.basic;

import launcher.helper.LogHelper;
import launchserver.LaunchServer;
import launchserver.command.Command;
import launchserver.command.CommandException;

import java.util.Map.Entry;

public final class HelpCommand extends Command
{
    public HelpCommand(LaunchServer server)
    {
        super(server);
    }

    private static void printCommand(String name, Command command)
    {
        String args = command.getArgsDescription();
        LogHelper.subInfo("%s %s - %s", name, args == null ? "[nothing]" : args, command.getUsageDescription());
    }

    @Override
    public String getArgsDescription()
    {
        return "[command name]";
    }

    @Override
    public String getUsageDescription()
    {
        return "Print command usage";
    }

    @Override
    public void invoke(String... args) throws CommandException
    {
        if (args.length < 1)
        {
            printCommands();
            return;
        }

        // Print command help
        printCommand(args[0]);
    }

    private void printCommand(String name) throws CommandException
    {
        printCommand(name, server.commandHandler.lookup(name));
    }

    private void printCommands()
    {
        for (Entry<String, Command> entry : server.commandHandler.commandsMap().entrySet())
        {
            printCommand(entry.getKey(), entry.getValue());
        }
    }
}