All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.yin.cli.Flag Maven / Gradle / Ivy

Go to download

Easy to use command-line parser, which enables you to define cmdline flags directly the class they are used in.

The newest version!
package com.github.yin.cli;

import com.google.auto.value.AutoValue;

/**
 * Provides Flag value and type-conversion to program classes. To access the argument value,
 * statically initialize a Flag by calling Cli.create(type, name). Then use
 * method {@link Flag#get()} to get the value.
 *
 * @author yin
 */
@AutoValue
public abstract class Flag {
    static  Flag create(FlagID flagID, Class type, ArgumentProvider index, TypeConversions typeConversions) {
        return new AutoValue_Flag(flagID, type, index, typeConversions);
    }

    abstract FlagID flagID();

    abstract Class type();

    protected abstract ArgumentProvider flags();

    protected abstract TypeConversions typeConversions();

    public T get() {
        TypeConversions.Conversion conversion = typeConversions().forType(type());
        if (conversion != null) {
            String value = flags().singleArgument(flagID());
            return conversion.apply(value);
        } else {
            throw new UnsupportedOperationException(
                    String.format("Type conversion for Flag<%s>s is registered, Flag: %s",
                            type().getCanonicalName(), flagID().toString()));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy