Flag.java

package net.morimekta.terminal.args;

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

import java.util.function.Consumer;

import static java.util.Objects.requireNonNull;
import static net.morimekta.terminal.args.impl.OptionUtils.requireValidLongName;
import static net.morimekta.terminal.args.impl.OptionUtils.requireValidShortNames;
import static net.morimekta.terminal.args.impl.OptionUtils.requireValidUsage;

/**
 * Similar to {@link Option}, but without any value argument. Can only
 * toggle boolean values.
 */
public interface Flag extends Option {
    /**
     * The alternative (negating) long name for the flag.
     *
     * @return The negating name.
     */
    String getNegateName();

    interface Builder extends Arg.Builder<Flag> {
        Builder negateName(String negateName);

        Builder negateShortName(char negateShortName);

        Builder defaultOn();

        Builder defaultOff();

        Builder defaultValue(boolean value);

        Builder repeated();

        Builder hidden();
    }

    static Builder flagLong(
            String name,
            String usage,
            Consumer<Boolean> setter) {
        return new FlagImpl.BuilderImpl(
                requireValidLongName(name),
                null,
                requireValidUsage(usage),
                requireNonNull(setter, "setter == null"));
    }

    static Builder flagShort(
            String shortNames,
            String usage,
            Consumer<Boolean> setter) {
        return new FlagImpl.BuilderImpl(
                null,
                requireValidShortNames(shortNames),
                requireValidUsage(usage),
                requireNonNull(setter, "setter == null"));
    }

    static Builder flag(
            String name,
            String shortNames,
            String usage,
            Consumer<Boolean> setter) {
        return new FlagImpl.BuilderImpl(
                requireValidLongName(name),
                requireValidShortNames(shortNames),
                requireValidUsage(usage),
                requireNonNull(setter, "setter == null"));
    }
}