Class BooleanArgumentType
- java.lang.Object
-
- net.sourceforge.argparse4j.impl.type.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
Booleanclass toArgument.type(Class)can achieve boolean conversion as well, but its conversion relies onBoolean.valueOf(String). That is any strings which matches "true" in case-insensitive fashion are converted toBoolean.TRUE. The other strings are converted toBoolean.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 toBoolean.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.StringfalseValue_private java.lang.StringtrueValue_
-
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.Booleanconvert(ArgumentParser parser, Argument arg, java.lang.String value)Convertsvalueto appropriate type.java.lang.String[]inferMetavar()Infers metavar based on given strings.
-
-
-
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 valuefalseValue- 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:ArgumentTypeConverts
valueto appropriate type.If the objects derived from
RuntimeExceptionare thrown in conversion because of invalid input from command line, subclass must catch these exceptions and wrap them inArgumentParserExceptionand give simple error message to explain what happened briefly.- Specified by:
convertin interfaceArgumentType<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.
-
inferMetavar
public java.lang.String[] inferMetavar()
Infers metavar based on given strings.
- Specified by:
inferMetavarin interfaceMetavarInference- Returns:
- inferred array of metavar strings, or null if there is no metavar inferred.
- See Also:
MetavarInference.inferMetavar()
-
-