ArgumentParser { @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "This is a false positive. A null check is present.") public Boolean getStringValueAsBoolean(Option option, Boolean defaultValue) throws ParseException { Boolean result; ensureCommandLineNotNull(); String stringValue = getStringValue(option); if (StringUtils.isNotBlank(stringValue)) { CustomBooleanEditor customBooleanEditor = new CustomBooleanEditor(false); try { customBooleanEditor.setAsText(stringValue); } catch (IllegalArgumentException e) { ParseException parseException = new ParseException(e.getMessage()); parseException.initCause(e); throw parseException; } result = (Boolean) customBooleanEditor.getValue(); } else { result = defaultValue; } return result; } ArgumentParser(String applicationName); String getApplicationName(); Option addArgument(Option option, boolean required); Option addArgument(String opt, String longOpt, boolean hasArg, String description, boolean required); Collection getConfiguredOptions(); void parseArguments(String[] args, boolean failOnMissingRequiredOptions); void parseArguments(String[] args); String getUsageInformation(); @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "This is a false positive. A null check is present.") Boolean getBooleanValue(Option option); @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "This is a false positive. A null check is present.") Boolean getStringValueAsBoolean(Option option, Boolean defaultValue); @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "This is a false positive. A null check is present.") String getStringValue(Option option); String getStringValue(Option option, String defaultValue); Integer getIntegerValue(Option option); Integer getIntegerValue(Option option, Integer defaultValue); Integer getIntegerValue(Option option, Integer defaultValue, Integer minValue, Integer maxValue); File getFileValue(Option option); File getFileValue(Option option, File defaultValue);  }
@Test public void testGetStringValueAsBoolean() throws ParseException { ArgumentParser argParser = new ArgumentParser(""); Option strOpt = argParser.addArgument("s", "str", true, "Some string input parameter to have a boolean value", false); final String shortStrOpt = String.format("-%s", strOpt.getOpt()); final String longStrOpt = String.format("--%s", strOpt.getLongOpt()); argParser.parseArguments(new String[] {}); assertFalse(argParser.getStringValueAsBoolean(strOpt, false)); assertTrue(argParser.getStringValueAsBoolean(strOpt, true)); for (String inputValue : Arrays .asList(CustomBooleanEditor.VALUE_TRUE, CustomBooleanEditor.VALUE_YES, CustomBooleanEditor.VALUE_ON, CustomBooleanEditor.VALUE_1)) { argParser.parseArguments(new String[] {shortStrOpt, inputValue}); assertTrue(argParser.getStringValueAsBoolean(strOpt, false)); argParser.parseArguments(new String[] {longStrOpt, inputValue}); assertTrue(argParser.getStringValueAsBoolean(strOpt, false)); } for (String inputValue : Arrays .asList(CustomBooleanEditor.VALUE_FALSE, CustomBooleanEditor.VALUE_NO, CustomBooleanEditor.VALUE_OFF, CustomBooleanEditor.VALUE_0)) { argParser.parseArguments(new String[] {shortStrOpt, inputValue}); assertFalse(argParser.getStringValueAsBoolean(strOpt, true)); } argParser.parseArguments(new String[] {shortStrOpt, INVALID_BOOLEAN_VALUE}); try { argParser.getStringValueAsBoolean(strOpt, false); fail("Suppose to throw a ParseException when option has an invalid boolean value."); } catch (ParseException e) { assertEquals(String.format("Invalid boolean value [%s]", INVALID_BOOLEAN_VALUE), e.getMessage()); } }