Class EnumStringArgumentType<T extends java.lang.Enum<T>>

  • Type Parameters:
    T - Type of enum
    All Implemented Interfaces:
    ArgumentType<T>, MetavarInference

    public class EnumStringArgumentType<T extends java.lang.Enum<T>>
    extends java.lang.Object
    implements ArgumentType<T>, MetavarInference

    ArgumentType subclass for enum type.

    Uses Enum.toString() instead of Enum.name() as the String representation of the enum. For enums that do not override Enum.toString(), this behaves the same as ReflectArgumentType.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Class<T> type_  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T convert​(ArgumentParser parser, Argument arg, java.lang.String value)
      Converts value to appropriate type.
      static <T extends java.lang.Enum<T>>
      EnumStringArgumentType<T>
      forEnum​(java.lang.Class<T> type)
      Creates an EnumStringArgumentType for the given enum type.
      java.lang.String[] inferMetavar()
      Infers metavar based on given type.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • type_

        private final java.lang.Class<T extends java.lang.Enum<T>> type_
    • Constructor Detail

      • EnumStringArgumentType

        public EnumStringArgumentType​(java.lang.Class<T> type)
    • Method Detail

      • forEnum

        public static <T extends java.lang.Enum<T>> EnumStringArgumentType<T> forEnum​(java.lang.Class<T> type)

        Creates an EnumStringArgumentType for the given enum type.

        Type Parameters:
        T - The type of the enum
        Parameters:
        type - type of the enum the EnumStringArgumentType should convert to
        Returns:
        an EnumStringArgumentType that converts Strings to type
      • convert

        public T convert​(ArgumentParser parser,
                         Argument arg,
                         java.lang.String value)
                  throws ArgumentParserException
        Description copied from interface: ArgumentType

        Converts value to appropriate type.

        If the objects derived from RuntimeException are thrown in conversion because of invalid input from command line, subclass must catch these exceptions and wrap them in ArgumentParserException and give simple error message to explain what happened briefly.

        Specified by:
        convert in interface ArgumentType<T extends java.lang.Enum<T>>
        Parameters:
        parser - The parser.
        arg - The argument this type attached to.
        value - The attribute value.
        Returns:
        Converted object.
        Throws:
        ArgumentParserException - If conversion fails.
      • inferMetavar

        public java.lang.String[] inferMetavar()

        Infers metavar based on given type.

        The inferred metavar contains all enum constant string representation, obtained by calling their Object.toString() method.

        Specified by:
        inferMetavar in interface MetavarInference
        Returns:
        inferred array of metavar strings, or null if there is no metavar inferred.
        Since:
        0.7.0
        See Also:
        MetavarInference.inferMetavar()