Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package net.sourceforge.argparse4j.impl.type;

ArgumentType subclass for enum type.

Since enum does not have a constructor with string argument, it cannot be used with net.sourceforge.argparse4j.inf.Argument.type(java.lang.Class). Instead use this class to specify enum type. The enums in its nature have limited number of members. In convert(net.sourceforge.argparse4j.inf.ArgumentParser,net.sourceforge.argparse4j.inf.Argument,java.lang.String), String value will be converted to one of them. If it cannot be converted, convert(net.sourceforge.argparse4j.inf.ArgumentParser,net.sourceforge.argparse4j.inf.Argument,java.lang.String) will throw net.sourceforge.argparse4j.inf.ArgumentParserException. This means it already act like a net.sourceforge.argparse4j.inf.Argument.choices(java.lang.Object[]).

Use ReflectArgumentType instead.
<T> Type of enum
public class EnumArgumentType<T extends Enum<T>> implements ArgumentType<T> {
    private Class<T> type_;
    public EnumArgumentType(Class<T> type) {
         = type;
    public T convert(ArgumentParser parserArgument argString value)
            throws ArgumentParserException {
        try {
            return Enum.valueOf(value);
        } catch (IllegalArgumentException e) {
            String choices = TextHelper.concat(.getEnumConstants(), 0,
            throw new ArgumentParserException(String.format(
                    "could not convert '%s' (choose from %s)"valuechoices),
New to GrepCode? Check out our FAQ X