SubCommandSet.java

/*
 * Copyright 2022 Terminal Utils Authors
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package net.morimekta.terminal.args;

import java.util.List;

/**
 * A set of subcommands with come helper methods and builder.
 *
 * @param <SubCommandDef> The type generic for the subcommand definition.
 */
public interface SubCommandSet<SubCommandDef> extends Arg {
    /**
     * @return Subcommands in the set ordered as declared.
     */
    List<SubCommand<? extends SubCommandDef>> getSubCommands();

    /**
     * @param name Name of subcommand to get.
     * @return The subcommand or null if not found.
     */
    SubCommand<? extends SubCommandDef> getSubCommandByName(String name);

    /**
     * @param name Name of subcommand to get argument parser for.
     * @return The argument parser for given subcommand.
     */
    ArgParser parserForSubCommand(String name);

    /**
     * Builder for subcommands.
     *
     * @param <SubCommandDef> The type generic for the subcommand definition.
     */
    interface Builder<SubCommandDef> extends ArgParserBuilder {
        /**
         * Make subcommand optional.
         *
         * @return The builder.
         */
        Builder<SubCommandDef> optionalCommand();

        /**
         * Set the default subcommand, if none is selected.
         *
         * @param name Name of default subcommand.
         * @return The builder.
         */
        Builder<SubCommandDef> defaultCommand(String name);

        /**
         * Add subcommand.
         *
         * @param subCommand The subcommand to add.
         * @return The builder.
         */
        Builder<SubCommandDef> add(
                SubCommand<? extends SubCommandDef> subCommand);

        /**
         * Add subcommand from builder.
         *
         * @param subCommand The subcommand to add.
         * @return The builder.
         */
        default Builder<SubCommandDef> add(
                SubCommand.Builder<? extends SubCommandDef> subCommand) {
            return add(subCommand.build());
        }
    }
}