package cli;

import java.util.HashMap;
import java.util.function.Function;

abstract public class Subcommand {

	/**
	 * Einfach ein Träumchen, dass das hier public sein muss, damit die Kinderklassen darauf zugreifen können
	 */

	final public HashMap<String, Function<String[], String>> commands = new HashMap<>();

	abstract public String getSubcommand();

	abstract public String getUsage();

	public String executeSubcommand(String[] args) {

		try {
			commandExists(args[0]);
			return commands.get(args[0]).apply(args);
		}
		catch (IndexOutOfBoundsException | IllegalArgumentException e) {
			throw new CliError("Missing a value! " + getUsage());
		}
	}

	private void commandExists(String command) {
		if (commands.get(command) == null) {
			throw new CliError("Subcommand " + command + " does not exist! " + getUsage());
		}
	}
}
