SubCommand.java

package net.morimekta.terminal.args;

import net.morimekta.terminal.args.impl.SubCommandImpl;

import java.util.List;
import java.util.function.Function;

public interface SubCommand<SubCommandDef> {
    /**
     * The sub-command name.
     *
     * @return The name.
     */
    String getName();

    /**
     * The basic usage description.
     *
     * @return The usage description.
     */
    String getUsage();

    /**
     * If the sub-command is hidden by default.
     *
     * @return True if hidden.
     */
    boolean isHidden();

    /**
     * Get the list of sub-command aliases.
     *
     * @return The aliases.
     */
    List<String> getAliases();

    /**
     * Instantiate the selected commands' implementation.
     *
     * @param builder The argument parser builder that should contain the subcommand.
     * @return The new sub-command instance.
     */
    SubCommandDef newInstance(ArgParser.Builder builder);

    interface Builder<SubCommandDef> {
        Builder<SubCommandDef> alias(String... aliases);

        Builder<SubCommandDef> hidden();

        SubCommand<SubCommandDef> build();
    }

    static <SubCommandDef> SubCommand.Builder<SubCommandDef> subCommand(
            String name,
            String usage,
            Function<ArgParser.Builder, ? extends SubCommandDef> instanceFactory) {
        return new SubCommandImpl.BuilderImpl<>(name, usage, instanceFactory);
    }
}