Class BooleanArgumentType

  • All Implemented Interfaces:
    ArgumentType<java.lang.Boolean>, MetavarInference

    public class BooleanArgumentType
    extends java.lang.Object
    implements ArgumentType<java.lang.Boolean>, MetavarInference

    BooleanArgumentType provides strict conversion from input string to Boolean value.

    Passing Boolean class to Argument.type(Class) can achieve boolean conversion as well, but its conversion relies on Boolean.valueOf(String). That is any strings which matches "true" in case-insensitive fashion are converted to Boolean.TRUE. The other strings are converted to Boolean.FALSE.

    On the other hand, BooleanArgumentType is strict when conversion is made. That is only the string which is defined as true value is converted to Boolean.TRUE. Similarly, only the string which is defined as false value is converted to Boolean.FALSE. The comparison is made in case-sensitive manner.

    When BooleanArgumentType is created by the default constructor, BooleanArgumentType(), the true value is "true", and the false value is "false". Probably, this is the most common use case. The another constructor, BooleanArgumentType(String, String) can take 2 strings, and application can specify what strings are used as true or false value.

    Since:
    0.7.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String falseValue_  
      private java.lang.String trueValue_  
    • Constructor Summary

      Constructors 
      Constructor Description
      BooleanArgumentType()
      Creates BooleanArgumentType with "true" as true value, and "false" as false value.
      BooleanArgumentType​(java.lang.String trueValue, java.lang.String falseValue)
      Creates BooleanArgumentType with given values.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Boolean convert​(ArgumentParser parser, Argument arg, java.lang.String value)
      Converts value to appropriate type.
      java.lang.String[] inferMetavar()
      Infers metavar based on given strings.
      • Methods inherited from class java.lang.Object

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

      • trueValue_

        private final java.lang.String trueValue_
      • falseValue_

        private final java.lang.String falseValue_
    • Constructor Detail

      • BooleanArgumentType

        public BooleanArgumentType()
        Creates BooleanArgumentType with "true" as true value, and "false" as false value.
      • BooleanArgumentType

        public BooleanArgumentType​(java.lang.String trueValue,
                                   java.lang.String falseValue)
        Creates BooleanArgumentType with given values.
        Parameters:
        trueValue - string used as true value
        falseValue - string used as false value
    • Method Detail

      • convert

        public java.lang.Boolean 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<java.lang.Boolean>
        Parameters:
        parser - The parser.
        arg - The argument this type attached to.
        value - The attribute value.
        Returns:
        Converted object.
        Throws:
        ArgumentParserException - If conversion fails.