package me.coley.recaf.command.impl;

import me.coley.recaf.command.MetaCommand;
import org.tinylog.Logger;
import picocli.CommandLine;

import java.io.File;
import java.util.concurrent.Callable;

/**
 * Command for quitting Recaf.
 *
 * @author Matt
 */
@CommandLine.Command(name = "help", description = "Prints usage for the specified command", helpCommand = true)
public class Help extends MetaCommand implements Callable<Void> {
	@CommandLine.Parameters(index = "0",  description = "The command to show usage for.", arity = "0..1")
	public String command;

	/**
	 * @return n/a
	 *
	 * @throws Exception
	 * 		<ul><li>IllegalStateException, help command missing cli context</li></ul>
	 */
	@Override
	public Void call() throws Exception {
		CommandLine subcommand = context.getSubcommands().get(command);
		if (subcommand != null)
			subcommand.usage(context.getOut());
		else {
			StringBuilder sb = new StringBuilder();
			if (command != null)
				sb.append("No such command: '" + command + "'\n");
			else
				sb.append("Specify a command to see it's usage.\n");
			sb.append("The existing commands are:");
			for (CommandLine com : context.getSubcommands().values())
				sb.append("\n - " + com.getCommandName());
			Logger.error(sb.toString());
		}
		return null;
	}
}

@@ -151,16 +151,36 @@
 o.File;%0A
+import java.util.*;%0A
 import j
@@ -209,16 +209,16 @@
 llable;%0A
-
 %0A/**%0A * 
@@ -572,16 +572,152 @@
 command;
+%0A%09@CommandLine.Parameters(index = %221%22,  description = %22The sub command name.%22, arity = %220..1%22, hidden = true)%0A%09public String subcommand;
 %0A%0A%09/**%0A%09
@@ -898,78 +898,166 @@
 %7B%0A%09%09
-CommandLine subcommand = context.getSubcommands().get(command);%0A%09%09if (
+// Get root command%0A%09%09String key = command;%0A%09%09CommandLine cmd = context.getSubcommands().get(key);%0A%09%09// Check if subcommand is specified%0A%09%09if (cmd != null && 
 subc
@@ -1075,21 +1075,203 @@
 ull)
+ %7B
 %0A%09%09%09
-subcomman
+key += %22 %22 + subcommand;%0A%09%09%09cmd = cmd.getSubcommands().get(subcommand);%0A%09%09%7D%0A%09%09// Check if fetched command exists, if so print usage%0A%09%09// Otherwise list all commands%0A%09%09if (cmd != null)%0A%09%09%09cm
 d.us
@@ -1406,23 +1406,19 @@
 d: '%22 + 
-command
+key
  + %22'%5Cn%22
@@ -1536,64 +1536,215 @@
 %0A%09%09%09
-for (CommandLine com : context.getSubcommands().values()
+// Sort commands alphabetically%0A%09%09%09List%3CCommandLine%3E list = new ArrayList%3C%3E(context.getSubcommands().values());%0A%09%09%09list.sort(Comparator.comparing(CommandLine::getCommandName));%0A%09%09%09for (CommandLine com : list
 )%0A%09%09
@@ -1802,13 +1802,12 @@
 ger.
-error
+info
 (sb.

Add subcommand support to help command